This repository was archived by the owner on Dec 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRecord.js
102 lines (88 loc) · 2.5 KB
/
Record.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
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
// TODO : Periodically revisit validation for this model, per this:
// todo https://github.com/conix-center/ATLAS/issues/1 --ltj
/**
* Record.js
*
* @description :: An ATLAS record which contains info to connect to ARENA
things
* @docs :: https://docs.google.com/presentation/d/1dc1RdlGROBYj1zIoPR8HX_RBIKn8-KRmNZscXVrdIs0/edit#slide=id.g60b507f38e_11_80
* https://sailsjs.com/documentation/concepts/models-and-orm/models
* RFC 4122 defines UUID
*/
const util = require('util');
module.exports = {
attributes: {
name: {
type: 'string',
required: true,
},
url: {
type: 'string',
required: true,
isURL: true,
},
lat: {
type: 'number',
required: false,
min: -90,
max: 90
},
long: {
type: 'number',
required: false,
min: -180,
max: 180
},
ele: {
type: 'number',
required: false,
},
pose: {
type: 'json',
required: false,
},
objectType: {
type: 'string',
required: false
}
},
afterCreate: async (record, proceed) => {
let key = sails.config.custom.redis.geokey;
await sails.getDatastore('redis').leaseConnection(async (db) => {
await (util.promisify(db.geoadd).bind(db))(key, record.long, record.lat, record.id);
});
proceed();
},
afterUpdate: async (record, proceed) => {
let key = sails.config.custom.redis.geokey;
await sails.getDatastore('redis').leaseConnection(async (db) => {
await (util.promisify(db.geoadd).bind(db))(key, record.long, record.lat, record.id);
});
proceed();
},
beforeDestroy: async (record, proceed) => {
let key = sails.config.custom.redis.geokey;
await sails.getDatastore('redis').leaseConnection(async (db) => {
await (util.promisify(db.zrem).bind(db))(key, record.where.id);
});
proceed();
},
mergeGeoResults: async (geoArr, units, ignore, filter) => {
let ids = [];
let distances = {};
for (let i = 0, len = geoArr.length; i < len; i++) {
if (geoArr[i][0] === ignore) { continue; }
ids.push(geoArr[i][0]);
distances[geoArr[i][0]] = geoArr[i][1];
}
let query = { id: ids };
if (filter) {
Object.assign(query, filter);
}
let mongoRecords = await Record.find(query);
for (let i = 0, len = mongoRecords.length; i < len; i++) {
mongoRecords[i].distance = distances[mongoRecords[i].id] + ' ' + units;
}
return mongoRecords;
}
};