app/page/statsShow.jsView |
---|
1 | 1 | const nest = require('depnest') |
2 | 2 | const { h, Value, Struct, Array: MutantArray, Dict, onceTrue, map, computed, dictToCollection, throttle } = require('mutant') |
3 | 3 | const pull = require('pull-stream') |
4 | 4 | const marksum = require('markdown-summary') |
| 5 | +const Chart = require('chart.js') |
| 6 | +const groupBy = require('lodash/groupBy') |
5 | 7 | |
6 | 8 | exports.gives = nest('app.page.statsShow') |
7 | 9 | |
8 | 10 | exports.needs = nest({ |
21 | 23 | }) |
22 | 24 | |
23 | 25 | var howFarBack = Value(0) |
24 | 26 | |
25 | | - const thirtyDays = 30 * 24 * 60 * 60 * 1000 |
| 27 | + const THIRTY_DAYS = 30 * 24 * 60 * 60 * 1000 |
26 | 28 | |
27 | 29 | |
28 | 30 | var range = computed([howFarBack], howFarBack => { |
29 | 31 | const now = Date.now() |
30 | 32 | return { |
31 | | - upper: now - howFarBack * thirtyDays, |
32 | | - lower: now - (howFarBack + 1) * thirtyDays |
| 33 | + upper: now - howFarBack * THIRTY_DAYS, |
| 34 | + lower: now - (howFarBack + 1) * THIRTY_DAYS |
33 | 35 | } |
34 | 36 | }) |
35 | 37 | |
36 | 38 | var rangeComments = computed([throttle(dictToCollection(store.comments), 1000), range], (comments, range) => { |
77 | 79 | |
78 | 80 | |
79 | 81 | |
80 | 82 | }) |
| 83 | + const canvas = h('canvas') |
81 | 84 | |
82 | | - return h('Page -statsShow', [ |
| 85 | + const page = h('Page -statsShow', [ |
83 | 86 | h('div.content', [ |
84 | 87 | h('h1', 'Stats'), |
85 | 88 | h('section.totals', [ |
86 | 89 | h('div.comments', [ |
96 | 99 | h('div.shares', [ |
97 | 100 | ]) |
98 | 101 | ]), |
99 | 102 | h('section.graph', [ |
| 103 | + canvas, |
100 | 104 | |
101 | 105 | h('div', [ |
102 | 106 | |
103 | 107 | |
104 | 108 | ]), |
105 | | - h('div', [ |
| 109 | + h('div.changeRange', [ |
106 | 110 | h('a', { href: '#', 'ev-click': () => howFarBack.set(howFarBack() + 1) }, '< Prev 30 days'), |
107 | 111 | ' | ', |
108 | | - h('a', { href: '#', 'ev-click': () => howFarBack.set(howFarBack() - 1) }, 'Next 30 days >'), |
109 | | - h('div', ['howFarBack:', howFarBack]) |
| 112 | + h('a', { href: '#', 'ev-click': () => howFarBack.set(howFarBack() - 1) }, 'Next 30 days >') |
110 | 113 | ]) |
111 | 114 | ]), |
112 | 115 | h('table.blogs', [ |
113 | 116 | h('thead', [ |
134 | 137 | ]))) |
135 | 138 | ]) |
136 | 139 | ]) |
137 | 140 | ]) |
| 141 | + |
| 142 | + Chart.scaleService.updateScaleDefaults('linear', { |
| 143 | + ticks: { min: 0 } |
| 144 | + }) |
| 145 | + var chart = new Chart(canvas.getContext('2d'), { |
| 146 | + type: 'bar', |
| 147 | + data: { |
| 148 | + datasets: [{ |
| 149 | + |
| 150 | + backgroundColor: 'hsla(215, 57%, 60%, 1)', |
| 151 | + borderColor: 'hsla(215, 57%, 60%, 1)', |
| 152 | + |
| 153 | + data: [ |
| 154 | + ] |
| 155 | + }] |
| 156 | + }, |
| 157 | + options: { |
| 158 | + legend: { |
| 159 | + display: false |
| 160 | + }, |
| 161 | + scales: { |
| 162 | + xAxes: [{ |
| 163 | + type: 'time', |
| 164 | + distribution: 'linear', |
| 165 | + time: { |
| 166 | + unit: 'day', |
| 167 | + min: new Date(range().lower), |
| 168 | + max: new Date(range().upper) |
| 169 | + }, |
| 170 | + bounds: 'ticks', |
| 171 | + ticks: { |
| 172 | + maxTicksLimit: 4 |
| 173 | + }, |
| 174 | + gridLines: { |
| 175 | + display: false |
| 176 | + }, |
| 177 | + maxBarThickness: 20 |
| 178 | + }], |
| 179 | + |
| 180 | + yAxes: [{ |
| 181 | + ticks: { |
| 182 | + suggestedMin: 0, |
| 183 | + suggestedMax: 10, |
| 184 | + maxTicksLimit: 5 |
| 185 | + } |
| 186 | + }] |
| 187 | + }, |
| 188 | + animation: { |
| 189 | + duration: 300 |
| 190 | + } |
| 191 | + } |
| 192 | + }) |
| 193 | + |
| 194 | + const toDay = ts => Math.floor(ts / (24 * 60 * 60 * 1000)) |
| 195 | + const rangeCommentData = computed(rangeComments, msgs => { |
| 196 | + const grouped = groupBy(msgs, m => toDay(m.value.timestamp)) |
| 197 | + |
| 198 | + var data = Object.keys(grouped) |
| 199 | + .map(day => { |
| 200 | + return { |
| 201 | + t: day * 24 * 60 * 60 * 1000, |
| 202 | + y: grouped[day].length |
| 203 | + } |
| 204 | + }) |
| 205 | + return data |
| 206 | + }) |
| 207 | + rangeCommentData((newData) => { |
| 208 | + chart.data.datasets[0].data = newData |
| 209 | + |
| 210 | + chart.options.scales.xAxes[0].time.min = new Date(range().lower) |
| 211 | + chart.options.scales.xAxes[0].time.max = new Date(range().upper) |
| 212 | + |
| 213 | + chart.update() |
| 214 | + }) |
| 215 | + |
| 216 | + return page |
138 | 217 | } |
139 | 218 | |
140 | 219 | function viewBlog (blog) { |
141 | 220 | return () => api.history.sync.push(blog) |