Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
cactus committed Jun 12, 2015
1 parent f2a7a15 commit 857aa52
Show file tree
Hide file tree
Showing 8 changed files with 367 additions and 117 deletions.
192 changes: 192 additions & 0 deletions public/javascripts/dashboards/nvd3_incidents_line_cumulative.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
//var chart;
var charts=[];

var _period;
var _aggregate;
var _dateField;//="openedAt";

function _getYear(period){
//console.log("**** getYear() called: period: "+period);
var _split = period.split("-");
var _year;
if (_split.length==2){
_year = parseInt(_split[1]);
}
else if (_split.length==1){
_year = parseInt(_split[0]);
}
if (_year>=2014 && _year <=moment().year()) return _year;
}

function _alterPeriodByYear(period,yearDelta){
var _year = _getYear(period)+yearDelta;
var _split = period.split("-");
if (_split.length==1){
return _year.toString();
}
else if (_split.length==2){
return _split[0]+"-"+_year;
}
return false;
}



function init(prio,dateField,chartId){
_dateField = dateField;
nv.addGraph(function() {
var incidents;
var chart;
var _chartId = chartId;//"chartP1";
var _prio = prio;//chartId.split("chart")[1];
_period =getUrlVars().period;
if (!_period) _period=moment().year().toString();
_aggregate =getUrlVars().aggregate;
if (_aggregate){
_url+="?aggregate="+_aggregate;
}
if (!_aggregate) _aggregate="monthly";
var _colors={};
// #ED1C24 #EF333A #F14A50 #F36166 #F5787C #F78F92 #F9A6A8 #FBBDBE #FDD4D4 #FFEBEA #FFFFFF
_colors["P01"]=["#ED1C24","#FDD4D4"];
// #F7931D #F89E34 #F9A94B #FAB462 #FBBF79 #FCCA90 #FDD5A7 #FEE0BE #FFEBD5 #FFF6EC #FFFFFF
_colors["P08"]=["#F7931D","#FFEBD5"];
// #38A4DC #4CADE0 #60B6E4 #74BFE8 #88C8EC #9CD1F0 #B0DAF4 #C4E3F8 #D8ECFC #ECF5FF #FFFFFF
_colors["P16"]=["#38A4DC","#D8ECFC"];
_colors["P120"]=["#aaaaaa","#efefef"];
console.log("prio: "+_prio+" colors: "+_colors[_prio]);
console.log("period: "+_period);
chart = nv.models.cumulativeLineChart()
.useInteractiveGuideline(true)
.x(function(d) { return d.date })
.y(function(d) { return d.y })
.color(d3.scale.category10().range())
//.average(function(d) { return d.mean/100; })
.tooltips(true)
.showLegend(true)
.color(_colors[_prio])


.showControls(false)
;
if (_period=="") _period ="All";
$("#period_"+_chartId).text(_period);
$("#aggregate_"+_chartId).text(_aggregate);
var _reduceXTicks = false;
//if (_aggregate=="daily" || _aggregate=="weekly") _reduceXTicks = true;
chart.yAxis
.tickFormat(d3.format(',d'));
redraw(_chartId,_period,_aggregate,_prio,dateField);

//function pointer
charts[chartId]=chart;
return chart;
});



// clickhandler for
$('a.dropdown.incidentstrend').click( function(event) {

var _prio =event.target.id.split("_")[0].split("chart")[1].split("-")[0];


var _chart=event.target.id.split("_")[0];
var _dateField = _chart.split("-")[1];

if (_.startsWith(event.target.id.split("_")[1],"aggregate")){
_aggregate =event.target.id.split("_")[1].split("-")[1];
}
else
{
_period = event.target.id.split("_")[1];
}
console.log("######################### ____id: "+event.target.id);
console.log("######################### chart: "+_chart);
console.log("######################### _dateField: "+_dateField);

$("#period_"+_chart).text(_period);
$("#aggregate_"+_chart).text(_aggregate);
redraw(_chart,_period,_aggregate,_prio,_dateField);
});


}

