git ssb

2+

mixmix / ticktack



Commit 99cb8e2543b997e4f1db57956811f7789da47388

working minimal graph!

mix irving committed on 4/19/2018, 11:50:32 AM
Parent: a893a560b31905d216fdb21487f862a094bb7d6f

Files changed

app/page/statsShow.jschanged
app/page/statsShow.mcsschanged
package-lock.jsonchanged
package.jsonchanged
ssb-server-blog-stats.jschanged
app/page/statsShow.jsView
@@ -1,8 +1,10 @@
11 const nest = require('depnest')
22 const { h, Value, Struct, Array: MutantArray, Dict, onceTrue, map, computed, dictToCollection, throttle } = require('mutant')
33 const pull = require('pull-stream')
44 const marksum = require('markdown-summary')
5+const Chart = require('chart.js')
6+const groupBy = require('lodash/groupBy')
57
68 exports.gives = nest('app.page.statsShow')
79
810 exports.needs = nest({
@@ -21,16 +23,16 @@
2123 })
2224
2325 var howFarBack = Value(0)
2426 // stats show a moving window of 30 days
25- const thirtyDays = 30 * 24 * 60 * 60 * 1000
27+ const THIRTY_DAYS = 30 * 24 * 60 * 60 * 1000
2628
2729 // TODO
2830 var range = computed([howFarBack], howFarBack => {
2931 const now = Date.now()
3032 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
3335 }
3436 })
3537
3638 var rangeComments = computed([throttle(dictToCollection(store.comments), 1000), range], (comments, range) => {
@@ -77,10 +79,11 @@
7779 // pull.log(() => console.log('DONE'))
7880 // )
7981 /// ///// test code /////
8082 })
83+ const canvas = h('canvas')
8184
82- return h('Page -statsShow', [
85+ const page = h('Page -statsShow', [
8386 h('div.content', [
8487 h('h1', 'Stats'),
8588 h('section.totals', [
8689 h('div.comments', [
@@ -96,18 +99,18 @@
9699 h('div.shares', [
97100 ])
98101 ]),
99102 h('section.graph', [
103+ canvas,
100104 // TODO insert actual graph
101105 h('div', [
102106 // h('div', [ 'Comments ', map(rangeComments, msg => [new Date(msg.value.timestamp).toDateString(), ' ']) ]),
103107 // h('div', [ 'Likes ', map(rangeLikes, msg => [new Date(msg.value.timestamp).toDateString(), ' ']) ])
104108 ]),
105- h('div', [
109+ h('div.changeRange', [
106110 h('a', { href: '#', 'ev-click': () => howFarBack.set(howFarBack() + 1) }, '< Prev 30 days'),
107111 ' | ',
108- h('a', { href: '#', 'ev-click': () => howFarBack.set(howFarBack() - 1) }, 'Next 30 days >'),
109- h('div', ['howFarBack:', howFarBack]) // TODO change - this is temporary
112+ h('a', { href: '#', 'ev-click': () => howFarBack.set(howFarBack() - 1) }, 'Next 30 days >')
110113 ])
111114 ]),
112115 h('table.blogs', [
113116 h('thead', [
@@ -134,8 +137,84 @@
134137 ])))
135138 ])
136139 ])
137140 ])
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+ // label: 'My First dataset',
150+ backgroundColor: 'hsla(215, 57%, 60%, 1)', // 'hsla(215, 57%, 43%, 1)',
151+ borderColor: 'hsla(215, 57%, 60%, 1)',
152+ // TODO set initial data as empty to make a good range
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
138217 }
139218
140219 function viewBlog (blog) {
141220 return () => api.history.sync.push(blog)
app/page/statsShow.mcssView
@@ -26,9 +26,20 @@
2626 }
2727 }
2828
2929 section.graph {
30+ display: flex
31+ flex-wrap: wrap
32+ justify-content: center
33+
3034 margin: 2rem 0
35+
36+ canvas {
37+ margin-bottom: 1rem
38+ }
39+ div.changeRange {
40+
41+ }
3142 }
3243
3344 table.blogs {
3445 margin: 1rem 0
package-lock.jsonView
The diff is too large to show. Use a local git client to view these changes.
Old file size: 248886 bytes
New file size: 250496 bytes
package.jsonView
@@ -20,8 +20,9 @@
2020 },
2121 "author": "",
2222 "license": "GPL-3.0",
2323 "dependencies": {
24+ "chart.js": "^2.7.2",
2425 "cross-script": "^1.0.5",
2526 "depject": "^4.1.1",
2627 "depnest": "^1.3.0",
2728 "electron-default-menu": "^1.0.1",
ssb-server-blog-stats.jsView
@@ -9,9 +9,9 @@
99 const getCommentRoot = (msg) => get(msg, 'value.content.root')
1010 const getLikeRoot = (msg) => get(msg, 'value.content.vote.link')
1111 const getTimestamp = (msg) => get(msg, 'value.timestamp')
1212
13-const FLUME_VIEW_VERSION = 1
13+const FLUME_VIEW_VERSION = 2
1414
1515 module.exports = {
1616 name: 'blogStats',
1717 version: 1,
@@ -88,9 +88,9 @@
8888 function readBlogs (options = {}) {
8989 const query = Object.assign({}, {
9090 gte: ['B', null, null],
9191 // null is the 'minimum' structure in bytewise ordering
92- lte: ['B~', undefined, undefined],
92+ lte: ['B', undefined, undefined],
9393 reverse: true,
9494 values: true,
9595 keys: false,
9696 seqs: false
@@ -125,12 +125,10 @@
125125 function readLikes (blog, options = {}) {
126126 var key = getBlogKey(blog)
127127
128128 const query = Object.assign({}, {
129- // gt: ['L', key, null],
130- // lt: ['L', key, undefined], // why doesn't this work?
131- gt: ['L', key, null], // null is minimum in bytewise ordering
132- lt: ['L', key + '~', undefined], // undefinted in maximum in bytewise ordering
129+ gt: ['L', key, null],
130+ lt: ['L', key, undefined],
133131 reverse: true,
134132 values: true,
135133 keys: false,
136134 seqs: false

Built with git-ssb-web