-
Notifications
You must be signed in to change notification settings - Fork 73.1k
Expand file tree
/
Copy pathdevicestatus.js
More file actions
149 lines (114 loc) · 3.21 KB
/
devicestatus.js
File metadata and controls
149 lines (114 loc) · 3.21 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
'use strict';
var moment = require('moment');
var find_options = require('./query');
function storage (collection, ctx) {
function create (statuses, fn) {
if (!Array.isArray(statuses)) { statuses = [statuses]; }
const r = [];
let errorOccurred = false;
for (let i = 0; i < statuses.length; i++) {
const obj = statuses[i];
if (errorOccurred) return;
// Normalize all dates to UTC
const d = moment(obj.created_at).isValid() ? moment.parseZone(obj.created_at) : moment();
obj.created_at = d.toISOString();
obj.utcOffset = d.utcOffset();
api().insertOne(obj, function(err, results) {
if (err) {
console.log('Error inserting the device status object', err.message);
errorOccurred = true;
fn(err.message, null);
return;
}
if (results) {
if (!obj._id) obj._id = results.insertedId;
r.push(obj);
ctx.bus.emit('data-update', {
type: 'devicestatus'
, op: 'update'
, changes: ctx.ddata.processRawDataForRuntime([obj])
});
// Last object! Return results
if (i == statuses.length - 1) {
fn(null, r);
ctx.bus.emit('data-received');
}
} else {
console.log('Error inserting the device status object', err.message);
errorOccurred = true;
fn(err.message, null);
return;
}
});
};
}
function last (fn) {
return list({ count: 1 }, function(err, entries) {
if (entries && entries.length > 0) {
fn(err, entries[0]);
} else {
fn(err, null);
}
});
}
function query_for (opts) {
return find_options(opts, storage.queryOpts);
}
function list (opts, fn) {
// these functions, find, sort, and limit, are used to
// dynamically configure the request, based on the options we've
// been given
// determine sort options
function sort () {
return opts && opts.sort || { created_at: -1 };
}
// configure the limit portion of the current query
function limit () {
if (opts && opts.count) {
return this.limit(parseInt(opts.count));
}
return this;
}
// handle all the results
function toArray (err, entries) {
fn(err, entries);
}
// now just stitch them all together
limit.call(api()
.find(query_for(opts))
.sort(sort())
).toArray(toArray);
}
function remove (opts, fn) {
function removed (err, stat) {
console.log('removed', err, stat);
ctx.bus.emit('data-update', {
type: 'devicestatus'
, op: 'remove'
, count: stat.deletedCount
, changes: opts.find._id
});
fn(err, stat);
}
return api().deleteMany(
query_for(opts), removed);
}
function api () {
return ctx.store.collection(collection);
}
api.list = list;
api.create = create;
api.query_for = query_for;
api.last = last;
api.remove = remove;
api.aggregate = require('./aggregate')({}, api);
api.indexedFields = [
'created_at'
, 'NSCLIENT_ID'
];
return api;
}
storage.queryOpts = {
dateField: 'created_at'
};
module.exports = storage;