git ssb

0+

cel / ssb-activity



Tree: e6771acdf6578c1d07533da3811ea9d6fc8575a7

Files: e6771acdf6578c1d07533da3811ea9d6fc8575a7 / run.js

2368 bytesRaw
1var fs = require('fs')
2var path = require('path')
3var pull = require('pull-stream')
4var File = require('pull-file')
5var Split = require('pull-split')
6var PNG = require('pngjs').PNG
7var common = require('./common')
8
9var start = new Date('2015-08-05').getTime()
10var startDay = Math.floor(start / 86400000)
11var startWeek = Math.floor(start / 86400000 / 7)
12var startMonth = Math.floor(start / 86400000 / 30.43666666666666666666)
13var startT
14
15var now = Date.now()
16var nowDay = Math.floor(now / 86400000)
17var nowWeek = Math.floor(now / 86400000 / 7)
18var nowMonth = Math.floor(now / 86400000 / 30.43666666666666666666)
19
20var series = process.argv[2]
21if (!series) throw new TypeError('Missing series argument')
22
23if (series === 'day') {
24 width = nowDay - startDay
25 startT = startDay
26} else if (series === 'week') {
27 width = nowWeek - startWeek
28 startT = startWeek
29} else if (series === 'month') {
30 width = nowMonth - startMonth
31 startT = startMonth
32} else {
33 throw new TypeError('Invalid series')
34}
35
36var info = JSON.parse(fs.readFileSync(path.join(__dirname, series + '.json')))
37var ids = JSON.parse(fs.readFileSync(path.join(__dirname, 'ids.json')))
38
39var width
40var height = info.max
41
42var png = new PNG({
43 width: width,
44 height: height,
45 colorType: 6
46})
47
48var userColors = {}
49var userNumbers = {}
50
51for (var i = 0; i < ids.length; i++) {
52 var id = ids[i]
53 userNumbers[id] = i
54 var x = i / ids.length
55 var colorI = Math.floor(x*20)
56 var inbetweenAmount = x*20 - colorI
57 var c = common.interpolate(common.colors20[colorI], common.colors20[colorI+1], inbetweenAmount)
58 c[3] = 255
59 userColors[id] = c
60}
61
62function setPixel(data, x, y, color) {
63 var i = (y * width + x) << 2
64 data[i] = color[0]
65 data[i+1] = color[1]
66 data[i+2] = color[2]
67 data[i+3] = color[3]
68}
69
70function compareIds(a, b) {
71 return userNumbers[a] - userNumbers[b]
72}
73
74pull(
75 File(path.join(__dirname, series + '.jsonp')),
76 Split(),
77 pull.filter(Boolean),
78 pull.map(JSON.parse),
79 pull.drain(function (interval) {
80 var x = interval.time - startT
81 var ids = interval.ids.sort(compareIds)
82 for (var i = 0; i < ids.length; i++) {
83 var id = ids[i]
84 var y = height - i
85 var color = userColors[id] || [0, 0, 0, 255]
86 setPixel(png.data, x, y, color)
87 }
88 }, function (err) {
89 if (err) throw err
90    png.pack().pipe(fs.createWriteStream(series + '.png'))
91 })
92)
93

Built with git-ssb-web