Skip to content

Commit 8ad4597

Browse files
committed
replaced fxa-geodb with load balancer header
1 parent 7d35e8f commit 8ad4597

File tree

9 files changed

+43
-98
lines changed

9 files changed

+43
-98
lines changed

package-lock.json

Lines changed: 0 additions & 68 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@
142142
"configstore": "github:dannycoates/configstore#master",
143143
"convict": "^5.2.0",
144144
"express": "^4.17.1",
145-
"fxa-geodb": "^1.0.4",
146145
"helmet": "^3.21.2",
147146
"mkdirp": "^0.5.1",
148147
"mozlog": "^2.2.0",

server/amplitude.js

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ const fetch = require('node-fetch');
33
const config = require('./config');
44
const pkg = require('../package.json');
55

6-
const geoip = config.ip_db
7-
? require('fxa-geodb')({ dbPath: config.ip_db })
8-
: () => ({});
9-
106
const HOUR = 1000 * 60 * 60;
117

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

27-
function location(ip) {
28-
try {
29-
return geoip(ip);
30-
} catch (e) {
31-
return {};
32-
}
33-
}
34-
3523
function statUploadEvent(data) {
36-
const loc = location(data.ip);
3724
const event = {
3825
session_id: -1,
39-
country: loc.country,
40-
region: loc.state,
26+
country: data.country,
27+
region: data.state,
4128
user_id: userId(data.id, data.owner),
4229
app_version: pkg.version,
4330
time: truncateToHour(Date.now()),
@@ -57,11 +44,10 @@ function statUploadEvent(data) {
5744
}
5845

5946
function statDownloadEvent(data) {
60-
const loc = location(data.ip);
6147
const event = {
6248
session_id: -1,
63-
country: loc.country,
64-
region: loc.state,
49+
country: data.country,
50+
region: data.state,
6551
user_id: userId(data.id, data.owner),
6652
app_version: pkg.version,
6753
time: truncateToHour(Date.now()),
@@ -77,11 +63,10 @@ function statDownloadEvent(data) {
7763
}
7864

7965
function statDeleteEvent(data) {
80-
const loc = location(data.ip);
8166
const event = {
8267
session_id: -1,
83-
country: loc.country,
84-
region: loc.state,
68+
country: data.country,
69+
region: data.state,
8570
user_id: userId(data.id, data.owner),
8671
app_version: pkg.version,
8772
time: truncateToHour(Date.now()),
@@ -97,11 +82,10 @@ function statDeleteEvent(data) {
9782
}
9883

9984
function statReportEvent(data) {
100-
const loc = location(data.ip);
10185
const event = {
10286
session_id: -1,
103-
country: loc.country,
104-
region: loc.state,
87+
country: data.country,
88+
region: data.state,
10589
user_id: userId(data.id, data.owner),
10690
app_version: pkg.version,
10791
time: truncateToHour(Date.now()),
@@ -118,8 +102,16 @@ function statReportEvent(data) {
118102
return sendBatch([event]);
119103
}
120104

121-
function clientEvent(event, ua, language, session_id, deltaT, platform, ip) {
122-
const loc = location(ip);
105+
function clientEvent(
106+
event,
107+
ua,
108+
language,
109+
session_id,
110+
deltaT,
111+
platform,
112+
country,
113+
state
114+
) {
123115
const ep = event.event_properties || {};
124116
const up = event.user_properties || {};
125117
const event_properties = {
@@ -155,15 +147,15 @@ function clientEvent(event, ua, language, session_id, deltaT, platform, ip) {
155147
};
156148
return {
157149
app_version: pkg.version,
158-
country: loc.country,
150+
country: country,
159151
device_id: event.device_id,
160152
event_properties,
161153
event_type: event.event_type,
162154
language,
163155
os_name: ua.os.name,
164156
os_version: ua.os.version,
165157
platform,
166-
region: loc.state,
158+
region: state,
167159
session_id,
168160
time: event.time + deltaT,
169161
user_id: event.user_id,

server/routes/delete.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ module.exports = async function(req, res) {
1111
statDeleteEvent({
1212
id,
1313
ip: req.ip,
14+
country: req.geo.country,
15+
state: req.geo.state,
1416
owner: meta.owner,
1517
download_count: meta.dl,
1618
ttl,

server/routes/download.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ module.exports = async function(req, res) {
3131
statDownloadEvent({
3232
id,
3333
ip: req.ip,
34+
country: req.geo.country,
35+
state: req.geo.state,
3436
owner: meta.owner,
3537
download_count: dl,
3638
ttl,

server/routes/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,19 @@ module.exports = function(app) {
9393
);
9494
next();
9595
});
96+
app.use(function(req, res, next) {
97+
try {
98+
// set by the load balancer
99+
const [country, state] = req.header('X-Client-Geo-Location').split(',');
100+
req.geo = {
101+
country,
102+
state
103+
};
104+
} catch (e) {
105+
req.geo = {};
106+
}
107+
next();
108+
});
96109
app.use(bodyParser.json());
97110
app.use(bodyParser.text());
98111
app.get('/', language, pages.index);

server/routes/metrics.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ module.exports = async function(req, res) {
1212
data.session_id + deltaT,
1313
deltaT,
1414
data.platform,
15-
req.ip
15+
req.geo.country,
16+
req.geo.state
1617
)
1718
);
1819
const status = await sendBatch(events);

server/routes/report.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ module.exports = async function(req, res) {
99
statReportEvent({
1010
id,
1111
ip: req.ip,
12+
country: req.geo.country,
13+
state: req.geo.state,
1214
owner: meta.owner,
1315
reason: req.body.reason,
1416
download_limit: meta.dlimit,

server/routes/ws.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ module.exports = function(ws, req) {
111111
statUploadEvent({
112112
id: newId,
113113
ip: req.ip,
114+
country: req.geo.country,
115+
state: req.geo.state,
114116
owner,
115117
dlimit,
116118
timeLimit,

0 commit comments

Comments
 (0)