define([
'timeline',
'view/base'
], function(
Timeline,
BaseView
){
var AlertTimelineView = BaseView.extend({
events : {
'shown .accordion-body' : 'publishTimelineHeight',
'hidden .accordion-body' : 'publishTimelineHeight'
},
subscriptions : {
'view:dashboard:complete' : 'render'
},
initialize : function(options) {
_.bindAll(this);
this.templar = options.templar;
this.dashboardId = options.dashboardId;
this.status = options.status;
this.popovers = [];
this.openPopovers = [];
this.alertData = [];
},
render : function() {
this.templar.render({
path : 'alerttimeline',
el : this.$el,
data : {}
});
this.$timeline = this.$el.find('.alert-timeline .accordion-inner');
this.$accordion = this.$el.find('.alert-timeline .accordion-body');
// this.setupAlertTimeline();
// // notify user with red bell indicator
// this.setAlertStatus();
},
addPopover : function() {
var self = this;
jobAlertIdList = _.uniq(self.jobAlertList);
for (var i = jobAlertIdList.length - 1; i >= 0; i--) {
var initPopover = function(jobId) {
var $content = $("
"),
$closeBtn = $('', {
'text' : 'Troubleshoot Monitor',
'class' : 'btn'
});
var $group = $('.timeline-group-' + jobId);
var el = $group.popover({
trigger : 'manual',
html : true,
placement : 'bottom',
delay : {
show : 100,
hide : 200
},
container : 'body',
content : $content
}).click(function(e) {
e.stopPropagation();
var $this = $(this);
// remove other open popovers from view
_.each(self.openPopovers, function(openPopover) {
openPopover.data('popover').tip().removeClass('active');
openPopover.data('popover').hide();
});
// reset list of open popovers
self.openPopovers = [];
// check that popover is active
if ($this.toggleClass('active').hasClass('active')) {
$closeBtn.off('click');
$('body').off('click');
$this.popover('show');
// throw up the loading text till the data is retrieved about the incident
// Note : better overlay method overiding base. Need to switch out base
self.showOverlay($content.parent(), 'Loading...', 'alert-data-overlay');
self.loadAlertIncidentData(jobId, $this, $content, function(incidentData) {
// create the table that will hold meta data about incident
var $metaTable = self.createMetaTable(incidentData);
if ( $this.data('popover').tip().find('.alert-meta-data').length > 0 ) {
$this.data('popover').tip().find('.alert-meta-data').replaceWith($metaTable);
} else {
$this.data('popover').tip().append($metaTable);
}
$this.data('popover').tip().append($closeBtn);
// add a popover hide trigger
$closeBtn.on('click', function(e) {
e.stopPropagation();
$this.popover('hide');
$this.toggleClass('active');
$closeBtn.off('click');
self.openPopovers = [];
Backbone.Mediator.pub('view:alerttimeline:troubleshoot', jobId);
});
// push the current element tied to a popover into an array
self.openPopovers.push($this);
// add a on body trigger to remove popovers as well
$('body').on('click', function() {
self.openPopovers = [];
$this.removeClass('active');
$this.popover('hide');
});
});
} else {
self.openPopovers = [];
$this.popover('hide');
}
});
}
// initialize popovers with correct jobId reference
initPopover(jobAlertIdList[i]);
};
},
loadAlertIncidentData : function(jobId, $alertEvent, $content, cb) {
var self = this;
// load graph data for popover
var job = self.collection.get(jobId),
incidentPattern = /timeline-event-(\d+)/g,
incidentId = parseInt(incidentPattern.exec($alertEvent.attr('class'))[1]),
incidentData = _.findWhere(self.alertData, {
'id' : incidentId
});
// get meta data for incident alert
var name = job.get('name'),
startDate = new XDate(incidentData.start),
endDate = new XDate(incidentData.end),
jobData = job.toJSON(),
incidentMinDiff = startDate.diffMinutes(endDate),
incidentHrsDiff = startDate.diffHours(endDate);
jobData.toDate = endDate.toString("MM/dd/yyyy HH:mm");
// restricting to 500 minutes back to help keep HighCharts from choking
jobData.minutes = ( incidentMinDiff < 500 ) ? incidentMinDiff : 500;
var chart = self.initGraph($content[0]);
$.ajax({
url : '/monitor.json',
type : 'post',
data : job.toJSON(),
async : false,
success : function( response ) {
if ( response.status == 'success' ) {
var formattedGraphData = self.formatGraphData( response.graph_data );
self.renderGraphData(chart, formattedGraphData);
if ( typeof cb === 'function' ) {
cb({
'name' : name,
'start' : startDate.toString("MM/dd/yyyy HH:mm"),
'end' : endDate.toString("MM/dd/yyyy HH:mm"),
'hrs' : Math.floor(incidentHrsDiff),
'min' : Math.floor(incidentMinDiff % 60)
});
}
// finally hide the loading text
self.hideOverlay($content.parent());
}
}
});
},
// NOTE : Need to abstract this out to a template
createMetaTable : function(incidentData) {
return $("