Skip to content
This repository was archived by the owner on Jan 8, 2019. It is now read-only.

Commit 38f6b01

Browse files
committed
Merge pull request #810 from Graylog2/graphs-tz-fix
Show rickshaw graph dates in configured/user timezone
2 parents 25013f4 + 4c2a136 commit 38f6b01

File tree

7 files changed

+41
-8
lines changed

7 files changed

+41
-8
lines changed

app/lib/DateTools.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,22 @@ public static String inUserTimeZoneShortFormat(DateTime esDate) {
8585
public static DateTime nowInUTC() {
8686
return DateTime.now(DateTimeZone.UTC);
8787
}
88+
89+
public static int getUserTimeZoneOffset() {
90+
DateTimeZone tz = globalTimezone;
91+
92+
final User currentUser = UserService.current();
93+
94+
if (currentUser != null && currentUser.getTimeZone() != null) {
95+
tz = currentUser.getTimeZone();
96+
}
97+
98+
int offsetMillis = tz.toTimeZone().getRawOffset() / 1000;
99+
100+
if (tz.toTimeZone().useDaylightTime()) {
101+
offsetMillis += 3600;
102+
}
103+
104+
return (offsetMillis / 60) * -1;
105+
}
88106
}

app/views/partials/top.scala.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
@import lib.security.RestPermissions._
22
@import views.helpers.Permissions._
33
@import lib.Configuration
4+
@import lib.DateTools
45

56
<script type="text/javascript">
67
// needs to be global!
78
gl2AppPathPrefix = "@Configuration.getApplicationContext";
9+
gl2UserTimeZoneOffset = @DateTools.getUserTimeZoneOffset;
810
</script>
911

1012
<div id="top" class="navbar navbar-fixed-top">

app/views/search/results.scala.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,15 @@ <h3>
163163
new Rickshaw.Graph.Axis.Time({
164164
graph: resultGraph,
165165
ticksTreatment: "glow",
166-
timeFixture: new Rickshaw.Fixtures.Graylog2Time() // Cares about correct TZ handling.
166+
timeFixture: new Rickshaw.Fixtures.Graylog2Time(gl2UserTimeZoneOffset) // Cares about correct TZ handling.
167167
});
168168
}
169169

170170
new Rickshaw.Graph.HoverDetail({
171171
graph: resultGraph,
172172
formatter: function(series, x, y) {
173-
var date = '<span class="date">' + new Date(x * 1000 ).toString() + '</span>';
173+
var dateMoment = moment(new Date(x * 1000 )).zone(gl2UserTimeZoneOffset);
174+
var date = '<span class="date">' + dateMoment.format('ddd MMM DD YYYY HH:mm:ss ZZ') + '</span>';
174175
var swatch = '<span class="detail_swatch"></span>';
175176
var content = parseInt(y) + ' messages<br>' + date;
176177
return content;

public/javascripts/Rickshaw.Fixtures.Graylog2Time.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Rickshaw.namespace('Rickshaw.Fixtures.Graylog2Time');
22

3-
Rickshaw.Fixtures.Graylog2Time = function() {
3+
Rickshaw.Fixtures.Graylog2Time = function(tzOffset) {
44

55
var self = this;
66

@@ -67,11 +67,23 @@ Rickshaw.Fixtures.Graylog2Time = function() {
6767
};
6868

6969
this.formatDate = function(d) {
70-
return d3.time.format('%b %e')(d);
70+
var dateMoment = moment(d);
71+
72+
if (tzOffset) {
73+
dateMoment = dateMoment.zone(tzOffset);
74+
}
75+
76+
return dateMoment.format('MMM DD');
7177
};
7278

7379
this.formatTime = function(d) {
74-
return d.toString().match(/(\d+:\d+):/)[1];
80+
var dateMoment = moment(d);
81+
82+
if (tzOffset) {
83+
dateMoment = dateMoment.zone(tzOffset);
84+
}
85+
86+
return dateMoment.format('HH:mm');
7587
};
7688

7789
this.ceil = function(time, unit) {

public/javascripts/analyzers/analyzers/fieldcharts.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ $(document).ready(function() {
168168
new Rickshaw.Graph.Axis.Time({
169169
graph: graph,
170170
ticksTreatment: "glow",
171-
timeFixture: new Rickshaw.Fixtures.Graylog2Time() // Cares about correct TZ handling.
171+
timeFixture: new Rickshaw.Fixtures.Graylog2Time(gl2UserTimeZoneOffset) // Cares about correct TZ handling.
172172
});
173173

174174
new Rickshaw.Graph.HoverDetail({

public/javascripts/dashboards/widgets/field_chart.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function updateWidget_field_chart(widget, data) {
8686
new Rickshaw.Graph.Axis.Time({
8787
graph: graph,
8888
ticksTreatment: "glow",
89-
timeFixture: new Rickshaw.Fixtures.Graylog2Time() // Cares about correct TZ handling.
89+
timeFixture: new Rickshaw.Fixtures.Graylog2Time(gl2UserTimeZoneOffset) // Cares about correct TZ handling.
9090
});
9191

9292
new Rickshaw.Graph.HoverDetail({

public/javascripts/dashboards/widgets/search_result_chart.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ function updateWidget_search_result_chart(widget, data) {
6161
new Rickshaw.Graph.Axis.Time({
6262
graph: graph,
6363
ticksTreatment: "glow",
64-
timeFixture: new Rickshaw.Fixtures.Graylog2Time() // Cares about correct TZ handling.
64+
timeFixture: new Rickshaw.Fixtures.Graylog2Time(gl2UserTimeZoneOffset) // Cares about correct TZ handling.
6565
});
6666
}
6767

0 commit comments

Comments
 (0)