Skip to content

Commit

Permalink
Merge from auxesisgh-98
Browse files Browse the repository at this point in the history
  • Loading branch information
Lindsay Holmwood committed Jul 15, 2012
2 parents b18c127 + 1ee5cbc commit b159df8
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 49 deletions.
46 changes: 42 additions & 4 deletions lib/visage-app/public/javascripts/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ var SearchToken = new Class({
values = parts[1].split(','),
value = options.add || options.remove;

if (key == 'timeframe') { return parameter }
if (value.contains('/') && key == "hosts") { return parameter }
if (!value.contains('/') && key == "metrics") { return parameter }

Expand Down Expand Up @@ -462,6 +463,15 @@ var ChartBuilder = new Class({
});
this.searchers.metric = searcher;
},
getURLTimeframe: function() {
var urlTimeframe = window.location.hash.split('|').filter(function(parameter) { return parameter.test('^timeframe') })[0];

if (urlTimeframe) {
return decodeURI(urlTimeframe.split('=')[1]);
} else {
return null;
}
},
setupTimeframeSelection: function() {
var currentDate = new Date;
var currentUnixTime = parseInt(currentDate.getTime() / 1000);
Expand Down Expand Up @@ -494,7 +504,33 @@ var ChartBuilder = new Class({
// Trigger all graphs to reload
graph.form.fireEvent('submit', e)
});
}

var parameters = window.location.hash.slice(1).split('|');
if (parameters.filter(function(parameter) { return parameter.test('^timeframe') }).length > 0) {
var parameters = parameters.map(function(parameter) {
if (!$chk(parameter)) { return parameter }

var parts = parameter.split('='),
key = parts[0],
values = parts[1].split(',');

if (key == 'timeframe') {
var string = key + '=' + encodeURI(globalTimeframe);
return string
} else {
return parameter
}
});
} else {
var timeframe = encodeURI("timeframe=" + globalTimeframe);
parameters.include(timeframe);
}

console.log(parameters);

var hash = parameters.join('|');
window.location.hash = hash;
},
}
});

Expand All @@ -513,15 +549,15 @@ var ChartBuilder = new Class({
'1 year': 8760,
'2 years': 17520 });

