————————————
angular.module('reportingDirectives', []).
directive('reportingSidebar', function() {
// Runs during compile
return {
//name: '',
// priority: 1,
// terminal: true,
scope: {
/*user: '=',
software_key: '=',
machineObj: '=',*/
}, // {} = isolate, true = child, false/undefined = no change
controller: function($scope, $rootScope, $element, $attrs, $transclude, $http, $location, $route, $q) {
$scope.disableInputs = false;
$scope.downloadReport = function() {
alert('Placeholder for PDF download');
};
$scope.saveCloudReport = function() {
alert('Placeholder for Cloud Saving');
};
$scope.appKey = 'custom-reporting';
$scope.cloudSave = {
reportName: '',
machine: '',
graphType: '',
xaxis: '',
viewDataOptions: '',
startDate: '',
endDate: '',
yaxismax: '',
input: '',
};
$scope.graphType = 'column';
var self = this;
$scope.reporting = {
machineSelect: '',
type: 'column',
xaxis: 'Day',
//yaxis: 'sum',
yaxismax: undefined,
startDate: '',
endDate: '',
viewDataOption: 'count',
shiftsEnabled: false,
};
$scope.showItems = true;
self.Initialise = function() {
$scope.data = {};
//$scope.data.shifts = [];
/* Fix This below to load dates dynamically for each machine when selected */
zeroOut();
$scope.machineList = $rootScope.machineObj;
var trendType = 'trendCount';
var regressionLineType = 'countRegLine';
$scope.y2CountMax = 0;
$scope.y2TimerMax = 0;
$scope.y2Max = undefined;
//getWorkPeriods();
/*if ($scope.reporting.viewDataOption === 'count') {
$scope.y2Max = $scope.y2CountMax;
} else {
$scope.y2Max = $scope.y2TimerMax;
}*/
//console.log($scope.options);
//console.log(item);
grabShifts('shift-profile', $rootScope.factoryKey).then(function(data) {
angular.forEach(data, function(items, key) {
//console.log(items.startTime * 1000);
if (isNaN(items.startTime * 1000)) {
items.startTime = new Date(items.startTime);
items.endTime = new Date(items.endTime);
} else {
items.startTime = new Date(items.startTime * 1000);
items.endTime = new Date(items.endTime * 1000);
}
});
$scope.shiftsObj = data;
console.log($scope.shiftsObj);
}, function(error) {
console.log('Failure...', error);
});
//alert($scope.shiftsObj);
};
function grabShifts(appKey, factoryKey) {
var defer = $q.defer();
$http.get('http://54.213.13.56/api/app/' + appKey + '/' + factoryKey).
success(function(data) {
//console.log(data);
defer.resolve(data);
}).error(function(data) {
console.log('Error in getShiftProfiles: ' + data);
});
return defer.promise;
}
$scope.$watchGroup(["reporting.type", "reporting.viewDataOption", "reporting.yaxismax", "reporting.xaxis"], function(newValues, oldValues) {
$scope.y2Max = undefined;
if ($scope.reporting.viewDataOption === 'count') {
//$scope.y2Max = $scope.y2CountMax;
$scope.reportLabel = 'Sum';
} else {
//$scope.y2Max = $scope.y2TimerMax;
$scope.reportLabel = 'Percentage';
}
if ($scope.reporting.xaxis === 'Month') {
$rootScope.monthDay = 'month';
} else {
$rootScope.monthDay = 'day';
}
$rootScope.counttimer = $scope.reporting.viewDataOption;
if ($scope.reporting.viewDataOption === 'count') {
$scope.maxyaxis = undefined;
trendType = 'trendCount';
regressionLineType = 'countRegLine';
} else if ($scope.reporting.viewDataOption === 'timer') {
//console.log($scope.reporting.yaxismax);
if ($scope.reporting.yaxismax === true) {
$scope.maxyaxis = 100;
} else {
$scope.maxyaxis = undefined;
}
trendType = 'trendTimer';
regressionLineType = 'timerRegLine';
}
$scope.graphType = newValues[0];
$scope.options = {
axes: {
x: {
key: 'x',
type: 'date'
},
y: {
type: 'linear',
max: $scope.maxyaxis
},
y2: {
type: 'linear',
min: $scope.y2Max,
//max: $scope.maxyaxis
}
},
series: [{
y: $scope.reporting.viewDataOption,
color: 'steelblue',
type: $scope.graphType,
axis: 'y',
striped: true,
label: toTitleCase($scope.reportLabel + ' of ' + $scope.reporting.viewDataOption),
id: "data"
}, {
y: trendType,
//label: "Trendline",
color: "#ff7f0e",
type: "line",
axis: "y",
id: "trend",
label: 'Average'
}, {
y: regressionLineType,
//label: "Trendline",
color: "#006600",
type: "line",
axis: "y",
id: "RegressionLine",
label: 'Regression',
visible: $scope.showItems,
}],
lineMode: 'linear',
tension: 0.7,
tooltip: {
mode: 'scrubber',
formatter: function(x, y, series) {
//series.id['data ']= 'The Data: ';
var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
if ($scope.reporting.xaxis === 'Month') {
return monthNames[x.getMonth()] + ' ' +
x.getFullYear() + ' - ' +
series.label + ': ' + Math.round(y);
} else {
return x.toDateString() + ' - ' +
series.label + ': ' + Math.round(y);
}
}
},
drawLegend: $scope.showItems,
drawDots: true,
columnsHGap: 5
};
});
$scope.$watchGroup(["reporting.startDate", "reporting.endDate"], function(newValues, oldValues) {
$rootScope.startingDate = $scope.reporting.startDate;
$rootScope.endingDate = $scope.reporting.endDate;
});
function toTitleCase(str) {
return str.replace(/\w\S*/g, function(txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
}
$scope.$watch("reporting.machineSelect", function(newValue, oldValue) {
//$scope.reporting.type = 'column';
//$scope.reporting.xaxis = 'Month';
//$scope.reporting.yaxis = 'sum';
//$scope.reporting.viewDataOption
//hardware: '',
$scope.reporting.hardware = undefined;
$scope.machineDates = $scope.reporting.machineSelect.Dates;
$scope.machineHardware = $scope.reporting.machineSelect.Hardware;
$rootScope.machinesName = $scope.reporting.machineSelect.Name;
/*if($scope.machineDates !== undefined) {
$scope.reporting.startDate = $scope.reporting.machineSelect.Dates[0].date;
$scope.reporting.endDate = $scope.reporting.machineSelect.Dates[0].date;
}*/
//console.log($scope.machineDates.date);
});
$scope.changedVars = function() {
//console.log($scope.reporting.machineSelect.SoftwareKey);
/*$scope.y2Max = undefined;
if ($scope.reporting.viewDataOption === 'count') {
$scope.y2Max = $scope.y2CountMax;
$scope.reportLabel = 'Sum';
} else {
$scope.y2Max = $scope.y2TimerMax;
$scope.reportLabel = 'Percentage';
}*/
$scope.shiftInfo = [];
$scope.disableInputs = true;
$scope.yaxe = $scope.reporting.yaxis;
if ($scope.reporting.hardware !== undefined && $scope.reporting.xaxis !== '' && $scope.reporting.machineSelect.Dates.length !== 0) {
$scope.data = [];
$scope.nonZeroData = [];
$rootScope.hardware = $scope.reporting.hardware;
if ($scope.reporting.xaxis === 'Day' || $scope.reporting.xaxis === 'Month') {
startDateSplit = $scope.reporting.startDate.date.split(' ');
endDateSplit = $scope.reporting.endDate.date.split(' ');
newStartDate = new Date(startDateSplit[0] + ' 1, ' + startDateSplit[1] + ' 00:00:00');
newEndDate = new Date(endDateSplit[0] + ' 1, ' + endDateSplit[1] + ' 00:00:00');
if (newEndDate.getMonth() === new Date().getMonth()) {
newEndDate = new Date();
} else {
//console.log($scope.reporting.startDate.date);
if ($scope.reporting.startDate.date === $scope.reporting.endDate.date && $scope.reporting.xaxis === 'Month') {
/*alert("Sorry you can't report on just one month with Month selected for time, " +
"I would suggest doing days if you want to complete this action");*/
//console.log('made it here');
newStartDate = new Date(startDateSplit[0] + ' 1, ' + startDateSplit[1] + ' 00:00:00');
newEndDate = new Date(endDateSplit[0] + ' 1, ' + endDateSplit[1] + ' 00:00:00');
//newEndDate = new Date(newEndDate.getFullYear(), newEndDate.getMonth() + 1, 0);
} else {
newEndDate = new Date(newEndDate.getFullYear(), newEndDate.getMonth() + 1, 0);
newEndDate.setDate(newEndDate.getDate() + 1);
}
}
} else {
if ($scope.reporting.xaxis === 'LastSeven') {
newEndDate = new Date();
newStartDate = new Date( /*newEndDate.getFullYear(), newEndDate.getMonth(), newEndDate.getDate(),0,0,0,0*/ );
newStartDate.setDate(newStartDate.getDate() - 7);
} else if ($scope.reporting.xaxis === 'LastThirty') {
newEndDate = new Date();
newStartDate = new Date();
newStartDate.setDate(newStartDate.getDate() - 30);
} else if ($scope.reporting.xaxis === 'LastMonth') {
newStartDate = new Date();
newStartDate = new Date(newStartDate.getFullYear(), newStartDate.getMonth() - 1, 1, 0, 0, 0, 0);
//console.log(newStartDate);
newEndDate = new Date(newStartDate.getFullYear(), newStartDate.getMonth() + 1, 0, 0, 0, 0, 0);
//console.log(newEndDate);
/*newStartDate = new Date();
newStartDate.setDate(newStartDate.getMonth() - 1);*/
} else if ($scope.reporting.xaxis === 'LastNinty') {
newEndDate = new Date();
newStartDate = new Date();
newStartDate.setDate(newStartDate.getDate() - 90);
}
}
$rootScope.theStartDate = newStartDate.toDateString();
$rootScope.theEndDate = newEndDate.toDateString();
switch ($scope.reporting.xaxis) {
case 'Day':
$scope.data = [];
$scope.options.series[2].visible = false;
$scope.options.drawLegend = false;
$scope.nonZeroData = [];
dailyChart(newStartDate, newEndDate);
break;
case 'Month':
$scope.data = [];
$scope.options.series[2].visible = false;
$scope.options.drawLegend = false;
$scope.nonZeroData = [];
monthlyChart(newStartDate, newEndDate);
break;
case 'LastMonth':
$scope.data = [];
$scope.options.series[2].visible = false;
$scope.options.drawLegend = false;
$scope.nonZeroData = [];
dailyChart(newStartDate, newEndDate);
break;
case 'LastSeven':
$scope.data = [];
$scope.options.series[2].visible = false;
$scope.options.drawLegend = false;
$scope.nonZeroData = [];
dailyChart(newStartDate, newEndDate);
break;
case 'LastThirty':
$scope.data = [];
$scope.options.series[2].visible = false;
$scope.options.drawLegend = false;
$scope.nonZeroData = [];
dailyChart(newStartDate, newEndDate);
break;
case 'LastNinty':
$scope.data = [];
$scope.options.series[2].visible = false;
$scope.options.drawLegend = false;
$scope.nonZeroData = [];
dailyChart(newStartDate, newEndDate);
break;
default:
break;
}
} else {
alert('You need to fill in all fields first!');
//console.log('I was run');
}
/*$http.get('http://54.213.13.56/api/' + $scope.factoryKey + '/'+ $scope.reporting.machineSelect.Address +
'?process[]=' + $scope.reporting.indicator +'&date[]=1418513801&date[]=1420513801&index=' +
$scope.reporting.hardware + '').
success(function(data) {
}).
error(function(data) {
alert(data);
});*/
};
function dailyChart(startDate, endDate) {
var daysOfYear = [];
/*for (var d = newStartDate; d <= newEndDate; d.setDate(d.getDate() + 1)) {
da = new Date(d);
da.setDate(da.getDate() + 1);
$scope.webCall(Math.floor(new Date(d) / 1000), Math.floor(new Date(da) / 1000));
}*/
zeroOut();
processObj = 'sum';
/*if ($scope.reporting.yaxis === 'percentage') {
processObj = 'sum';
} else {
processObj = $scope.reporting.yaxis;
}*/
$scope.dataCount = 0;
$scope.shiftData = [];
nextDay(startDate, endDate, processObj);
}
function nextDay(startDate, endDate, processObj) {
//console.log('I am in the next()');
d = startDate;
if (d < endDate) {
da = new Date(d);
da.setDate(da.getDate() + 1);
//processObj = '';
$scope.fullDay = (((24 * 60) * 60) * 1000);
$scope.WorkPeriod = ((($scope.reporting.machineSelect.WorkPeriod * 60) * 60) * 1000);
//console.log($scope.WorkPeriod);
webCall($scope.reporting.machineSelect.SoftwareKey, $scope.reporting.machineSelect.Address,
processObj, Math.floor(new Date(d) / 1000), Math.floor(new Date(da) / 1000),
$scope.reporting.hardware).then(function(data) {
dataAggregator(data, d, da);
$scope.cntr = 0;
if ($scope.reporting.shiftsEnabled === true) {
//zeroOutShifts();
//$scope.cntr = 0;
$scope.defferShift = $q.defer();
nextShift($scope.defferShift);
//console.trace(nextShift());
}
//console.log($scope.dataCount);
$scope.dataCount++;
d.setDate(d.getDate() + 1);
nextDay(d, endDate, processObj);
//confirm('Was that as good for you as it was for me?');
}, function(error) {
console.log('Failure...', error);
});
} else {
//console.log($scope.data);
//if ($scope.reporting.yaxis === 'sum') {
// create array_x & array_y
$scope.options.series[2].visible = true;
$scope.options.drawLegend = true;
array_x = [];
array_y = [];
for (i = 0; i < $scope.data.length; ++i) {
array_x.push($scope.data[i].xCountCounter);
array_y.push($scope.data[i].count);
if ($scope.data[i].count > $scope.y2countmax) {
$scope.y2CountMax = $scope.data[i].count;
}
}
var countRegLine = findLineByLeastSquares(array_x, array_y); //findLineByLeastSquaresLog(array_x, array_y);
//findLineByLeastSquaresLog(array_x, array_y);
$rootScope.countSlope = (Math.round($scope.tempSlope * 10) / 10) /* * 100*/ ;
//console.log(countRegLine);
tmpCount = 0;
angular.forEach(countRegLine[1], function(value, key) {
if (tmpCount !== countRegLine[1].length) {
$scope.data[tmpCount].countRegLine = value;
tmpCount++;
}
});
array_x = [];
array_y = [];
for (i = 0; i < $scope.data.length; ++i) {
array_x.push($scope.data[i].xTimerCounter);
array_y.push($scope.data[i].timer);
if ($scope.data[i].timer > $scope.y2TimerMax) {
$scope.y2TimerMax = $scope.data[i].timer;
}
}
var timerRegLine = findLineByLeastSquares(array_x, array_y); //findLineByLeastSquaresLog(array_x, array_y);
//findLineByLeastSquaresLog(array_x, array_y);
$rootScope.timeSlope = (Math.round($scope.tempSlope * 10) / 10) /* * 100*/ ;
//console.log(countRegLine);
tmpCount = 0;
angular.forEach(timerRegLine[1], function(value, key) {
if (tmpCount !== countRegLine[1].length) {
$scope.data[tmpCount].timerRegLine = value;
tmpCount++;
}
});
console.log($scope.data);
$scope.disableInputs = false;
}
}
function nextShift(shiftPromise) {
if ($scope.cntr < $scope.shiftsObj.length) {
//console.log($scope.dataCount);
tempStartDate = new Date($scope.shiftsObj[$scope.cntr].startTime);
//console.log(tempStartDate);
tempStartDate = new Date(d.getFullYear(), d.getMonth(), d.getDate(), tempStartDate.getHours(), tempStartDate.getMinutes(), 0, 0);
//console.log('start date: ' + tempStartDate);
tempEndDate = new Date($scope.shiftsObj[$scope.cntr].endTime);
if ($scope.shiftsObj[$scope.cntr].endTime < $scope.shiftsObj[$scope.cntr].startTime) {
tempEndDate = new Date(da.getFullYear(), da.getMonth(), da.getDate(), tempEndDate.getHours(), tempEndDate.getMinutes(), 0, 0);
} else {
tempEndDate = new Date(d.getFullYear(), d.getMonth(), d.getDate(), tempEndDate.getHours(), tempEndDate.getMinutes(), 0, 0);
}
//console.log('end date: ' + tempEndDate);
var webShiftPromise = $q.defer();
webShiftCall($scope.reporting.machineSelect.SoftwareKey, $scope.reporting.machineSelect.Address,
processObj, tempStartDate, tempEndDate,
$scope.reporting.hardware,webShiftPromise).then(function(retData) {
$scope.shiftData.push({
day: $scope.data.length - 1,
sum: {
count: retData.sum.count,
timer: retData.sum.timer
}
});
var thePromise = $q.defer();
shiftDataAggregator(retData, tempStartDate, tempEndDate, $scope.dataCount, thePromise);
//confirm('Was that as good for you as it was for me?');
}, function(error) {
console.log('Failure...', error);
});
$scope.cntr++;
nextShift(shiftPromise);
} else {
//console.log($scope.shiftData);
shiftPromise.resolve($scope.data);
}
return shiftPromise.promise;
}
function zeroOutShifts() {
$scope.shiftCounter = 1;
$scope.trendShiftTimeData = 0;
$scope.trendShiftCountData = 0;
$rootScope.rootShiftAccTimeData = $scope.accShiftTimeData = 0;
$rootScope.rootShiftAccCountData = $scope.accShiftCountData = 0;
$rootScope.rootShiftAvgTimeData = $scope.avgShiftTimeData = 0;
$rootScope.rootShiftAvgCountData = $scope.avgShiftCountData = 0;
$scope.avgShiftTimerData = 0;
$rootScope.timeShiftSlope = 0;
$rootScope.countShiftSlope = 0;
}
function shiftDataAggregator(data, d, da, datCounter, tatPromise) {
//console.log(datCounter);
/*console.log(d);
console.log(da);*/
tempShiftTimerData = data.sum.timer / ((da - d) / 1000) * 100;
//console.log(tempTimerData);
//console.log(tempTimerData);
if (tempShiftTimerData !== 0 && data.sum.count !== 0) {
$scope.isThereData = true;
}
//var trendCountData = 0;
//console.log('Temp Timer data after calc: ' + data.sum.timer);
if ($scope.isThereData === true) {
$scope.trendShiftTimeData = trendLineCalculator(tempTimerData, $scope.shiftCounter, $scope.avgShiftTimerData);
$scope.trendShiftCountData = trendLineCalculator(data.sum.count, $scope.shiftCounter, $scope.accShiftCountData);
$rootScope.rootShiftAccCountData = $scope.accShiftCountData += data.sum.count;
//console.log('count: ' + data.sum.count);
$rootScope.rootShiftAccTimeData = $scope.accShiftTimeData += data.sum.timer;
$scope.avgShiftTimerData += tempShiftTimerData;
//console.log('timer: ' + $scope.avgTimerData);
$rootScope.rootShiftAvgTimeData = ($scope.avgShiftTimerData / $scope.shiftCounter);
/*$rootScope.rootAvgTimeData /= 60;
$rootScope.rootAvgTimeData /= 60;*/
/*console.log('Accumulated Time: ' + $scope.accTimeData);
console.log('Time Counter: ' + $scope.timeCount);*/
$rootScope.rootShiftAvgTimeData = Math.round($rootScope.rootShiftAvgTimeData);
$rootScope.rootShiftAvgCountData = $scope.accShiftCountData / $scope.countCount;
$rootScope.rootShiftAvgCountData = Math.round($rootScope.rootShiftAvgCountData);
/*console.log($rootScope.rootAvgTimeData);
console.log($rootScope.rootAvgCountData);
console.log($rootScope.rootAvgCountData);*/
/*$scope.data.push({
xCountCounter: $scope.countCount,
xTimerCounter: $scope.timeCount
});*/
$scope.shiftCounter++;
$rootScope.rootShiftAccTimeData = ($rootScope.rootShiftAccTimeData / 60) / 60;
$rootScope.rootShiftAccTimeData = Math.round($rootScope.rootShiftAccTimeData);
}
//$rootScope.rootAccTimeData = Math.round($rootScope.bbrootAccTimeData);
//
//blank out shiftsData array everytime it is pushed to shiftInfo array
var ttt = $scope.data[$scope.data.length - 1].id;
console.log('Data length: ' + ttt);
tatPromise.resolve($scope.data[$scope.data.length - 1].shifts.push({
count: Math.round(data.sum.count * 100) / 100,
timer: Math.round(tempShiftTimerData * 100) / 100,
trendTimer: Math.round($scope.trendShiftTimeData),
trendCount: Math.round($scope.trendShiftCountData),
shiftCounter: $scope.shiftCounter - 1,
startDate: d,
endDate: da
}));
//console.log($scope.data);
//$scope.shiftInfo.push($scope.shiftsData)
return tatPromise.promise;
}
var within_std_of = 2;
average = function(a) {
var r = {
mean: 0,
variance: 0,
deviation: 0
},
t = a.length;
for (var m, s = 0, l = t; l--; s += a[l]);
for (m = r.mean = s / t, l = t, s = 0; l--; s += Math.pow(a[l] - m, 2));
return r.deviation = Math.sqrt(r.variance = s / t), r;
};
withinStd = function(mean, val, stdev) {
var low = mean - (stdev * x.deviation);
var hi = mean + (stdev * x.deviation);
return (val > low) && (val < hi);
};
/*function weeklyChart(startDate, endDate) {
var daysOfYear = [];
for (var d = newStartDate; d <= newEndDate; d.setDate(d.getDate() + 1)) {
da = new Date(d);
da.setDate(da.getDate() + 1);
$scope.webCall(Math.floor(new Date(d) / 1000), Math.floor(new Date(da) / 1000));
}
if ($scope.reporting.yaxis === 'percentage') {
processObj = 'sum';
} else {
processObj = $scope.reporting.yaxis;
}
d = startDate;
nextWeek(startDate, endDate, processObj);
}*/
/*function nextWeek(startDate, endDate, processObj) {
if (d < endDate) {
da = new Date(d);
da.setDate(da.getDate() + 7);
$http.get('http://54.213.13.56/api/' + $scope.reporting.machineSelect.SoftwareKey + '/' + $scope.reporting.machineSelect.Address +
'?process[]=' + 'sum' + '&date[]=' + Math.floor(new Date(d) / 1000) + '&date[]=' + Math.floor(new Date(da) / 1000) + '&index=' +
$scope.reporting.hardware).success(
function(data) {
console.log(data);
tempTimerData = data.sum.timer / ((da - d) / 1000) * 100;
$scope.data.push({
x: new Date(d),
count: data.sum.count,
timer: tempTimerData
});
d.setDate(d.getDate() + 7);
nextWeek(startDate, endDate, processObj);
});
}
}*/
function monthlyChart(startDate, endDate) {
//console.log('Monthly was called');
var daysOfYear = [];
d = startDate;
zeroOut();
processObj = 'sum';
/*if ($scope.reporting.yaxis === 'percentage') {
processObj = 'sum';
} else {
processObj = $scope.reporting.yaxis;
}*/
nextMonth(startDate, endDate, processObj);
}
function nextMonth(startDate, endDate, processObj) {
//console.log('I am in the next()');
//console.log(d);
//console.log(d <= endDate);
if (d <= endDate) {
$scope.WorkPeriod = ((($scope.reporting.machineSelect.WorkPeriod * 60) * 60) * 1000);
if (d === endDate) {
da = new Date(d);
da = new Date(da.getFullYear(), da.getMonth() + 1, 0);
//da.setMonth(da.getMonth() + 1);
webCall($scope.reporting.machineSelect.SoftwareKey, $scope.reporting.machineSelect.Address,
processObj, Math.floor(new Date(d) / 1000), Math.floor(new Date(da) / 1000),
$scope.reporting.hardware).then(function(data) {
/*console.log(Math.floor(new Date(d) / 1000));
console.log(Math.floor(new Date(da) / 1000));*/
dataAggregator(data, d, da);
//d.setMonth(d.getMonth() + 1);
//nextMonth(startDate, endDate, processObj);
});
} else {
da = new Date(d);
da.setMonth(da.getMonth() + 1);
webCall($scope.reporting.machineSelect.SoftwareKey, $scope.reporting.machineSelect.Address,
processObj, Math.floor(new Date(d) / 1000), Math.floor(new Date(da) / 1000),
$scope.reporting.hardware).then(function(data) {
/*console.log(Math.floor(new Date(d) / 1000));
console.log(Math.floor(new Date(da) / 1000));*/
dataAggregator(data, d, da);
d.setMonth(d.getMonth() + 1);
nextMonth(startDate, endDate, processObj);
});
}
} else {
$rootScope.countSlope = 0;
$rootScope.timeSlope = 0;
if ($scope.reporting.xaxis === 'Month') {
$scope.options.series[2].visible = false;
$scope.options.drawLegend = false;
} else {
$scope.options.series[2].visible = true;
$scope.options.drawLegend = true;
}
//console.log($scope.data);
//create if statement for percentage or sum
//if ($scope.reporting.yaxis === 'sum') {
// create array_x & array_y
array_x = [];
array_y = [];
for (i = 0; i < $scope.data.length; ++i) {
array_x.push($scope.data[i].xCountCounter);
array_y.push($scope.data[i].count);
if ($scope.data[i].count > $scope.y2CountMax) {
$scope.y2CountMax = $scope.data[i].count;
}
}
var countRegLine = findLineByLeastSquares(array_x, array_y); //findLineByLeastSquaresLog(array_x, array_y);
//findLineByLeastSquaresLog(array_x, array_y);
$rootScope.countSlope = (Math.round($scope.tempSlope * 10) / 10) /* * 100*/ ;
//console.log(countRegLine);
tmpCount = 0;
angular.forEach(countRegLine[1], function(value, key) {
if (tmpCount !== countRegLine[1].length) {
$scope.data[tmpCount].countRegLine = value;
tmpCount++;
}
});
array_x = [];
array_y = [];
for (i = 0; i < $scope.data.length; ++i) {
array_x.push($scope.data[i].xTimerCounter);
array_y.push($scope.data[i].timer);
if ($scope.data[i].timer > $scope.y2TimerMax) {
$scope.y2TimerMax = $scope.data[i].timer;
}
}
var timerRegLine = findLineByLeastSquares(array_x, array_y); //findLineByLeastSquaresLog(array_x, array_y);
//findLineByLeastSquaresLog(array_x, array_y);
$rootScope.timeSlope = (Math.round($scope.tempSlope * 10) / 10) /* * 100*/ ;
//console.log(countRegLine);
tmpCount = 0;
angular.forEach(timerRegLine[1], function(value, key) {
if (tmpCount !== countRegLine[1].length) {
$scope.data[tmpCount].timerRegLine = value;
tmpCount++;
}
});
//console.log($scope.data);
/*if ($scope.data.length > 1) {
$rootScope.countSlope = ($scope.data[0].countRegLine - $scope.data[$scope.data.length - 1].countRegLine) /
($scope.data[0].xCountCounter - $scope.data[$scope.data.length - 1].xCountCounter);
$rootScope.timeSlope = ($scope.data[0].timerRegLine - $scope.data[$scope.data.length - 1].timerRegLine) /
($scope.data[0].xTimerCounter - $scope.data[$scope.data.length - 1].xTimerCounter);
} else {
$rootScope.countSlope = 0;
$rootScope.timeSlope = 0;
}*/
//}
$scope.disableInputs = false;
}
}
function zeroOut() {
$scope.countCount = 1;
$scope.timeCount = 1;
$scope.shiftCounter = 1;
$scope.trendTimeData = 0;
$scope.trendCountData = 0;
$rootScope.rootAccTimeData = $scope.accTimeData = 0;
$rootScope.rootAccCountData = $scope.accCountData = 0;
$rootScope.rootAvgTimeData = $scope.avgTimeData = 0;
$rootScope.rootAvgCountData = $scope.avgCountData = 0;
$scope.avgTimerData = 0;
$scope.isThereData = false;
$rootScope.timeSlope = 0;
$rootScope.countSlope = 0;
/*$rootScope.machinesName = '';
$rootScope.startingDate = '';
$rootScope.endingDate = '';*/
}
/*for (var d = startDate; d <= endDate; d.setMonth(d.getMonth() + 1)) {
da = new Date(d);
da.setMonth(da.getMonth() + 1);
$scope.webCall(Math.floor(new Date(d) / 1000), Math.floor(new Date(da) / 1000));
}*/
/*next();
};*/
/*$scope.webCall = function(startDate, endDate) {
getData($scope.reporting.machineSelect.SoftwareKey, $scope.reporting.machineSelect.Address,
startDate, endDate, $scope.reporting.hardware).then(function(data) {
console.log(data);
$scope.data.push({
x: new Date(startDate * 1000),
count: data.sum.count,
timer: data.sum.timer
});
}, function(error) {
console.log('Failure...', error);
});
};*/
function webCall(softwareKey, address, processObj, startDate, endDate, hardware) {
var defer = $q.defer();
/*console.log('http://54.213.13.56/api/' + softwareKey + '/' + address +
'?process[]=' + processObj + '&date[]=' + startDate + '&date[]=' + endDate + '&index=' +
hardware);*/
$http.get('http://54.213.13.56/api/' + softwareKey + '/' + address +
'?process[]=' + processObj + '&date[]=' + startDate + '&date[]=' + endDate + '&index=' +
hardware).
success(function(data) {
//console.log('Data: ' + data);
defer.resolve(data);
}).
error(function(data) {
alert(data);
});
return defer.promise;
}
function webShiftCall(softwareKey, address, processObj, startDate, endDate, hardware, webPromise) {
startDate = startDate / 1000;
endDate = endDate / 1000;
//var defer = $q.defer();
/*console.log('http://54.213.13.56/api/' + softwareKey + '/' + address +
'?process[]=' + processObj + '&date[]=' + startDate + '&date[]=' + endDate + '&index=' +
hardware);*/
$http.get('http://54.213.13.56/api/' + softwareKey + '/' + address +
'?process[]=' + processObj + '&date[]=' + startDate + '&date[]=' + endDate + '&index=' +
hardware).
success(function(data) {
webPromise.resolve(data);
}).
error(function(data) {
alert(data);
});
return webPromise.promise;
}
$scope.isThereData = false;
function dataAggregator(data, d, da) {
//if ($scope.reporting.yaxis === 'sum') {
// Methods to do for sum
//
// This gets the percentage of time
if ($scope.reporting.xaxis === 'Month') {
tempDate = d;
tempDate = new Date(tempDate.getFullYear(), tempDate.getMonth() + 1, 0);
tempDate.setDate(tempDate.getDate());
//console.log(tempDate.getDate());
tempTimerData = data.sum.timer / ((tempDate.getDate() * $scope.WorkPeriod) / 1000) * 100;
} else {
tempTimerData = data.sum.timer / (($scope.WorkPeriod) / 1000) * 100;
/*console.log('Timer: ' + data.sum.timer);
console.log('Work Period: ' + $scope.WorkPeriod);
console.log('Initial Work Period: ' + $scope.reporting.machineSelect.WorkPeriod);
console.log('Calculations: ' + data.sum.timer / (($scope.WorkPeriod) / 1000) * 100);*/
}
//console.log(tempTimerData);
if (tempTimerData !== 0 && data.sum.count !== 0) {
$scope.isThereData = true;
}
//var trendCountData = 0;
//console.log('Temp Timer data after calc: ' + data.sum.timer);
if ($scope.isThereData === true) {
$scope.trendTimeData = trendLineCalculator(tempTimerData, $scope.timeCount, $scope.avgTimerData);
$scope.trendCountData = trendLineCalculator(data.sum.count, $scope.countCount, $scope.accCountData);
$rootScope.rootAccCountData = $scope.accCountData += data.sum.count;
//console.log('count: ' + data.sum.count);
$rootScope.rootAccTimeData = $scope.accTimeData += data.sum.timer;
$scope.avgTimerData += tempTimerData;
//console.log('timer: ' + $scope.avgTimerData);
$rootScope.rootAvgTimeData = ($scope.avgTimerData / $scope.timeCount);
/*$rootScope.rootAvgTimeData /= 60;
$rootScope.rootAvgTimeData /= 60;*/
/*console.log('Accumulated Time: ' + $scope.accTimeData);
console.log('Time Counter: ' + $scope.timeCount);*/
$rootScope.rootAvgTimeData = Math.round($rootScope.rootAvgTimeData);
$rootScope.rootAvgCountData = $scope.accCountData / $scope.countCount;
$rootScope.rootAvgCountData = Math.round($rootScope.rootAvgCountData);
/*console.log($rootScope.rootAvgTimeData);
console.log($rootScope.rootAvgCountData);
console.log($rootScope.rootAvgCountData);*/
/*$scope.data.push({
xCountCounter: $scope.countCount,
xTimerCounter: $scope.timeCount
});*/
$scope.countCount++;
$scope.timeCount++;
$rootScope.rootAccTimeData = ($rootScope.rootAccTimeData / 60) / 60;
$rootScope.rootAccTimeData = Math.round($rootScope.rootAccTimeData);
}
//$rootScope.rootAccTimeData = Math.round($rootScope.rootAccTimeData);
$scope.data.push({
id: $scope.dataCount,
x: new Date(d),
count: Math.round(data.sum.count * 100) / 100,
timer: Math.round(tempTimerData * 100) / 100,
trendTimer: Math.round($scope.trendTimeData),
trendCount: Math.round($scope.trendCountData),
xCountCounter: $scope.countCount - 1,
xTimerCounter: $scope.timeCount - 1,
shifts: [],
});
/*console.log(data.sum.timer);
console.log($scope.data.trendTimer);
console.log(data.sum.count);
console.log($scope.data.trendCount);*/
/*} else {
// Methods to do for percentage
tempTimerData = data.sum.timer / ((da - d) / 1000) * 100;
//var trendCountData = 0;
//console.log('Temp Timer data after calc: ' + data.sum.timer);
$scope.trendTimeData = trendLineCalculator(tempTimerData, $scope.timeCount, $scope.accTimeData);
$scope.trendCountData = trendLineCalculator(data.sum.count, $scope.countCount, $scope.accCountData);
$rootScope.rootAccCountData = $scope.accCountData += data.sum.count;
$rootScope.rootAccTimeData = $scope.accTimeData += tempTimerData;
$rootScope.rootAvgTimeData = ($scope.accTimeData / $scope.timeCount);*/
/*console.log('Accumulated Time: ' + $scope.accTimeData);
console.log('Time Counter: ' + $scope.timeCount);*/
/*$rootScope.rootAvgTimeData = Math.round($rootScope.rootAvgTimeData);
$rootScope.rootAvgCountData = $scope.accCountData / $scope.countCount;
$rootScope.rootAvgCountData = Math.round($rootScope.rootAvgCountData);*/
/*$scope.data.push({
xCountCounter: $scope.countCount,
xTimerCounter: $scope.timeCount
});*/
/*$scope.countCount++;
$scope.timeCount++;
$rootScope.rootAccTimeData = $rootScope.rootAccTimeData;
$rootScope.rootAccTimeData = Math.round($rootScope.rootAccTimeData);
$scope.data.push({
x: new Date(d),
count: Math.round(data.sum.count * 10) / 10,
timer: Math.round(tempTimerData * 10) / 10,
trendTimer: Math.round($scope.trendTimeData * 10) / 10,
trendCount: Math.round($scope.trendCountData * 10) / 10,
xCountCounter: $scope.countCount - 1,
xTimerCounter: $scope.timeCount - 1
});
}*/
}
function dataAggregatorNoZeros(data, d, da) {
if ($scope.reporting.yaxis === 'sum') {
// Methods to do for sum
//
// This gets the percentage of time
//tempTimerData = data.sum.timer / ((da - d) / 1000) * 100;
//var trendCountData = 0;
//console.log('Temp Timer data after calc: ' + data.sum.timer);
$scope.trendTimeDataNoZeros = trendLineCalculator(data.sum.timer, $scope.timeCountNoZeros, $scope.accTimeDataNoZeros);
$scope.trendCountDataNoZeros = trendLineCalculator(data.sum.count, $scope.countCountNoZeros, $scope.accCountDataNoZeros);
$rootScope.rootAccCountDataNoZeros = $scope.accCountDataNoZeros += data.sum.count;
//console.log('count: ' + data.sum.count);
$rootScope.rootAccTimeDataNoZeros = $scope.accTimeDataNoZeros += data.sum.timer;
//console.log('timer: ' + data.sum.timer);
$rootScope.rootAvgTimeDataNoZeros = (($scope.accTimeDataNoZeros / $scope.timeCountNoZeros) / 60) / 60;
/*$rootScope.rootAvgTimeData /= 60;
$rootScope.rootAvgTimeData /= 60;*/
/*console.log('Accumulated Time: ' + $scope.accTimeData);
console.log('Time Counter: ' + $scope.timeCount);*/
$rootScope.rootAvgTimeDataNoZeros = Math.round($rootScope.rootAvgTimeDataNoZeros);
$rootScope.rootAvgCountDataNoZeros = $scope.accCountDataNoZeros / $scope.countCountNoZeros;
$rootScope.rootAvgCountDataNoZeros = Math.round($rootScope.rootAvgCountDataNoZeros);
/*$scope.data.push({
xCountCounter: $scope.countCount,
xTimerCounter: $scope.timeCount
});*/
$scope.countCountNoZeros++;
$scope.timeCountNoZeros++;
$rootScope.rootAccTimeDataNoZeros = ($rootScope.rootAccTimeDataNoZeros / 60) / 60;
$rootScope.rootAccTimeDataNoZeros = Math.round($rootScope.rootAccTimeDataNoZeros);
//$rootScope.rootAccTimeData = Math.round($rootScope.rootAccTimeData);
$scope.nonZeroData.push({
x: new Date(d),
count: Math.round(data.sum.count),
timer: Math.round(data.sum.timer),
trendTimer: Math.round($scope.trendTimeDataNoZeros),
trendCount: Math.round($scope.trendCountDataNoZeros),
xCountCounter: $scope.countCountNoZeros - 1,
xTimerCounter: $scope.timeCountNoZeros - 1
});
/*console.log(data.sum.timer);
console.log($scope.data.trendTimer);
console.log(data.sum.count);
console.log($scope.data.trendCount);*/
} else {
// Methods to do for percentage
tempTimerData = data.sum.timer / ((da - d) / 1000) * 100;
//var trendCountData = 0;
//console.log('Temp Timer data after calc: ' + data.sum.timer);
$scope.trendTimeDataNoZeros = trendLineCalculator(tempTimerData, $scope.timeCountNoZeros, $scope.accTimeDataNoZeros);
$scope.trendCountDataNoZeros = trendLineCalculator(data.sum.count, $scope.countCountNoZeros, $scope.accCountDataNoZeros);
$rootScope.rootAccCountDataNoZeros = $scope.accCountDataNoZeros += data.sum.countNoZeros;
$rootScope.rootAccTimeDataNoZeros = $scope.accTimeDataNoZeros += tempTimerDataNoZeros;
$rootScope.rootAvgTimeDataNoZeros = ($scope.accTimeDataNoZeros / $scope.timeCountNoZeros);
/*console.log('Accumulated Time: ' + $scope.accTimeData);
console.log('Time Counter: ' + $scope.timeCount);*/
$rootScope.rootAvgTimeData = Math.round($rootScope.rootAvgTimeData);
$rootScope.rootAvgCountData = $scope.accCountData / $scope.countCount;
$rootScope.rootAvgCountData = Math.round($rootScope.rootAvgCountData);
/*$scope.data.push({
xCountCounter: $scope.countCount,
xTimerCounter: $scope.timeCount
});*/
$scope.countCount++;
$scope.timeCount++;
$rootScope.rootAccTimeData = $rootScope.rootAccTimeData;
$rootScope.rootAccTimeData = Math.round($rootScope.rootAccTimeData);
$scope.dataNoZeros.push({
x: new Date(d),
count: Math.round(data.sum.count * 10) / 10,
timer: Math.round(tempTimerData * 10) / 10,
trendTimer: Math.round($scope.trendTimeData * 10) / 10,
trendCount: Math.round($scope.trendCountData * 10) / 10,
xCountCounter: $scope.countCount - 1,
xTimerCounter: $scope.timeCount - 1
});
}
}
function trendLineCalculator(value, count, accValue) {
/*console.log('value: ' + value);
console.log('count: ' + count);
console.log('accValue: ' + accValue);*/
average = (accValue + value) / count;
//console.log('After Claculations and Such: ' + average);
return average;
}
$scope.changedMachine = function(item) {
$scope.machineDates = item.Dates;
//console.log(item);
};
/**
* Write to the Console
* @param {string} message
*/
self.log = function(message) {
console.log(message);
};
function findLineByLeastSquares(values_x, values_y) {
var sum_x = 0;
var sum_y = 0;
var sum_xy = 0;
var sum_xx = 0;
var count = 0;
/*
* We'll use those variables for faster read/write access.
*/
var x = 0;
var y = 0;
/*****************************WHAT MARKO ADDED***************************************/
//index of array where y array will progress into a sequence of non-zero elements
var index_nonZero;
//first if statement checks if first element is zero
//if y[0]!=0 then we have nothing to worry about
//if y[0] = 0 then we see where the elements begin to be non zero
//once we find that spot, we break out of loop and remove beginning elements that are zero.
if (values_y[0] === 0) {
for (i = 0; i < values_y.length; i++) {
if (values_y[i] !== 0) {
index_nonZero = i;
break;
}
}
}
values_y = values_y.splice(index_nonZero, values_y.length);
//tempValuesX = values_x.splice(0, index_nonZero);
values_x = values_x.splice(index_nonZero, values_x.length);
console.log(values_y);
//console.log(values_X);
/******************************END OF WHAT MARKO ADDED***************************/
var values_length = values_x.length;
if (values_length != values_y.length) {
throw new Error('The parameters values_x and values_y need to have same size!');
}
/*
* Nothing to do.
*/
if (values_length === 0) {
return [
[],
[]
];
}
// values_y is shorter than x and so it fails trying to do calculations below. Need to
// look into for a fix
/*
* Calculate the sum for each of the parts necessary.
*/
for (var v = 0; v < values_length; v++) {
x = values_x[v];
y = values_y[v];
sum_x += x;
sum_y += y;
sum_xx += x * x;
sum_xy += x * y;
count++;
}
/*
* Calculate m and b for the formular:
* y = x * m + b
*/
var m = (count * sum_xy - sum_x * sum_y) / (count * sum_xx - sum_x * sum_x);
$scope.tempSlope = m;
var b = (sum_y / count) - (m * sum_x) / count;
/*
* We will make the x and y result line now
*/
var result_values_x = [];
var result_values_y = [];
for (i = 0; i < index_nonZero; i++) {
result_values_y.push(0);
result_values_x.push(0);
}
for (var v = 0; v < values_length; v++) {
x = values_x[v];
y = x * m + b;
result_values_x.push(x);
result_values_y.push(y);
}
return [result_values_x, result_values_y];
}
//console.log(findLineByLeastSquares(new Array(1, 2, 3, 4, 5, 6, 7), new Array(0, 0, 0, 100, 50, 41, 57)));
function findLineByLeastSquaresLog(values_x, values_y) {
var sum_x = 0;
var sum_y = 0;
var sum_xy = 0;
var sum_xx = 0;
var count = 0;
/*
* We'll use those variables for faster read/write access.
*/
var x = 0;
var y = 0;
var values_length = values_x.length;
if (values_length != values_y.length) {
throw new Error('The parameters values_x and values_y need to have same size!');
}
/*
* Nothing to do.
*/
if (values_length === 0) {
return [
[],
[]
];
}
/*
* Calculate the sum for each of the parts necessary.
*/
for (var v = 0; v < values_length; v++) {
x = values_x[v];
y = values_y[v];
if (y !== 0) {
y = Math.log(y);
}
sum_x += x;
sum_y += y;
sum_xx += x * x;
sum_xy += x * y;
count++;
}
/*
* Calculate m and b for the formular:
* y = x * m + b
*/
var m = (count * sum_xy - sum_x * sum_y) / (count * sum_xx - sum_x * sum_x);
//$scope.tempSlope = m;
var b = (sum_y / count) - (m * sum_x) / count;
/*
* We will make the x and y result line now
*/
var result_values_x = [];
var result_values_y = [];
for (var v = 0; v < values_length; v++) {
x = values_x[v];
y = x * m + b;
result_values_x.push(x);
result_values_y.push(y);
}
return [result_values_x, result_values_y];
}
// Initialise Directive
self.Initialise();
},
// require: 'ngModel', // Array = multiple requires, ? = optional, ^ = check parent elements
restrict: 'E', // E = Element, A = Attribute, C = Class, M = Comment
// template: 'hi
',
templateUrl: "view/reportingSidebar.html?v=0.1"
// replace: true,
// transclude: true,
// compile: function(tElement, tAttrs, function transclude(function(scope, cloneLinkingFn){ return function linking(scope, elm, attrs){}})),
//link: function($scope, iElm, iAttrs, controller) {
//}
};
});