From 64ea0de3792b7b13a5d3b340342d2a98992f2f3f Mon Sep 17 00:00:00 2001 From: Juan Carlos Heredia Date: Tue, 20 Aug 2024 22:07:02 +0200 Subject: [PATCH] Improve performance in queries --- grafana/dashboards/drive-stats.json | 55 ++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 13 deletions(-) diff --git a/grafana/dashboards/drive-stats.json b/grafana/dashboards/drive-stats.json index 6063342936..3de2b4bc38 100644 --- a/grafana/dashboards/drive-stats.json +++ b/grafana/dashboards/drive-stats.json @@ -11,7 +11,7 @@ "type": "grafana", "id": "grafana", "name": "Grafana", - "version": "11.0.1" + "version": "11.1.0" }, { "type": "datasource", @@ -30,11 +30,11 @@ "list": [ { "builtIn": 1, - "definition": "TeslaMate|U2FsdGVkX1/cEWK+8cz7pjEKXtzJnDN7b21ZDXt1MGneFGPWTLqOPtxKmu02mJPLzi/f29I+NBHd3vi0FB8R4Xn0+GtobWDgk6VAVSBTdSNniOKO8i2WPlhRaOsl2+hG7gnZ7wrf1Th2nxR7f1uYCrbwOek0IzkfLzrkjh7gkr6inT6bbDuJqrmogZajLxmAMrQ6V+/vHxBRGiwjJhgiEeq3hM1q2h04JKkNiZ8RHbsF5Cd/xd8Q9u0JVrZzIrtnhM/SFlaApU7RtRMu8CSj1llTX7WEOj6VDZAMSf+XUAanWdk725kEPN9MNu89o2zEq5P3E3cju8IbbBdPzXLV3oVuzD6/tMnxFToIIV1E/BrpF7s2RtNa8+KJJ1PF8xgs6m+/KTD2hy+WsP0636AgObRAmYg7+qotGrgNvpNPdE0EgrB7WHYlV7R/1q66bcq6tCe51X1Un70k+zo+K6AK0o4B1H6IyMlEVuRH/Fz8QVl9aYu2ztd08RbuKJlYVKpkH+pxVETAO9MclYQ90tzE6TfwDZrQZzsAlMenr4s1ZB1OlFXjLjVjnddnUilzO76cqv4yI2THQEuyQ47nuVQ4gUbx02K59vMQhns3C01JOAYokOaSXe66Y7QYdMlk09Lf|aes-256-cbc", "datasource": { "type": "datasource", "uid": "grafana" }, + "definition": "TeslaMate|U2FsdGVkX1/cEWK+8cz7pjEKXtzJnDN7b21ZDXt1MGneFGPWTLqOPtxKmu02mJPLzi/f29I+NBHd3vi0FB8R4Xn0+GtobWDgk6VAVSBTdSNniOKO8i2WPlhRaOsl2+hG7gnZ7wrf1Th2nxR7f1uYCrbwOek0IzkfLzrkjh7gkr6inT6bbDuJqrmogZajLxmAMrQ6V+/vHxBRGiwjJhgiEeq3hM1q2h04JKkNiZ8RHbsF5Cd/xd8Q9u0JVrZzIrtnhM/SFlaApU7RtRMu8CSj1llTX7WEOj6VDZAMSf+XUAanWdk725kEPN9MNu89o2zEq5P3E3cju8IbbBdPzXLV3oVuzD6/tMnxFToIIV1E/BrpF7s2RtNa8+KJJ1PF8xgs6m+/KTD2hy+WsP0636AgObRAmYg7+qotGrgNvpNPdE0EgrB7WHYlV7R/1q66bcq6tCe51X1Un70k+zo+K6AK0o4B1H6IyMlEVuRH/Fz8QVl9aYu2ztd08RbuKJlYVKpkH+pxVETAO9MclYQ90tzE6TfwDZrQZzsAlMenr4s1ZB1OlFXjLjVjnddnUilzO76cqv4yI2THQEuyQ47nuVQ4gUbx02K59vMQhns3C01JOAYokOaSXe66Y7QYdMlk09Lf|aes-256-cbc", "enable": true, "hide": true, "iconColor": "rgba(0, 211, 255, 1)", @@ -106,6 +106,7 @@ "graphMode": "area", "justifyMode": "auto", "orientation": "horizontal", + "percentChangeColorMode": "standard", "reduceOptions": { "calcs": [ "sum" @@ -117,7 +118,7 @@ "textMode": "value", "wideLayout": true }, - "pluginVersion": "11.0.1", + "pluginVersion": "11.1.0", "targets": [ { "datasource": { @@ -219,6 +220,7 @@ "graphMode": "area", "justifyMode": "auto", "orientation": "horizontal", + "percentChangeColorMode": "standard", "reduceOptions": { "calcs": [ "sum" @@ -230,18 +232,19 @@ "textMode": "value", "wideLayout": true }, - "pluginVersion": "11.0.1", + "pluginVersion": "11.1.0", "targets": [ { "datasource": { "type": "grafana-postgresql-datasource", "uid": "TeslaMate" }, + "editorMode": "code", "format": "time_series", "group": [], "metricColumn": "none", "rawQuery": true, - "rawSql": "WITH since as (\n\tSELECT date FROM positions\n\tWHERE car_id = $car_id\n\tORDER BY date ASC\n\tLIMIT 1\n),\nactual AS (\n\tSELECT\n\t\tdate_trunc('day', date)::date AS date,\n\t\tmax(odometer) - min(odometer) AS distance\n\tFROM positions\n\tWHERE car_id = $car_id\n\tGROUP BY 1\n),\nbase_line AS (\n\tSELECT date_trunc('day', dd)::date AS date\n FROM generate_series((select date from since) , now(), '1 day'::interval) dd\n)\nSELECT \n $__time(base_line.date), \n convert_km(COALESCE(distance, 0)::numeric, '$length_unit') as \"distance_$length_unit\"\nFROM base_line\nLEFT JOIN actual ON actual.date = base_line.date\nWHERE $__timeFilter(base_line.date)\nORDER BY 1;", + "rawSql": "SELECT \n DATE(start_date) AS \"time\",\n SUM(convert_km((end_km - start_km)::numeric, '$length_unit')) AS \"distance_$length_unit\"\nFROM drives \nWHERE car_id = $car_id\n\tAND $__timeFilter(start_date)\nGROUP BY DATE(start_date)\nORDER BY DATE(start_date);\n\n", "refId": "A", "select": [ [ @@ -253,6 +256,23 @@ } ] ], + "sql": { + "columns": [ + { + "parameters": [], + "type": "function" + } + ], + "groupBy": [ + { + "property": { + "type": "string" + }, + "type": "groupBy" + } + ], + "limit": 50 + }, "table": "addresses", "timeColumn": "inserted_at", "timeColumnType": "timestamp", @@ -307,6 +327,7 @@ "graphMode": "area", "justifyMode": "auto", "orientation": "horizontal", + "percentChangeColorMode": "standard", "reduceOptions": { "calcs": [ "sum" @@ -318,7 +339,7 @@ "textMode": "value", "wideLayout": true }, - "pluginVersion": "11.0.1", + "pluginVersion": "11.1.0", "targets": [ { "datasource": { @@ -430,6 +451,7 @@ "graphMode": "none", "justifyMode": "auto", "orientation": "horizontal", + "percentChangeColorMode": "standard", "reduceOptions": { "calcs": [ "mean" @@ -441,7 +463,7 @@ "textMode": "value", "wideLayout": true }, - "pluginVersion": "11.0.1", + "pluginVersion": "11.1.0", "targets": [ { "datasource": { @@ -543,6 +565,7 @@ "graphMode": "none", "justifyMode": "auto", "orientation": "horizontal", + "percentChangeColorMode": "standard", "reduceOptions": { "calcs": [ "mean" @@ -554,7 +577,7 @@ "textMode": "value", "wideLayout": true }, - "pluginVersion": "11.0.1", + "pluginVersion": "11.1.0", "targets": [ { "datasource": { @@ -631,6 +654,7 @@ "graphMode": "none", "justifyMode": "auto", "orientation": "horizontal", + "percentChangeColorMode": "standard", "reduceOptions": { "calcs": [ "mean" @@ -642,7 +666,7 @@ "textMode": "value", "wideLayout": true }, - "pluginVersion": "11.0.1", + "pluginVersion": "11.1.0", "targets": [ { "datasource": { @@ -744,6 +768,7 @@ "graphMode": "none", "justifyMode": "auto", "orientation": "horizontal", + "percentChangeColorMode": "standard", "reduceOptions": { "calcs": [ "mean" @@ -755,7 +780,7 @@ "textMode": "value", "wideLayout": true }, - "pluginVersion": "11.0.1", + "pluginVersion": "11.1.0", "targets": [ { "datasource": { @@ -857,6 +882,7 @@ "graphMode": "none", "justifyMode": "auto", "orientation": "horizontal", + "percentChangeColorMode": "standard", "reduceOptions": { "calcs": [ "mean" @@ -868,7 +894,7 @@ "textMode": "value", "wideLayout": true }, - "pluginVersion": "11.0.1", + "pluginVersion": "11.1.0", "targets": [ { "datasource": { @@ -961,7 +987,7 @@ "sizing": "auto", "valueMode": "color" }, - "pluginVersion": "11.0.1", + "pluginVersion": "11.1.0", "targets": [ { "datasource": { @@ -1139,6 +1165,10 @@ }, { "current": {}, + "datasource": { + "type": "grafana-postgresql-datasource", + "uid": "TeslaMate" + }, "definition": "WITH splits AS (\n SELECT unnest(string_to_array('$exclude', ', ')) AS part\n),\nsplit_strings AS (\n\tSELECT COALESCE(part, ' ') AS part\n\tFROM (VALUES (NULL)) AS v(dummy)\n\tLEFT JOIN splits ON TRUE\n),\nexclude_string AS (\n\tSELECT array_to_string(array_agg('''%' || part || '%'''), ', ') AS formatted_string\n\tFROM split_strings\n)\nSELECT '(ARRAY[' || formatted_string || '])' FROM exclude_string", "hide": 2, "includeAll": false, @@ -1158,7 +1188,6 @@ "from": "now-1y", "to": "now" }, - "timeRangeUpdatedDuringEditOrView": false, "timepicker": { "hidden": false, "refresh_intervals": [