git ssb

0+

cel / ssb-activity



Tree: cec705118d65476d8ba5b903fb836cae42f2f1dd

Files: cec705118d65476d8ba5b903fb836cae42f2f1dd / index.js

2352 bytesRaw
1var FlumeViewLevel = require('flumeview-level')
2var pull = require('pull-stream')
3
4function map(msg) {
5 var author = msg.value.author
6 var ts = msg.value.timestamp
7 if (!ts) return []
8
9 var day = ts / 86400000
10 var week = day / 7
11 var month = day / 30.43666666666666666666 // 365.24 / 12
12
13 // record that the feed author was active this day, week and month
14 return [
15 ['day', Math.floor(day), author],
16 ['week', Math.floor(week), author],
17 ['month', Math.floor(month), author]
18 ]
19}
20
21var validSeries = {
22 day: true,
23 week: true,
24 month: true
25}
26
27function groupByIds(opts) {
28 var prevTime, ids
29 var listIds = Boolean(opts.ids)
30 return pull.map(function (indexed) {
31 var key = indexed.key
32 var id = key[2]
33 var time = key[1]
34 if (time === prevTime) {
35 // count unique feed ids in the time interval
36 if (listIds) ids.push(id)
37 else ids++
38 } else {
39 var ret = prevTime && {time: prevTime, ids: ids}
40 prevTime = time
41 ids = listIds ? [id] : 1
42 return ret
43 }
44 })
45}
46
47exports.name = 'activity'
48exports.version = '0.0.0'
49exports.manifest = {
50 read: 'source'
51}
52
53exports.init = function (sbot, config) {
54 var version = 2
55 var deleteTheDb = false
56 var view = FlumeViewLevel(deleteTheDb, map)
57 var activity = sbot._flumeUse('activity' + version, view)
58
59 return {
60 read: function (opts) {
61 if (!opts) return pull.error(new TypeError('Missing opts'))
62 var series = opts.series
63 if (!series) return pull.error(new TypeError('Missing opts.series'))
64 if (!validSeries[series]) return pull.error(new TypeError('Invalid series'))
65 var readOpts = {}
66 var groupOpts = {
67 ids: opts.ids
68 }
69 for (var k in opts) {
70 if (k === 'series' || k === 'ids') continue
71 var value = opts[k]
72 // transform key ranges from query opts into internal key ranges
73 if (k === 'gt' || k === 'gte') value = [series, value, null]
74 else if (k === 'lt' || k === 'lte') value = [series, value, undefined]
75 readOpts[k] = value
76 }
77 if (!readOpts.gt && !readOpts.gte) readOpts.gt = [series, null, null]
78 if (!readOpts.lt && !readOpts.lte) readOpts.lt = [series, undefined, undefined]
79 return pull(
80 activity.read(readOpts),
81 groupByIds(groupOpts),
82 pull.filter(Boolean)
83 )
84 }
85 }
86}
87

Built with git-ssb-web