diff --git a/smartreport_app/kb_interface.py b/smartreport_app/kb_interface.py index e42944d..8228d12 100644 --- a/smartreport_app/kb_interface.py +++ b/smartreport_app/kb_interface.py @@ -8,6 +8,7 @@ def kb_interface(params): start_time = params['start_time'] end_time = params['end_time'] frequency = params['kpi_frequency_list'] + predict = params['predict'] if plot_type == 'semaphore': resp = get_kpi_value(kpi_list[0]) # is only one @@ -35,12 +36,11 @@ def kb_interface(params): 'rgb(75, 193, 193)', 'rgb(255, 159, 64)', 'rgb(154, 102, 255)', - 'rgb(255, 205, 86)', - 'rgb(201, 203, 208)' + 'rgb(255, 205, 86)' ] transp_colors = [color[:-1]+', 0.2)' for color in colors] frequencies_lbls = { - 'monthly': [ + 'annual': [ 'January', 'February', 'March', @@ -54,8 +54,8 @@ def kb_interface(params): 'November', 'December' ], - 'hourly': [str(i)+':00' for i in range(24)], - 'daily': [ + 'daily': [str(i)+':00' for i in range(24)], + 'weekly': [ 'Monday', 'Tuesday', 'Wednesday', @@ -66,14 +66,16 @@ def kb_interface(params): ] } - if frequency == 'monthly': - start_freq = start_time.month # int in [0,11] - elif frequency == 'daily': - start_freq = start_time.day # int in [0,6] - elif frequency == 'hourly': - start_freq = start_time.hour # int in [0,11] - elif frequency == 'weekly': - start_freq = 0 + # if frequency == 'monthly': + # start_freq = start_time.month # int in [0,11] + # elif frequency == 'daily': + # start_freq = start_time.day # int in [0,6] + # elif frequency == 'hourly': + # start_freq = start_time.hour # int in [0,11] + # elif frequency == 'weekly': + # start_freq = 0 + + start_freq = 0 kb_resps = [] @@ -81,7 +83,19 @@ def kb_interface(params): kn_resp = get_kpi_value(kpi_uid, start_time, end_time) # call to group 1 api kb_resps.append(kn_resp) - frequencies_lbls['weekly'] = [f'Week {i}' for i in range(len(kb_resps[0]['value']))] # assuming they all have the same length + + if predict: + for key, value in frequencies_lbls.items(): + value.append(['Monday' ,'Prediction']) # TODO start after the current date + value.append(['Tuesday' ,'Prediction']) + + + for kb_resp in kb_resps: + kb_resp['value'].append(random.random()) # TODO actual prediction + kb_resp['value'].append(random.random()) + + + labels = [] datasets = [] @@ -95,7 +109,31 @@ def kb_interface(params): dataset['fill'] = False dataset['borderColor'] = colors[i%len(colors)] dataset['backgroundColor'] = transp_colors[i%len(colors)] - dataset['tension'] = 0.1 + dataset['tension'] = 0 + if predict: + dashed_dataset = {} + dashed_dataset['fill'] = False + dashed_dataset['borderColor'] = colors[i%len(colors)] + dashed_dataset['backgroundColor'] = transp_colors[i%len(colors)] + dashed_dataset['tension'] = 0.8 + dashed_dataset['label'] = kb_resp['name'] + ' ' +'Prediction' + dashed_dataset['data'] = [] + + # put to None all the values of the dashed dataset except the last two + for i, val in enumerate(dataset['data']): + if i == len(dataset['data'])-1 or i == len(dataset['data'])-2 or i == len(dataset['data'])-3: + dashed_dataset['data'].append(val) + else: + dashed_dataset['data'].append(None) + + # remove the last two value from the original dataset + dataset['data'][-1] = None + dataset['data'][-2] = None + + dashed_dataset['borderDash'] = [1, 1] + + datasets.append(dashed_dataset) + elif plot_type == 'bar': dataset['borderColor'] = colors[i%len(colors)] dataset['backgroundColor'] = colors[i%len(colors)] @@ -108,6 +146,8 @@ def kb_interface(params): dataset['backgroundColor'] = colors datasets.append(dataset) + datasets.reverse() + response = { 'labels': labels, 'datasets': datasets diff --git a/smartreport_app/views.py b/smartreport_app/views.py index 48f9273..6a7e9d7 100644 --- a/smartreport_app/views.py +++ b/smartreport_app/views.py @@ -112,7 +112,7 @@ def __init__(self, **kwargs: Any) -> None: super().__init__(**kwargs) def list(self, request): - params = request.query_params + params = request.query_params.copy() if 'kpis' not in params or params.getlist("kpis") == []: return Response({"message": "The required parameter 'kpis' is missing"}, status=status.HTTP_400_BAD_REQUEST) @@ -127,6 +127,10 @@ def list(self, request): if 'chart_type' not in params: return Response({"message": "The required parameter 'chart_type' is missing"}, status=status.HTTP_400_BAD_REQUEST) + # TODO remove after testing + if params["chart_type"] == 'line': + params['predict'] = True + if list_of_KB_uids == []: return Response({"message": "No kpis found"}, status=status.HTTP_400_BAD_REQUEST) @@ -136,8 +140,10 @@ def list(self, request): if params["chart_type"] in ["semaphore", "value"] and len(list_of_KB_uids) > 1: return Response({"message": "This plot only supports one kpi"}, status=status.HTTP_400_BAD_REQUEST) + + if 'predict' in params and params["predict"] == "true" and params['chart_type'] != 'line': + return Response({"message": "This plot does not support prediction"}, status=status.HTTP_400_BAD_REQUEST) - # TODO implement start and end time # TODO for now frequency is only weekly kb_interface_params = { @@ -146,6 +152,7 @@ def list(self, request): 'kpi_frequency_list' : 'weekly', 'start_time' : 'boh', 'end_time' : 'boh', + 'predict' : params['predict'] if 'predict' in params else False, } data = kb_interface(params = kb_interface_params)