Skip to content

Commit

Permalink
make log datetime easier
Browse files Browse the repository at this point in the history
  • Loading branch information
tillsteinbach committed Mar 5, 2022
1 parent f6db65b commit 2e5337f
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions grafana/dashboards/vwsfriend/VWsFriend/overview.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"editable": true,
"fiscalYearStartMonth": 0,
"graphTooltip": 1,
"iteration": 1646384370679,
"iteration": 1646511156133,
"links": [
{
"asDropdown": false,
Expand Down Expand Up @@ -1182,7 +1182,7 @@
"html": "<div>\n <div id=\"warningsDiv\" style=\"display: none;\">\n <h2>Warnings:</h2>\n <ul id=\"warningsList\">\n <ul>\n </div>\n\n <div id=\"newsDiv\" style=\"display: none;\">\n <h2>News:</h2>\n </div>\n\n <div id=\"logDiv\" style=\"display: none;\">\n <h2>Recent Events:</h2>\n <ul id=\"logsList\" style=\"list-style-type:none\">\n <ul>\n </div>\n</div>",
"onInit": "",
"onInitOnResize": false,
"onRender": "const warningsDiv = htmlNode.getElementById('warningsDiv');\nconst warningsList = htmlNode.getElementById('warningsList');\nwarningsDiv.style.display = 'none'\nwhile (warningsList.firstChild) {\n warningsList.firstChild.remove()\n}\nconst logsDiv = htmlNode.getElementById('logDiv');\nconst logsList = htmlNode.getElementById('logsList');\nlogsDiv.style.display = 'none'\nwhile (logsList.firstChild) {\n logsList.firstChild.remove()\n}\n\nfunction addWarning(html) {\n warningsDiv.style.display = ''\n warning = document.createElement(\"li\")\n warning.style.color = 'yellow'\n var template = document.createElement('template');\n html = html.trim();\n template.innerHTML = html;\n warning.append(template.content);\n warningsList.appendChild(warning)\n}\n\nfunction addLog(html) {\n logsDiv.style.display = ''\n log = document.createElement(\"li\")\n var template = document.createElement('template');\n html = html.trim();\n template.innerHTML = html;\n log.append(template.content);\n logsList.appendChild(log)\n}\n\nfunction getMetric(seriesName, metricName) {\n const filteredSeries = data.series.filter((series) => series.refId == seriesName);\n if(filteredSeries.length > 0 && filteredSeries[0].fields != null\n && filteredSeries[0].fields.length > 0){\n const filteredFields = filteredSeries[0].fields.filter((fields) => fields.name == metricName)\n if(filteredFields.length > 0){\n return filteredFields[0].values.get(0)\n }\n }\n return null\n}\n\nconst vwsfriend_url = getTemplateSrv().replace(`$vwsfriend_url`);\nconst vin = getTemplateSrv().replace(`$VIN`);\nif(vwsfriend_url == null || vwsfriend_url== ''){\n addWarning(\"You have to set the URL for the VWsFriend user interface correctly: Go to the user interface on port 4000 and select \\\"Database\\\" > \\”Connection Settings\\\"\")\n}\n\nconst carType = getMetric('vehicle_settings', 'carType')\nconst nickname = getMetric('vehicle_settings', 'nickname')\nconst primaryCapacity = getMetric('vehicle_settings', 'primary_capacity')\nconst primaryCapacityTotal = getMetric('vehicle_settings', 'primary_capacity_total')\nconst secondaryCapacity = getMetric('vehicle_settings', 'secondary_capacity')\nconst secondaryCapacityTotal = getMetric('vehicle_settings', 'secondary_capacity_total')\n\nif(carType == null){\n addWarning(\"The engine type of \"+nickname+\" could not be detected yet. This should happen soon when receiving data.\")\n}\nelse{\n switch (carType) {\n case 'HYBRID':\n if(primaryCapacity == null || primaryCapacityTotal == null){\n addWarning(\"Some panels need the tank size of \"+nickname+\" to be set. Please set the size here: <a href=\\\"\"+vwsfriend_url+\"/settings/vehicle/database/\"+vin+\"\\\" target=\\”_blank\\”>Vehicle Parameters</a>\")\n }\n if(secondaryCapacity == null || secondaryCapacityTotal == null){\n addWarning(\"Some panels need the battery capacity of \"+nickname+\" to be set. Please set the size here: <a href=\\\"\"+vwsfriend_url+\"/settings/vehicle/database/\"+vin+\"\\\" target=\\”_blank\\”>Vehicle Parameters</a>\")\n }\n break;\n default:\n if(primaryCapacity == null || primaryCapacityTotal == null){\n addWarning(\"Some panels need the battery capacity of \"+nickname+\" to be set. Please set the size here: <a href=\\\"\"+vwsfriend_url+\"/settings/vehicle/database/\"+vin+\"\\\" target=\\”_blank\\”>Vehicle Parameters</a>\")\n }\n break;\n }\n}\nconst hasError = getMetric('error', 'has_error')\nif(hasError){\n addWarning(\"There were more then two errors in the last 30 minutes when fetching data from the servers.<br/>It seems that there are technical issues.\")\n}\n\n//Log\nconst filteredSeries = data.series.filter((series) => series.refId == 'log');\n if(filteredSeries.length > 0 && filteredSeries[0].fields != null\n && filteredSeries[0].fields.length > 0){\n const filteredFields = filteredSeries[0].fields.filter((fields) => (fields.name == 'logtime'|| fields.name == 'timestring' || fields.name == 'log'))\n timeZone = htmlGraphics.props.timeZone;\n if(filteredFields.length == 3){\n time = filteredFields[0];\n timestring = filteredFields[1];\n logstring = filteredFields[2];\n var options = { dateStyle: 'short' };\n if(timeZone!='browser'){\n options.timeZone = timeZone;\n }\n var timeOptions = { timeStyle: 'short' };\n if(timeZone!='browser'){\n timeOptions.timeZone = timeZone;\n }\n var beforedate = new Intl.DateTimeFormat('default', options).format(new Date());\n for(let i = 0; i < time.values.length; i++) {\n var date = new Intl.DateTimeFormat('default', options).format(new Date(timestring.values.get(i)));\n if(beforedate != date){\n addLog('<b>'+date+'</b>');\n }\n beforedate = date;\n var timestr = new Intl.DateTimeFormat('default', timeOptions).format(new Date(timestring.values.get(i)));\n addLog(timestr+\": \"+logstring.values.get(i));\n }\n }\n }",
"onRender": "const warningsDiv = htmlNode.getElementById('warningsDiv');\nconst warningsList = htmlNode.getElementById('warningsList');\nwarningsDiv.style.display = 'none'\nwhile (warningsList.firstChild) {\n warningsList.firstChild.remove()\n}\nconst logsDiv = htmlNode.getElementById('logDiv');\nconst logsList = htmlNode.getElementById('logsList');\nlogsDiv.style.display = 'none'\nwhile (logsList.firstChild) {\n logsList.firstChild.remove()\n}\n\nfunction addWarning(html) {\n warningsDiv.style.display = ''\n warning = document.createElement(\"li\")\n warning.style.color = 'yellow'\n var template = document.createElement('template');\n html = html.trim();\n template.innerHTML = html;\n warning.append(template.content);\n warningsList.appendChild(warning)\n}\n\nfunction addLog(html) {\n logsDiv.style.display = ''\n log = document.createElement(\"li\")\n var template = document.createElement('template');\n html = html.trim();\n template.innerHTML = html;\n log.append(template.content);\n logsList.appendChild(log)\n}\n\nfunction getMetric(seriesName, metricName) {\n const filteredSeries = data.series.filter((series) => series.refId == seriesName);\n if(filteredSeries.length > 0 && filteredSeries[0].fields != null\n && filteredSeries[0].fields.length > 0){\n const filteredFields = filteredSeries[0].fields.filter((fields) => fields.name == metricName)\n if(filteredFields.length > 0){\n return filteredFields[0].values.get(0)\n }\n }\n return null\n}\n\nconst vwsfriend_url = getTemplateSrv().replace(`$vwsfriend_url`);\nconst vin = getTemplateSrv().replace(`$VIN`);\nif(vwsfriend_url == null || vwsfriend_url== ''){\n addWarning(\"You have to set the URL for the VWsFriend user interface correctly: Go to the user interface on port 4000 and select \\\"Database\\\" > \\”Connection Settings\\\"\")\n}\n\nconst carType = getMetric('vehicle_settings', 'carType')\nconst nickname = getMetric('vehicle_settings', 'nickname')\nconst primaryCapacity = getMetric('vehicle_settings', 'primary_capacity')\nconst primaryCapacityTotal = getMetric('vehicle_settings', 'primary_capacity_total')\nconst secondaryCapacity = getMetric('vehicle_settings', 'secondary_capacity')\nconst secondaryCapacityTotal = getMetric('vehicle_settings', 'secondary_capacity_total')\n\nif(carType == null){\n addWarning(\"The engine type of \"+nickname+\" could not be detected yet. This should happen soon when receiving data.\")\n}\nelse{\n switch (carType) {\n case 'HYBRID':\n if(primaryCapacity == null || primaryCapacityTotal == null){\n addWarning(\"Some panels need the tank size of \"+nickname+\" to be set. Please set the size here: <a href=\\\"\"+vwsfriend_url+\"/settings/vehicle/database/\"+vin+\"\\\" target=\\”_blank\\”>Vehicle Parameters</a>\")\n }\n if(secondaryCapacity == null || secondaryCapacityTotal == null){\n addWarning(\"Some panels need the battery capacity of \"+nickname+\" to be set. Please set the size here: <a href=\\\"\"+vwsfriend_url+\"/settings/vehicle/database/\"+vin+\"\\\" target=\\”_blank\\”>Vehicle Parameters</a>\")\n }\n break;\n default:\n if(primaryCapacity == null || primaryCapacityTotal == null){\n addWarning(\"Some panels need the battery capacity of \"+nickname+\" to be set. Please set the size here: <a href=\\\"\"+vwsfriend_url+\"/settings/vehicle/database/\"+vin+\"\\\" target=\\”_blank\\”>Vehicle Parameters</a>\")\n }\n break;\n }\n}\nconst hasError = getMetric('error', 'has_error')\nif(hasError){\n addWarning(\"There were more then two errors in the last 30 minutes when fetching data from the servers.<br/>It seems that there are technical issues.\")\n}\n\n//Log\nconst filteredSeries = data.series.filter((series) => series.refId == 'log');\n if(filteredSeries.length > 0 && filteredSeries[0].fields != null\n && filteredSeries[0].fields.length > 0){\n const filteredFields = filteredSeries[0].fields.filter((fields) => (fields.name == 'logtime' || fields.name == 'log'))\n timeZone = htmlGraphics.props.timeZone;\n if(filteredFields.length == 2){\n time = filteredFields[0];\n logstring = filteredFields[1];\n var options = { dateStyle: 'short' };\n if(timeZone!='browser'){\n options.timeZone = timeZone;\n }\n var timeOptions = { timeStyle: 'short' };\n if(timeZone!='browser'){\n timeOptions.timeZone = timeZone;\n }\n var beforedate = new Intl.DateTimeFormat('default', options).format(new Date());\n for(let i = 0; i < time.values.length; i++) {\n var date = new Intl.DateTimeFormat('default', options).format(new Date(time.values.get(i)));\n if(beforedate != date){\n addLog('<b>'+date+'</b>');\n }\n beforedate = date;\n var timestr = new Intl.DateTimeFormat('default', timeOptions).format(new Date(time.values.get(i)));\n addLog(timestr+\": \"+logstring.values.get(i));\n }\n }\n }",
"overflow": "scroll",
"panelupdateOnMount": true,
"reduceOptions": {
Expand Down Expand Up @@ -1229,7 +1229,7 @@
"hide": false,
"metricColumn": "none",
"rawQuery": true,
"rawSql": "SELECT\n logtime,\n to_json(logtime at time zone 'UTC')#>>'{}' AS timestring,\n log FROM\n(\n (SELECT\n started AS logtime,\n 'charging started' AS log\n FROM charging_sessions\n WHERE started IS NOT NULL AND vehicle_vin = '${VIN}'\n ORDER BY started DESC\n LIMIT 3)\n UNION\n (SELECT\n connected AS logtime,\n 'charger connected' AS log\n FROM charging_sessions\n WHERE connected IS NOT NULL AND vehicle_vin = '${VIN}'\n ORDER BY connected DESC\n LIMIT 3)\n UNION\n (SELECT\n locked AS logtime,\n 'connector locked' AS log\n FROM charging_sessions\n WHERE locked IS NOT NULL AND vehicle_vin = '${VIN}'\n ORDER BY locked DESC\n LIMIT 3)\n UNION\n (SELECT\n ended AS logtime,\n 'charging ended' AS log\n FROM charging_sessions\n WHERE ended IS NOT NULL AND vehicle_vin = '${VIN}'\n ORDER BY ended DESC\n LIMIT 3)\n UNION\n (SELECT\n disconnected AS logtime,\n 'charger disconnected' AS log\n FROM charging_sessions\n WHERE disconnected IS NOT NULL AND vehicle_vin = '${VIN}'\n ORDER BY disconnected DESC\n LIMIT 3)\n UNION\n (SELECT\n unlocked AS logtime,\n 'connector unlocked' AS log\n FROM charging_sessions\n WHERE unlocked IS NOT NULL AND vehicle_vin = '${VIN}'\n ORDER BY unlocked DESC\n LIMIT 3)\n UNION\n (SELECT\n date AS logtime,\n 'car was refueled' AS log\n FROM refuel_sessions\n WHERE date IS NOT NULL AND vehicle_vin = '${VIN}'\n ORDER BY date DESC\n LIMIT 3)\n UNION\n (SELECT\n \"startDate\" AS logtime,\n 'trip started' AS log\n FROM trips\n WHERE \"startDate\" IS NOT NULL AND vehicle_vin = '${VIN}'\n ORDER BY \"startDate\" DESC\n LIMIT 10)\n UNION\n (SELECT\n \"endDate\" AS logtime,\n 'trip ended' AS log\n FROM trips\n WHERE \"endDate\" IS NOT NULL AND vehicle_vin = '${VIN}'\n ORDER BY \"endDate\" DESC\n LIMIT 10)\n UNION\n (SELECT\n \"carCapturedTimestamp\" AS logtime,\n CASE WHEN \"climatisationState\"= 'OFF' THEN 'climatisation stopped'\n WHEN \"climatisationState\"= 'HEATING' THEN 'climatisation heating'\n WHEN \"climatisationState\"= 'COOLING' THEN 'climatisation cooling'\n WHEN \"climatisationState\"= 'VENTILATION' THEN 'climatisation ventilating'\n ELSE 'unkown'\n END AS log\nFROM\n (SELECT\n \"carCapturedTimestamp\",\n \"climatisationState\",\n LAG(\"climatisationState\") OVER (ORDER BY \"carCapturedTimestamp\") AS \"beforeClimatisationState\",\n 'climatisation started' AS log\n FROM climatization\n WHERE \"carCapturedTimestamp\" IS NOT NULL AND \"climatisationState\" IS NOT NULL AND vehicle_vin = '${VIN}'\n ORDER BY \"carCapturedTimestamp\" DESC\n ) AS climatization\n WHERE \"climatisationState\" != \"beforeClimatisationState\"\n ORDER BY \"carCapturedTimestamp\" DESC\n LIMIT 10)\n) AS logs\nORDER BY 1 DESC\nLIMIT 20",
"rawSql": "SELECT\n logtime,\n log FROM\n(\n (SELECT\n started AS logtime,\n 'charging started' AS log\n FROM charging_sessions\n WHERE started IS NOT NULL AND vehicle_vin = '${VIN}'\n ORDER BY started DESC\n LIMIT 3)\n UNION\n (SELECT\n connected AS logtime,\n 'charger connected' AS log\n FROM charging_sessions\n WHERE connected IS NOT NULL AND vehicle_vin = '${VIN}'\n ORDER BY connected DESC\n LIMIT 3)\n UNION\n (SELECT\n locked AS logtime,\n 'connector locked' AS log\n FROM charging_sessions\n WHERE locked IS NOT NULL AND vehicle_vin = '${VIN}'\n ORDER BY locked DESC\n LIMIT 3)\n UNION\n (SELECT\n ended AS logtime,\n 'charging ended' AS log\n FROM charging_sessions\n WHERE ended IS NOT NULL AND vehicle_vin = '${VIN}'\n ORDER BY ended DESC\n LIMIT 3)\n UNION\n (SELECT\n disconnected AS logtime,\n 'charger disconnected' AS log\n FROM charging_sessions\n WHERE disconnected IS NOT NULL AND vehicle_vin = '${VIN}'\n ORDER BY disconnected DESC\n LIMIT 3)\n UNION\n (SELECT\n unlocked AS logtime,\n 'connector unlocked' AS log\n FROM charging_sessions\n WHERE unlocked IS NOT NULL AND vehicle_vin = '${VIN}'\n ORDER BY unlocked DESC\n LIMIT 3)\n UNION\n (SELECT\n date AS logtime,\n 'car was refueled' AS log\n FROM refuel_sessions\n WHERE date IS NOT NULL AND vehicle_vin = '${VIN}'\n ORDER BY date DESC\n LIMIT 3)\n UNION\n (SELECT\n \"startDate\" AS logtime,\n 'trip started' AS log\n FROM trips\n WHERE \"startDate\" IS NOT NULL AND vehicle_vin = '${VIN}'\n ORDER BY \"startDate\" DESC\n LIMIT 10)\n UNION\n (SELECT\n \"endDate\" AS logtime,\n 'trip ended' AS log\n FROM trips\n WHERE \"endDate\" IS NOT NULL AND vehicle_vin = '${VIN}'\n ORDER BY \"endDate\" DESC\n LIMIT 10)\n UNION\n (SELECT\n \"carCapturedTimestamp\" AS logtime,\n CASE WHEN \"climatisationState\"= 'OFF' THEN 'climatisation stopped'\n WHEN \"climatisationState\"= 'HEATING' THEN 'climatisation heating'\n WHEN \"climatisationState\"= 'COOLING' THEN 'climatisation cooling'\n WHEN \"climatisationState\"= 'VENTILATION' THEN 'climatisation ventilating'\n ELSE 'unkown'\n END AS log\nFROM\n (SELECT\n \"carCapturedTimestamp\",\n \"climatisationState\",\n LAG(\"climatisationState\") OVER (ORDER BY \"carCapturedTimestamp\") AS \"beforeClimatisationState\",\n 'climatisation started' AS log\n FROM climatization\n WHERE \"carCapturedTimestamp\" IS NOT NULL AND \"climatisationState\" IS NOT NULL AND vehicle_vin = '${VIN}'\n ORDER BY \"carCapturedTimestamp\" DESC\n ) AS climatization\n WHERE \"climatisationState\" != \"beforeClimatisationState\"\n ORDER BY \"carCapturedTimestamp\" DESC\n LIMIT 10)\n) AS logs\nORDER BY 1 DESC\nLIMIT 20",
"refId": "log",
"select": [
[
Expand Down

0 comments on commit 2e5337f

Please sign in to comment.