From f608340c35d1191e1c1346cf01375010ac1bb303 Mon Sep 17 00:00:00 2001 From: Sven Klemm Date: Wed, 31 Oct 2018 19:26:18 +0100 Subject: [PATCH] Add delta window function to postgres query builder Unlike the increase function delta doesn't check for resets and can go negative. This is similar to the prometheus delta function. --- public/app/plugins/datasource/postgres/postgres_query.ts | 5 +++++ public/app/plugins/datasource/postgres/query_ctrl.ts | 1 + public/app/plugins/datasource/postgres/sql_part.ts | 2 +- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/public/app/plugins/datasource/postgres/postgres_query.ts b/public/app/plugins/datasource/postgres/postgres_query.ts index 89518bb15e0..e315f1fecd2 100644 --- a/public/app/plugins/datasource/postgres/postgres_query.ts +++ b/public/app/plugins/datasource/postgres/postgres_query.ts @@ -184,6 +184,11 @@ export default class PostgresQuery { switch (windows.type) { case 'window': switch (windows.params[0]) { + case 'delta': + curr = query; + prev = 'lag(' + curr + ') OVER (' + over + ')'; + query = curr + ' - ' + prev; + break; case 'increase': curr = query; prev = 'lag(' + curr + ') OVER (' + over + ')'; diff --git a/public/app/plugins/datasource/postgres/query_ctrl.ts b/public/app/plugins/datasource/postgres/query_ctrl.ts index 9343a260a9e..aa66a1594cb 100644 --- a/public/app/plugins/datasource/postgres/query_ctrl.ts +++ b/public/app/plugins/datasource/postgres/query_ctrl.ts @@ -158,6 +158,7 @@ export class PostgresQueryCtrl extends QueryCtrl { text: 'Window Functions', value: 'window', submenu: [ + { text: 'Delta', value: 'delta' }, { text: 'Increase', value: 'increase' }, { text: 'Rate', value: 'rate' }, { text: 'Sum', value: 'sum' }, diff --git a/public/app/plugins/datasource/postgres/sql_part.ts b/public/app/plugins/datasource/postgres/sql_part.ts index 695060f6366..f8caaf0e39e 100644 --- a/public/app/plugins/datasource/postgres/sql_part.ts +++ b/public/app/plugins/datasource/postgres/sql_part.ts @@ -107,7 +107,7 @@ register({ { name: 'function', type: 'string', - options: ['increase', 'rate', 'sum'], + options: ['delta', 'increase', 'rate', 'sum'], }, ], defaultParams: ['increase'],