-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Isolated dashboard
app JavaScript
#2155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
adamzap
wants to merge
1
commit into
django:main
Choose a base branch
from
adamzap:isolate-dashboard-javascript
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,19 @@ | ||
{% extends "base_dashboard.html" %} | ||
{% load i18n %} | ||
{% load static %} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not load it alongside |
||
|
||
{% block title %}{{ metric }} | {{ block.super }}{% endblock %} | ||
|
||
{% block content %} | ||
<div class="dashboard-detail"> | ||
<div> | ||
<h2><a href="{{ metric.link }}">{{ metric }}</a></h2> | ||
<div class="graph-wrapper"> | ||
<div id="graph" class="graph" data-path="{% url "metric-list" host "dashboard" %}" data-metric="{{ metric.slug }}"></div> | ||
</div> | ||
<a class="link-readmore back-link" href="{% url "dashboard-index" host "dashboard" %}">{% translate "All metrics" %}</a> | ||
</div> | ||
{% endblock %} | ||
|
||
{% block dashboard_javascript %} | ||
<script src="{% static "js/dashboard/detail.js" %}"></script> | ||
{% endblock %} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,83 @@ | ||
define('dashboard/detail', [ | ||
'jquery', | ||
'jquery.flot', | ||
'dashboard/utils', | ||
], function ($, plot, utils) { | ||
$(function () { | ||
var element = $('#graph'); | ||
var url = element.data('path') + element.data('metric') + '.json?days=365'; | ||
var hover = { | ||
show: function (x, y, message) { | ||
$('<div id="hover">') | ||
.html(message) | ||
.css({ top: y, left: x }) | ||
.appendTo('body') | ||
.show(); | ||
}, | ||
hide: function () { | ||
$('#hover').remove(); | ||
$(function () { | ||
var element = $('#graph'); | ||
var url = element.data('path') + element.data('metric') + '.json?days=365'; | ||
var hover = { | ||
show: function (x, y, message) { | ||
$('<div id="hover">') | ||
.html(message) | ||
.css({ top: y, left: x }) | ||
.appendTo('body') | ||
.show(); | ||
}, | ||
hide: function () { | ||
$('#hover').remove(); | ||
}, | ||
}; | ||
|
||
$.getJSON(url, function (response) { | ||
for (var i = 0; i < response.data.length; i++) { | ||
response.data[i][0] = response.data[i][0] * 1000; | ||
} | ||
var options = { | ||
xaxis: { | ||
mode: 'time', | ||
tickColor: 'rgba(0,0,0,0)', | ||
minTickSize: [1, 'day'], | ||
}, | ||
yaxis: { min: 0, ticks: 4 }, | ||
grid: { borderWidth: 0, hoverable: true, color: '#0C3C26' }, | ||
colors: ['#0C4B33'], | ||
}; | ||
|
||
$.getJSON(url, function (response) { | ||
for (var i = 0; i < response.data.length; i++) { | ||
response.data[i][0] = response.data[i][0] * 1000; | ||
} | ||
var options = { | ||
xaxis: { | ||
mode: 'time', | ||
tickColor: 'rgba(0,0,0,0)', | ||
minTickSize: [1, 'day'], | ||
}, | ||
yaxis: { min: 0, ticks: 4 }, | ||
grid: { borderWidth: 0, hoverable: true, color: '#0C3C26' }, | ||
colors: ['#0C4B33'], | ||
if (response.period == 'daily') { | ||
options.bars = { | ||
show: true, | ||
barWidth: 22 * 60 * 60 * 1000, | ||
align: 'center', | ||
}; | ||
if (response.period == 'daily') { | ||
options.bars = { | ||
show: true, | ||
barWidth: 22 * 60 * 60 * 1000, | ||
align: 'center', | ||
}; | ||
} else if (response.period == 'weekly') { | ||
options.bars = { | ||
show: true, | ||
barWidth: 22 * 60 * 60 * 7 * 1000, | ||
align: 'center', | ||
}; | ||
} | ||
var plot = $.plot(element, [response.data], options); | ||
|
||
var format_message = function (timestamp, measurement) { | ||
var unit = measurement == 1 ? response.unit : response.unit_plural; | ||
return ( | ||
utils.formatTimestamp(timestamp, response.period) + | ||
'<br>' + | ||
measurement + | ||
' ' + | ||
unit | ||
); | ||
} else if (response.period == 'weekly') { | ||
options.bars = { | ||
show: true, | ||
barWidth: 22 * 60 * 60 * 7 * 1000, | ||
align: 'center', | ||
}; | ||
} | ||
var plot = $.plot(element, [response.data], options); | ||
|
||
var previousPoint = null; | ||
element.bind('plothover', function (event, pos, item) { | ||
if (item) { | ||
if (previousPoint != item.dataIndex) { | ||
previousPoint = item.dataIndex; | ||
hover.hide(); | ||
var x, y; | ||
var message = format_message.apply(null, item.datapoint); | ||
if (response.period == 'instant') { | ||
x = item.pageX + 10; | ||
y = item.pageY + 10; | ||
} else { | ||
// I'd like this hover to be centered over the bar. This | ||
// simple math sorta works, but it assumes a *lot* about | ||
// the plot and basically won't scale. Grr. | ||
x = item.pageX - 40; | ||
y = item.pageY - 50; | ||
} | ||
hover.show(x, y, message); | ||
} | ||
} else { | ||
var format_message = function (timestamp, measurement) { | ||
var unit = measurement == 1 ? response.unit : response.unit_plural; | ||
return ( | ||
formatTimestamp(timestamp, response.period) + | ||
'<br>' + | ||
measurement + | ||
' ' + | ||
unit | ||
); | ||
}; | ||
|
||
var previousPoint = null; | ||
element.bind('plothover', function (event, pos, item) { | ||
if (item) { | ||
if (previousPoint != item.dataIndex) { | ||
previousPoint = item.dataIndex; | ||
hover.hide(); | ||
previousPoint = null; | ||
var x, y; | ||
var message = format_message.apply(null, item.datapoint); | ||
if (response.period == 'instant') { | ||
x = item.pageX + 10; | ||
y = item.pageY + 10; | ||
} else { | ||
// I'd like this hover to be centered over the bar. This | ||
// simple math sorta works, but it assumes a *lot* about | ||
// the plot and basically won't scale. Grr. | ||
x = item.pageX - 40; | ||
y = item.pageY - 50; | ||
} | ||
hover.show(x, y, message); | ||
} | ||
}); | ||
} else { | ||
hover.hide(); | ||
previousPoint = null; | ||
} | ||
}); | ||
}); | ||
}); |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,26 @@ | ||
define('dashboard/utils', ['jquery'], function ($) { | ||
return { | ||
formatTimestamp: function formatTimestamp(timestamp, period) { | ||
var d = new Date(timestamp); | ||
if (period == 'instant') { | ||
return $.plot.formatDate(d, '%b %d, %h:%M %p'); | ||
} else if (period == 'daily') { | ||
return $.plot.formatDate(d, '%b %d'); | ||
} else if (period == 'weekly') { | ||
// A bit more complicated than the above: the timestamp is in the | ||
// middle of the week, so we have to bracket the date. This is | ||
// something of a fudge here, but it works well enough. | ||
var start = new Date(d.getTime() - 3 * 24 * 60 * 60 * 1000); | ||
var end = new Date(d.getTime() + 3 * 24 * 60 * 60 * 1000); | ||
return ( | ||
$.plot.formatDate(start, '%b %d') + | ||
' - ' + | ||
$.plot.formatDate(end, '%b %d') | ||
); | ||
} | ||
}, | ||
convertSecondsToMilliseconds: function convertSecondsToMilliseconds(data) { | ||
for (var i = 0; i < data.length; i++) { | ||
data[i][0] = data[i][0] * 1000; | ||
} | ||
return data; | ||
}, | ||
}; | ||
}); | ||
function formatTimestamp(timestamp, period) { | ||
var d = new Date(timestamp); | ||
if (period == 'instant') { | ||
return $.plot.formatDate(d, '%b %d, %h:%M %p'); | ||
} else if (period == 'daily') { | ||
return $.plot.formatDate(d, '%b %d'); | ||
} else if (period == 'weekly') { | ||
// A bit more complicated than the above: the timestamp is in the | ||
// middle of the week, so we have to bracket the date. This is | ||
// something of a fudge here, but it works well enough. | ||
var start = new Date(d.getTime() - 3 * 24 * 60 * 60 * 1000); | ||
var end = new Date(d.getTime() + 3 * 24 * 60 * 60 * 1000); | ||
return ( | ||
$.plot.formatDate(start, '%b %d') + | ||
' - ' + | ||
$.plot.formatDate(end, '%b %d') | ||
); | ||
} | ||
} | ||
|
||
function convertSecondsToMilliseconds(data) { | ||
for (var i = 0; i < data.length; i++) { | ||
data[i][0] = data[i][0] * 1000; | ||
} | ||
return data; | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be simpler to not have
dashboard_javascript
(and arguably it's already dashboard JS as we're in dashboard templates). Then when more JS is needed, use the super...