git ssb

0+

cel / ssb-activity



Tree: 9241af5555557951a99069e9e9a1f2c29a6623d6

Files: 9241af5555557951a99069e9e9a1f2c29a6623d6 / run.js

2662 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 = common.startTime
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 info1 = JSON.parse(fs.readFileSync(path.join(__dirname, 'ids-colors.json')))
38var ids = info1.ids
39var userColors = info1.colors
40var userColorsFile = path.join(__dirname, 'colors-flat.json')
41var userColorsFlat = fs.existsSync(userColorsFile)
42 && JSON.parse(fs.readFileSync(userColorsFile))
43
44var width
45var height = info.max
46
47var png = new PNG({
48 width: width,
49 height: height,
50 colorType: 6
51})
52
53var pngFlat = userColorsFlat && new PNG({
54 width: width,
55 height: height,
56 colorType: 6
57})
58
59var userNumbers = {}
60for (var i = 0; i < ids.length; i++) {
61 var id = ids[i]
62 userNumbers[id] = i
63}
64
65function setPixel(data, x, y, color) {
66 var i = (y * width + x) << 2
67 data[i] = color[0]
68 data[i+1] = color[1]
69 data[i+2] = color[2]
70 data[i+3] = color[3]
71}
72
73function compareIds(a, b) {
74 return userNumbers[a] - userNumbers[b]
75}
76
77pull(
78 File(path.join(__dirname, series + '.jsonp')),
79 Split(),
80 pull.filter(Boolean),
81 pull.map(JSON.parse),
82 pull.drain(function (interval) {
83 var x = interval.time - startT
84 if (x <= 0 || x >= width) return
85 var ids = interval.ids.sort(compareIds)
86 for (var i = 0; i < ids.length; i++) {
87 var id = ids[i]
88 var y = height - i
89 var color = userColors[id] || [0, 0, 0, 255]
90 setPixel(png.data, x, y, color)
91 if (userColorsFlat) {
92 color = userColorsFlat[id] || [0, 0, 0, 255]
93 setPixel(pngFlat.data, x, y, color)
94 }
95 }
96 }, function (err) {
97 if (err) throw err
98    png.pack().pipe(fs.createWriteStream(series + '.png'))
99    pngFlat.pack().pipe(fs.createWriteStream(series + '-flat.png'))
100 })
101)
102

Built with git-ssb-web