git ssb

2+

mixmix / ticktack



Commit a893a560b31905d216fdb21487f862a094bb7d6f

MVP workings stats on stats page

mix irving committed on 4/19/2018, 12:21:03 AM
Parent: 0dff233ce29767d6fc042cf4bf59dcecdb8b347d

Files changed

app/page/statsShow.jschanged
app/page/statsShow.mcsschanged
ssb-server-blog-stats.jschanged
app/page/statsShow.jsView
@@ -1,7 +1,8 @@
11 const nest = require('depnest')
2-const { h, Value, Struct, Array: MutantArray, Dict, onceTrue, map, computed, dictToCollection } = require('mutant')
2+const { h, Value, Struct, Array: MutantArray, Dict, onceTrue, map, computed, dictToCollection, throttle } = require('mutant')
33 const pull = require('pull-stream')
4+const marksum = require('markdown-summary')
45
56 exports.gives = nest('app.page.statsShow')
67
78 exports.needs = nest({
@@ -20,20 +21,20 @@
2021 })
2122
2223 var howFarBack = Value(0)
2324 // stats show a moving window of 30 days
24- const now = Date.now()
2525 const thirtyDays = 30 * 24 * 60 * 60 * 1000
2626
2727 // TODO
2828 var range = computed([howFarBack], howFarBack => {
29+ const now = Date.now()
2930 return {
3031 upper: now - howFarBack * thirtyDays,
3132 lower: now - (howFarBack + 1) * thirtyDays
3233 }
3334 })
3435
35- var rangeComments = computed([dictToCollection(store.comments), range], (comments, range) => {
36+ var rangeComments = computed([throttle(dictToCollection(store.comments), 1000), range], (comments, range) => {
3637 return comments
3738 .map(c => c.value)
3839 .reduce((n, sofar) => [...n, ...sofar], [])
3940 .filter(msg => {
@@ -41,9 +42,9 @@
4142 return ts >= range.lower && ts <= range.upper
4243 })
4344 })
4445
45- var rangeLikes = computed([dictToCollection(store.likes), range], (likes, range) => {
46+ var rangeLikes = computed([throttle(dictToCollection(store.likes), 1000), range], (likes, range) => {
4647 return likes
4748 .map(c => c.value)
4849 .reduce((n, sofar) => [...n, ...sofar], [])
4950 .filter(msg => {
@@ -97,10 +98,10 @@
9798 ]),
9899 h('section.graph', [
99100 // TODO insert actual graph
100101 h('div', [
101- h('div', [ 'Comments ', map(rangeComments, msg => [new Date(msg.value.timestamp).toDateString(), ' ']) ]),
102- h('div', [ 'Likes ', map(rangeLikes, msg => [new Date(msg.value.timestamp).toDateString(), ' ']) ])
102+ // h('div', [ 'Comments ', map(rangeComments, msg => [new Date(msg.value.timestamp).toDateString(), ' ']) ]),
103+ // h('div', [ 'Likes ', map(rangeLikes, msg => [new Date(msg.value.timestamp).toDateString(), ' ']) ])
103104 ]),
104105 h('div', [
105106 h('a', { href: '#', 'ev-click': () => howFarBack.set(howFarBack() + 1) }, '< Prev 30 days'),
106107 ' | ',
@@ -115,11 +116,11 @@
115116 h('th.comment', 'Comments'),
116117 h('th.likes', 'Likes')
117118 ])
118119 ]),
119- h('tbody', map(store.blogs, blog => h('tr.blog', [
120+ h('tbody', map(store.blogs, blog => h('tr.blog', { id: blog.key }, [
120121 h('td.details', [
121- h('div.title', {}, blog.value.content.title),
122+ h('div.title', {}, getTitle(blog)),
122123 h('a',
123124 {
124125 href: '#',
125126 'ev-click': viewBlog(blog)
@@ -128,8 +129,9 @@
128129 )
129130 ]),
130131 h('td.comments', computed(store.comments.get(blog.key), msgs => msgs ? msgs.length : 0)),
131132 h('td.likes', computed(store.likes.get(blog.key), msgs => msgs ? msgs.length : 0))
133+ // ]), { comparer: (a, b) => a === b }))
132134 ])))
133135 ])
134136 ])
135137 ])
@@ -139,8 +141,14 @@
139141 return () => api.history.sync.push(blog)
140142 }
141143 }
142144
145+function getTitle (blog) {
146+ if (blog.value.content.title) return blog.value.content.title
147+ else if (blog.value.content.text) return marksum.title(blog.value.content.text)
148+ else return blog.key
149+}
150+
143151 function fetchBlogs ({ server, store }) {
144152 pull(
145153 server.blogStats.readBlogs({ reverse: false }),
146154 pull.drain(blog => {
@@ -156,10 +164,10 @@
156164 if (!store.comments.has(blog.key)) store.comments.put(blog.key, MutantArray())
157165
158166 pull(
159167 server.blogStats.readComments(blog),
160- pull.drain(comment => {
161- store.comments.get(blog.key).push(comment)
168+ pull.drain(msg => {
169+ store.comments.get(blog.key).push(msg)
162170 // TODO remove my comments from count?
163171 })
164172 )
165173 }
@@ -168,10 +176,10 @@
168176 if (!store.likes.has(blog.key)) store.likes.put(blog.key, MutantArray())
169177
170178 pull(
171179 server.blogStats.readLikes(blog),
172- pull.drain(comment => {
173- store.likes.get(blog.key).push(comment)
180+ pull.drain(msg => {
181+ store.likes.get(blog.key).push(msg)
174182 // TODO this needs reducing... like + unlike are muddled in here
175183 // find any thing by same author
176184 // if exists - over-write or delete
177185 })
app/page/statsShow.mcssView
@@ -2,9 +2,9 @@
22 div.content {
33 flex-grow: 0
44 $backgroundPrimaryText
55 margin-top: 1rem
6- width: 800px
6+ width: 1000px
77
88 h1 {
99 font-size: .8rem
1010 letter-spacing: 4px
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 = 5
13+const FLUME_VIEW_VERSION = 1
1414
1515 module.exports = {
1616 name: 'blogStats',
1717 version: 1,
@@ -47,9 +47,9 @@
4747 var root
4848
4949 switch (getType(msg)) {
5050 case 'blog':
51- if (isBlog(msg) && myBlog(msg)) return [['B', msg.key, getTimestamp(msg)]]
51+ if (isBlog(msg) && isMyMsg(msg)) return [['B', msg.key, getTimestamp(msg)]]
5252 else return []
5353
5454 case 'vote':
5555 // process.stdout.write('L')
@@ -62,12 +62,13 @@
6262 // - all likes, on all things D:
6363 // - likes AND unlikes
6464
6565 case 'post':
66- // process.stdout.write('C')
66+ // process.stdout.write('POST ')
6767 root = getCommentRoot(msg)
68- // TODO figure out how to only store likes I care about
69- if (root) return [['C', root, getTimestamp(msg)]]
68+ // TODO figure out how to only store comments I care about
69+ if (!root && isMyMsg(msg) && isPlog(msg)) return [['B', msg.key, getTimestamp(msg)]]
70+ else if (root) return [['C', root, getTimestamp(msg)]]
7071 else return []
7172
7273 // Note this catches:
7374 // - all comments, on all things D:
@@ -76,8 +77,15 @@
7677 return []
7778 }
7879 }
7980
81+ // a Plog is a Blog shaped Post
82+ function isPlog (msg) {
83+ // return false // Disable plogs
84+ if (get(msg, 'value.content.text', '').length >= 3000) console.log(get(msg, 'value.content.text', '').length)
85+ return get(msg, 'value.content.text', '').length >= 3000
86+ }
87+
8088 function readBlogs (options = {}) {
8189 const query = Object.assign({}, {
8290 gte: ['B', null, null],
8391 // null is the 'minimum' structure in bytewise ordering
@@ -98,11 +106,9 @@
98106 )
99107 }
100108
101109 function readComments (blog, options = {}) {
102- var key
103- if (isMsgRef(blog)) key = blog
104- else if (isMsgRef(blog.key) && isBlog(blog)) key = blog.key
110+ var key = getBlogKey(blog)
105111
106112 const query = Object.assign({}, {
107113 gt: ['C', key, null],
108114 lt: ['C', key, undefined],
@@ -116,11 +122,9 @@
116122 return view.read(query)
117123 }
118124
119125 function readLikes (blog, options = {}) {
120- var key
121- if (isMsgRef(blog)) key = blog
122- else if (isMsgRef(blog.key) && isBlog(blog)) key = blog.key
126+ var key = getBlogKey(blog)
123127
124128 const query = Object.assign({}, {
125129 // gt: ['L', key, null],
126130 // lt: ['L', key, undefined], // why doesn't this work?
@@ -134,9 +138,15 @@
134138
135139 return view.read(query)
136140 }
137141
138- function myBlog (msg) {
142+ function getBlogKey (blog) {
143+ if (isMsgRef(blog)) return blog
144+ // else if (isMsgRef(blog.key) && isBlog(blog)) return blog.key
145+ else if (isMsgRef(blog.key) && (isBlog(blog) || isPlog(blog))) return blog.key
146+ }
147+
148+ function isMyMsg (msg) {
139149 return getAuthor(msg) === myKey
140150 }
141151 }
142152 }

Built with git-ssb-web