-
Notifications
You must be signed in to change notification settings - Fork 0
/
activity-db.js
300 lines (266 loc) · 8.49 KB
/
activity-db.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
var sqlite = require('sqlite');
var path = require('path');
var Promise = require('promise');
function Database()
{
this.dbPromise = Promise.resolve(sqlite.open(path.join(__dirname, 'storage', 'database')));
this.dbPromise.done(function() {});
}
function sqliteAffinity(value)
{
switch(typeof value)
{
case 'string':
return 'TEXT';
case 'number':
return 'NUMERIC';
case 'object':
return 'BLOB';
default:
return 'BLOB';
}
}
function objToSqliteSpec(obj)
{
return Object.keys(obj).map(function(name) { return name + ' ' + sqliteAffinity(obj[name]); }).join(', ');
};
Database.prototype =
{
initialize: function initialize(ActivityMonitor)
{
this.exampleKey = ActivityMonitor.Window.exampleKey;
this.exampleData = ActivityMonitor.Window.exampleData;
this.keyScores = ActivityMonitor.Window.keyScores;
this.initializedPromise = this.dbPromise.then(function(db)
{
return Promise.all
([
db.run('PRAGMA foreign_keys = ON;'),
db.run
(
'CREATE TABLE IF NOT EXISTS applications ' +
'(' +
'id INTEGER PRIMARY KEY, ' +
objToSqliteSpec(this.exampleKey) + ', ' +
objToSqliteSpec(this.exampleData) +
');'
),
db.run
(
'CREATE TABLE IF NOT EXISTS tasks ' +
'( ' +
'id INTEGER PRIMARY KEY, ' +
'name TEXT, ' +
'parent INTEGER, ' +
'FOREIGN KEY(parent) REFERENCES tasks(parent)' +
');'
),
db.run
(
'CREATE TABLE IF NOT EXISTS slices ' +
'( ' +
'id INTEGER PRIMARY KEY, ' +
'begin INTEGER, ' +
'end INTEGER, ' +
'application INTEGER, ' +
'task INTEGER, ' +
'FOREIGN KEY(application) REFERENCES applications(id), ' +
'FOREIGN KEY(task) REFERENCES tasks(id)' +
');'
),
db.run
(
'CREATE TABLE IF NOT EXISTS scheduler ' +
'( ' +
'id INTEGER PRIMARY KEY, ' +
'type TEXT, ' +
'parent INTEGER, ' +
'category TEXT, ' +
'data TEXT, ' +
'root INTEGER, ' +
'FOREIGN KEY(parent) REFERENCES scheduler(id) ' +
');'
)
]);
}.bind(this)).then(function(results)
{
console.log("DB initialized");
return this.dbPromise;
}.bind(this), function(err)
{
console.log("DB error", err);
throw err;
});
this.initializedPromise.done(function() {});
return this.initializedPromise;
},
insertApplication: function insertApplication(key, data)
{
var fields = Object.keys(this.exampleKey).concat(Object.keys(this.exampleData));
var values = fields.map(function(name) { if(typeof key[name] !== 'undefined') return key[name]; else return data[name]; });
return this.initializedPromise.then(function(db)
{
var statement = 'INSERT INTO applications (' + fields.join(', ') + ') VALUES(' + fields.map(function() { return '?'; }).join(', ') + ')';
return db.run(statement, values);
}).then(function(result)
{
if(result.changes > 0)
{
return Object.assign({}, key, data, {id: result.lastID});
}
else
throw "insert failed"; //FIXME
});
},
updateApplication: function updateApplication(id, key, data)
{
var fields = Object.keys(this.exampleKey).concat(Object.keys(this.exampleData));
var values = {};
fields.forEach(function(propName)
{
values['$' + propName] = typeof key[propName] != 'undefined' ? key[propName] : data[propName];
});
values.$id = id;
var db = null;
return this.initializedPromise.then(function(idb)
{
db = idb;
var statement = 'UPDATE applications SET ' +
fields.map(function(propName) { return propName + ' = $' + propName; }).join(', ') +
' WHERE id = $id';
return db.run(statement, values);
}).then(function(result)
{
if(result.changes > 0)
{
return Object.assign({}, key, data, { id: id });
}
else
throw "update failed"; //FIXME
});
},
matchApplication: function matchApplication(key)
{
var scores = this.keyScores;
var minScore = 6;
var statement;
var promise = this.initializedPromise.then(function(db)
{
var newKey = {};
Object.keys(key).forEach(function(propName)
{
newKey['$' + propName] = key[propName];
});
var whereClauses = [];
Object.keys(key).forEach(function(propName)
{
whereClauses.push(propName + ' = $' + propName);
});
var scoreClauses = [];
Object.keys(key).forEach(function(propName)
{
scoreClauses.push('(CASE WHEN ' + propName + ' = $' + propName + ' THEN ' + (scores[propName] || 1) + ' ELSE 0 END)');
});
statement = 'SELECT *, ';
statement += scoreClauses.join(' + ') + ' AS score';
statement += ' FROM applications WHERE ';
statement += '(' + whereClauses.join(' OR ') + ') AND score >= ' + minScore;
statement += ' ORDER BY score DESC LIMIT 1';
return db.get(statement, newKey);
});
promise.done(function() {}, function(err) { throw new Error(statement) });
return promise;
},
//returns: db id of application
matchOrInsertApplication: function matchOrInserApplication(window)
{
return window.getKeyObject().then(function(iKey)
{
key = iKey;
return this.matchApplication(key);
}.bind(this)).then(function(result)
{
return window.getFrozen().then(function(iData)
{
data = iData;
if(!result)
return this.insertApplication(key, data);
else //FIXME: possible optimization: just update key here and only periodically get full frozen object
{
console.log("Match score", result.score);
return this.updateApplication(result.id, key, data);
}
}.bind(this)).then(function(result)
{
return result;
}.bind(this));
}.bind(this));
},
addSlice: function addSlice(begin, end, application, task)
{
if(typeof begin == 'object') begin = begin.getTime();
if(typeof end == 'object') end = end.getTime();
if(typeof application == 'object') application = application.id;
var values = { $begin: begin, $end: end, $application: application, $task: task };
var db = null;
return this.initializedPromise.then(function(idb)
{
db = idb;
var statement = 'UPDATE slices ' +
'SET end = max(end, $end), begin = min(begin, $begin) ' +
'WHERE (application = $application OR IFNULL(application, $application) IS NULL) ' +
'AND (task = $task OR IFNULL(task, $task) IS NULL) ' +
'AND end >= $begin AND begin <= $end';
return db.run(statement, values);
}.bind(this)).then(function(result)
{
if(result.changes < 1)
{
var statement = 'INSERT INTO slices (begin, end, application, task) VALUES ($begin, $end, $application, $task)';
return db.run(statement, values);
}
return result;
});
},
getSlices: function getSlices(begin, end)
{
if(typeof begin == 'object') begin = begin.getTime();
if(typeof end == 'object') end = end.getTime();
var values = { $begin: begin, $end: end };
var db = null;
return this.initializedPromise.then(function(idb)
{
db = idb;
var statement = 'SELECT * FROM slices WHERE end >= $begin AND begin <= $end';
return db.all(statement, values);
}.bind(this)).then(function(result)
{
console.log("Get slices", result, values, "now", new Date().getTime());
return result;
});
},
getApplications: function getApplications(ids)
{
if(ids && ids.length > 0)
{
var where = ' WHERE id IN (' + ids.map(function() { return '?'; }).join(', ') + ')';
}
else
{
var where = '';
ids = [];
}
return this.initializedPromise.then(function(idb)
{
db = idb;
var statement = 'SELECT * FROM applications' + where;
return db.all(statement, ids);
}.bind(this)).then(function(result)
{
var applications = {};
result.forEach(function(app) { applications[app.id] = app; });
return applications;
});
}
}
module.exports = exports = new Database();