-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
72 lines (55 loc) · 1.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const MClient = require('mongodb').MongoClient;
const collectionName = process.env.MAA_MONGODB_COLLECTION_NAME || 'ma-events';
const URL = process.env.MAA_MONGODB_URL || `mongodb://127.0.0.1:27017/ma-analyticsdb-test`;
let DB;
async function getMACollection() {
if(!DB) {
DB = await MClient.connect(URL);
}
return await DB.collection(collectionName);
}
async function get(key, options) {
const collection = await getMACollection();
const doc = await collection.findOne({ pathname: `${key}` }, { fields: { views: 1 } });
const views = (doc && doc.views ? doc.views : []).filter((view) => {
if(options && options.before && view.time > options.before ) return false;
if(options && options.after && view.time < options.after ) return false;
return true;
});
return { views };
}
async function put(key, value) {
const collection = await getMACollection();
if( value.views.length <= 1 ) {
return collection.insertOne({ pathname: `${key}`, views: value.views });
}
return collection.updateOne({ pathname: `${key}` }, { $set : { views: value.views } });
}
async function getAll(options) {
const paths = await keys();
const eventStats = {};
for(let i=0;i<paths.length;i++) {
const path = paths[i];
if( !options || (options && ((options.pathname && path.startsWith(options.pathname)) || !options.pathname)) ) {
eventStats[path] = await get(path, { before: options.before, after: options.after });
}
}
return eventStats;
}
async function has(key) {
const collection = await getMACollection();
const doc = await collection.find({ pathname: `${key}` }).toArray();
return Array.isArray(doc) && !!doc.length;
}
async function keys() {
const collection = await getMACollection();
const docs = await collection.find({}).toArray();
return (docs || []).map((doc) => { return doc.pathname; });
}
module.exports = {
get,
put,
getAll,
has,
keys
};