Created
August 18, 2015 08:47
-
-
Save esshka/3d6ffa646c22d21cf3cc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var _ = require('lodash'), | |
moment = require('moment'), | |
Promise = require("bluebird"); | |
module.exports = function (clamps) { | |
let start = +new Date(), | |
dayms = 1000 * 60 * 60 * 24; | |
function toDateDayKeyFormat(date) { | |
var result; | |
if (date.time) { | |
result = date.time; | |
} else { | |
result = date; | |
} | |
return moment(result).format("MM-DD-YYYY"); | |
} | |
function createAllDates(dates) { | |
let firstDate, | |
lastDate, | |
counter, | |
step, | |
result = []; | |
firstDate = Date.parse(_.first(dates)); | |
lastDate = Date.parse(_.last(dates)); | |
counter = firstDate; | |
step = dayms; | |
result.push(toDateDayKeyFormat(firstDate)); | |
while ((counter + step) < lastDate) { | |
counter += step; | |
result.push(toDateDayKeyFormat.call(null,counter)); | |
} | |
result.push(toDateDayKeyFormat(lastDate)); | |
return result; | |
} | |
var processRawData = function(data){ | |
// функция разбирает сырую выборку из базы данных | |
var groupedData, | |
groupedDataKeys, | |
result, | |
offcutBuffer, | |
groupedData = _.groupBy(data, toDateDayKeyFormat), // группируем смыкания по дате формата dd-mm-yyyy | |
groupedDataKeys = _.keys(groupedData), | |
allDates, // все ключи groupedData | |
intervals = [], | |
daySec = 60 * 60 * 24; | |
allDates = createAllDates(groupedDataKeys); | |
function processInterval(clamps, dk) { | |
let sn_list = [], | |
sa_list = [], | |
sf_list = [], | |
si_list = [], | |
sn, sa, sf, si; | |
_.each(clamps, function(clamp){ | |
switch (clamp.type) { | |
case 'norm': | |
sn_list.push(clamp) | |
break; | |
case 'acl': | |
sa_list.push(clamp) | |
break; | |
case 'fail': | |
sf_list.push(clamp) | |
break; | |
case 'idle': | |
si_list.push(clamp) | |
break; | |
} | |
}); | |
sn = _.reduce(sn_list, function(sum,n){ | |
sum = sum + +n.duration; | |
return sum; | |
}, 0); | |
sa = _.reduce(sa_list, function(sum,n){ | |
sum = sum + +n.duration; | |
return sum; | |
}, 0); | |
sf = _.reduce(sf_list, function(sum,n){ | |
sum = sum + +n.duration; | |
return sum; | |
}, 0); | |
si = _.reduce(si_list, function(sum,n){ | |
sum = sum + +n.duration; | |
return sum; | |
}, 0); | |
return { | |
dk: dk, | |
sn: sn, | |
sa: sa, | |
sf: sf, | |
si: si, | |
sn_count: sn_list.length, | |
sa_count: sa_list.length, | |
sf_count: sf_list.length, | |
si_count: si_list.length | |
}; | |
} | |
_(allDates).reverse().each(function(date, idx){ | |
let clamps = groupedData[date], | |
intervalStartDate = Date.parse(date), | |
result, | |
clampsGroup = []; | |
if (!clamps) { | |
if (!offcutBuffer) { | |
return; | |
} else { | |
let offcutClamp = { | |
time: intervalStartDate + daySec * 1000, | |
type: offcutBuffer.type, | |
duration: daySec | |
} | |
clampsGroup.push(offcutClamp); | |
offcutBuffer.duration -= offcutClamp.duration; | |
} | |
} else { | |
let longClamp = _.first(clamps); | |
if (!offcutBuffer) { | |
offcutBuffer = { | |
type: longClamp.type, | |
duration: longClamp.duration | |
}; | |
} | |
let diff = Date.parse(longClamp.time) - intervalStartDate; | |
clampsGroup = clampsGroup.concat(clamps); | |
offcutBuffer.duration -= diff / 1000; | |
offcutBuffer.type = longClamp.type; | |
let lastClamp = _.last(clamps); | |
let lastDiff = intervalStartDate + daySec * 1000 - Date.parse(lastClamp.time); | |
let offcutClamp = { | |
time: Date.parse(date) + daySec * 1000, | |
type: offcutBuffer.type, | |
duration: lastDiff / 1000 | |
}; | |
let offcutLongClamp = { | |
time: longClamp.time, | |
type: longClamp.type, | |
duration: (Date.parse(longClamp.time) - intervalStartDate) / 1000 | |
} | |
clampsGroup.splice(0,1); | |
clampsGroup.push(offcutClamp); | |
clampsGroup.push(offcutLongClamp); | |
} | |
result = processInterval(clampsGroup, date); | |
intervals.push(result); | |
}).value(); | |
return intervals; | |
} | |
return processRawData(clamps); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment