-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathrender.ts
More file actions
232 lines (209 loc) · 6.23 KB
/
render.ts
File metadata and controls
232 lines (209 loc) · 6.23 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import {
createGist, findTarget, getJsonFileContent, updateGist,
type FileForm, type GistForm,
} from "@api/gist"
import {
init,
type ComposeOption, type EChartsType, type GridComponentOption, type LineSeriesOption, type TitleComponentOption,
} from "echarts"
import { writeFileSync } from "fs"
import { exit } from 'process'
import { filenameOf, getExistGist, validateTokenFromEnv } from "./common"
type EcOption = ComposeOption<
| LineSeriesOption
| TitleComponentOption
| GridComponentOption
>
const ALL_BROWSERS: Browser[] = ['firefox', 'edge', 'chrome']
const POINT_COUNT = 500
type OriginData = {
[browser in Browser]: UserCount
}
type ChartData = {
xAxis: string[]
yAxises: {
[browser in Browser]: number[]
}
}
function preProcess(originData: OriginData): ChartData {
// 1. sort dates
const dateSet = new Set<string>()
Object.values(originData).forEach(ud => Object.keys(ud).forEach(date => dateSet.add(date)))
let allDates = Array.from(dateSet).sort()
// 2. smooth the count
const ctx: { [browser in Browser]: SmoothContext } = {
chrome: new SmoothContext(),
firefox: new SmoothContext(),
edge: new SmoothContext(),
}
allDates.forEach(
date => ALL_BROWSERS.forEach(b => ctx[b].process(originData[b][date]))
)
const result: ChartData = {
xAxis: allDates,
yAxises: {
chrome: ctx.chrome.end(),
firefox: ctx.firefox.end(),
edge: ctx.edge.end(),
}
}
// 3. zoom
const reduction = Math.floor(Object.keys(allDates).length / POINT_COUNT)
result.xAxis = zoom(result.xAxis, reduction)
ALL_BROWSERS.forEach(b => result.yAxises[b] = zoom(result.yAxises[b], reduction))
return result
}
class SmoothContext {
lastVal: number
step: number
data: number[]
constructor() {
this.lastVal = 0
this.step = 0
this.data = []
}
/**
* Process value
*/
process(newVal: number | undefined) {
if (newVal) {
this.smooth(newVal)
} else {
this.increaseStep()
}
}
smooth(currentValue: number): void {
if (this.step < 0) {
return
}
const unitVal = (currentValue - this.lastVal) / (this.step + 1)
Object.keys(Array.from(new Array(this.step)))
.map(key => parseInt(key))
.map(i => Math.floor(unitVal * (i + 1) + this.lastVal))
.forEach(smoothedVal => this.data.push(smoothedVal))
this.data.push(currentValue)
// Reset
this.lastVal = currentValue
this.step = 0
}
increaseStep(): void {
this.step += 1
}
end(): number[] {
Object.keys(Array.from(new Array(this.step)))
.forEach(() => this.data.push(this.lastVal))
return this.data
}
}
function zoom<T>(data: T[], reduction: number): T[] {
let i = 0
const newData: T[] = []
while (i < data.length) {
newData.push(data[i])
i += reduction
}
return newData
}
function render2Svg(chartData: ChartData): string {
const { xAxis, yAxises } = chartData
const chart: EChartsType = init(null, null, {
renderer: 'svg',
ssr: true,
width: 960,
height: 640
})
const totalUserCount = Object.values(yAxises)
.map(v => v[v.length - 1] || 0)
.reduce((a, b) => a + b)
const option: EcOption = {
title: {
text: 'Total Active User Count',
subtext: `${xAxis[0]} to ${xAxis[xAxis.length - 1]} | currently ${totalUserCount} `
},
legend: { data: ALL_BROWSERS },
grid: {
left: '3%',
right: '4%',
bottom: '8%',
containLabel: true
},
xAxis: { type: 'time' },
yAxis: {
type: 'value',
minInterval: 100,
axisLabel: {
formatter(value) {
const text = value.toString()
const textLen = text.length
return textLen < 4 ? text : text.substring(0, textLen - 3) + 'K'
},
},
},
series: ALL_BROWSERS.map(b => ({
name: b,
type: 'line',
stack: 'Total',
// Fill the area
areaStyle: {},
lineStyle: { width: 0 },
showSymbol: false,
data: yAxises[b].map((val, idx) => [xAxis[idx], val]),
}))
}
chart.setOption(option)
return chart.renderToSVGString()
}
const USER_COUNT_GIST_DESC = "User count of timer, auto-generated"
const USER_COUNT_SVG_FILE_NAME = "user_count.svg"
async function getOriginData(token: string): Promise<OriginData> {
const [firefox, edge, chrome]: UserCount[] = await Promise.all(
ALL_BROWSERS.map(b => getDataFromGist(token, b))
)
return { chrome, firefox, edge }
}
/**
* Get the data from gist
*/
async function getDataFromGist(token: string, browser: Browser): Promise<UserCount> {
const gist = await getExistGist(token, browser)
const file = gist?.files[filenameOf(browser)]
return (file && await getJsonFileContent<UserCount>(file)) ?? {}
}
/**
* Upload svg string to gist
*/
async function upload2Gist(token: string, svg: string) {
const files: Record<string, FileForm> = {}
files[USER_COUNT_SVG_FILE_NAME] = {
filename: USER_COUNT_SVG_FILE_NAME,
content: svg
}
const form: GistForm = {
public: true,
description: USER_COUNT_GIST_DESC,
files
}
const gist = await findTarget(token, gist => gist.description === USER_COUNT_GIST_DESC)
if (gist) {
await updateGist(token, gist.id, form)
console.log('Updated gist')
} else {
await createGist(token, form)
console.log('Created new gist')
}
}
async function main(): Promise<void> {
const token = validateTokenFromEnv()
// 1. get all data
const originData: OriginData = await getOriginData(token)
// 2. pre-process data
const chartData = preProcess(originData)
// 3. render csv
const svg = render2Svg(chartData)
writeFileSync('user-chart.svg', svg, 'utf-8')
// 4. upload
await upload2Gist(token, svg)
// 5. finish
exit()
}
main()