This repository has been archived by the owner on Oct 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add end-point to collect brave-core usage data
* Add collection endpoint * Updated failing tests * Add tests for new collection end point
- Loading branch information
Showing
9 changed files
with
170 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
let Joi = require('joi') | ||
|
||
let platforms = ['osx-bc', 'winia32-bc', 'winx64-bc', 'linux-bc'] | ||
let channels = ['dev', 'release', 'nightly', 'beta'] | ||
let booleanString = ['true', 'false'] | ||
|
||
let validator = { | ||
query: { | ||
platform: Joi.valid(platforms).required(), | ||
channel: Joi.valid(channels).required(), | ||
version: Joi.string().required(), | ||
daily: Joi.valid(booleanString).required(), | ||
weekly: Joi.valid(booleanString).required(), | ||
monthly: Joi.valid(booleanString).required(), | ||
first: Joi.valid(booleanString).required(), | ||
woi: Joi.string(), | ||
ref: Joi.string() | ||
} | ||
} | ||
|
||
// Build a usage object if query parameters passed in | ||
let buildUsage = (request) => { | ||
if (request.query.daily) { | ||
return { | ||
daily: request.query.daily === 'true', | ||
weekly: request.query.weekly === 'true', | ||
monthly: request.query.monthly === 'true', | ||
platform: request.query.platform || 'unknown', | ||
version: request.query.version || 'unknown', | ||
first: request.query.first === 'true', | ||
channel: request.query.channel || 'unknown', | ||
woi: request.query.woi || '2016-01-04', | ||
ref: request.query.ref || 'none' | ||
} | ||
} else { | ||
return null | ||
} | ||
} | ||
|
||
exports.setup = (runtime) => { | ||
const get = { | ||
method: 'GET', | ||
path: '/1/usage/brave-core', | ||
config: { | ||
handler: function (request, reply) { | ||
var usage = buildUsage(request) | ||
runtime.mongo.models.insertBraveCoreUsage(usage, (err, results) => { | ||
if (err) { | ||
console.log(err.toString()) | ||
reply({ ts: (new Date()).getTime(), status: 'error', message: err }).code(500) | ||
} else { | ||
reply({ ts: (new Date()).getTime(), status: 'ok' }) | ||
} | ||
}) | ||
}, | ||
validate: validator | ||
} | ||
} | ||
|
||
return [get] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
var tap = require('tap') | ||
var _ = require('underscore') | ||
var ctrl = require('../src/controllers/braveCore') | ||
|
||
var query = { | ||
daily: 'true', | ||
weekly: 'true', | ||
monthly: 'true', | ||
version: '1.2.3', | ||
first: 'true', | ||
channel: 'dev', | ||
platform: 'winia32-bc' | ||
} | ||
|
||
var expected = { | ||
daily: true, | ||
weekly: true, | ||
monthly: true, | ||
version: '1.2.3', | ||
first: true, | ||
channel: 'dev', | ||
platform: 'winia32-bc', | ||
ref: 'none', | ||
woi: '2016-01-04' | ||
} | ||
|
||
tap.test('Brave Core Controller', function (t) { | ||
var runtimeMock = { | ||
mongo: { | ||
models: { | ||
insertBraveCoreUsage: function (usage, cb) { | ||
t.ok(_.isObject(usage), 'usage is an object') | ||
t.same(usage, expected, 'usage built correctly') | ||
cb(null, 'ok') | ||
} | ||
} | ||
} | ||
} | ||
var replyMock = function (obj) { | ||
t.ok(obj.ts, 'timestamp returned') | ||
t.ok(obj.status === 'ok', 'status ok') | ||
} | ||
var requestMock = { | ||
query: query, | ||
info: { | ||
remoteAddress: '1.1.1.1' | ||
}, | ||
headers: {} | ||
} | ||
var endpoints = ctrl.setup(runtimeMock) | ||
endpoints[0].config.handler(requestMock, replyMock) | ||
t.plan(4) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters