-
Notifications
You must be signed in to change notification settings - Fork 2
/
db_helper.js
248 lines (210 loc) · 6.49 KB
/
db_helper.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
const mongoose = require("mongoose");
/** Database setup */
const uri = "mongodb://127.0.0.1/executions";
mongoose
.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log("Successful MongoDB connection."));
const db = mongoose.connection;
db.on("error", console.error.bind(console, "MongoDB connection error:"));
/** Define database structure. */
const executionSchema = new mongoose.Schema({
date: Date,
network_name: String,
network_group: String,
}, {collection : 'executions'});
const Execution = mongoose.model("Execution", executionSchema);
const gitSchema = new mongoose.Schema({
date: Date,
traffic: Number,
clones: Number,
}, {collection : 'git'});
const Git = mongoose.model("Git", gitSchema);
const biocondaSchema = new mongoose.Schema({
date: Date,
downloads: Number,
pkg_name: String,
}, {collection : 'bioconda'});
const Bioconda = mongoose.model("Bioconda", biocondaSchema);
const pypiSchema = new mongoose.Schema({
date: Date,
downloads: Number,
pkg_name: String,
}, {collection : 'pypi'});
const Pypi = mongoose.model("Pypi", pypiSchema);
/** Define public functions */
const addExecution = async (network, is_custom) => {
console.log(is_custom)
return Execution.create({
date: new Date(),
network_group: is_custom ? "Loaded by user" :network,
network_name: network
});
};
const aggregateBioconda = async () => {
// console.log(Git.count())
let yearlyBioconda = Bioconda.aggregate([
{$match: { date: { $gte: new Date(new Date().getYear(), 1, 1) }, pkg_name: "domino" }},
{$group: {_id : null, downloads: { $sum: "$downloads" }}}, {$project: {_id: 0 , downloads : "$downloads"}}]);
let monthlyBioconda = Bioconda.aggregate([
{
$match : {
pkg_name: "domino"
}
},
{
$group: {
_id: {
year: { $year: "$date" },
month: { $month: "$date" }
},
downloads: { $sum: "$downloads" }
}
}
]);
return await Promise.all([
yearlyBioconda,
monthlyBioconda,
]);
};
const aggregatePypi = async () => {
// console.log(Git.count())
let yearlyPypi = Pypi.aggregate([
{$match: { date: { $gte: new Date(new Date().getYear(), 1, 1) }, pkg_name: "domino-python" }},
{$group: {_id : null, downloads: { $sum: "$downloads" }}}, {$project: {_id: 0 , downloads : "$downloads"}}]);
let monthlyPypi = Pypi.aggregate([
{
$match : {
pkg_name: "domino-python"
}
},
{
$group: {
_id: {
year: { $year: "$date" },
month: { $month: "$date" }
},
downloads: { $sum: "$downloads" }
}
}
]);
return await Promise.all([
yearlyPypi,
monthlyPypi,
]);
};
const aggregateGit = async () => {
// console.log(Git.count())
let yearlyGit = Git.aggregate([
{$match: { date: { $gte: new Date(new Date().getYear(), 1, 1) },repository: "DOMINO" }},
{$group: {_id : null, traffic: { $sum: "$traffic" }, clones: { $sum: "$clones" }}}, {$project: {_id: 0 , traffic : "$traffic", clones : "$clones"}}]);
let monthlyGit = Git.aggregate([
{
$match : {
repository: "DOMINO"
}
},
{
$group: {
_id: {
year: { $year: "$date" },
month: { $month: "$date" }
},
traffic: { $sum: "$traffic" },
clones: { $sum: "$clones" }
}
}
]);
return await Promise.all([
yearlyGit,
monthlyGit,
]);
};
const aggregateExecutions = async () => {
// console.log(JSON.stringify([
// {
// $match : { date:
// { $gte: new Date(new Date().getFullYear(), 1, 1) }
// }
// },
// {
// $count: "total",
// },
// ]));
let totalExecutions = Execution.aggregate([
{
$match : { date:
{ $gte: new Date(new Date().getYear(), 1, 1) }
}
},
{
$count: "total",
},
]);
let networkExecutions = Execution.aggregate([
{
$group: {
_id: "$network_group",
freq: { $sum: 1 },
},
}
]);
let monthlyExecutionsByNetworks = Execution.aggregate([
{
$group: {
_id: {
year: { $year: "$date" },
month: { $month: "$date" },
network: "$network_group",
},
count: { $sum: 1 },
},
},
{
$group: {
_id: { year: "$_id.year", month: "$_id.month" },
networks: { $push: { k: "$_id.network", v: "$count" } },
},
},
{ $addFields: { networks: { $arrayToObject: "$networks" } } },
{
$replaceRoot: {
newRoot: { $mergeObjects: [{ _id: "$_id" }, "$networks"] },
},
},
]);
return await Promise.all([
totalExecutions,
networkExecutions,
monthlyExecutionsByNetworks,
]);
};
const createDummyValues = async () => {
return Promise.all(
[...Array(5).keys()]
.map((_) =>
Execution.create({ date: new Date(), network: "dip.sif" })
)
.concat(
[...Array(10).keys()].map((_) =>
Execution.create({
date: `10/${_ + 1}/21`,
network: "huri.sif",
})
),
[...Array(15).keys()].map((_) =>
Execution.create({
date: `11/${_ + 1}/21`,
network: "string.sif",
})
)
)
);
};
module.exports = {
addExecution,
aggregateExecutions,
aggregateGit,
aggregateBioconda,
aggregatePypi,
createDummyValues,
};