diff --git a/models/acc_operation.py b/models/acc_operation.py index 96d6986b95fbce321ec45ca714de0fa86528579f..373dd2bb4052a13e1a4bfaf366bd47c9823b675a 100644 --- a/models/acc_operation.py +++ b/models/acc_operation.py @@ -281,28 +281,16 @@ class AccOperation(models.Model): if delta <= 1: step_display_courbe = "hour" step = "hour" - if not scale: - scale = "day" elif delta < 32: step_display_courbe = "day" step = "hour" - # if not scale: scale = "week" - elif delta <= 365: - step = "month" - step_display_courbe = "month" - if not scale: - scale = "month" elif delta > 365: step = "year" step_display_courbe = "year" - if not scale: - scale = "month" else: step = "month" step_display_courbe = "month" - if not scale: - scale = "year" return scale, step, step_display_courbe @@ -363,18 +351,18 @@ class AccOperation(models.Model): self.env.cr.execute(query, query_params) raw_data = self.env.cr.fetchall() for row in raw_data: - if slot_type == "month" or slot_type == "year": - data_autocons.append(int(row[1])) - data_prod.append(int(row[2])) - data_allocons.append(int(row[3])) - data_cons.append(int(row[4])) - label.append(row[0]) - else: - data_autocons.append({"x": row[0], "y": round(row[1], 2)}) - data_prod.append({"x": row[0], "y": round(row[2], 2)}) - data_allocons.append({"x": row[0], "y": round(row[3], 2)}) - data_cons.append({"x": row[0], "y": round(row[4], 2)}) - label.append(row[0]) + # if slot_type == "month" or slot_type == "year": + # data_autocons.append(int(row[1])) + # data_prod.append(int(row[2])) + # data_allocons.append(int(row[3])) + # data_cons.append(int(row[4])) + # label.append(row[0]) + # else: + data_autocons.append({"x": row[0], "y": round(row[1], 2)}) + data_prod.append({"x": row[0], "y": round(row[2], 2)}) + data_allocons.append({"x": row[0], "y": round(row[3], 2)}) + data_cons.append({"x": row[0], "y": round(row[4], 2)}) + label.append(row[0]) cdc_jour = { "autocons": data_autocons, @@ -575,512 +563,11 @@ class AccOperation(models.Model): } return cdc_jour - def chart_data_line_cons(self, chart_data, scale): - """ - Fonction retournant le dictionnaire permettant la construiction - des graphes de la partie consommation par mois - :param chart_data: données à afficher dans les chart (labels et data) - :return: un dictionnaire de chart - """ - offsetGridLines = True - result = {} - - result["line_chart_conso_line"] = { - "type": "line", - "data": { - "labels": chart_data["label"], - "datasets": [ - { - "label": "Conso", - "data": chart_data["cons"], - "backgroundColor": "rgba(57, 120, 187, 0.2)", - "borderColor": "rgba(57, 120, 187, 1)", - "borderWidth": 1, - "hoverRadius": 1, - "radius": 0, - }, - { - "label": "Production solaire", - "data": chart_data["prod"], - "backgroundColor": "rgba(244, 165, 25, 0)", - "borderColor": "rgba(244, 165, 25, 1)", - "borderWidth": 2, - "hoverRadius": 1, - "radius": 0, - "hidden": "true", - }, - { - "label": "AutoProd", - "data": chart_data["autocons"], - "backgroundColor": "rgba(91, 154, 81, 0.4)", - "borderColor": "rgba(91, 154, 81, 1)", - "borderWidth": 2, - "hoverRadius": 1, - "radius": 0, - }, - ], - }, - "options": { - "scales": { - "xAxes": [ - { - "type": "time", - "time": { - "unit": scale, - "displayFormats": {"quarter": "D MMM"}, - }, - "gridLines": {"offsetGridLines": offsetGridLines}, - } - ], - "yAxes": [ - { - "scaleLabel": { - "display": True, - "labelString": "kW", - } - } - ], - }, - "tooltips": { - "backgroundColor": "#f5f5f5", - "titleFontColor": "#333", - "bodyFontColor": "#666", - "bodySpacing": 4, - "xPadding": 12, - "mode": "x", - "intersect": False, - }, - "elements": {"point": {"radius": 0}}, - }, - } - result["line_chart_prod_prm"] = { - "type": "line", - "data": { - "labels": chart_data["label"], - "datasets": [ - { - "label": "Production solaire", - "data": chart_data["prod"], - "backgroundColor": "rgba(244, 165, 25, 0)", - "borderColor": "rgba(244, 165, 25, 1)", - }, - ], - }, - } - return result - - def chart_data_histo_cons(self, chart_data, scale, scale_spe): - """ - Fonction retournant le dictionnaire permettant la construiction - des graphes de la partie consommation par mois - :param chart_data: données à afficher dans les chart (labels et data) - :return: un dictionnaire de chart - """ - - result = {} - if scale_spe == "week": - data_autocons = chart_data["autocons_histo"] - data_allocons = chart_data["allocons_histo"] - data_label = chart_data["label_histo"] - else: - data_autocons = chart_data["autocons"] - data_allocons = chart_data["allocons"] - data_label = chart_data["label"] - result["histo_chart_conso"] = { - "type": "bar", - "data": { - "labels": data_label, - "datasets": [ - { - "label": "AutoProd", - "data": data_autocons, - "backgroundColor": "rgba(91, 154, 81, 0.4)", - "hoverBackgroundColor": "rgba(91, 154, 81, 0.7)", - }, - { - "label": "AlloConso", - "data": data_allocons, - "backgroundColor": "rgba(57, 120, 187, 0.4)", - "hoverBackgroundColor": "rgba(57, 120, 187, 0.7)", - }, - ], - }, - "options": { - "plugins": { - "datalabels": { - "color": "white", - "font": {"weight": "bold"}, - } - }, - "interaction": { - "intersect": False, - }, - "scales": { - "xAxes": [ - { - "type": "time", - "time": { - "unit": scale, - "displayFormats": {"quarter": "D MMM"}, - }, - "stacked": True, - "offset": True, - "gridLines": {"offsetGridLines": True}, - } - ], - "yAxes": [ - { - "stacked": True, - "ticks": { - "beginAtZero": True, - }, - "scaleLabel": { - "display": True, - "labelString": "kWh", - }, - } - ], - }, - "tooltips": { - "mode": "x", - }, - }, - } - return result - - def chart_data_donuts_cons(self, chart_data, scale): - """ - Fonction retournant le dictionnaire permettant la construiction - des graphes de la partie consommation par mois - :param chart_data: données à afficher dans les chart (labels et data) - :return: un dictionnaire de chart - """ - - result = {} - - if scale == "hour": - sum_res1 = sum(item["y"] for item in chart_data["autocons"]) - sum_res2 = sum(item["y"] for item in chart_data["allocons"]) - else: - sum_res1 = sum(chart_data["autocons"]) - sum_res2 = sum(chart_data["allocons"]) - - label = ["AutoProd", "AlloConso"] - res = [int(sum_res1), int(sum_res2)] - - result["donuts_chart_conso"] = { - "type": "doughnut", - "data": { - "labels": label, - "datasets": [ - { - "label": "Inférieur à 3", - "data": res, - "backgroundColor": [ - "rgba(91, 154, 81, 0.4)", - "rgba(57, 120, 187, 0.4)", - ], - "hoverBackgroundColor": [ - "rgba(91, 154, 81, 0.7)", - "rgba(57, 120, 187, 0.7)", - ], - "borderWidth": 1, - } - ], - }, - "options": { - "cutoutPercentage": 60, - "animation": { - "animateScale": True, - }, - "plugins": { - "datalabels": { - "color": "white", - "font": {"weight": "bold"}, - "padding": 6, - } - }, - "tooltips": {}, - }, - } - return result - - def chart_data_line_prod(self, chart_data, scale): - """ - Fonction retournant le dictionnaire permettant la construiction - des graphes de la partie production - :param chart_data: données à afficher dans les chart (labels et data) - :return: un dictionnaire de chart - """ - - result = {} - result["line_chart_prod_line"] = { - "type": "line", - "data": { - "labels": chart_data["label"], - "datasets": [ - { - "label": "AutoConso", - "data": chart_data["autocons_prod"], - "backgroundColor": "rgba(91, 154, 81, 0.4)", - "borderColor": "rgba(91, 154, 81, 1)", - "borderWidth": 2, - "hoverRadius": 1, - "radius": 0, - "fill": "origin", - }, - { - "label": "Surplus", - "data": chart_data["surplus"], - "backgroundColor": "rgba(225, 80, 96, 0.4)", - "borderColor": "rgba(225, 80, 96, 1)", - "borderWidth": 2, - "hoverRadius": 1, - "radius": 0, - "fill": "-1", - }, - ], - }, - "options": { - "scales": { - "xAxes": [ - { - "type": "time", - "time": { - "unit": scale, - "displayFormats": {"quarter": "D MMM"}, - }, - "ticks": { - # 'min': result.date_start, - }, - "gridLines": {"offsetGridLines": True}, - } - ], - "yAxes": [ - { - "stacked": True, - "scaleLabel": { - "display": True, - "labelString": "kW", - }, - } - ], - }, - "tooltips": { - "backgroundColor": "#f5f5f5", - "titleFontColor": "#333", - "bodyFontColor": "#666", - "bodySpacing": 4, - "xPadding": 12, - "mode": "x", - "intersect": False, - }, - "elements": {"point": {"radius": 0}}, - }, - } - return result - - def chart_data_donuts_prod(self, chart_data, scale): - """ - Fonction retournant le dictionnaire permettant la construction - des graphes de la partie production - :param chart_data: données à afficher dans les chart (labels et data) - :return: un dictionnaire de chart - """ - - result = {} - if scale == "hour": - sum_res1 = sum(item["y"] for item in chart_data["autocons_prod"]) - sum_res2 = sum(item["y"] for item in chart_data["surplus"]) - else: - sum_res1 = sum(chart_data["autocons_prod"]) - sum_res2 = sum(chart_data["surplus"]) - - label = ["AutoConso", "Surplus"] - res = [int(sum_res1), int(sum_res2)] - - result["donuts_chart_prod"] = { - "type": "doughnut", - "data": { - "labels": label, - "datasets": [ - { - "label": "Inférieur à 3", - "data": res, - "backgroundColor": [ - "rgba(91, 154, 81, 0.4)", - "rgba(225, 80, 96, 0.4)", - ], - "hoverBackgroundColor": [ - "rgba(91, 154, 81, 0.7)", - "rgba(225, 80, 96, 0.7)", - ], - "borderWidth": 1, - } - ], - }, - "options": { - "plugins": { - "datalabels": { - "color": "white", - "font": {"weight": "bold"}, - "padding": 6, - } - }, - "cutoutPercentage": 60, - "animation": { - "animateScale": True, - }, - "tooltips": {}, - }, - } - - return result - - def chart_data_histo_prod(self, chart_data, scale, scale_spe): - """ - Fonction retournant le dictionnaire permettant la construiction - des graphes de la partie production - :param chart_data: données à afficher dans les chart (labels et data) - :return: un dictionnaire de chart - """ - - result = {} - if scale_spe == "week": - data_autocons = chart_data["autocons_prod_histo"] - data_surplus = chart_data["surplus_histo"] - data_label = chart_data["label_histo"] - else: - data_autocons = chart_data["autocons_prod"] - data_surplus = chart_data["surplus"] - data_label = chart_data["label"] - - result["histo_chart_prod"] = { - "type": "bar", - "data": { - "labels": data_label, - "datasets": [ - { - "label": "AutoConso", - "data": data_autocons, - "backgroundColor": "rgba(91, 154, 81, 0.4)", - "hoverBackgroundColor": "rgba(91, 154, 81, 0.7)", - "borderColor": "rgba(91, 154, 81, 1)", - }, - { - "label": "Surplus", - "data": data_surplus, - "backgroundColor": "rgba(225, 80, 96, 0.4)", - "hoverBackgroundColor": "rgba(225, 80, 96, 0.7)", - "borderColor": "rgba(225, 80, 96, 1)", - }, - ], - }, - "options": { - "plugins": { - "datalabels": { - "color": "white", - "font": {"weight": "bold"}, - } - }, - "interaction": { - "intersect": False, - }, - "scales": { - "xAxes": [ - { - "type": "time", - "time": { - "unit": scale, - "displayFormats": {"quarter": "D MMM"}, - }, - "stacked": True, - "offset": True, - "gridLines": {"offsetGridLines": True}, - } - ], - "yAxes": [ - { - "stacked": True, - "ticks": { - "beginAtZero": True, - }, - "type": "linear", - "scaleLabel": { - "display": True, - "labelString": "kWh", - }, - } - ], - }, - "tooltips": { - "mode": "x", - }, - }, - } - - return result - - # ------------------------------------------------------ - # Functions to manage route - # ------------------------------------------------------ - def build_graph_data_options( - self, chart_data, step_display_courbe=None, scale=None, step_courbe=None - ): - result_graph = {} - result_graph_line = self.chart_data_line_prod(chart_data, step_display_courbe) - result_graph.update(result_graph_line) - result_graph_histo = self.chart_data_histo_prod( - chart_data, step_display_courbe, scale - ) - result_graph.update(result_graph_histo) - result_graph_donuts = self.chart_data_donuts_prod(chart_data, step_courbe) - result_graph.update(result_graph_donuts) - - result_graph_line = self.chart_data_line_cons(chart_data, step_display_courbe) - result_graph.update(result_graph_line) - result_graph_histo = self.chart_data_histo_cons( - chart_data, step_display_courbe, scale - ) - result_graph.update(result_graph_histo) - result_graph_donuts = self.chart_data_donuts_cons(chart_data, step_courbe) - result_graph.update(result_graph_donuts) - return result_graph - - def build_graph_data_options_prod( - self, chart_data, step_display_courbe=None, scale=None, step_courbe=None - ): - result_graph = {} - result_graph_line = self.chart_data_line_prod(chart_data, step_display_courbe) - result_graph.update(result_graph_line) - result_graph_histo = self.chart_data_histo_prod( - chart_data, step_display_courbe, scale - ) - result_graph.update(result_graph_histo) - result_graph_donuts = self.chart_data_donuts_prod(chart_data, step_courbe) - result_graph.update(result_graph_donuts) - return result_graph - - def build_graph_data_options_cons( - self, chart_data, step_display_courbe=None, scale=None, step_courbe=None - ): - result_graph = {} - result_graph_line = self.chart_data_line_cons(chart_data, step_display_courbe) - result_graph.update(result_graph_line) - result_graph_histo = self.chart_data_histo_cons( - chart_data, step_display_courbe, scale - ) - result_graph.update(result_graph_histo) - result_graph_donuts = self.chart_data_donuts_cons(chart_data, step_courbe) - result_graph.update(result_graph_donuts) - return result_graph - # ------------------------------------------------------ # Functions to manage route # ------------------------------------------------------ def graph_view_global( self, - scale=None, date_start=None, date_end=None, partner_id=None, @@ -1096,18 +583,13 @@ class AccOperation(models.Model): :return: dictionnaire pour la construction des graphes """ result_graph = {} - scale_init = scale - if not date_start and not date_end: - date_start, date_end = self.get_last_day(scale) - is_scale = True - else: - date_end = date_utils.end_of(date_end, "day") - is_scale = False + + date_end = date_utils.end_of(date_end, "day") date_start = datetime.strptime(date_start, "%d/%m/%Y") date_end = datetime.strptime(date_end, "%d/%m/%Y") scale, step_courbe, step_display_courbe = self.get_step_from_date( - date_start=date_start, date_end=date_end, scale=scale + date_start=date_start, date_end=date_end, scale=False ) if data_type == "pmo": @@ -1132,49 +614,41 @@ class AccOperation(models.Model): else: is_curve_line = False - chart_data = self.get_cdc_by_query_cons( - step_courbe, date_start, date_end, acc_counter_ids - ) - if scale == "week": - chart_data_histo = self.get_cdc_by_query_histo_cons( - "day", date_start, date_end, acc_counter_ids + chart_data = {} + if data_type == "cons" or data_type == "pmo": + chart_data_cons = self.get_cdc_by_query_cons( + step_courbe, date_start, date_end, acc_counter_ids ) - chart_data.update(chart_data_histo) - - chart_data_prod = self.get_cdc_by_query_prod( - step_courbe, date_start, date_end, acc_counter_ids - ) - if scale == "week": - chart_data_histo = self.get_cdc_by_query_histo_prod( - "day", date_start, date_end, acc_counter_ids + if scale == "week": + chart_data_histo = self.get_cdc_by_query_histo_cons( + "day", date_start, date_end, acc_counter_ids + ) + chart_data_cons.update(chart_data_histo) + chart_data.update(chart_data_cons) + if data_type == "prod" or data_type == "pmo": + chart_data_prod = self.get_cdc_by_query_prod( + step_courbe, date_start, date_end, acc_counter_ids ) - chart_data_prod.update(chart_data_histo) - - chart_data.update(chart_data_prod) + if scale == "week": + chart_data_histo = self.get_cdc_by_query_histo_prod( + "day", date_start, date_end, acc_counter_ids + ) + chart_data_prod.update(chart_data_histo) + chart_data.update(chart_data_prod) - # Build the chart with data and options - result_graph = self.build_graph_data_options( - chart_data, - step_display_courbe=step_display_courbe, - scale=scale, - step_courbe=step_courbe, - ) + result_graph["chart_data"] = chart_data date_deb, date_max = self.get_last_day("day") date_max = date_max.strftime("%d/%m/%Y") date_min = self.get_first_day() date_min = date_min.strftime("%d/%m/%Y") - - if not is_scale: - scale_init = False - result_graph.update( { "date_start": date_start, "date_end": date_end, "date_min": date_min, "date_max": date_max, - "scale": scale_init, + "scale": step_display_courbe, "is_curve_line": is_curve_line, } )