Skip to content

Commit

Permalink
perf-improvements-drive-stats
Browse files Browse the repository at this point in the history
  • Loading branch information
swiffer committed Oct 13, 2024
1 parent ad34131 commit 0582bb6
Showing 1 changed file with 143 additions and 91 deletions.
234 changes: 143 additions & 91 deletions grafana/dashboards/drive-stats.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,12 @@
"type": "grafana-postgresql-datasource",
"uid": "TeslaMate"
},
"editorMode": "code",
"format": "time_series",
"group": [],
"metricColumn": "none",
"rawQuery": true,
"rawSql": "SELECT\n date_trunc('day', start_date) as \"time\",\n count(*)\nFROM drives\nWHERE $__timeFilter(start_date) AND car_id = $car_id\nGROUP BY 1\nORDER BY 1;",
"rawSql": "WITH since as (\n\tSELECT min(start_date) at time zone 'UTC' as date FROM drives\n\tWHERE car_id = $car_id\n\tGROUP BY car_id\n),\n\nactual AS (\n\tSELECT\n\t\tdate_trunc('day', start_date at time zone 'UTC') AS date,\n\t\tcount(*) AS distance\n\tFROM drives\n\tWHERE car_id = $car_id\n\tGROUP BY 1\n),\n\nbase_line AS (\n\tSELECT date from generate_series(date_trunc('day', (select date from since)), NOW(), '1 day'::interval) date\n)\n\nSELECT\n base_line.date as time,\n\tconvert_km(COALESCE(actual.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 base_line.date\n",
"refId": "A",
"select": [
[
Expand All @@ -141,6 +142,23 @@
}
]
],
"sql": {
"columns": [
{
"parameters": [],
"type": "function"
}
],
"groupBy": [
{
"property": {
"type": "string"
},
"type": "groupBy"
}
],
"limit": 50
},
"table": "addresses",
"timeColumn": "inserted_at",
"timeColumnType": "timestamp",
Expand Down Expand Up @@ -244,7 +262,7 @@
"group": [],
"metricColumn": "none",
"rawQuery": true,
"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",
"rawSql": "WITH since as (\n\tSELECT min(start_date) at time zone 'UTC' as date FROM drives\n\tWHERE car_id = $car_id\n\tGROUP BY car_id\n),\n\nactual AS (\n\tSELECT\n\t\tdate_trunc('day', start_date at time zone 'UTC') AS date,\n\t\tsum(distance) AS distance\n\tFROM drives\n\tWHERE car_id = $car_id\n\tGROUP BY 1\n),\n\nbase_line AS (\n\tSELECT date from generate_series(date_trunc('day', (select date from since)), NOW(), '1 day'::interval) date\n)\n\nSELECT\n base_line.date as time,\n\tconvert_km(COALESCE(actual.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 base_line.date\n",
"refId": "A",
"select": [
[
Expand Down Expand Up @@ -346,11 +364,12 @@
"type": "grafana-postgresql-datasource",
"uid": "TeslaMate"
},
"editorMode": "code",
"format": "time_series",
"group": [],
"metricColumn": "none",
"rawQuery": true,
"rawSql": "SELECT\n $__time(start_date),\n NULLIF(GREATEST(start_[[preferred_range]]_range_km - end_[[preferred_range]]_range_km, 0), 0) * car.efficiency AS energy\nFROM drives\nJOIN cars car ON car.id = car_id\nWHERE $__timeFilter(start_date) AND car_id = $car_id\nORDER BY 1",
"rawSql": "WITH since as (\n\tSELECT min(start_date) at time zone 'UTC' as date FROM drives\n\tWHERE car_id = $car_id\n\tGROUP BY car_id\n),\n\nactual AS (\n\tSELECT\n\t\tdate_trunc('day', start_date at time zone 'UTC') AS date,\n\t\tsum(NULLIF(GREATEST(start_[[preferred_range]]_range_km - end_[[preferred_range]]_range_km, 0), 0) * cars.efficiency) AS energy\n\tFROM drives\n INNER JOIN cars on drives.car_id = cars.id\n\tWHERE car_id = $car_id\n\tGROUP BY 1\n),\n\nbase_line AS (\n\tSELECT date from generate_series(date_trunc('day', (select date from since)), NOW(), '1 day'::interval) date\n)\n\nSELECT\n base_line.date as time,\n\tcoalesce(energy, 0)\nFROM base_line\nLEFT JOIN actual ON actual.date = base_line.date\nWHERE $__timeFilter(base_line.date)\norder by base_line.date\n",
"refId": "A",
"select": [
[
Expand All @@ -362,6 +381,23 @@
}
]
],
"sql": {
"columns": [
{
"parameters": [],
"type": "function"
}
],
"groupBy": [
{
"property": {
"type": "string"
},
"type": "groupBy"
}
],
"limit": 50
},
"table": "addresses",
"timeColumn": "inserted_at",
"timeColumnType": "timestamp",
Expand Down Expand Up @@ -453,9 +489,7 @@
"orientation": "horizontal",
"percentChangeColorMode": "standard",
"reduceOptions": {
"calcs": [
"mean"
],
"calcs": [],
"fields": "",
"values": false
},
Expand Down Expand Up @@ -498,7 +532,7 @@
]
}
],
"title": "Average distance of a drive",
"title": "Median distance of a drive",
"type": "stat"
},
{
Expand Down Expand Up @@ -567,9 +601,7 @@
"orientation": "horizontal",
"percentChangeColorMode": "standard",
"reduceOptions": {
"calcs": [
"mean"
],
"calcs": [],
"fields": "",
"values": false
},
Expand All @@ -584,35 +616,31 @@
"type": "grafana-postgresql-datasource",
"uid": "TeslaMate"
},
"editorMode": "code",
"format": "table",
"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', start_date)::date AS date,\n\t\tsum(distance) AS distance\n\tFROM drives\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),\ncombined as (\n SELECT base_line.date, COALESCE(actual.distance, 0) as distance\n FROM base_line\n LEFT JOIN actual ON actual.date = base_line.date\n WHERE $__timeFilter(base_line.date)\n)\nSELECT convert_km((percentile_disc(0.5) WITHIN GROUP (ORDER BY distance))::numeric, '$length_unit') AS \"distance_$length_unit\"\nFROM combined;",
"rawSql": "WITH since as (\r\n\tSELECT min(start_date) at time zone 'UTC' as date FROM drives\r\n\tWHERE car_id = $car_id\r\n\tGROUP BY car_id\r\n),\r\n\r\nactual AS (\r\n\tSELECT\r\n\t\tdate_trunc('day', start_date at time zone 'UTC') AS date,\r\n\t\tsum(distance) AS distance\r\n\tFROM drives\r\n\tWHERE car_id = $car_id\r\n\tGROUP BY 1\r\n),\r\n\r\nbase_line AS (\r\n\tSELECT date from generate_series(date_trunc('day', (select date from since)), NOW(), '1 day'::interval) date\r\n)\r\n\r\nSELECT\r\n convert_km(percentile_disc(0.5) WITHIN GROUP (ORDER BY COALESCE(actual.distance, 0))::numeric, '$length_unit') as \"distance_$length_unit\"\r\nFROM base_line\r\nLEFT JOIN actual ON actual.date = base_line.date\r\nWHERE $__timeFilter(base_line.date)\r\n",
"refId": "A",
"select": [
[
"sql": {
"columns": [
{
"params": [
"efficiency"
],
"type": "column"
"parameters": [],
"type": "function"
}
]
],
"table": "cars",
"timeColumn": "inserted_at",
"timeColumnType": "timestamp",
"where": [
{
"name": "$__timeFilter",
"params": [],
"type": "macro"
}
]
],
"groupBy": [
{
"property": {
"type": "string"
},
"type": "groupBy"
}
],
"limit": 50
}
}
],
"title": "Average distance driven per day",
"title": "Median distance driven per day",
"type": "stat"
},
{
Expand Down Expand Up @@ -656,9 +684,7 @@
"orientation": "horizontal",
"percentChangeColorMode": "standard",
"reduceOptions": {
"calcs": [
"mean"
],
"calcs": [],
"fields": "",
"values": false
},
Expand All @@ -673,35 +699,31 @@
"type": "grafana-postgresql-datasource",
"uid": "TeslaMate"
},
"editorMode": "code",
"format": "table",
"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', start_date)::date AS date,\n\t\tsum(NULLIF(GREATEST(start_[[preferred_range]]_range_km - end_[[preferred_range]]_range_km, 0), 0) * car.efficiency) AS energy\n\tFROM drives\n\tJOIN cars car ON car.id = car_id\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),\ncombined as (\n SELECT base_line.date, COALESCE(actual.energy, 0) as energy\n FROM base_line\n LEFT JOIN actual ON actual.date = base_line.date\n WHERE $__timeFilter(base_line.date)\n)\nSELECT percentile_disc(0.5) WITHIN GROUP (ORDER BY energy) AS energy\nFROM combined;",
"rawSql": "WITH since as (\r\n\tSELECT min(start_date) at time zone 'UTC' as date FROM drives\r\n\tWHERE car_id = $car_id\r\n\tGROUP BY car_id\r\n),\r\n\r\nactual AS (\r\n\tSELECT\r\n\t\tdate_trunc('day', start_date at time zone 'UTC') AS date,\r\n\t\tsum(NULLIF(GREATEST(start_[[preferred_range]]_range_km - end_[[preferred_range]]_range_km, 0), 0) * cars.efficiency) AS energy\r\n\tFROM drives\r\n INNER JOIN cars on drives.car_id = cars.id\r\n\tWHERE car_id = $car_id\r\n\tGROUP BY 1\r\n),\r\n\r\nbase_line AS (\r\n\tSELECT date from generate_series(date_trunc('day', (select date from since)), NOW(), '1 day'::interval) date\r\n)\r\n\r\nSELECT\r\n\tpercentile_disc(0.5) WITHIN GROUP (ORDER BY coalesce(energy, 0))\r\nFROM base_line\r\nLEFT JOIN actual ON actual.date = base_line.date\r\nWHERE $__timeFilter(base_line.date)",
"refId": "A",
"select": [
[
"sql": {
"columns": [
{
"params": [
"latitude"
],
"type": "column"
"parameters": [],
"type": "function"
}
]
],
"table": "addresses",
"timeColumn": "inserted_at",
"timeColumnType": "timestamp",
"where": [
{
"name": "$__timeFilter",
"params": [],
"type": "macro"
}
]
],
"groupBy": [
{
"property": {
"type": "string"
},
"type": "groupBy"
}
],
"limit": 50
}
}
],
"title": "Average kWh used per day",
"title": "Median kWh used per day",
"type": "stat"
},
{
Expand Down Expand Up @@ -787,11 +809,12 @@
"type": "grafana-postgresql-datasource",
"uid": "TeslaMate"
},
"editorMode": "code",
"format": "table",
"group": [],
"metricColumn": "none",
"rawQuery": true,
"rawSql": "WITH first_position AS (\n\tSELECT date, odometer\n\tFROM positions\n\tWHERE car_id = $car_id AND $__timeFilter(date)\n\tORDER BY date ASC\n\tLIMIT 1\n),\nlast_position AS (\n\tSELECT date, odometer\n\tFROM positions\n\tWHERE car_id = $car_id AND $__timeFilter(date)\n\tORDER BY date DESC\n\tLIMIT 1\n)\nSELECT\n\tconvert_km((((SELECT odometer FROM last_position) - (SELECT odometer\tFROM first_position)) /\n\tEXTRACT(days FROM (SELECT date FROM last_position) - (SELECT date\tFROM first_position)) * \n\t(365/12))::numeric, '$length_unit') AS \"mileage_$length_unit\";",
"rawSql": "select convert_km(((max(end_km) - min(start_km)) / extract(days from (max(end_date) - min(start_date))) * (365/12))::numeric, '$length_unit') as \"mileage_$length_unit\" from drives where car_id = $car_id and $__timeFilter(start_date) and $__timeFilter(end_date) group by car_id",
"refId": "A",
"select": [
[
Expand All @@ -803,6 +826,23 @@
}
]
],
"sql": {
"columns": [
{
"parameters": [],
"type": "function"
}
],
"groupBy": [
{
"property": {
"type": "string"
},
"type": "groupBy"
}
],
"limit": 50
},
"table": "addresses",
"timeColumn": "inserted_at",
"timeColumnType": "timestamp",
Expand All @@ -820,8 +860,9 @@
},
{
"datasource": {
"type": "grafana-postgresql-datasource",
"uid": "TeslaMate"
"default": false,
"type": "datasource",
"uid": "-- Dashboard --"
},
"fieldConfig": {
"defaults": {
Expand Down Expand Up @@ -898,38 +939,48 @@
"targets": [
{
"datasource": {
"type": "grafana-postgresql-datasource",
"uid": "TeslaMate"
"type": "datasource",
"uid": "-- Dashboard --"
},
"format": "table",
"group": [],
"metricColumn": "none",
"rawQuery": true,
"rawSql": "WITH first_position AS (\n\tSELECT date, odometer\n\tFROM positions\n\tWHERE car_id = $car_id AND $__timeFilter(date)\n\tORDER BY date ASC\n\tLIMIT 1\n),\nlast_position AS (\n\tSELECT date, odometer\n\tFROM positions\n\tWHERE car_id = $car_id AND $__timeFilter(date)\n\tORDER BY date DESC\n\tLIMIT 1\n)\nSELECT\n\tconvert_km(((lp.odometer - fp.odometer) /\n\tEXTRACT(days FROM lp.date - fp.date) * \n\t365)::numeric, '$length_unit') AS \"mileage_$length_unit\" from first_position as fp, last_position as lp;",
"refId": "A",
"select": [
[
{
"params": [
"latitude"
],
"type": "column"
}
]
],
"table": "addresses",
"timeColumn": "inserted_at",
"timeColumnType": "timestamp",
"where": [
{
"name": "$__timeFilter",
"params": [],
"type": "macro"
}
]
"panelId": 32,
"refId": "A"
}
],
"title": "Extrapolated annual mileage",
"transformations": [
{
"id": "calculateField",
"options": {
"alias": "mileage_km",
"binary": {
"left": "mileage_km",
"operator": "*",
"right": "12"
},
"mode": "binary",
"reduce": {
"reducer": "sum"
},
"replaceFields": true
}
},
{
"id": "calculateField",
"options": {
"alias": "mileage_mi",
"binary": {
"left": "mileage_mi",
"operator": "*",
"right": "12"
},
"mode": "binary",
"reduce": {
"reducer": "sum"
},
"replaceFields": true
}
}
],
"type": "stat"
},
{
Expand Down Expand Up @@ -1169,13 +1220,13 @@
"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",
"definition": "WITH splits AS (\n SELECT unnest(string_to_array('$exclude', ', ')) AS part\n),\nsplit_strings AS (\n\tSELECT 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(case when part is null then '''''' else '''%' || part || '%''' end), ', ') AS formatted_string\n\tFROM split_strings\n)\nSELECT '(ARRAY[' || formatted_string || '])' FROM exclude_string",
"hide": 2,
"includeAll": false,
"multi": false,
"name": "exclude_formatted_string",
"options": [],
"query": "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",
"query": "WITH splits AS (\n SELECT unnest(string_to_array('$exclude', ', ')) AS part\n),\nsplit_strings AS (\n\tSELECT 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(case when part is null then '''''' else '''%' || part || '%''' end), ', ') AS formatted_string\n\tFROM split_strings\n)\nSELECT '(ARRAY[' || formatted_string || '])' FROM exclude_string",
"refresh": 1,
"regex": "",
"skipUrlSync": false,
Expand All @@ -1188,6 +1239,7 @@
"from": "now-1y",
"to": "now"
},
"timeRangeUpdatedDuringEditOrView": false,
"timepicker": {
"hidden": false,
"refresh_intervals": [
Expand Down Expand Up @@ -1217,6 +1269,6 @@
"timezone": "",
"title": "Drive Stats",
"uid": "_7WkNSyWk",
"version": 4,
"version": 8,
"weekStart": ""
}

0 comments on commit 0582bb6

Please sign in to comment.