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

Commit 7b16bc0

Browse files
committed
Fixes for issue #777
- Resolution of histogram now depends on time range of search, making it more reasonable
1 parent 642d3bf commit 7b16bc0

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

app/controllers/SearchController.java

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
import org.graylog2.restclient.models.*;
3838
import org.graylog2.restclient.models.api.results.DateHistogramResult;
3939
import org.graylog2.restclient.models.api.results.SearchResult;
40+
import org.joda.time.DateTime;
41+
import org.joda.time.Minutes;
4042
import play.mvc.Result;
4143
import views.helpers.Permissions;
4244

@@ -107,17 +109,16 @@ public Result index(String q,
107109
savedSearch = null;
108110
}
109111

110-
// Histogram interval.
111-
if (interval == null || interval.isEmpty() || !SearchTools.isAllowedDateHistogramInterval(interval)) {
112-
interval = "minute";
113-
}
114-
115112
searchResult = search.search();
116113
if (searchResult.getError() != null) {
117114
return ok(views.html.search.queryerror.render(currentUser(), q, searchResult, savedSearch, fields, null));
118115
}
119116
searchResult.setAllFields(getAllFields());
120117

118+
// histogram resolution (strangely aka interval)
119+
if (interval == null || interval.isEmpty() || !SearchTools.isAllowedDateHistogramInterval(interval)) {
120+
interval = determineHistogramResolution(searchResult);
121+
}
121122
histogramResult = search.dateHistogram(interval);
122123
formattedHistogramResults = formatHistogramResults(histogramResult.getResults(), displayWidth);
123124
} catch (IOException e) {
@@ -134,6 +135,33 @@ public Result index(String q,
134135
}
135136
}
136137

138+
protected String determineHistogramResolution(final SearchResult searchResult) {
139+
final String interval;
140+
final int queryRangeInMinutes = Minutes.minutesBetween(searchResult.getFromDateTime(), searchResult.getToDateTime()).getMinutes();
141+
final int HOUR = 60;
142+
final int DAY = HOUR * 24;
143+
final int WEEK = DAY * 7;
144+
final int MONTH = HOUR * 24 * 30;
145+
final int YEAR = MONTH * 12;
146+
147+
if (queryRangeInMinutes < DAY / 2) {
148+
interval = "minute";
149+
} else if (queryRangeInMinutes < DAY * 2) {
150+
interval = "hour";
151+
} else if (queryRangeInMinutes < MONTH) {
152+
interval = "day";
153+
} else if (queryRangeInMinutes < MONTH * 6) {
154+
interval = "week";
155+
} else if (queryRangeInMinutes < YEAR * 2) {
156+
interval = "month";
157+
} else if (queryRangeInMinutes < YEAR * 10) {
158+
interval = "quarter";
159+
} else {
160+
interval = "year";
161+
}
162+
return interval;
163+
}
164+
137165
/**
138166
* [{ x: -1893456000, y: 92228531 }, { x: -1577923200, y: 106021568 }]
139167
*

app/controllers/StreamSearchController.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,16 @@ public Result index(String streamId,
8181
savedSearch = null;
8282
}
8383

84-
// Histogram interval.
85-
if (interval == null || interval.isEmpty() || !SearchTools.isAllowedDateHistogramInterval(interval)) {
86-
interval = "minute";
87-
}
88-
8984
searchResult = search.search();
9085
if (searchResult.getError() != null) {
9186
return ok(views.html.search.queryerror.render(currentUser(), q, searchResult, savedSearch, fields, stream));
9287
}
9388
searchResult.setAllFields(getAllFields());
9489

90+
// histogram resolution (strangely aka interval)
91+
if (interval == null || interval.isEmpty() || !SearchTools.isAllowedDateHistogramInterval(interval)) {
92+
interval = determineHistogramResolution(searchResult);
93+
}
9594
histogramResult = search.dateHistogram(interval);
9695
formattedHistogramResults = formatHistogramResults(histogramResult.getResults(), displayWidth);
9796
} catch (IOException e) {

0 commit comments

Comments
 (0)