Skip to content

Commit

Permalink
Update: Add client information to performance logging (#80)
Browse files Browse the repository at this point in the history
- Add same client information we are sending along with API requests to performance logging
  • Loading branch information
tonyjin authored Apr 18, 2017
1 parent 81fc4e4 commit f7bea68
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
9 changes: 9 additions & 0 deletions src/lib/Logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ const BROWSER_INFO = {
}
};

/* eslint-disable no-undef */
const CLIENT_NAME = __NAME__;
const CLIENT_VERSION = __VERSION__;
/* eslint-enable no-undef */

class Logger {

/**
Expand All @@ -30,6 +35,10 @@ class Logger {
locale,
event: 'preview',
browser: BROWSER_INFO,
client: {
name: CLIENT_NAME,
version: CLIENT_VERSION
},
converted: true,
cache: {
hit: false,
Expand Down
6 changes: 6 additions & 0 deletions src/lib/__tests__/Logger-test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-unused-expressions */
import Logger from '../Logger';

let logger;
Expand Down Expand Up @@ -28,6 +29,11 @@ describe('lib/Logger', () => {

assert.notOk(log.cache.hit, 'Cache should not be hit');
assert.notOk(log.cache.stale, 'Cache should not be stale');

/* eslint-disable no-undef */
expect(log.client.name).to.equal(__NAME__);
expect(log.client.version).to.equal(__VERSION__);
/* eslint-enable no-undef */
});

it('should set and get correctly', () => {
Expand Down
8 changes: 4 additions & 4 deletions src/lib/__tests__/util-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ describe('lib/util', () => {
expect(headers.foo).to.equal(fooHeader);
expect(headers.Authorization).to.equal(`Bearer ${token}`);
expect(headers.BoxApi).to.equal(`shared_link=${sharedLink}`);
expect(headers['X-Box-Client-Name']).to.equal('box-content-preview');
expect(headers['X-Box-Client-Name']).to.equal(__NAME__);
expect(headers['X-Box-Client-Version']).to.equal(__VERSION__);
});

Expand All @@ -230,7 +230,7 @@ describe('lib/util', () => {
assert.equal(headers.foo, 'bar');
assert.equal(headers.Authorization, 'Bearer token');
assert.equal(headers.BoxApi, 'shared_link=https://sharename&shared_link_password=password');
assert.equal(headers['X-Box-Client-Name'], 'box-content-preview');
assert.equal(headers['X-Box-Client-Name'], __NAME__);
assert.equal(headers['X-Box-Client-Version'], __VERSION__);
});
});
Expand All @@ -245,15 +245,15 @@ describe('lib/util', () => {
const url = 'foo';
const token = 'sometoken';
const sharedLink = 'someSharedLink';
expect(util.appendAuthParams(url, token, sharedLink)).to.equal(`${url}?access_token=${token}&shared_link=${sharedLink}&box_client_name=box-content-preview&box_client_version=${__VERSION__}`);
expect(util.appendAuthParams(url, token, sharedLink)).to.equal(`${url}?access_token=${token}&shared_link=${sharedLink}&box_client_name=${__NAME__}&box_client_version=${__VERSION__}`);
});

it('should return correct url with password', () => {
const url = 'foobar';
const token = 'sometoken';
const sharedLink = 'someSharedLink';
const sharedLinkPassword = 'somePass';
expect(util.appendAuthParams(url, token, sharedLink, sharedLinkPassword)).to.equal(`foobar?access_token=${token}&shared_link=${sharedLink}&shared_link_password=${sharedLinkPassword}&box_client_name=box-content-preview&box_client_version=${__VERSION__}`);
expect(util.appendAuthParams(url, token, sharedLink, sharedLinkPassword)).to.equal(`foobar?access_token=${token}&shared_link=${sharedLink}&shared_link_password=${sharedLinkPassword}&box_client_name=${__NAME__}&box_client_version=${__VERSION__}`);
});
});
/* eslint-enable no-undef */
Expand Down
20 changes: 10 additions & 10 deletions src/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const HEADER_CLIENT_VERSION = 'X-Box-Client-Version';
const PARAM_CLIENT_NAME = 'box_client_name';
const PARAM_CLIENT_VERSION = 'box_client_version';
/* eslint-disable no-undef */
const NAME = __NAME__;
const VERSION = __VERSION__;
const CLIENT_NAME = __NAME__;
const CLIENT_VERSION = __VERSION__;
/* eslint-enable no-undef */

const parseJSON = (response) => {
Expand Down Expand Up @@ -276,12 +276,12 @@ export function getHeaders(headers = {}, token = '', sharedLink = '', password =
}

// Following headers are for API analytics
if (NAME) {
headers[HEADER_CLIENT_NAME] = NAME;
if (CLIENT_NAME) {
headers[HEADER_CLIENT_NAME] = CLIENT_NAME;
}

if (VERSION) {
headers[HEADER_CLIENT_VERSION] = VERSION;
if (CLIENT_VERSION) {
headers[HEADER_CLIENT_VERSION] = CLIENT_VERSION;
}

/* eslint-enable no-param-reassign */
Expand Down Expand Up @@ -324,12 +324,12 @@ export function appendAuthParams(url, token = '', sharedLink = '', password = ''
}

// Following params are for API analytics
if (NAME) {
params = `${params}&${PARAM_CLIENT_NAME}=${encodeURI(NAME)}`;
if (CLIENT_NAME) {
params = `${params}&${PARAM_CLIENT_NAME}=${encodeURI(CLIENT_NAME)}`;
}

if (VERSION) {
params = `${params}&${PARAM_CLIENT_VERSION}=${encodeURI(VERSION)}`;
if (CLIENT_VERSION) {
params = `${params}&${PARAM_CLIENT_VERSION}=${encodeURI(CLIENT_VERSION)}`;
}

return `${url}${delim}${params}`;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/viewers/text/__tests__/CSVViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe('lib/viewers/text/CSVViewer', () => {

sandbox.stub(util, 'get').returns(Promise.resolve());

const csvUrlWithAuth = `csvUrl?access_token=token&shared_link=sharedLink&shared_link_password=sharedLinkPassword&box_client_name=box-content-preview&box_client_version=${__VERSION__}`;
const csvUrlWithAuth = `csvUrl?access_token=token&shared_link=sharedLink&shared_link_password=sharedLinkPassword&box_client_name=${__NAME__}&box_client_version=${__VERSION__}`;

return csv.load().then(() => {
expect(window.Papa.parse).to.be.calledWith(csvUrlWithAuth, {
Expand Down

0 comments on commit f7bea68

Please sign in to comment.