Skip to content
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ var serviceinfoSchema = mongoose.Schema({
success_rate: Number,

readme_status: String, //I think I am going to deprecate this (by status/status_msg)
monthlyCounts: [Number] //
});
exports.Serviceinfo = mongoose.model('Serviceinfo', serviceinfoSchema);

68 changes: 67 additions & 1 deletion bin/serviceinfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ db.init(function(err) {
}
}
service_info[status._id.service].counts[status._id.status] = status.count;

//calculate success_rate
let finished = service_info[status._id.service].counts.finished;
let failed = service_info[status._id.service].counts.failed;
Expand Down Expand Up @@ -120,6 +119,73 @@ db.init(function(err) {
});
},

async next=>{
/* Get all service info */
db.Serviceinfo.find({}).exec((err,services)=>{
if(err) return next(err);
services.forEach(async(service)=>{
let countData;
const currentDate = new Date();
if(!service.monthlyCounts || !service.monthlyCounts.length) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a way to combine these 2 giant if statements into one. :) Can you think of a way?

service.monthlyCounts = [];
//if no monthly count then add data from 2017
for(let year = 2017; year <= currentDate.getFullYear(); year++){
for(let month = 1; month <=12; month++){
if(year == currentDate.getFullYear() && month > currentDate.getMonth()+1) break;
const start = new Date(year+"-"+month+"-01");
const end = new Date(start);
end.setMonth(end.getMonth()+1);
countData = await db.Task.aggregate([
{$match: {
create_date: {$gte: start, $lt: end},
service: service.service
}},{
$group: {
_id: {service: "$service"},
count: {$sum: 1},
walltime: {$sum: "$runtime"}
}
}
]);
if(!countData.length) service.monthlyCounts.push(undefined);
else service.monthlyCounts.push(countData[0].count);
}
}
// console.log(service.service,service.monthlyCounts.length);
service.save();
} else {
//if the month is already marked then updating it instead of pushing element
const start = currentDate;
const end = new Date(start);
start.setMonth(end.getMonth()-1);
const month_difference = (end.getFullYear() - new Date('2017-01-01').getFullYear())*12 + (end.getMonth() - new Date('2017-01-01').getMonth()) - 1;
countData = await db.Task.aggregate([
{$match: {
create_date: {$gte: start, $lt: end},
service: service.service
}},{
$group: {
_id: {service: "$service"},
count: {$sum: 1},
walltime: {$sum: "$runtime"}
}
}
]);

/* if index exists then update else push values*/
if(service.monthlyCounts[month_difference] == undefined || service.monthlyCounts[month_difference]) {
if(!countData.length) service.monthlyCounts[month_difference] = undefined;
else service.monthlyCounts[month_difference] = countData[0].count;
} else {
if(!countData.length) service.monthlyCounts.push(undefined);
else service.monthlyCounts.push(countData[0].count);
}
service.save();
}
});
});
},


/* not useful?
next=>{
Expand Down