From dd82e9f14dfe2274a02847051b40143f8f54327e Mon Sep 17 00:00:00 2001
From: Emanuele Laface <emanuele.laface@esss.se>
Date: Fri, 24 Jul 2020 14:28:07 +0200
Subject: [PATCH] New TS2 Screen with Archiver data

---
 PythonServer/screens/ts2_screens.py | 53 ++++++++++++++-----
 WebSites/pvs/dashboard.json         |  2 +-
 WebSites/ts2_screens/dashboard.json | 82 ++++++++++++++++-------------
 3 files changed, 85 insertions(+), 52 deletions(-)

diff --git a/PythonServer/screens/ts2_screens.py b/PythonServer/screens/ts2_screens.py
index e77abbd..5971637 100755
--- a/PythonServer/screens/ts2_screens.py
+++ b/PythonServer/screens/ts2_screens.py
@@ -2,6 +2,9 @@ import epics
 import json
 import time
 import sys
+import pandas
+import requests
+import datetime
 from threading import Thread
 
 
@@ -16,7 +19,7 @@ class ts2_screensScreen(Thread):
             "TS2-010CDL:Cryo-TE-82313:MeasValue",
             "TS2-010CDL:Cryo-TE-82360:MeasValue",
             "TS2-010CRM:Cryo-TE-068:MeasValue",
-            "TS2-010CRM:Cryo-TE-063:MeasValue",
+#            "TS2-010CRM:Cryo-TE-063:MeasValue",
             "TS2-010CRM:Cryo-TE-069:MeasValue",
             "TS2-010CRM:Cryo-TE-064:MeasValue",
             "TS2-010CDL:Cryo-TE-82365:MeasValue",
@@ -28,23 +31,46 @@ class ts2_screensScreen(Thread):
         for pv in pvs:
             epics_dict[pv] = epics.PV(pv, auto_monitor=True)
 
+        counter = 0
         while not self.stop_signal.isSet():
             try:
