Skip to content

Commit

Permalink
replaced fxa-geodb with load balancer header
Browse files Browse the repository at this point in the history
  • Loading branch information
dannycoates committed Jul 28, 2020
1 parent 7d35e8f commit 8ad4597
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 98 deletions.
68 changes: 0 additions & 68 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@
"configstore": "github:dannycoates/configstore#master",
"convict": "^5.2.0",
"express": "^4.17.1",
"fxa-geodb": "^1.0.4",
"helmet": "^3.21.2",
"mkdirp": "^0.5.1",
"mozlog": "^2.2.0",
Expand Down
48 changes: 20 additions & 28 deletions server/amplitude.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ const fetch = require('node-fetch');
const config = require('./config');
const pkg = require('../package.json');

const geoip = config.ip_db
? require('fxa-geodb')({ dbPath: config.ip_db })
: () => ({});

const HOUR = 1000 * 60 * 60;

function truncateToHour(timestamp) {
Expand All @@ -24,20 +20,11 @@ function userId(fileId, ownerId) {
return hash.digest('hex').substring(32);
}

function location(ip) {
try {
return geoip(ip);
} catch (e) {
return {};
}
}

function statUploadEvent(data) {
const loc = location(data.ip);
const event = {
session_id: -1,
country: loc.country,
region: loc.state,
country: data.country,
region: data.state,
user_id: userId(data.id, data.owner),
app_version: pkg.version,
time: truncateToHour(Date.now()),
Expand All @@ -57,11 +44,10 @@ function statUploadEvent(data) {
}

function statDownloadEvent(data) {
const loc = location(data.ip);
const event = {
session_id: -1,
country: loc.country,
region: loc.state,
country: data.country,
region: data.state,
user_id: userId(data.id, data.owner),
app_version: pkg.version,
time: truncateToHour(Date.now()),
Expand All @@ -77,11 +63,10 @@ function statDownloadEvent(data) {
}

function statDeleteEvent(data) {
const loc = location(data.ip);
const event = {
session_id: -1,
country: loc.country,
region: loc.state,
country: data.country,
region: data.state,
user_id: userId(data.id, data.owner),
app_version: pkg.version,
time: truncateToHour(Date.now()),
Expand All @@ -97,11 +82,10 @@ function statDeleteEvent(data) {
}

function statReportEvent(data) {
const loc = location(data.ip);
const event = {
session_id: -1,
country: loc.country,
region: loc.state,
country: data.country,
region: data.state,
user_id: userId(data.id, data.owner),
app_version: pkg.version,
time: truncateToHour(Date.now()),
Expand All @@ -118,8 +102,16 @@ function statReportEvent(data) {
return sendBatch([event]);
}

function clientEvent(event, ua, language, session_id, deltaT, platform, ip) {
const loc = location(ip);
function clientEvent(
event,
ua,
language,
session_id,
deltaT,
platform,
country,
state
) {
const ep = event.event_properties || {};
const up = event.user_properties || {};
const event_properties = {
Expand Down Expand Up @@ -155,15 +147,15 @@ function clientEvent(event, ua, language, session_id, deltaT, platform, ip) {
};
return {
app_version: pkg.version,
country: loc.country,
country: country,
device_id: event.device_id,
event_properties,
event_type: event.event_type,
language,
os_name: ua.os.name,
os_version: ua.os.version,
platform,
region: loc.state,
region: state,
session_id,
time: event.time + deltaT,
user_id: event.user_id,
Expand Down
2 changes: 2 additions & 0 deletions server/routes/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ module.exports = async function(req, res) {
statDeleteEvent({
id,
ip: req.ip,
country: req.geo.country,
state: req.geo.state,
owner: meta.owner,
download_count: meta.dl,
ttl,
Expand Down
2 changes: 2 additions & 0 deletions server/routes/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ module.exports = async function(req, res) {
statDownloadEvent({
id,
ip: req.ip,
country: req.geo.country,
state: req.geo.state,
owner: meta.owner,
download_count: dl,
ttl,
Expand Down
13 changes: 13 additions & 0 deletions server/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@ module.exports = function(app) {
);
next();
});
app.use(function(req, res, next) {
try {
// set by the load balancer
const [country, state] = req.header('X-Client-Geo-Location').split(',');
req.geo = {
country,
state
};
} catch (e) {
req.geo = {};
}
next();
});
app.use(bodyParser.json());
app.use(bodyParser.text());
app.get('/', language, pages.index);
Expand Down
3 changes: 2 additions & 1 deletion server/routes/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ module.exports = async function(req, res) {
data.session_id + deltaT,
deltaT,
data.platform,
req.ip
req.geo.country,
req.geo.state
)
);
const status = await sendBatch(events);
Expand Down
2 changes: 2 additions & 0 deletions server/routes/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module.exports = async function(req, res) {
statReportEvent({
id,
ip: req.ip,
country: req.geo.country,
state: req.geo.state,
owner: meta.owner,
reason: req.body.reason,
download_limit: meta.dlimit,
Expand Down
2 changes: 2 additions & 0 deletions server/routes/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ module.exports = function(ws, req) {
statUploadEvent({
id: newId,
ip: req.ip,
country: req.geo.country,
state: req.geo.state,
owner,
dlimit,
timeLimit,
Expand Down

0 comments on commit 8ad4597

Please sign in to comment.