diff --git a/controllers/main.py b/controllers/main.py index 10c0807e0280b1f5de8810e9ec0fe3e04e260b1b..540b75f3f94375f6cf79f58c01bffc6b51448004 100644 --- a/controllers/main.py +++ b/controllers/main.py @@ -4,202 +4,14 @@ import json from odoo import http from odoo.http import request -from odoo.http import Response -from datetime import datetime, timedelta -from pytz import timezone class Main(http.Controller): - @http.route("/get_data", auth='none', type='http', method=['GET']) - def method_get(self, **post): - headers = {'Content-Type': 'application/json'} - # if post.get('type_cdc', ''): - # type_cdc = post.get('type_cdc', '') + @http.route(['/get_data_conso'], type='json', auth="user", website=True) + def get_conso(self, operation_id, scale, **kw): + values = operation_id.graph_view_conso(scale) - if post.get('operation_id', ''): - operation_id = post.get('operation_id', '') - - if post.get('date_start', ''): - date_start = datetime.strptime(post.get('date_start', ''), "%Y-%m-%dT%H:%M:%S") - - if post.get('date_end', ''): - date_end = datetime.strptime(post.get('date_end', ''), "%Y-%m-%dT%H:%M:%S") - - if post.get('type_date', ''): - type_date = post.get('type_date', '') - - cdc_week_month = [] - data_autocons = [] - data_cons = [] - data_prod = [] - data_surplus = [] - - # Data by year / month - if type_date == "year": - cdc_ids = request.env['enercoop.enedis.cdc'].sudo().read_group( - [('enercoop_operation_id', '=', operation_id), - ('date_slot', '>=', date_start), - ('date_slot', '<', date_end)], - ['power', 'enercoop_operation_id', 'date_slot'], - ['comp_data_type', 'date_slot:month'], orderby='comp_data_type', lazy=False) - - for cdc in cdc_ids: - value_power = cdc['power'] / 1000 / 2 - value_hour = cdc['date_slot:month'] - data = { - 'x': value_hour, - 'y': value_power, - } - if cdc['comp_data_type'] == 'autocons': - data_autocons.append(data) - if cdc['comp_data_type'] == 'cons': - data_cons.append(data) - if cdc['comp_data_type'] == 'prod': - data_prod.append(data) - if cdc['comp_data_type'] == 'surplus': - data_surplus.append(data) - - # Data by week,month / day - if type_date == "week" or type_date == "month": - cdc_ids = request.env['enercoop.enedis.cdc'].sudo().read_group( - [('enercoop_operation_id', '=', operation_id), - ('date_slot', '>=', date_start), - ('date_slot', '<', date_end)], - ['power', 'enercoop_operation_id', 'date_slot'], - ['comp_data_type', 'date_slot:day'], orderby='comp_data_type', lazy=False) - - for cdc in cdc_ids: - value_power = cdc['power'] / 1000 / 2 - value_hour = cdc['date_slot:day'] - data = { - 'x': value_hour, - 'y': value_power, - } - if cdc['comp_data_type'] == 'autocons': - data_autocons.append(data) - if cdc['comp_data_type'] == 'cons': - data_cons.append(data) - if cdc['comp_data_type'] == 'prod': - data_prod.append(data) - if cdc['comp_data_type'] == 'surplus': - data_surplus.append(data) - - cdc_jour = [] - # Data by day / hour - if type_date == "day": - date_end = date_start + timedelta(days=1) - cdc_ids = request.env['enercoop.enedis.cdc'].sudo().read_group( - [('enercoop_operation_id', '=', operation_id), - ('date_slot', '>=', date_start), - ('date_slot', '<', date_end)], - ['power', 'enercoop_operation_id', 'date_slot'], - ['comp_data_type', 'date_slot:hour'], orderby='comp_data_type' ,lazy=False) - - for cdc in cdc_ids: - value_power = cdc['power'] - value_hour = cdc['date_slot:hour'] - data = { - 'x': value_hour, - 'y': value_power, - } - if cdc['comp_data_type'] == 'autocons': - data_autocons.append(data) - if cdc['comp_data_type'] == 'cons': - data_cons.append(data) - if cdc['comp_data_type'] == 'prod': - data_prod.append(data) - if cdc['comp_data_type'] == 'surplus': - data_surplus.append(data) - - bilan_cdc = request.env['enercoop.enedis.cdc'].sudo().read_group( - [('enercoop_operation_id', '=', operation_id), - ('date_slot', '>=', date_start), - ('date_slot', '<', date_end)], - ['power', 'enercoop_operation_id'], - ['comp_data_type'], orderby='comp_data_type', lazy=False) - - for bilan in bilan_cdc: - if bilan['comp_data_type'] == 'autocons': - power_autocons = bilan['power'] / 1000 / 2 - if bilan['comp_data_type'] == 'cons': - power_cons = bilan['power'] / 1000 / 2 - if bilan['comp_data_type'] == 'surplus': - power_surplus = bilan['power'] / 1000 / 2 - if bilan['comp_data_type'] == 'prod': - power_prod = bilan['power'] / 1000 / 2 - - power_tot = power_autocons + power_cons - percent_autocons = power_autocons * 100 / power_tot - percent_cons = power_cons * 100 / power_tot - - power_prod_tot = power_autocons + power_surplus - percent_autocons_prod = power_autocons * 100 / power_prod_tot - percent_surplus_prod = power_surplus * 100 / power_prod_tot - - data_autocons.append({ - 'bilan_cons': - { - 'power': power_autocons, - 'percent': percent_autocons - }, - 'bilan_prod': - { - 'power': power_autocons, - 'percent': percent_autocons_prod - }, - }) - data_cons.append({ - 'bilan_cons': - { - 'power': power_cons, - 'percent': percent_cons - } - }) - data_surplus.append({ - 'bilan_prod': - { - 'power': power_surplus, - 'percent': percent_surplus_prod - } - }) - - if type_date == "day": - cdc_jour.append({ - 'type': 'autocons', - 'data': data_autocons, - }) - cdc_jour.append({ - 'type': 'cons', - 'data': data_cons - }) - cdc_jour.append({ - 'type': 'prod', - 'data': data_prod - }) - cdc_jour.append({ - 'type': 'surplus', - 'data': data_surplus - }) - - if type_date == "week" or type_date == "month": - cdc_week_month.append({ - 'type': 'autocons', - 'data': data_autocons, - }) - cdc_week_month.append({ - 'type': 'cons', - 'data': data_cons - }) - cdc_week_month.append({ - 'type': 'prod', - 'data': data_prod - }) - cdc_week_month.append({ - 'type': 'surplus', - 'data': data_surplus - }) - - print("---- TEST ------") - - return True \ No newline at end of file + @http.route(['/get_data_prod'], type='json', auth="user", website=True) + def get_conso(self, operation_id, scale, **kw): + values = operation_id.graph_view_prod(scale) \ No newline at end of file diff --git a/models/enercoop_operation.py b/models/enercoop_operation.py index 62ff12c86290b194af398648dfb18a0f27a050c9..a8b2419989266345d04429a2ecd7c06ee8ce9950 100644 --- a/models/enercoop_operation.py +++ b/models/enercoop_operation.py @@ -32,6 +32,47 @@ class EnercoopOperation(models.Model): # ------------------------------------------------------ # CRUD methods (ORM overrides) # ------------------------------------------------------ + @api.model + def graph_view(self, domain, scale): + # Function call when load Qweb views + result_graph = {} + # Get the operations depending to the domain + operation_ids = self.env['enercoop.operation'].search(domain) + + if operation_ids: + # Get date start and date end depending on type of scale + date_start, date_end = operation_ids.get_last_day(scale) + + # Get the data to display in chart + chart_data = operation_ids.get_cdc(scale, date_start, date_end) + + # Build the chart depending on data calculated + result_graph = operation_ids.chart_data_cons(chart_data) + result_graph_prod = operation_ids.chart_data_prod(chart_data) + result_graph.update(result_graph_prod) + + return result_graph + + # ------------------------------------------------------ + # Actions + # ------------------------------------------------------ + def action_view_courbes(self): + self.ensure_one() + action = self.env["ir.actions.actions"]._for_xml_id( + "enercoop_cdc.enercoop_operation_action_client_courbes") + action['params'] = { + 'operation_ids': self.ids, + } + action['context'] = { + 'active_id': self.id, + 'active_ids': self.ids, + 'search_default_name': self.name, + } + return action + + # ------------------------------------------------------ + # Business methods + # ------------------------------------------------------ def get_last_day(self, scale): # Get last date slot recorded last_record = self.env['enercoop.enedis.cdc'].search([ @@ -55,172 +96,140 @@ class EnercoopOperation(models.Model): return date_start, date_end - @api.model - def graph_view(self, domain, scale): - - # Création des variables - # date_start = datetime.strptime('2021-03-09T00:00:00', "%Y-%m-%dT%H:%M:%S") + def chart_data_cons(self, chart_data): + # Creation chart result = {} - operation_ids = self.env['enercoop.operation'].search(domain) - - # Get last date slot recorded - if operation_ids: - date_start, date_end = operation_ids.get_last_day(scale) - # last_record = self.env['enercoop.enedis.cdc'].search([ - # ('enercoop_operation_id', '=', operation_ids.ids), - # ], limit=1, order='date_slot DESC') - # date_end = last_record.date_slot - - chart_data = operation_ids.get_cdc(scale, date_start, date_end) - - # Création des graphes - result = { - 'line_chart_conso': { - 'type': 'line', - 'data': { - 'labels': chart_data['label'], - 'datasets': [ - { - 'label': 'Allo Conso', - 'data': chart_data['cons'], - 'backgroundColor': 'rgba(79, 129, 189, 0.7)', - 'borderColor': 'rgba(79, 129, 189, 1)', - }, - { - 'label': 'Production solaire', - 'data': chart_data['prod'], - 'backgroundColor': 'rgba(255, 255, 0, 0)', - 'borderColor': 'rgba(255, 255, 0, 1)', - }, - { - 'label': 'Autoconso', - 'data': chart_data['autocons'], - 'backgroundColor': 'rgba(155, 187, 89, 0.7)', - 'borderColor': 'rgba(155, 187, 89, 1)', - }, - ], - }, - }, - 'donuts_chart_conso': { - 'type': 'doughnut', - 'data': { - 'labels': chart_data['label_doughnut'], - 'datasets': [{ - 'label': 'Inférieur à 3', - 'data': chart_data['doughnut_cons'], - 'backgroundColor': [ - 'rgb(155, 187, 89)', - 'rgb(79, 129, 189)', - ], - 'borderWidth': 1 - }], - }, + result['line_chart_conso'] = { + 'type': 'line', + 'data': { + 'labels': chart_data['label'], + 'datasets': [ + { + 'label': 'Allo Conso', + 'data': chart_data['cons'], + 'backgroundColor': 'rgba(79, 129, 189, 0.7)', + 'borderColor': 'rgba(79, 129, 189, 1)', + }, + { + 'label': 'Production solaire', + 'data': chart_data['prod'], + 'backgroundColor': 'rgba(255, 255, 0, 0)', + 'borderColor': 'rgba(255, 255, 0, 1)', + }, + { + 'label': 'Autoconso', + 'data': chart_data['autocons'], + 'backgroundColor': 'rgba(155, 187, 89, 0.7)', + 'borderColor': 'rgba(155, 187, 89, 1)', + }, + ], }, - 'histo_chart_conso': { - 'type': 'bar', - 'data': { - 'labels': chart_data['label'], - 'datasets': [ - { - 'label': 'AutoConso', - 'data': chart_data['autocons'], - 'backgroundColor': 'rgba(155, 187, 89, 0.7)', - 'borderColor': 'rgba(155, 187, 89, 1)', - }, - { - 'label': 'AlloConso', - 'data': chart_data['cons'], - 'backgroundColor': 'rgba(79, 129, 189, 0.7)', - 'borderColor': 'rgba(79, 129, 189, 1)', - }] - } - }, - 'line_chart_prod': { - 'type': 'line', - 'data': { - 'labels': chart_data['label'], - 'datasets': [ - { - 'label': 'Surplus', - 'data': chart_data['surplus'], - 'backgroundColor': 'rgba(192, 80, 77, 0.7)', - 'borderColor': 'rgba(192, 80, 77, 1)', - }, - { - 'label': 'Autoconso', - 'data': chart_data['autocons'], - 'backgroundColor': 'rgba(155, 187, 89, 0.7)', - 'borderColor': 'rgba(155, 187, 89, 1)', - }, - ], - }, - }, - 'donuts_chart_prod': { - 'type': 'doughnut', - 'data': { - 'labels': chart_data['label_doughnut_prod'], - 'datasets': [{ - 'label': 'Inférieur à 3', - 'data': chart_data['doughnut_prod'], - 'backgroundColor': [ - 'rgb(155, 187, 89)', - 'rgb(192, 80, 77)', - ], - 'borderWidth': 1 - }], + } + result['donuts_chart_conso'] = { + 'type': 'doughnut', + 'data': { + 'labels': chart_data['label_doughnut'], + 'datasets': [{ + 'label': 'Inférieur à 3', + 'data': chart_data['doughnut_cons'], + 'backgroundColor': [ + 'rgb(155, 187, 89)', + 'rgb(79, 129, 189)', + ], + 'borderWidth': 1 + }], + }, + } + result['histo_chart_conso'] = { + 'type': 'bar', + 'data': { + 'labels': chart_data['label'], + 'datasets': [ + { + 'label': 'AutoConso', + 'data': chart_data['autocons'], + 'backgroundColor': 'rgba(155, 187, 89, 0.7)', + 'borderColor': 'rgba(155, 187, 89, 1)', }, - }, - 'histo_chart_prod': { - 'type': 'bar', - 'data': { - 'labels': chart_data['label'], - 'datasets': [ - { - 'label': 'AutoConso', - 'data': chart_data['autocons'], - 'backgroundColor': 'rgba(155, 187, 89, 0.7)', - 'borderColor': 'rgba(155, 187, 89, 1)', - }, - { - 'label': 'Surplus', - 'data': chart_data['surplus'], - 'backgroundColor': 'rgba(192, 80, 77, 0.7)', - 'borderColor': 'rgba(192, 80, 77, 1)', - }] - } - }, + { + 'label': 'AlloConso', + 'data': chart_data['cons'], + 'backgroundColor': 'rgba(79, 129, 189, 0.7)', + 'borderColor': 'rgba(79, 129, 189, 1)', + }] + } } return result - # ------------------------------------------------------ - # Actions - # ------------------------------------------------------ - def action_view_courbes(self): - self.ensure_one() - action = self.env["ir.actions.actions"]._for_xml_id( - "enercoop_cdc.enercoop_operation_action_client_courbes") - action['params'] = { - 'operation_ids': self.ids, + def chart_data_prod(self, chart_data): + # Creation chart + result = {} + result['line_chart_prod']= { + 'type': 'line', + 'data': { + 'labels': chart_data['label'], + 'datasets': [ + { + 'label': 'Surplus', + 'data': chart_data['surplus'], + 'backgroundColor': 'rgba(192, 80, 77, 0.7)', + 'borderColor': 'rgba(192, 80, 77, 1)', + }, + { + 'label': 'Autoconso', + 'data': chart_data['autocons'], + 'backgroundColor': 'rgba(155, 187, 89, 0.7)', + 'borderColor': 'rgba(155, 187, 89, 1)', + }, + ], + }, } - action['context'] = { - 'active_id': self.id, - 'active_ids': self.ids, - 'search_default_name': self.name, + result['donuts_chart_prod'] = { + 'type': 'doughnut', + 'data': { + 'labels': chart_data['label_doughnut_prod'], + 'datasets': [{ + 'label': 'Inférieur à 3', + 'data': chart_data['doughnut_prod'], + 'backgroundColor': [ + 'rgb(155, 187, 89)', + 'rgb(192, 80, 77)', + ], + 'borderWidth': 1 + }], + }, } - return action + result['histo_chart_prod'] = { + 'type': 'bar', + 'data': { + 'labels': chart_data['label'], + 'datasets': [ + { + 'label': 'AutoConso', + 'data': chart_data['autocons'], + 'backgroundColor': 'rgba(155, 187, 89, 0.7)', + 'borderColor': 'rgba(155, 187, 89, 1)', + }, + { + 'label': 'Surplus', + 'data': chart_data['surplus'], + 'backgroundColor': 'rgba(192, 80, 77, 0.7)', + 'borderColor': 'rgba(192, 80, 77, 1)', + }] + } + } + return result - # ------------------------------------------------------ - # Business methods - # ------------------------------------------------------ def get_cdc(self, type, date_start, date_end): cdc_jour = [] - # for operation in self: + label_line_cons = [] data_autocons = [] data_cons = [] data_prod = [] data_surplus = [] + # Depending on type scale, define the date slot type # Data by day / hour for type = day # Data by week / day for type = week # Data by month / day for type = month @@ -232,6 +241,7 @@ class EnercoopOperation(models.Model): else: type_date = 'date_slot:day' + # Get all data group by curves type and date cdc_ids = self.env['enercoop.enedis.cdc'].sudo().read_group( [('enercoop_operation_id', '=', self.ids), ('date_slot', '>=', date_start), @@ -239,6 +249,7 @@ class EnercoopOperation(models.Model): ['power', 'enercoop_operation_id', 'date_slot'], ['comp_data_type', type_date], orderby='comp_data_type, date_slot ASC', lazy=False) + # Get the date for the abscissa axis cdc_date_ids = self.env['enercoop.enedis.cdc'].sudo().read_group( [('enercoop_operation_id', '=', self.ids), ('date_slot', '>=', date_start), @@ -246,10 +257,12 @@ class EnercoopOperation(models.Model): ['enercoop_operation_id', 'date_slot'], [type_date], orderby='date_slot ASC', lazy=False) + # Build the abscissa axis with the right format date for cdc in cdc_date_ids: value_hour = cdc[type_date] label_line_cons.append(value_hour) + # Build the ordinate axis for each data type (autoconso/conso/prod/surplus) for cdc in cdc_ids: if type == 'day': value_power = round((cdc['power'] / 2), 2) @@ -265,6 +278,7 @@ class EnercoopOperation(models.Model): if cdc['comp_data_type'] == 'surplus': data_surplus.append(value_power) + # Get the data to build the chart Bilan bilan_cdc = self.env['enercoop.enedis.cdc'].sudo().read_group( [('enercoop_operation_id', '=', self.ids), ('date_slot', '>=', date_start), @@ -272,6 +286,7 @@ class EnercoopOperation(models.Model): ['power', 'enercoop_operation_id'], ['comp_data_type'], orderby='comp_data_type', lazy=False) + # Build the ordinate axis for each data type (autoconso/conso/prod/surplus) for bilan in bilan_cdc: if bilan['comp_data_type'] == 'autocons': power_autocons = bilan['power'] / 1000 / 2 @@ -311,4 +326,37 @@ class EnercoopOperation(models.Model): 'doughnut_prod': [percent_autocons_prod, percent_surplus_prod] } - return cdc_jour \ No newline at end of file + return cdc_jour + + # ------------------------------------------------------ + # Functions to manage route + # ------------------------------------------------------ + def graph_view_conso(self, scale): + # Function call when load Qweb views + result_graph = {} + + # Get date start and date end depending on type of scale + date_start, date_end = self.get_last_day(scale) + + # Get the data to display in chart + chart_data = self.get_cdc(scale, date_start, date_end) + + # Build the chart depending on data calculated + result_graph = self.chart_data_cons(chart_data) + + return result_graph + + def graph_view_prod(self, scale): + # Function call when load Qweb views + result_graph = {} + + # Get date start and date end depending on type of scale + date_start, date_end = self.get_last_day(scale) + + # Get the data to display in chart + chart_data = self.get_cdc(scale, date_start, date_end) + + # Build the chart depending on data calculated + result_graph = self.chart_data_prod(chart_data) + + return result_graph \ No newline at end of file diff --git a/models/enercoop_operation_graph.py b/models/enercoop_operation_graph.py index 47d04ddc30180e0d71557221d427d7926f18a4e0..8fe5ad782259aea9f764908c0c88066e66761b31 100644 --- a/models/enercoop_operation_graph.py +++ b/models/enercoop_operation_graph.py @@ -13,6 +13,8 @@ class EnercoopOperation(models.Model): def _qweb_prepare_qcontext(self, view_id, domain): values = super()._qweb_prepare_qcontext(view_id, domain) operations = self.search(domain) + + # Prepare Values to display in qweb view values.update(operations._plan_prepare_values()) return values @@ -43,15 +45,4 @@ class EnercoopOperation(models.Model): } values['data_values'] = data_values - return values - - def plan_prepare_values(self): - - values = { - 'operations': self, - } - - date_start = datetime.strptime('2021-03-09T17:00:00', "%Y-%m-%dT%H:%M:%S") - conso_by_day = self.get_cdc_conso_day(date_start) - values['conso_by_day'] = conso_by_day - return values + return values \ No newline at end of file