Prechádzať zdrojové kódy

add first and last support

Sven Klemm 7 rokov pred
rodič
commit
26ea88252b

+ 9 - 4
public/app/plugins/datasource/postgres/postgres_query.ts

@@ -138,16 +138,21 @@ export default class PostgresQuery {
     let special = _.find(column, (g: any) => g.type === 'window');
     let special = _.find(column, (g: any) => g.type === 'window');
 
 
     if (aggregate) {
     if (aggregate) {
+      let func = aggregate.params[0];
       switch (aggregate.type) {
       switch (aggregate.type) {
         case 'aggregate':
         case 'aggregate':
-          if (special) {
-            query = aggregate.params[0] + '(' + query + ' ORDER BY ' + this.target.timeColumn + ')';
+          if (func === 'first' || func === 'last') {
+            query = func + '(' + query + ',' + this.target.timeColumn + ')';
           } else {
           } else {
-            query = aggregate.params[0] + '(' + query + ')';
+            if (special) {
+              query = func + '(' + query + ' ORDER BY ' + this.target.timeColumn + ')';
+            } else {
+              query = func + '(' + query + ')';
+            }
           }
           }
           break;
           break;
         case 'percentile':
         case 'percentile':
-          query = aggregate.params[0] + '(' + aggregate.params[1] + ') WITHIN GROUP (ORDER BY ' + query + ')';
+          query = func + '(' + aggregate.params[1] + ') WITHIN GROUP (ORDER BY ' + query + ')';
           break;
           break;
       }
       }
     }
     }

+ 43 - 32
public/app/plugins/datasource/postgres/query_ctrl.ts

@@ -98,38 +98,49 @@ export class PostgresQueryCtrl extends QueryCtrl {
   }
   }
 
 
   buildSelectMenu() {
   buildSelectMenu() {
-    this.selectMenu = [
-      {
-        text: 'Aggregate Functions',
-        value: 'aggregate',
-        submenu: [
-          { text: 'Average', value: 'avg' },
-          { text: 'Count', value: 'count' },
-          { text: 'Maximum', value: 'max' },
-          { text: 'Minimum', value: 'min' },
-          { text: 'Sum', value: 'sum' },
-          { text: 'Standard deviation', value: 'stddev' },
-          { text: 'Variance', value: 'variance' },
-        ],
-      },
-      {
-        text: 'Ordered-Set Aggregate Functions',
-        value: 'percentile',
-        submenu: [
-          { text: 'Percentile (continuous)', value: 'percentile_cont' },
-          { text: 'Percentile (discrete)', value: 'percentile_disc' },
-        ],
-      },
-      {
-        text: 'Window Functions',
-        value: 'window',
-        submenu: [
-          { text: 'Increase', value: 'increase' },
-          { text: 'Rate', value: 'rate' },
-          { text: 'Sum', value: 'sum' },
-        ],
-      },
-    ];
+    this.selectMenu = [];
+    let aggregates = {
+      text: 'Aggregate Functions',
+      value: 'aggregate',
+      submenu: [
+        { text: 'Average', value: 'avg' },
+        { text: 'Count', value: 'count' },
+        { text: 'Maximum', value: 'max' },
+        { text: 'Minimum', value: 'min' },
+        { text: 'Sum', value: 'sum' },
+        { text: 'Standard deviation', value: 'stddev' },
+        { text: 'Variance', value: 'variance' },
+      ],
+    };
+
+    // first and last are timescaledb specific
+    aggregates.submenu.push({ text: 'First', value: 'first' });
+    aggregates.submenu.push({ text: 'Last', value: 'last' });
+
+    this.selectMenu.push(aggregates);
+
+    // ordered set aggregates require postgres 9.4+
+    let aggregates2 = {
+      text: 'Ordered-Set Aggregate Functions',
+      value: 'percentile',
+      submenu: [
+        { text: 'Percentile (continuous)', value: 'percentile_cont' },
+        { text: 'Percentile (discrete)', value: 'percentile_disc' },
+      ],
+    };
+    this.selectMenu.push(aggregates2);
+
+    let windows = {
+      text: 'Window Functions',
+      value: 'window',
+      submenu: [
+        { text: 'Increase', value: 'increase' },
+        { text: 'Rate', value: 'rate' },
+        { text: 'Sum', value: 'sum' },
+      ],
+    };
+    this.selectMenu.push(windows);
+
     this.selectMenu.push({ text: 'Alias', value: 'alias' });
     this.selectMenu.push({ text: 'Alias', value: 'alias' });
     this.selectMenu.push({ text: 'Column', value: 'column' });
     this.selectMenu.push({ text: 'Column', value: 'column' });
   }
   }