From a24a6624e339611ea5c43e6b19ae5a4b17ea1f71 Mon Sep 17 00:00:00 2001 From: Sven Klemm Date: Thu, 12 Jul 2018 13:15:19 +0200 Subject: [PATCH] handle counter overflow and resets in rate --- public/app/plugins/datasource/postgres/postgres_query.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/public/app/plugins/datasource/postgres/postgres_query.ts b/public/app/plugins/datasource/postgres/postgres_query.ts index 9e32736b405..b371af3f1b1 100644 --- a/public/app/plugins/datasource/postgres/postgres_query.ts +++ b/public/app/plugins/datasource/postgres/postgres_query.ts @@ -240,7 +240,7 @@ export default class PostgresQuery { let special = _.find(column, (g: any) => g.type === 'special'); if (special) { let over = ''; - if (target.metricColumn) { + if (target.metricColumn !== 'None') { over = 'PARTITION BY ' + target.metricColumn; } switch (special.params[0]) { @@ -249,7 +249,9 @@ export default class PostgresQuery { break; case 'rate': let timeColumn = target.timeColumn; - query = '(' + query + ' - lag(' + query + ') OVER (' + over + '))'; + let curr = query; + let prev = 'lag(' + curr + ') OVER (' + over + ')'; + query = '(CASE WHEN ' + curr + ' >= ' + prev + ' THEN ' + curr + ' - ' + prev + ' ELSE ' + curr + ' END)'; query += '/extract(epoch from ' + timeColumn + ' - lag(' + timeColumn + ') OVER (' + over + '))'; break; }