Fix db connection query post processing
This commit is contained in:
@@ -92,7 +92,7 @@ func (ds *ZabbixDatasource) DBConnectionPostProcessingHandler(rw http.ResponseWr
|
|||||||
reqData.Query.TimeRange.From = time.Unix(reqData.TimeRange.From, 0)
|
reqData.Query.TimeRange.From = time.Unix(reqData.TimeRange.From, 0)
|
||||||
reqData.Query.TimeRange.To = time.Unix(reqData.TimeRange.To, 0)
|
reqData.Query.TimeRange.To = time.Unix(reqData.TimeRange.To, 0)
|
||||||
|
|
||||||
frames, err := dsInstance.applyDataProcessing(req.Context(), &reqData.Query, reqData.Series)
|
frames, err := dsInstance.applyDataProcessing(req.Context(), &reqData.Query, reqData.Series, true)
|
||||||
|
|
||||||
resultJson, err := json.Marshal(frames)
|
resultJson, err := json.Marshal(frames)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -102,10 +102,10 @@ func (ds *ZabbixDatasourceInstance) queryNumericDataForItems(ctx context.Context
|
|||||||
}
|
}
|
||||||
|
|
||||||
series := convertHistoryToTimeSeries(history, items)
|
series := convertHistoryToTimeSeries(history, items)
|
||||||
return ds.applyDataProcessing(ctx, query, series)
|
return ds.applyDataProcessing(ctx, query, series, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ds *ZabbixDatasourceInstance) applyDataProcessing(ctx context.Context, query *QueryModel, series []*timeseries.TimeSeriesData) ([]*data.Frame, error) {
|
func (ds *ZabbixDatasourceInstance) applyDataProcessing(ctx context.Context, query *QueryModel, series []*timeseries.TimeSeriesData, DBPostProcessing bool) ([]*data.Frame, error) {
|
||||||
consolidateBy := ds.getConsolidateBy(query)
|
consolidateBy := ds.getConsolidateBy(query)
|
||||||
useTrend := ds.isUseTrend(query.TimeRange)
|
useTrend := ds.isUseTrend(query.TimeRange)
|
||||||
|
|
||||||
@@ -117,7 +117,7 @@ func (ds *ZabbixDatasourceInstance) applyDataProcessing(ctx context.Context, que
|
|||||||
// Align time series data if possible
|
// Align time series data if possible
|
||||||
disableDataAlignment := query.Options.DisableDataAlignment || ds.Settings.DisableDataAlignment || query.QueryType == MODE_ITSERVICE
|
disableDataAlignment := query.Options.DisableDataAlignment || ds.Settings.DisableDataAlignment || query.QueryType == MODE_ITSERVICE
|
||||||
if !disableDataAlignment {
|
if !disableDataAlignment {
|
||||||
if useTrend {
|
if useTrend && !DBPostProcessing {
|
||||||
for _, s := range series {
|
for _, s := range series {
|
||||||
// Trend data is already aligned (by 1 hour interval), but null values should be added
|
// Trend data is already aligned (by 1 hour interval), but null values should be added
|
||||||
s.TS = s.TS.FillTrendWithNulls()
|
s.TS = s.TS.FillTrendWithNulls()
|
||||||
|
|||||||
@@ -154,7 +154,7 @@ export function seriesToDataFrame(timeseries, target: ZabbixMetricsQuery, valueM
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Converts DataResponse to the format which backend works with (for data processing)
|
// Converts DataResponse to the format which backend works with (for data processing)
|
||||||
export function dataResponseToTimeSeries(response: DataFrameJSON[], items) {
|
export function dataResponseToTimeSeries(response: DataFrameJSON[], items, request) {
|
||||||
const series = [];
|
const series = [];
|
||||||
if (response.length === 0) {
|
if (response.length === 0) {
|
||||||
return [];
|
return [];
|
||||||
@@ -181,7 +181,7 @@ export function dataResponseToTimeSeries(response: DataFrameJSON[], items) {
|
|||||||
const item = _.find(items, { 'itemid': itemid });
|
const item = _.find(items, { 'itemid': itemid });
|
||||||
|
|
||||||
// Convert interval to nanoseconds in order to unmarshall it on the backend to time.Duration
|
// Convert interval to nanoseconds in order to unmarshall it on the backend to time.Duration
|
||||||
let interval = utils.parseItemInterval(item.delay) * 1000000;
|
let interval = request.intervalMs * 1000000;
|
||||||
if (interval === 0) {
|
if (interval === 0) {
|
||||||
interval = null;
|
interval = null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,29 +3,31 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
function historyQuery(itemids, table, timeFrom, timeTill, intervalSec, aggFunction) {
|
function historyQuery(itemids, table, timeFrom, timeTill, intervalSec, aggFunction) {
|
||||||
|
const time_expression = `clock DIV ${intervalSec} * ${intervalSec}`;
|
||||||
return `
|
return `
|
||||||
SELECT CAST(itemid AS CHAR) AS metric, MIN(clock) AS time_sec, ${aggFunction}(value) AS value
|
SELECT CAST(itemid AS CHAR) AS metric, ${time_expression} AS time_sec, ${aggFunction}(value) AS value
|
||||||
FROM ${table}
|
FROM ${table}
|
||||||
WHERE itemid IN (${itemids})
|
WHERE itemid IN (${itemids})
|
||||||
AND clock
|
AND clock
|
||||||
> ${timeFrom}
|
> ${timeFrom}
|
||||||
AND clock
|
AND clock
|
||||||
< ${timeTill}
|
< ${timeTill}
|
||||||
GROUP BY (clock-${timeFrom}) DIV ${intervalSec}, metric
|
GROUP BY ${time_expression}, metric
|
||||||
ORDER BY time_sec ASC
|
ORDER BY time_sec ASC
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function trendsQuery(itemids, table, timeFrom, timeTill, intervalSec, aggFunction, valueColumn) {
|
function trendsQuery(itemids, table, timeFrom, timeTill, intervalSec, aggFunction, valueColumn) {
|
||||||
|
const time_expression = `clock DIV ${intervalSec} * ${intervalSec}`;
|
||||||
return `
|
return `
|
||||||
SELECT CAST(itemid AS CHAR) AS metric, MIN(clock) AS time_sec, ${aggFunction}(${valueColumn}) AS value
|
SELECT CAST(itemid AS CHAR) AS metric, ${time_expression} AS time_sec, ${aggFunction}(${valueColumn}) AS value
|
||||||
FROM ${table}
|
FROM ${table}
|
||||||
WHERE itemid IN (${itemids})
|
WHERE itemid IN (${itemids})
|
||||||
AND clock
|
AND clock
|
||||||
> ${timeFrom}
|
> ${timeFrom}
|
||||||
AND clock
|
AND clock
|
||||||
< ${timeTill}
|
< ${timeTill}
|
||||||
GROUP BY (clock-${timeFrom}) DIV ${intervalSec}, metric
|
GROUP BY ${time_expression}, metric
|
||||||
ORDER BY time_sec ASC
|
ORDER BY time_sec ASC
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -456,24 +456,24 @@ export class Zabbix implements ZabbixConnector {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
getHistoryTS(items, timeRange, options) {
|
getHistoryTS(items, timeRange, request) {
|
||||||
const [timeFrom, timeTo] = timeRange;
|
const [timeFrom, timeTo] = timeRange;
|
||||||
if (this.enableDirectDBConnection) {
|
if (this.enableDirectDBConnection) {
|
||||||
return this.getHistoryDB(items, timeFrom, timeTo, options)
|
return this.getHistoryDB(items, timeFrom, timeTo, request)
|
||||||
.then(history => responseHandler.dataResponseToTimeSeries(history, items));
|
.then(history => responseHandler.dataResponseToTimeSeries(history, items, request));
|
||||||
} else {
|
} else {
|
||||||
return this.zabbixAPI.getHistory(items, timeFrom, timeTo)
|
return this.zabbixAPI.getHistory(items, timeFrom, timeTo)
|
||||||
.then(history => responseHandler.handleHistory(history, items));
|
.then(history => responseHandler.handleHistory(history, items));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getTrends(items, timeRange, options) {
|
getTrends(items, timeRange, request) {
|
||||||
const [timeFrom, timeTo] = timeRange;
|
const [timeFrom, timeTo] = timeRange;
|
||||||
if (this.enableDirectDBConnection) {
|
if (this.enableDirectDBConnection) {
|
||||||
return this.getTrendsDB(items, timeFrom, timeTo, options)
|
return this.getTrendsDB(items, timeFrom, timeTo, request)
|
||||||
.then(history => responseHandler.dataResponseToTimeSeries(history, items));
|
.then(history => responseHandler.dataResponseToTimeSeries(history, items, request));
|
||||||
} else {
|
} else {
|
||||||
const valueType = options.consolidateBy || options.valueType;
|
const valueType = request.consolidateBy || request.valueType;
|
||||||
return this.zabbixAPI.getTrend(items, timeFrom, timeTo)
|
return this.zabbixAPI.getTrend(items, timeFrom, timeTo)
|
||||||
.then(history => responseHandler.handleTrends(history, items, valueType))
|
.then(history => responseHandler.handleTrends(history, items, valueType))
|
||||||
.then(responseHandler.sortTimeseries); // Sort trend data, issue #202
|
.then(responseHandler.sortTimeseries); // Sort trend data, issue #202
|
||||||
|
|||||||
Reference in New Issue
Block a user