-                json_dict = {}
+                if counter%7200 == 0:
+                    json_dict = {}
+                    for pv in epics_dict:
+                        start_date=(datetime.datetime.now()-datetime.timedelta(days=14)).isoformat()+'Z'
+                        end_date=datetime.datetime.now().isoformat()+'Z'
+                        url = 'http://archiver-01.tn.esss.lu.se:17668/retrieval/data/getData.json?pv=count({})&from={}&to={}'.format(pv,start_date,end_date)
+                        r = requests.get(url).json()[0]['data']
+                        entries = 0
+                        for i in r:
+                            entries+=i['val']
+                        url = 'http://archiver-01.tn.esss.lu.se:17668/retrieval/data/getData.json?pv=nth_{}({})&from={}&to={}'.format(int(entries//1400),pv,start_date,end_date)
+                        r = requests.get(url).json()[0]['data']
+                        raw_data = pandas.DataFrame.from_dict(r)
+                        final_data={}
+                        final_data['2w'] = raw_data.groupby(raw_data.index//(raw_data.shape[0]/100)).mean()
+                        tmp = raw_data[raw_data['secs']>(datetime.datetime.now()-datetime.timedelta(days=7)).timestamp()]
+                        final_data['1w'] = tmp.groupby(tmp.index//(tmp.shape[0]/100)).mean()
+                        tmp = raw_data[raw_data['secs']>(datetime.datetime.now()-datetime.timedelta(days=2)).timestamp()]
+                        final_data['2d'] = tmp.groupby(tmp.index//(tmp.shape[0]/100)).mean()
+                        tmp = raw_data[raw_data['secs']>(datetime.datetime.now()-datetime.timedelta(days=1)).timestamp()]
+                        final_data['1d'] = tmp.groupby(tmp.index//(tmp.shape[0]/100)).mean()
+                        final_data['live'] = raw_data[-100:]
+                        json_dict[pv] = {}
+                        json_dict[pv]['2w']=list(zip((final_data['2w']['secs']*1000).astype('int')+(final_data['2w']['nanos']/1e6).astype('int'), round(final_data['2w']['val'],3)))
+                        json_dict[pv]['1w']=list(zip((final_data['1w']['secs']*1000).astype('int')+(final_data['1w']['nanos']/1e6).astype('int'), round(final_data['1w']['val'],3)))
+                        json_dict[pv]['2d']=list(zip((final_data['2d']['secs']*1000).astype('int')+(final_data['2d']['nanos']/1e6).astype('int'), round(final_data['2d']['val'],3)))
+                        json_dict[pv]['1d']=list(zip((final_data['1d']['secs']*1000).astype('int')+(final_data['1d']['nanos']/1e6).astype('int'), round(final_data['1d']['val'],3)))
+                        json_dict[pv]['live']=list(zip((final_data['live']['secs']*1000).astype('int')+(final_data['live']['nanos']/1e6).astype('int'), round(final_data['live']['val'],3)))
+                    counter = 0
+
                 for pv in epics_dict:
-                    json_dict[pv] = {}
                     if epics_dict[pv].connected:
-                        json_dict[pv]["units"] = epics_dict[pv].units
-                        json_dict[pv]["timestamp"] = epics_dict[pv].timestamp
                         value = epics_dict[pv].value
-                        if type(value) == float:
-                            json_dict[pv]["value"] = round(value, 3)
-                        else:
-                            json_dict[pv]["value"] = value
-                    else:
-                        json_dict[pv]["units"] = "n.c."
-                        json_dict[pv]["timestamp"] = ""
-                        json_dict[pv]["value"] = ""
+                        timestamp = int(epics_dict[pv].timestamp*1000)
+                        if type(value) == float and timestamp != json_dict[pv]['live'][-1][0]:
+                            json_dict[pv]['live']=json_dict[pv]['live'][1:]
+                            json_dict[pv]['live'].append((timestamp, round(value, 3)))
 
                 tmp_json = json.dumps(json_dict)
                 tmp_json = tmp_json.replace("NaN", "0")
@@ -55,6 +81,7 @@ class ts2_screensScreen(Thread):
                 print(sys.exc_info())
                 time.sleep(1)
             time.sleep(0.5)
+            counter+=1
 
         for pv in epics_dict:
             epics_dict[pv].clear_auto_monitor()
diff --git a/WebSites/pvs/dashboard.json b/WebSites/pvs/dashboard.json
index 9cd0476..91d9d36 100644
--- a/WebSites/pvs/dashboard.json
+++ b/WebSites/pvs/dashboard.json
@@ -202,4 +202,4 @@
 		}
 	],
 	"columns": 4
-}
\ No newline at end of file
+}
diff --git a/WebSites/ts2_screens/dashboard.json b/WebSites/ts2_screens/dashboard.json
index 7eb7f03..d413e28 100644
--- a/WebSites/ts2_screens/dashboard.json
+++ b/WebSites/ts2_screens/dashboard.json
@@ -4,6 +4,26 @@
 	"allow_edit": false,
 	"plugins": [],
 	"panes": [
+		{
+			"title": "Time Selector",
+			"width": 1,
+			"row": {
+				"3": 7
+			},
+			"col": {
+				"3": 1
+			},
+			"col_width": 4,
+			"widgets": [
+				{
+					"type": "html",
+					"settings": {
+						"html": "<script>\n    function setDatasource(event) {\n\t\tfreeboard.showLoadingIndicator(true);\n\t};\n</script>\n<div style=\"position:relative;height:100%;display:flex;flex-direction:column;justify-content:center;align-items:center;\">\n        <select onchange=\"setDatasource()\" id=\"time-series\">\n            <option value=\"2w\">Two Weeks</option>\n            <option value=\"1w\">One Week</option>\n            <option value=\"2d\">Two Days</option>\n            <option value=\"1d\">One Day</option>\n            <option value=\"live\">Live</option>\n        </select>\n</div>",
+						"height": 1
+					}
+				}
+			]
+		},
 		{
 			"width": 1,
 			"row": {
@@ -35,7 +55,7 @@
 			"row": {
 				"1": 7,
 				"2": 7,
-				"3": 7,
+				"3": 11,
 				"4": 7,
 				"5": 39,
 				"6": 39,
@@ -53,51 +73,37 @@
 			"col_width": 4,
 			"widgets": [
 				{
-					"type": "highcharts-timeseries",
+					"type": "flot_extended_plugin",
 					"settings": {
-						"timeframe": 300,
-						"blocks": 4,
-						"chartType": "spline",
-						"xaxis": "{\"title\":{\"text\" : \"Time\"}, \"type\": \"datetime\", \"floor\":0}",
-						"yaxis": "{\"title\":{\"text\" : \"Values\"}, \"minorTickInterval\":\"auto\", \"floor\":0}",
-						"series1": "datasources[\"TS2\"][\"CrS-TICP:Cryo-TE-31491:Val\"][\"value\"]",
-						"series1label": "CrS-TICP:Cryo-TE-31491",
-						"series2": "datasources[\"TS2\"][\"TS2-010CDL:Cryo-TE-82313:MeasValue\"][\"value\"]",
-						"series2label": "TS2-010CDL:Cryo-TE-82313",
-						"series3": "datasources[\"TS2\"][\"TS2-010CDL:Cryo-TE-82360:MeasValue\"][\"value\"]",
-						"series3label": "TS2-010CDL:Cryo-TE-82360"
+						"plot_type": "line",
+						"legend": true,
+						"height": 4,
+						"value": "var series = document.getElementById(\"time-series\").value;\ndata = [\n {label: 'CrS-TICP:Cryo-TE-31491', data: datasources[\"TS2\"][\"CrS-TICP:Cryo-TE-31491:Val\"][series]},\n {label: 'TS2-010CDL:Cryo-TE-82313', data: datasources[\"TS2\"][\"TS2-010CDL:Cryo-TE-82313:MeasValue\"][series]},\n {label: 'TS2-010CDL:Cryo-TE-82360', data: datasources[\"TS2\"][\"TS2-010CDL:Cryo-TE-82360:MeasValue\"][series]}\n];\nfreeboard.showLoadingIndicator(false);\nreturn data;",
+						"x_timestamp": true,
+						"zoom": ""
 					}
 				},
 				{
-					"type": "highcharts-timeseries",
+					"type": "flot_extended_plugin",
 					"settings": {
-						"timeframe": 300,
-						"blocks": 4,
-						"chartType": "spline",
-						"xaxis": "{\"title\":{\"text\" : \"Time\"}, \"type\": \"datetime\", \"floor\":0}",
-						"yaxis": "{\"title\":{\"text\" : \"Values\"}, \"minorTickInterval\":\"auto\", \"floor\":0}",
-						"series1": "datasources[\"TS2\"][\"TS2-010CRM:Cryo-TE-068:MeasValue\"][\"value\"]",
-						"series1label": "TS2-010CRM:Cryo-TE-068",
-						"series2": "datasources[\"TS2\"][\"TS2-010CRM:Cryo-TE-064:MeasValue\"][\"value\"]",
-						"series2label": "TS2-010CRM:Cryo-TE-064",
-						"series3": "datasources[\"TS2\"][\"TS2-010CRM:Cryo-TE-069:MeasValue\"][\"value\"]",
-						"series3label": "TS2-010CRM:Cryo-TE-069"
+						"plot_type": "line",
+						"legend": true,
+						"height": 4,
+						"value": "var series = document.getElementById(\"time-series\").value;\ndata = [\n {label: 'TS2-010CRM:Cryo-TE-068', data: datasources[\"TS2\"][\"TS2-010CRM:Cryo-TE-068:MeasValue\"][series]},\n {label: 'TS2-010CRM:Cryo-TE-064', data: datasources[\"TS2\"][\"TS2-010CRM:Cryo-TE-064:MeasValue\"][series]},\n {label: 'TS2-010CRM:Cryo-TE-069', data: datasources[\"TS2\"][\"TS2-010CRM:Cryo-TE-069:MeasValue\"][series]}\n];\nfreeboard.showLoadingIndicator(false);\nreturn data;\n",
+						"x_timestamp": true,
+						"zoom": ""
 					}
 				},
 				{
-					"type": "highcharts-timeseries",
+					"type": "flot_extended_plugin",
 					"settings": {
-						"timeframe": 300,
-						"blocks": 4,
-						"chartType": "spline",
-						"xaxis": "{\"title\":{\"text\" : \"Time\"}, \"type\": \"datetime\", \"floor\":0}",
-						"yaxis": "{\"title\":{\"text\" : \"Values\"}, \"minorTickInterval\":\"auto\", \"floor\":0}",
-						"series1": "datasources[\"TS2\"][\"TS2-010CRM:Cryo-TE-064:MeasValue\"][\"value\"]",
-						"series1label": "TS2-010CRM:Cryo-TE-064",
-						"series2": "datasources[\"TS2\"][\"TS2-010CDL:Cryo-TE-82314:MeasValue\"][\"value\"]",
-						"series2label": "TS2-010CDL:Cryo-TE-82314",
-						"series3": "datasources[\"TS2\"][\"CrS-TICP:Cryo-TE-31492:Val\"][\"value\"]",
-						"series3label": "CrS-TICP:Cryo-TE-31492:Val"
+						"plot_type": "line",
+						"legend": true,
+						"height": 4,
+						"value": "var series = document.getElementById(\"time-series\").value;\ndata = [\n {label: 'TS2-010CRM:Cryo-TE-064', data: datasources[\"TS2\"][\"TS2-010CRM:Cryo-TE-064:MeasValue\"][series]},\n {label: 'TS2-010CDL:Cryo-TE-82314', data: datasources[\"TS2\"][\"TS2-010CDL:Cryo-TE-82314:MeasValue\"][series]},\n {label: 'CrS-TICP:Cryo-TE-31492', data: datasources[\"TS2\"][\"CrS-TICP:Cryo-TE-31492:Val\"][series]}\n];\nfreeboard.showLoadingIndicator(false);\nreturn data;",
+						"xticks": "",
+						"x_timestamp": true,
+						"zoom": ""
 					}
 				}
 			]
@@ -122,5 +128,5 @@
 			}
 		}
 	],
-	"columns": 4
+	"columns": 3
 }
-- 
GitLab