Files: 8b2bb74986427753f3b1628bd266bcc7bd7693ea / counts.js
1995 bytesRaw
1 | var fs = require('fs') |
2 | var path = require('path') |
3 | var pull = require('pull-stream') |
4 | var File = require('pull-file') |
5 | var Split = require('pull-split') |
6 | var common = require('./common') |
7 | |
8 | var start = common.startTime |
9 | var startDay = Math.floor(start / 86400000) |
10 | var startWeek = Math.floor(start / 86400000 / 7) |
11 | var startMonth = Math.floor(start / 86400000 / 30.43666666666666666666) |
12 | var startT |
13 | |
14 | var now = Date.now() |
15 | var nowDay = Math.floor(now / 86400000) |
16 | var nowWeek = Math.floor(now / 86400000 / 7) |
17 | var nowMonth = Math.floor(now / 86400000 / 30.43666666666666666666) |
18 | |
19 | var series = process.argv[2] |
20 | if (!series) throw new TypeError('Missing series argument') |
21 | var unit = 1 |
22 | |
23 | if (series === 'day') { |
24 | startT = startDay |
25 | unit = 86400e3 |
26 | } else if (series === 'week') { |
27 | startT = startWeek |
28 | unit = 86400e3*7 |
29 | } else if (series === 'month') { |
30 | startT = startMonth |
31 | unit = 86400e3*(365.24/12) |
32 | } else { |
33 | throw new TypeError('Invalid series') |
34 | } |
35 | |
36 | var info = JSON.parse(fs.readFileSync(path.join(__dirname, series + '.json'))) |
37 | var info1 = JSON.parse(fs.readFileSync(path.join(__dirname, 'ids-colors.json'))) |
38 | var ids = info1.ids |
39 | var userColors = info1.colors |
40 | |
41 | var height = info.max |
42 | |
43 | var last = Date.now() - 86400e3 |
44 | |
45 | function toDateString(ms) { |
46 | var d |
47 | if (series === 'month') { |
48 | // Pick a date in the middle of the month, to correct for variations in |
49 | // month length. |
50 | ms += 3*86400e3 |
51 | d = new Date(ms) |
52 | return d.toISOString().replace(/-..T.*/, '') |
53 | } else { |
54 | d = new Date(ms) |
55 | return d.toISOString().replace(/T.*/, '') |
56 | } |
57 | } |
58 | |
59 | var max = 0 |
60 | var maxTs |
61 | |
62 | pull( |
63 | File(path.join(__dirname, series + '.jsonp')), |
64 | Split(), |
65 | pull.filter(Boolean), |
66 | pull.map(JSON.parse), |
67 | pull.drain(function (interval) { |
68 | var ms = interval.time * unit |
69 | if (ms > last) return |
70 | var count = interval.ids.length |
71 | if (count > max) max = count, maxTs = ms |
72 | console.log(toDateString(ms), count) |
73 | }, function (err) { |
74 | if (err) throw err |
75 | console.log(toDateString(maxTs), max, 'max') |
76 | }) |
77 | ) |
78 |
Built with git-ssb-web