aboutsummaryrefslogtreecommitdiffstats
path: root/resources/tools/dash/app/pal
diff options
context:
space:
mode:
authorTibor Frank <tifrank@cisco.com>2022-05-11 14:45:35 +0200
committerTibor Frank <tifrank@cisco.com>2022-05-11 15:43:29 +0200
commit735853394ad417b9f72e32a31524934e79e76fab (patch)
tree6791ac0e84d8c9afc89ef43b4ffed0f9248f4794 /resources/tools/dash/app/pal
parent9e5fff4c2f51f77f9bb44f09914e428a8e377597 (diff)
feat(uti): Statistics - base64 encoded url
Change-Id: Iab6d5c62ab35778127bb328a032a63cd7c21d0bd Signed-off-by: Tibor Frank <tifrank@cisco.com>
Diffstat (limited to 'resources/tools/dash/app/pal')
-rw-r--r--resources/tools/dash/app/pal/data/url_processing.py81
-rw-r--r--resources/tools/dash/app/pal/stats/layout.py56
2 files changed, 114 insertions, 23 deletions
diff --git a/resources/tools/dash/app/pal/data/url_processing.py b/resources/tools/dash/app/pal/data/url_processing.py
new file mode 100644
index 0000000000..e74eb41689
--- /dev/null
+++ b/resources/tools/dash/app/pal/data/url_processing.py
@@ -0,0 +1,81 @@
+# Copyright (c) 2022 Cisco and/or its affiliates.
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""URL decoding and parsing and URL encoding.
+"""
+
+import logging
+
+from base64 import urlsafe_b64encode, urlsafe_b64decode
+from urllib.parse import urlencode, urlunparse, urlparse, parse_qs
+from zlib import compress, decompress
+from zlib import error as ZlibErr
+from binascii import Error as BinasciiErr
+
+
+def url_encode(params: dict) -> str:
+ """
+ """
+ url_params = params.get("params", None)
+ if url_params:
+ encoded_params = urlsafe_b64encode(
+ compress(urlencode(url_params).encode("utf-8"))
+ ).rstrip(b"=").decode("utf-8")
+ else:
+ encoded_params = str()
+
+ return urlunparse((
+ params.get("scheme", "http"),
+ params.get("netloc", str()),
+ params.get("path", str()),
+ str(), # params
+ params.get("query", str()),
+ encoded_params
+ ))
+
+
+def url_decode(url: str) -> dict:
+ """
+ """
+ try:
+ parsed_url = urlparse(url)
+ except ValueError as err:
+ logging.warning(f"\nThe url {url} is not valid, ignoring.\n{repr(err)}")
+ return None
+
+ if parsed_url.fragment:
+ try:
+ padding = b"=" * (4 - (len(parsed_url.fragment) % 4))
+ params = parse_qs(decompress(
+ urlsafe_b64decode(
+ (parsed_url.fragment.encode("utf-8") + padding)
+ )).decode("utf-8")
+ )
+ except (BinasciiErr, UnicodeDecodeError, ZlibErr) as err:
+ logging.warning(
+ f"\nNot possible to decode the parameters from url: {url}"
+ f"\nEncoded parameters: '{parsed_url.fragment}'"
+ f"\n{repr(err)}"
+ )
+ return None
+ else:
+ params = None
+
+ return {
+ "scheme": parsed_url.scheme,
+ "netloc": parsed_url.netloc,
+ "path": parsed_url.path,
+ "query": parsed_url.query,
+ "fragment": parsed_url.fragment,
+ "params": params
+ }
diff --git a/resources/tools/dash/app/pal/stats/layout.py b/resources/tools/dash/app/pal/stats/layout.py
index d265145d48..8b4de8491d 100644
--- a/resources/tools/dash/app/pal/stats/layout.py
+++ b/resources/tools/dash/app/pal/stats/layout.py
@@ -15,7 +15,6 @@
"""
import logging
-import urllib
import pandas as pd
import dash_bootstrap_components as dbc
@@ -30,6 +29,7 @@ from datetime import datetime, timedelta
from copy import deepcopy
from ..data.data import Data
+from ..data.url_processing import url_decode, url_encode
from .graphs import graph_statistics, select_data
@@ -578,10 +578,12 @@ class Layout:
@staticmethod
def _generate_options(opts: list) -> list:
- """
- """
return [{"label": i, "value": i} for i in opts]
+ @staticmethod
+ def _get_date(s_date: str) -> datetime:
+ return datetime(int(s_date[0:4]), int(s_date[5:7]), int(s_date[8:10]))
+
def callbacks(self, app):
@app.callback(
@@ -614,12 +616,15 @@ class Layout:
ctrl_panel = self.ControlPanel(cp_data, self.default)
- start = datetime(int(start[0:4]), int(start[5:7]), int(start[8:10]))
- end = datetime(int(end[0:4]), int(end[5:7]), int(end[8:10]))
+ start = self._get_date(start)
+ end = self._get_date(end)
- parsed_url = urllib.parse.urlparse(href)
- url = f"{parsed_url.netloc}{parsed_url.path}"
- url_params = urllib.parse.parse_qs(parsed_url.fragment)
+ # Parse the url:
+ parsed_url = url_decode(href)
+ if parsed_url:
+ url_params = parsed_url["params"]
+ else:
+ url_params = None
trigger_id = callback_context.triggered[0]["prop_id"].split(".")[0]
if trigger_id == "ri-duts":
@@ -678,12 +683,8 @@ class Layout:
new_start = url_params.get("start", list())[0]
new_end = url_params.get("end", list())[0]
if new_job and new_start and new_end:
- start = datetime(
- int(new_start[0:4]), int(new_start[5:7]),
- int(new_start[8:10]))
- end = datetime(
- int(new_end[0:4]), int(new_end[5:7]),
- int(new_end[8:10]))
+ start = self._get_date(new_start)
+ end = self._get_date(new_end)
job_params = self._set_job_params(new_job)
ctrl_panel = self.ControlPanel(None, job_params)
else:
@@ -701,27 +702,36 @@ class Layout:
ctrl_panel.get("ri-cadences-value"),
ctrl_panel.get("dd-tbeds-value")
)
- url_params = {
- "job": job,
- "start": start,
- "end": end
- }
ctrl_panel.set({"al-job-children": job})
- fig_passed, fig_duration = graph_statistics(
- self.data, job, self.layout, start, end)
+ fig_passed, fig_duration = graph_statistics(self.data, job,
+ self.layout, start, end)
+
+ if parsed_url:
+ new_url = url_encode({
+ "scheme": parsed_url["scheme"],
+ "netloc": parsed_url["netloc"],
+ "path": parsed_url["path"],
+ "params": {
+ "job": job,
+ "start": start,
+ "end": end
+ }
+ })
+ else:
+ new_url = str()
ret_val = [
ctrl_panel.panel,
fig_passed,
fig_duration,
- [
+ [ # URL
dcc.Clipboard(
target_id="card-url",
title="Copy URL",
style={"display": "inline-block"}
),
- f"{url}#{urllib.parse.urlencode(url_params)}"
+ new_url
]
]
ret_val.extend(ctrl_panel.values())