timeframe = this.getURLTimeframe();
timescales.each(function(hour, label) {
var current = this.currentTimePeriod == 'last {label}'.substitute({'label': label });
var value = "start={start}".substitute({'start': currentUnixTime - (hour * 3600)});
var html = 'last {label}'.substitute({'label': label });

var option = new Element('option', {
'html': html,
'value': value,
'selected': (current ? 'selected' : ''),
'selected': (html == timeframe),
});
select.grab(option)
});
Expand Down Expand Up @@ -685,6 +721,7 @@ var ChartBuilder = new Class({
save = this.save,
profile_name = this.profile_name,
percentiles = this.options.percentiles;
timeframe = this.getURLTimeframe(),

graphs.empty();
hosts.each(function(host) {
Expand All @@ -709,7 +746,8 @@ var ChartBuilder = new Class({

var graph = new VisageGraph(element, host, plugin, {
pluginInstance: metrics.join(','),
percentiles: percentiles
percentiles: percentiles,
timeframe: timeframe,
});

window.Graphs.include(graph);
Expand Down
108 changes: 63 additions & 45 deletions lib/visage-app/public/javascripts/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,21 +165,63 @@ var VisageBase = new Class({
options: {
secureJSON: false,
httpMethod: 'get',
live: false,
stacked: false
live: false,
stacked: false,
},
initialize: function(element, host, plugin, options) {
this.parentElement = element;
this.options.host = host;
this.options.plugin = plugin;
this.query = window.location.search.slice(1).parseQueryString();
this.options = Object.merge(this.options, this.query);
this.currentDate = new Date;
this.currentUnixTime = parseInt(this.currentDate.getTime() / 1000);

this.timeframes = new Hash({
'last 1 hour': { start: -1, unit: 'hours' },
'last 2 hours': { start: -2, unit: 'hours' },
'last 6 hours': { start: -6, unit: 'hours' },
'last 12 hours': { start: -12, unit: 'hours' },
'last 24 hours': { start: -24, unit: 'hours' },
'last 3 days': { start: -72, unit: 'hours' },
'last 7 days': { start: -168, unit: 'hours' },
'last 2 weeks': { start: -336, unit: 'hours' },
'last 1 month': { start: -774, unit: 'hours' },
'last 3 months': { start: -2322, unit: 'hours' },
'last 6 months': { start: -4368, unit: 'hours' },
'last 1 year': { start: -8760, unit: 'hours' },
'last 2 years': { start: -17520, unit: 'hours' },
'current month': { start: 0, finish: 1, unit: 'months' },
'previous month': { start: -1, finish: 0, unit: 'months' },
'two months ago': { start: -2, finish: -1, unit: 'months' },
'three months ago': { start: -3, finish: -2, unit: 'months' },
}).map(function(time) {
switch(true) {
case (time.unit == 'hours'):
return this.currentUnixTime - (Math.abs(time.start) * 3600)
case (time.unit == 'months'):
delete(time.unit) // nuke the unit, so it doesn't get transformed
return Object.map(time, function(value, key) {
if (value < 0) {
return new Date().decrement('month', Math.abs(value)).set('date', 1).clearTime().getTime() / 1000;
}
if (value > 0) {
return new Date().increment('month', value).set('date', 1).clearTime().getTime() / 1000;
}
return new Date().set('date', 1).clearTime().getTime() / 1000;
});
}
}, this);

this.setOptions(options);

this.requestData = new Object();
this.requestData.start = this.options.start;
this.requestData.finish = this.options.finish;
if (this.options.timeframe) {
this.setupTimeframe();
} else {
this.requestData.start = this.options.start;
this.requestData.finish = this.options.finish;
}

this.getData(); // calls graphData
},
Expand Down Expand Up @@ -233,6 +275,12 @@ var VisageBase = new Class({
}
return title
},
setupTimeframe: function() {
var timeframe = this.options.timeframe;
var timeframes = this.timeframes

this.requestData.start = timeframes[timeframe];
},
});


Expand Down Expand Up @@ -544,9 +592,6 @@ var VisageGraph = new Class({
* |
* - option
*/
var currentDate = new Date;
var currentUnixTime = parseInt(currentDate.getTime() / 1000);

var container = $(this.parentElement);
var form = this.form = new Element('form', {
'method': 'get',
Expand Down Expand Up @@ -576,49 +621,22 @@ var VisageGraph = new Class({
}
});

/* Timescales available in the dropdown */
var timescales = new Hash({ '1 hour': 1,
'2 hours': 2,
'6 hours': 6,
'12 hours': 12,
'24 hours': 24,
'3 days': 72,
'7 days': 168,
'2 weeks': 336,
'1 month': 774,
'3 months': 2322,
'6 months': 4368,
'1 year': 8760,
'2 years': 17520 });

timescales.each(function(hour, label) {
var current = this.currentTimePeriod == 'last {label}'.substitute({'label': label });
var value = "start={start}".substitute({'start': currentUnixTime - (hour * 3600)});
var html = 'last {label}'.substitute({'label': label });

var option = new Element('option', {
'html': html,
'value': value,
'selected': (current ? 'selected' : ''),
});
select.grab(option)
});

/* Calendar month timescales dropdown */
var monthlyTimescales = new Hash({ 'current month': 0,
'previous month': 1,
'two months ago': 2,
'three months ago': 3});

monthlyTimescales.each(function(monthsAgo, label) {
var current = this.currentTimePeriod == label;
var value = "start=" + (new Date().decrement('month', monthsAgo).set('date', 1).set('hr', 0).set('min', 0).set('sec', 0).getTime() / 1000);
value += '&finish=' + (new Date().decrement('month', monthsAgo - 1).set('date', 1).set('hr', 0).set('min', 0).set('sec', 0).getTime() / 1000);
timeframe = this.options.timeframe;
this.timeframes.each(function(time, label) {
switch(true) {
case (typeof(time) == 'number'):
var value = "start=" + time;
break
case (typeof(time) == 'object'):
var value = Object.toQueryString(time);
break
}

var option = new Element('option', {
'html': label,
'value': value,
'selected': (current ? 'selected' : ''),
'selected': label == timeframe,
});
select.grab(option)
});
Expand Down

0 comments on commit b159df8

Please sign in to comment.