Skip to content
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

Fix extrapolation NaN #653

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 27 additions & 18 deletions src/datasource/sql-series/toTimeSeries.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {each, isArray} from "lodash";
import {_toFieldType, convertTimezonedDateToUTC} from "./sql_series";
import {FieldType} from "@grafana/data";

import { each, isArray } from 'lodash';
import { _toFieldType, convertTimezonedDateToUTC } from './sql_series';
import { FieldType } from '@grafana/data';

const _formatValue = (value: any) => {
if (value === null) {
Expand All @@ -18,45 +17,56 @@ const _formatValue = (value: any) => {
} else {
return numeric;
}
}
};

const extrapolateDataPoints = (datapoints: any, self) => {
if (datapoints.length < 10 || (!self.tillNow && datapoints[0][0] !== 0)) {
return datapoints;
}

// Duration between first/last samples and boundary of range.
let durationToStart = datapoints[0][1] / 1000 - self.from,
durationToEnd = self.to - datapoints[datapoints.length - 1][1] / 1000;
const durationToStart = datapoints[0][1] / 1000 - self.from;
const durationToEnd = self.to - datapoints[datapoints.length - 1][1] / 1000;

// If the first/last samples are close to the boundaries of the range,
// extrapolate the result.
let sampledInterval = (datapoints[datapoints.length - 1][1] - datapoints[0][1]) / 1000,
averageDurationBetweenSamples = sampledInterval / (datapoints.length - 1);
const sampledInterval = (datapoints[datapoints.length - 1][1] - datapoints[0][1]) / 1000;
const averageDurationBetweenSamples = sampledInterval / (datapoints.length - 1);

let diff;
// close to left border and value is 0 because of runningDifference function
if (durationToStart < averageDurationBetweenSamples && datapoints[0][0] === 0) {
diff = ((datapoints[1][0] - datapoints[2][0]) / datapoints[1][0]) * 0.1;
diff %= 1;

if (isNaN(diff)) {
diff = 0;
}
datapoints[0][0] = datapoints[1][0] * (1 + diff);

const newDatapointValue = datapoints[1][0] * (1 + diff);
if (!isNaN(newDatapointValue)) {
datapoints[0][0] = newDatapointValue;
}
}

if (durationToEnd < averageDurationBetweenSamples) {
let l = datapoints.length;
diff = ((datapoints[l - 2][0] - datapoints[l - 3][0]) / datapoints[l - 2][0]) * 0.1;
diff %= 1;

if (isNaN(diff)) {
diff = 0;
}
datapoints[l - 1][0] = datapoints[l - 2][0] * (1 + diff);

const newDatapointValue = datapoints[l - 2][0] * (1 + diff);

if (!isNaN(newDatapointValue)) {
datapoints[l - 1][0] = newDatapointValue;
}
}

return datapoints;
}
};

const _pushDatapoint = (metrics: any, timestamp: number, key: string, value: number) => {
if (!metrics[key]) {
Expand All @@ -73,10 +83,9 @@ const _pushDatapoint = (metrics: any, timestamp: number, key: string, value: num
}

metrics[key].push([_formatValue(value), timestamp]);
}

};

export const toTimeSeries = (extrapolate = true, self): any => {
export const toTimeSeries = (extrapolate = true, self): any => {
let timeSeries: any[] = [];
if (self.series.length === 0) {
return timeSeries;
Expand All @@ -85,7 +94,7 @@ export const toTimeSeries = (extrapolate = true, self): any => {
let metrics: { [key: string]: any[] } = {};
// timeCol have to be the first column always
let timeCol = self.meta[0];
let timeColType = _toFieldType(timeCol.type || '')
let timeColType = _toFieldType(timeCol.type || '');
let lastTimeStamp = self.series[0][timeCol.name];
let keyColumns = self.keys.filter((name: string) => {
return name !== timeCol.name;
Expand Down Expand Up @@ -135,7 +144,7 @@ export const toTimeSeries = (extrapolate = true, self): any => {
key = metricKey;
}
if (timeColType?.fieldType === FieldType.time) {
t = convertTimezonedDateToUTC(t, timeColType.timezone)
t = convertTimezonedDateToUTC(t, timeColType.timezone);
}

if (isArray(val)) {
Expand All @@ -158,4 +167,4 @@ export const toTimeSeries = (extrapolate = true, self): any => {
});

return timeSeries;
}
};
Loading