import pandas as pd
from ..utils.constants import Constants as C
-from ..utils.utils import classify_anomalies, get_color, get_hdrh_latencies
+from ..utils.utils import get_color, get_hdrh_latencies
+from ..utils.anomalies import classify_anomalies
def select_trending_data(data: pd.DataFrame, itm: dict) -> pd.DataFrame:
return fig_tput, fig_lat
-def graph_tm_trending(data: pd.DataFrame, layout: dict) -> list:
+def graph_tm_trending(
+ data: pd.DataFrame,
+ layout: dict,
+ all_in_one: bool=False
+ ) -> list:
"""Generates one trending graph per test, each graph includes all selected
metrics.
:param data: Data frame with telemetry data.
:param layout: Layout of plot.ly graph.
+ :param all_in_one: If True, all telemetry traces are placed in one graph,
+ otherwise they are split to separate graphs grouped by test ID.
:type data: pandas.DataFrame
:type layout: dict
+ :type all_in_one: bool
:returns: List of generated graphs together with test names.
list(tuple(plotly.graph_objects.Figure(), str()), tuple(...), ...)
:rtype: list
"""
+ if data.empty:
+ return list()
- def _generate_graph(
+ def _generate_traces(
data: pd.DataFrame,
test: str,
- layout: dict
- ) -> go.Figure:
+ all_in_one: bool,
+ color_index: int
+ ) -> list:
"""Generates a trending graph for given test with all metrics.
:param data: Data frame with telemetry data for the given test.
:param test: The name of the test.
- :param layout: Layout of plot.ly graph.
+ :param all_in_one: If True, all telemetry traces are placed in one
+ graph, otherwise they are split to separate graphs grouped by
+ test ID.
+ :param color_index: The index of the test used if all_in_one is True.
:type data: pandas.DataFrame
:type test: str
- :type layout: dict
- :returns: A trending graph.
- :rtype: plotly.graph_objects.Figure
+ :type all_in_one: bool
+ :type color_index: int
+ :returns: List of traces.
+ :rtype: list
"""
- graph = None
traces = list()
- for idx, metric in enumerate(data.tm_metric.unique()):
+ metrics = data.tm_metric.unique().tolist()
+ for idx, metric in enumerate(metrics):
if "-pdr" in test and "='pdr'" not in metric:
continue
if "-ndr" in test and "='ndr'" not in metric:
hover.append(
f"date: "
f"{row['start_time'].strftime('%Y-%m-%d %H:%M:%S')}<br>"
- f"value: {y_data[i]:,.0f}<br>"
+ f"value: {y_data[i]:,.2f}<br>"
f"{rate}"
f"{row['dut_type']}-ref: {row['dut_version']}<br>"
f"csit-ref: {row['job']}/{row['build']}<br>"
hover_trend.append(
f"date: "
f"{row['start_time'].strftime('%Y-%m-%d %H:%M:%S')}<br>"
- f"trend: {avg:,.0f}<br>"
- f"stdev: {stdev:,.0f}<br>"
+ f"trend: {avg:,.2f}<br>"
+ f"stdev: {stdev:,.2f}<br>"
f"{row['dut_type']}-ref: {row['dut_version']}<br>"
f"csit-ref: {row['job']}/{row['build']}"
)
else:
anomalies = None
- color = get_color(idx)
+ if all_in_one:
+ color = get_color(color_index * len(metrics) + idx)
+ metric_name = f"{test}<br>{metric}"
+ else:
+ color = get_color(idx)
+ metric_name = metric
+
traces.append(
go.Scatter( # Samples
x=x_axis,
y=y_data,
- name=metric,
+ name=metric_name,
mode="markers",
marker={
"size": 5,
text=hover,
hoverinfo="text+name",
showlegend=True,
- legendgroup=metric
+ legendgroup=metric_name
)
)
if anomalies:
go.Scatter( # Trend line
x=x_axis,
y=trend_avg,
- name=metric,
+ name=metric_name,
mode="lines",
line={
"shape": "linear",
text=hover_trend,
hoverinfo="text+name",
showlegend=False,
- legendgroup=metric
+ legendgroup=metric_name
)
)
text=hover,
hoverinfo="text+name",
showlegend=False,
- legendgroup=metric,
- name=metric,
+ legendgroup=metric_name,
+ name=metric_name,
marker={
"size": 15,
"symbol": "circle-open",
)
)
- if traces:
- graph = go.Figure()
- graph.add_traces(traces)
- graph.update_layout(layout.get("plot-trending-telemetry", dict()))
-
- return graph
-
+ unique_metrics = set()
+ for itm in metrics:
+ unique_metrics.add(itm.split("{", 1)[0])
+ return traces, unique_metrics
tm_trending_graphs = list()
+ graph_layout = layout.get("plot-trending-telemetry", dict())
- if data.empty:
- return tm_trending_graphs
+ if all_in_one:
+ all_traces = list()
- for test in data.test_name.unique():
+ all_metrics = set()
+ all_tests = list()
+ for idx, test in enumerate(data.test_name.unique()):
df = data.loc[(data["test_name"] == test)]
- graph = _generate_graph(df, test, layout)
- if graph:
- tm_trending_graphs.append((graph, test, ))
-
- return tm_trending_graphs
+ traces, metrics = _generate_traces(df, test, all_in_one, idx)
+ if traces:
+ all_metrics.update(metrics)
+ if all_in_one:
+ all_traces.extend(traces)
+ all_tests.append(test)
+ else:
+ graph = go.Figure()
+ graph.add_traces(traces)
+ graph.update_layout(graph_layout)
+ tm_trending_graphs.append((graph, [test, ], ))
+
+ if all_in_one:
+ graph = go.Figure()
+ graph.add_traces(all_traces)
+ graph.update_layout(graph_layout)
+ tm_trending_graphs.append((graph, all_tests, ))
+
+ return tm_trending_graphs, all_metrics