This repository has been archived by the owner on Nov 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
crater-db.js
382 lines (359 loc) · 10.3 KB
/
crater-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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
* Stores and retrieves results from test runs.
*/
'use strict';
var debug = require('debug')(__filename.slice(__dirname.length + 1));
var Promise = require('promise');
var pg = require('pg');
var util = require('./crater-util');
/**
* Connects to a PostgreSQL DB and returns a promise of an opaque type
* accepted as context to other functions here.
*/
function connect(config) {
var credentials = config.dbCredentials;
var dbctx = new Promise(function(resolve, reject) {
var client = new pg.Client({
database: config.dbName,
user: credentials.username,
password: credentials.password,
host: credentials.host || null,
port: credentials.port || null
});
client.connect(function(err) {
if (!err) {
resolve({ client: client });
} else {
reject(err);
}
});
});
return dbctx.then(function(dbctx) {
var p = populate(dbctx);
var p = p.then(function() { return dbctx; });
return p;
});
}
function disconnect(dbctx) {
dbctx.client.end();
return Promise.resolve();
}
/**
* Creates the tables of a database return a promise of nothing. Taks
* a promise of a database context created by `connect`.
*/
function populate(dbctx) {
var q = "create table if not exists \
build_results ( \
toolchain text not null, \
crate_name text not null, crate_vers text not null, \
status text not null, \
task_id text not null, \
primary key ( \
toolchain, crate_name, crate_vers ) ) \
";
return new Promise(function (resolve, reject) {
dbctx.client.query(q, function(e, r) {
if (e) { reject(e); }
else { resolve(r); }
});
}).then(function() {
return new Promise(function(resolve, reject) {
var q = "create table if not exists \
custom_toolchains ( \
toolchain text not null, \
url text not null, \
task_id text not null, \
primary key (toolchain) )";
dbctx.client.query(q, function(e, r) {
if (e) { reject(e); }
else { resolve(r); }
});
});
}).then(function() {
return new Promise(function(resolve, reject) {
var q = "create table if not exists \
crate_versions ( \
name text not null, \
version text not null, \
primary key (name, version) )";
dbctx.client.query(q, function(e, r) {
if (e) { reject(e); }
else { resolve(r); }
});
});
}).then(function() {
return new Promise(function(resolve, reject) {
var q = "create table if not exists \
crate_rank ( \
name text not null, \
rank integer not null, \
primary key (name) )";
dbctx.client.query(q, function(e, r) {
if (e) { reject(e); }
else { resolve(r); }
});
});
}).then(function() {
return new Promise(function(resolve, reject) {
var q = "create table if not exists \
dep_edges ( \
name text not null, \
dep text not null, \
primary key (name, dep) )";
dbctx.client.query(q, function(e, r) {
if (e) { reject(e); }
else { resolve(r); }
});
});
});
}
/**
* Destroys the tables.
*/
function depopulate(dbctx) {
var q = "drop table if exists build_results";
debug(q);
return new Promise(function (resolve, reject) {
dbctx.client.query(q, function(e, r) {
if (e) { reject(e); }
else { resolve(r); }
});
}).then(function() {
var q = "drop table if exists custom_toolchains";
debug(q);
return new Promise(function(resolve, reject) {
dbctx.client.query(q, function(e, r) {
if (e) { reject(e); }
else { resolve(r); }
});
});
}).then(function() {
var q = "drop table if exists crate_versions";
return new Promise(function(resolve, reject) {
dbctx.client.query(q, function(e, r) {
if (e) { reject(e); }
else { resolve(r); }
});
});
}).then(function() {
var q = "drop table if exists crate_rank";
return new Promise(function(resolve, reject) {
dbctx.client.query(q, function(e, r) {
if (e) { reject(e); }
else { resolve(r); }
});
});
}).then(function() {
var q = "drop table if exists dep_edges";
return new Promise(function(resolve, reject) {
dbctx.client.query(q, function(e, r) {
if (e) { reject(e); }
else { resolve(r); }
});
});
});
}
/**
* Adds a build result and returns a promise of nothing. buildResult should
* look like `{ toolchain: ..., crateName: ..., crateVers: ..., status: ...,
* taskId: ... }`.
*/
function addBuildResult(dbctx, buildResult) {
return new Promise(function (resolve, reject) {
var f = function(e, r) {
dbctx.client.query('commit', function(err, res) {
if (e) { reject(e); }
else { resolve(); }
});
};
dbctx.client.query('begin', function(err, res) {
if (err) {
reject(err);
return;
}
var p = getBuildResult(dbctx, buildResult);
p.then(function(r) {
if (r == null) {
var q = "insert into build_results values ($1, $2, $3, $4, $5)";
debug(q);
dbctx.client.query(q, [util.toolchainToString(buildResult.toolchain),
buildResult.crateName,
buildResult.crateVers,
buildResult.status,
buildResult.taskId],
f);
} else {
var q = "update build_results set status = $4, task_id = $5 where \
toolchain = $1 and crate_name = $2 and crate_vers = $3";
debug(q);
dbctx.client.query(q, [util.toolchainToString(buildResult.toolchain),
buildResult.crateName,
buildResult.crateVers,
buildResult.status,
buildResult.taskId],
f);
}
}).catch(function(e) {
reject(e);
});
});
});
}
/**
* Adds a build result and returns a promise of a build
* result. buildResultKey should look like `{ toolchain: ...,
* crateName: ..., crateVers: ... }`.
*
* Returns a promised null if there is no build result for the key.
*/
function getBuildResult(dbctx, buildResultKey) {
var q = "select * from build_results where \
toolchain = $1 and crate_name = $2 and crate_vers = $3";
debug(q);
return new Promise(function (resolve, reject) {
var f = function(e, r) {
if (e) { reject(e); }
else {
if (r.rows.length > 0) {
var row = r.rows[0];
resolve({
toolchain: util.parseToolchain(row.toolchain),
crateName: row.crate_name,
crateVers: row.crate_vers,
status: row.status,
taskId: row.task_id
});
} else {
resolve(null);
}
}
};
dbctx.client.query(q, [util.toolchainToString(buildResultKey.toolchain),
buildResultKey.crateName,
buildResultKey.crateVers],
f);
});
}
/**
* Returns a promise of an array of pairs of build results for a given
* pair of toolchains. Each element of the array looks like
* `{ crateName: ..., crateVers: ..., from: ..., to: ... }`,
* and `from` and `to` look like `{ succes: bool }`.
*/
function getResultPairs(dbctx, fromToolchain, toToolchain) {
var q = "select a.crate_name, a.crate_vers, a.status as from_status, b.status as to_status, \
a.task_id as from_task_id, b.task_id as to_task_id \
from build_results a, build_results b \
where a.toolchain = $1 and b.toolchain = $2 \
and a.crate_name = b.crate_name and a.crate_vers = b.crate_vers \
order by a.crate_name, a.crate_vers";
debug(q);
return new Promise(function(resolve, reject) {
var f = function(e, r) {
if (e) { reject(e); }
else {
var results = []
r.rows.forEach(function(row) {
debug("result row: " + JSON.stringify(row));
results.push({
crateName: row.crate_name,
crateVers: row.crate_vers,
from: { status: row.from_status, taskId: row.from_task_id },
to: { status: row.to_status, taskId: row.to_task_id }
});
});
resolve(results);
}
};
dbctx.client.query(q, [util.toolchainToString(fromToolchain),
util.toolchainToString(toToolchain)],
f);
});
}
function getResults(dbctx, toolchain) {
var q = "select * from build_results \
where toolchain = $1 order by crate_name";
debug(q);
return new Promise(function(resolve, reject) {
var f = function(e, r) {
if (e) { reject(e); }
else {
var results = [];
r.rows.forEach(function(row) {
results.push({
crateName: row.crate_name,
crateVers: row.crate_vers,
status: row.status,
taskId: row.task_id
});
});
resolve(results);
}
};
dbctx.client.query(q, [util.toolchainToString(toolchain)], f);
});
}
function addCustomToolchain(dbctx, custom) {
return new Promise(function(resolve, reject) {
var f = function(e, r) {
dbctx.client.query('commit', function(err, res) {
if (e) { reject(e); }
else { resolve(); }
});
};
dbctx.client.query('begin', function(err, res) {
if (err) { reject(err); return; }
var p = getCustomToolchain(dbctx, custom.toolchain);
p.then(function(r) {
if (r == null) {
var q = "insert into custom_toolchains values ($1, $2, $3)";
debug(q);
dbctx.client.query(q, [util.toolchainToString(custom.toolchain),
custom.url,
custom.taskId], f);
} else {
var q = "update custom_toolchains set url = $2, task_id = $3 where toolchain = $1";
debug(q);
dbctx.client.query(q, [util.toolchainToString(custom.toolchain),
custom.url,
custom.taskId], f);
}
}).catch(function(e) {
reject(e);
});
});
});
}
function getCustomToolchain(dbctx, toolchain) {
var q = "select * from custom_toolchains where toolchain = $1";
debug(q);
return new Promise(function(resolve, reject) {
var f = function(e, r) {
if (e) { reject(e); }
else {
if (r.rows.length > 0) {
var row = r.rows[0];
resolve({
toolchain: toolchain,
url: row.url,
taskId: row.task_id
});
} else {
resolve(null);
}
}
};
dbctx.client.query(q, [util.toolchainToString(toolchain)], f);
});
}
exports.connect = connect;
exports.disconnect = disconnect;
exports.populate = populate;
exports.depopulate = depopulate;
exports.addBuildResult = addBuildResult;
exports.getBuildResult = getBuildResult;
exports.getResultPairs = getResultPairs;
exports.getResults = getResults;
exports.addCustomToolchain = addCustomToolchain;
exports.getCustomToolchain = getCustomToolchain;