git ssb

0+

cel / ssb-activity



Tree: 6349a39796d35a2159558bb5333e2a3762fd9463

Files: 6349a39796d35a2159558bb5333e2a3762fd9463 / run.js

2601 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 = {}
50var colorsUsed = {}
51
52for (var i = 0; i < ids.length; i++) {
53 var id = ids[i]
54 userNumbers[id] = i
55 var x = i / ids.length
56 var colorI = Math.floor(x*20)
57 var inbetweenAmount = x*20 - colorI
58 var color = common.interpolate(common.colors20[colorI], common.colors20[colorI+1], inbetweenAmount)
59 color[0] = Math.floor(color[0])
60 color[1] = Math.floor(color[1])
61 color[2] = Math.floor(color[2])
62 color[3] = 255
63 // Ensure uniqueness
64 while (colorsUsed[color]) {
65 color[3]--
66 }
67 colorsUsed[color] = true
68 userColors[id] = color
69}
70
71function setPixel(data, x, y, color) {
72 var i = (y * width + x) << 2
73 data[i] = color[0]
74 data[i+1] = color[1]
75 data[i+2] = color[2]
76 data[i+3] = color[3]
77}
78
79function compareIds(a, b) {
80 return userNumbers[a] - userNumbers[b]
81}
82
83pull(
84 File(path.join(__dirname, series + '.jsonp')),
85 Split(),
86 pull.filter(Boolean),
87 pull.map(JSON.parse),
88 pull.drain(function (interval) {
89 var x = interval.time - startT
90 var ids = interval.ids.sort(compareIds)
91 for (var i = 0; i < ids.length; i++) {
92 var id = ids[i]
93 var y = height - i
94 var color = userColors[id] || [0, 0, 0, 255]
95 setPixel(png.data, x, y, color)
96 }
97 }, function (err) {
98 if (err) throw err
99    png.pack().pipe(fs.createWriteStream(series + '.png'))
100 })
101)
102

Built with git-ssb-web