-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsummary.js
56 lines (49 loc) · 1.7 KB
/
summary.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const bookshelf = require("../db/bookshelf_init");
/*
MissingMaps began tracking OSM edit statistics in January 2016,
and this endpoint provides summary statistics for changesets
submitted after that date. The user count is an exception, as it
represents the entirety of participating users since 2014.
On the MissingMaps.org website, these statistics
are combined with a pre-2016 snapshot of edit statistics to
represent the entirety of edits since 2014.
*/
module.exports = [
{
method: "GET",
path: "/stats/{hashtag?}",
handler: async (req, res) => {
const { knex } = bookshelf;
let statsTable = knex
.select(
knex.raw("SUM(changesets) changesets"),
knex.raw("SUM(users) users"),
knex.raw("SUM(road_km_added + road_km_modified) as roads"),
knex.raw("SUM(buildings_added + buildings_modified) as buildings"),
knex.raw(
"SUM(buildings_added + buildings_modified + roads_added + roads_modified + waterways_added + waterways_modified + pois_added + pois_modified) as edits"
),
knex.raw("MAX(updated_at) as latest")
)
.from("hashtag_stats");
if (req.params.hashtag) {
statsTable = statsTable.where("hashtag", "=", req.params.hashtag);
}
try {
const results = await statsTable;
const row = results[0];
return res({
...row,
hashtag: req.params.hashtag || "*",
users: Number(row.users),
changesets: Number(row.changesets),
buildings: Number(row.buildings),
roads: Number(row.roads),
edits: Number(row.edits)
});
} catch (err) {
return res(err);
}
}
}
];