/**
* prepares data to be consumable by NVd3
*/
function _prepareData(incidents,incidentsPrev,prio,period,dateField){
/*
console.log("----------------- _prepareData: prio= "+prio);
console.log("----------------- _prepareData: period= "+period);
console.log("----------------- _prepareData: dateField= "+dateField);
console.log("----------------- _prepareData: incidents[0]="+JSON.stringify(incidents[0]));
*/
var _P=[];
var _PBase=[];
var _PSum=0;
var _PBaseSum=0;

var _PPrev=[];
var _PBasePrev=[];
var _PSumPrev=0;
var _PBaseSumPrev=0;

var _year = _getYear(period);
var _yearPrev = _getYear(_alterPeriodByYear(period,-1));

for (var day in incidents){
/*
console.log(JSON.stringify(incidents[day]));
console.log("++ dateField: "+dateField);
console.log("++ prio: "+prio);
console.log("date: "+incidents[day].date+" "+prio+": "+incidents[day][dateField][prio].total);
*/
_P.push({"date":incidents[day].date,"y":parseInt(incidents[day][dateField][prio].total)});
_PSum+=parseInt(incidents[day][dateField][prio].total);
}
//console.log(prio+"Sum = "+_PSum+" "+prio+"BaseSum = "+_PBaseSum);

for (var day in incidentsPrev){
//console.log("date: "+incidentsPrev[day].date+" "+prio+": "+incidentsPrev[day][dateField][prio].total);
//date of previous year must be "normalized" to this year ....
_PPrev.push({"date":_alterPeriodByYear(incidentsPrev[day].date,1),"y":parseInt(incidentsPrev[day][dateField][prio].total)});
_PSumPrev+=parseInt(incidentsPrev[day][dateField][prio]);

}
//console.log(prio+"Sum = "+_PSumPrev+" "+prio+"BaseSum = "+_PBaseSumPrev);

var incData = [{key:_year,values:_P},{key:_yearPrev,values:_PPrev}]

return incData
}

function redraw(chartId,period,aggregate,prio,dateField) {
var _url = "/api/space/rest/incidenttracker/"+period;
var _urlPrev = "/api/space/rest/incidenttracker/"+_alterPeriodByYear(period,-1);


if (aggregate){
_url+="?aggregate="+aggregate;
_urlPrev+="?aggregate="+aggregate;
}
console.log("_url: "+_url);


d3.json(_url, function(data) {
d3.json(_urlPrev, function(dataPrev) {
d3.select('#'+chartId+' svg')
.datum(_prepareData(data.tracker,dataPrev.tracker,prio,period,dateField))
.transition().duration(500)
.call(charts[chartId]);


$("#"+chartId+"_sum").text(data.statistics.sum[prio][dateField]);
$("#"+chartId+"_sumPrev").text(dataPrev.statistics.sum[prio][dateField]);


nv.utils.windowResize(charts[chartId].update);
});
});
}
5 changes: 4 additions & 1 deletion public/javascripts/dashboards/nvd3_incidents_multi.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ function redraw(chartId,period,aggregate,prio,dateField) {
}
console.log("_url: "+_url);


d3.json(_url, function(data) {
d3.json(_urlPrev, function(dataPrev) {
d3.select('#'+chartId+' svg')
Expand All @@ -181,9 +182,11 @@ function redraw(chartId,period,aggregate,prio,dateField) {
.call(charts[chartId]);


nv.utils.windowResize(charts[chartId].update);
$("#"+chartId+"_sum").text(data.statistics.sum[prio][dateField]);
$("#"+chartId+"_sumPrev").text(dataPrev.statistics.sum[prio][dateField]);


nv.utils.windowResize(charts[chartId].update);
});
});
}
6 changes: 4 additions & 2 deletions routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,10 @@ function findIncidenttrackerByDate(req, res , next){
logger.warn("[error] incidentTrackerService.findTrackerByDate says: "+err.message);
}
else {
res.send(data);
return;
incidentTrackerService.buildStatistics(data.tracker,function(err,result){
res.send(result);
return;
})
}
});
}
Expand Down
13 changes: 9 additions & 4 deletions routes/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,20 @@ router.get('/availability', function(req, res) {

router.get('/incidents', function(req, res) {
var _period = req.query.period;
//"openedAt", "resolvedAt", "closedAt"


res.locals.period = _period;

res.locals.moment = moment;
res.render('dashboard/incidents', { title: 's p a c e - incidents dashboard' });
});

router.get('/incidents_line', function(req, res) {
var _period = req.query.period;
res.locals.period = _period;
res.locals.moment = moment;
res.render('dashboard/incidents_line', { title: 's p a c e - incidents linedashboard' });
});



router.get('/qos', function(req, res) {
res.render('dashboard/qos', { title: 's p a c e - QoS dashboard' });
});
Expand Down
Loading

0 comments on commit 857aa52

Please sign in to comment.