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