-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsom_indexada_webforms_helpers.py
383 lines (332 loc) · 14.9 KB
/
som_indexada_webforms_helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# -*- coding: utf-8 -*-
from osv import osv
from som_polissa.exceptions import exceptions
from www_som.helpers import www_entry_point
import json
from datetime import datetime, timedelta
import pytz
SUBSYSTEMS = [
'PENINSULA',
'BALEARES',
'CANARIAS',
]
class SomIndexadaWebformsHelpers(osv.osv_memory):
_name = "som.indexada.webforms.helpers"
def get_k_from_pricelist(self, cursor, uid, pricelist_id):
pricelist_obj = self.pool.get("product.pricelist")
pricelist = pricelist_obj.browse(cursor, uid, pricelist_id)
today = datetime.today().strftime("%Y-%m-%d")
vlp = None
coefficient_k = None
for lp in pricelist.version_id:
if lp.date_start <= today and (not lp.date_end or lp.date_end >= today):
vlp = lp
break
if vlp:
for item in vlp.items_id:
if item.name == "Coeficient K":
coefficient_k = item.base_price
break
if coefficient_k is not None:
return coefficient_k
else:
raise exceptions.KCoefficientNotFound(pricelist_id)
def _get_change_type(self, cursor, uid, polissa_id):
change_type = "from_period_to_index"
cfg_obj = self.pool.get("res.config")
# 'flag_change_tariff_switch' enables change tariff switching.
# If value == 0, just change from period to index is available
flag_change_tariff_switch = int(
cfg_obj.get(cursor, uid, "som_flag_change_tariff_switch", "0")
)
if flag_change_tariff_switch:
polissa_obj = self.pool.get("giscedata.polissa")
polissa = polissa_obj.browse(cursor, uid, polissa_id)
if polissa.mode_facturacio == "index":
change_type = "from_index_to_period"
return change_type
@www_entry_point(
expected_exceptions=exceptions.SomPolissaException,
)
def check_new_pricelist_www(self, cursor, uid, polissa_id, context=None):
if context is None:
context = {}
tariff_name = context.get("tariff_name", "name")
change_type = self._get_change_type(cursor, uid, polissa_id)
polissa_obj = self.pool.get("giscedata.polissa")
pricelist_obj = self.pool.get("product.pricelist")
polissa = polissa_obj.browse(cursor, uid, polissa_id)
wiz_o = self.pool.get("wizard.change.to.indexada")
wiz_o.validate_polissa_can_change(
cursor,
uid,
polissa,
change_type,
only_standard_prices=True,
)
pricelist_id = wiz_o.calculate_new_pricelist(
cursor,
uid,
polissa,
change_type,
context=context,
)
pricelist_name = pricelist_obj.read(
cursor,
uid,
pricelist_id,
[tariff_name],
)[tariff_name]
coefficient_k = (
self.get_k_from_pricelist(cursor, uid, pricelist_id)
if change_type == "from_period_to_index"
else None
)
return {
"tariff_name": pricelist_name,
"k_coefficient_eurkwh": coefficient_k,
}
def change_to_indexada_www(self, cursor, uid, polissa_id, context=None):
"""DEPRECATED: use change_pricelist_www instead"""
return self.change_pricelist_www(cursor, uid, polissa_id, context)
@www_entry_point(
expected_exceptions=exceptions.SomPolissaException,
)
def change_pricelist_www(self, cursor, uid, polissa_id, context=None):
change_type = self._get_change_type(cursor, uid, polissa_id)
wiz_o = self.pool.get("wizard.change.to.indexada")
context = {
"active_id": polissa_id,
"change_type": change_type,
"webapps": True,
}
wiz_id = wiz_o.create(cursor, uid, {}, context=context)
return wiz_o.change_to_indexada(
cursor,
uid,
[wiz_id],
context=context,
)
def has_indexada_prova_pilot_category_www(self, cursor, uid, polissa_id):
polissa_obj = self.pool.get("giscedata.polissa")
polissa_categories = polissa_obj.read(
cursor,
uid,
polissa_id,
["category_id"],
)
imd_obj = self.pool.get("ir.model.data")
prova_pilot_cat = imd_obj._get_obj(
cursor,
uid,
"som_indexada",
"category_indexada_prova_pilot",
)
if prova_pilot_cat.id in polissa_categories["category_id"]:
return True
return False
def validate_parameters(self, cursor, uid, geo_zone, first_date, last_date, tariff=None):
if geo_zone not in SUBSYSTEMS:
raise exceptions.InvalidSubsystem(geo_zone)
tariff_obj = self.pool.get('giscedata.polissa.tarifa')
if tariff and not tariff_obj.search(cursor, uid, [('name', '=', tariff)]):
raise exceptions.TariffNonExists(tariff)
if first_date is None or last_date is None or \
(first_date is not None and last_date is not None and last_date < first_date):
raise exceptions.InvalidDates(first_date, last_date)
def initial_final_times(self, first_date, last_date):
initial_time = (datetime.strptime(first_date, '%Y-%m-%d')
+ timedelta(hours=1)).strftime("%Y-%m-%d %H:%M:%S")
final_time = (datetime.strptime(last_date, '%Y-%m-%d')
+ timedelta(days=1)).strftime("%Y-%m-%d %H:%M:%S")
return initial_time, final_time
def toUtcTime(self, geom_zone, initial_time, final_time):
localtimezone = pytz.timezone('Europe/Madrid')
winter_offset = '1 HOUR'
summer_offset = '2 HOUR'
if geom_zone == 'CANARIAS':
localtimezone = pytz.timezone('Atlantic/Canary')
winter_offset = '0 HOUR'
summer_offset = '1 HOUR'
initial_time_str = datetime.strptime(initial_time, "%Y-%m-%d %H:%M:%S")
final_time_str = datetime.strptime(final_time, "%Y-%m-%d %H:%M:%S")
first_timestamp_utc = localtimezone.normalize(
localtimezone.localize(initial_time_str, is_dst=True))
last_timestamp_utc = localtimezone.normalize(
localtimezone.localize(final_time_str, is_dst=True))
return first_timestamp_utc, last_timestamp_utc, winter_offset, summer_offset
def _get_prices(self, cursor, geom_zone, winter_offset, summer_offset, tariff_id,
first_timestamp_utc, last_timestamp_utc):
"""
SQL query breakdown:
1. Common Table Expressions (CTEs):
* `filtered_data`: Filters data from `giscedata_next_days_energy_price`
based on specified criteria
(geom_zone, tarifa_id if asked, first_timestamp_utc, and last_timestamp_utc).
* `filled_data`: Generates a series of timestamps between `first_timestamp_utc`
and `last_timestamp_utc`, and fills in NULL values for `geom_zone`, `maturity`,
`tarifa_id`, `initial_price` and `prm_diari` where there are gaps in data.
* `final_data`: Joins the filtered and filled data, ensuring no gaps exist.
* `ranked_data`: Assigns a rank to each record based on the maturity level.
2. Main Query:
* Selects aggregated data as a JSON object.
* Aggregates data by `timestamp`.
* Includes the following information in the JSON object:
* `geo_zone`: `PENINSULA', `CANARIAS` or `BALEARES'.
* `tariff_id`: tariff id.
* `first_timestamp_utc`: Start timestamp parameter.
* `last_timestamp_utc`: End timestamp parameter.
* `initial_price`: Array of actual initial_price, ordered by timestamp.
* `prm_diari`: Array of actual prm_diari, ordered by timestamp.
* `maturity`: Array of maturity levels, ordered by timestamp.
3. Final Filtering:
* Filters the results to only include records where qwith the highest
id for each timestamp.
"""
cursor.execute(
'''
WITH filtered_data AS (
SELECT
CASE
WHEN season = 'W' THEN hour_timestamp - INTERVAL %(winter_offset)s
WHEN season = 'S' THEN hour_timestamp - INTERVAL %(summer_offset)s
ELSE NULL
END AT TIME ZONE 'UTC' AS hourtimestamp,
geom_zone,
tarifa_id,
CASE
WHEN %(tariff_id)s IS NULL THEN prm_diari / 1000
ELSE initial_price
END AS price,
maturity,
id
FROM
giscedata_next_days_energy_price
WHERE
geom_zone = %(geom_zone)s
AND (%(tariff_id)s IS NULL OR tarifa_id = %(tariff_id)s)
AND CASE
WHEN season = 'W' THEN hour_timestamp - INTERVAL %(winter_offset)s
WHEN season = 'S' THEN hour_timestamp - INTERVAL %(summer_offset)s
ELSE NULL
END AT TIME ZONE 'UTC'
BETWEEN %(first_timestamp_utc)s AND %(last_timestamp_utc)s
),
filled_data AS (
SELECT
generate_series AS hourtimestamp,
NULL AS geom_zone,
NULL AS tarifa_id,
NULL AS price,
NULL AS maturity,
NULL AS id
FROM
generate_series(
%(first_timestamp_utc)s,
%(last_timestamp_utc)s,
INTERVAL '1 HOUR'
) generate_series
LEFT JOIN
filtered_data fd ON generate_series.generate_series = fd.hourtimestamp
WHERE
fd.hourtimestamp IS NULL
),
final_data AS (
SELECT
COALESCE(fd.hourtimestamp, fd2.hourtimestamp) AS hourtimestamp,
COALESCE(fd.geom_zone, NULL) AS geom_zone,
COALESCE(fd.tarifa_id, NULL) AS tarifa_id,
COALESCE(fd.price, NULL) AS price,
COALESCE(fd.maturity, NULL) AS maturity,
COALESCE(fd.id, NULL) AS id
FROM
filtered_data fd
FULL JOIN
filled_data fd2 ON fd.hourtimestamp = fd2.hourtimestamp
),
collapsed_data AS (
SELECT DISTINCT ON(hourtimestamp)
*
FROM
final_data
ORDER BY hourtimestamp ASC, id DESC
)
SELECT
JSON_BUILD_OBJECT(
'first_date', %(first_timestamp_utc)s,
'last_date', %(last_timestamp_utc)s,
'geo_zone', %(geom_zone)s,
'tariff_id', %(tariff_id)s,
'prices', COALESCE(ARRAY_AGG(price
ORDER BY hourtimestamp ASC),
ARRAY[]::numeric[]),
'maturity', COALESCE(ARRAY_AGG(maturity
ORDER BY hourtimestamp ASC), ARRAY[]::text[])
) AS data
FROM
collapsed_data
''',
{
'geom_zone': geom_zone,
'winter_offset': winter_offset,
'summer_offset': summer_offset,
'tariff_id': tariff_id,
'first_timestamp_utc': first_timestamp_utc,
'last_timestamp_utc': last_timestamp_utc
}
)
return cursor.fetchall()
@www_entry_point(
expected_exceptions=exceptions.SomPolissaException,
)
def get_indexed_prices(
self, cursor, uid, geo_zone, tariff, first_date, last_date, context=None
):
self.validate_parameters(cursor, uid, geo_zone, first_date, last_date, tariff)
tariff_obj = self.pool.get('giscedata.polissa.tarifa')
tariff_id = tariff_obj.search(cursor, uid, [('name', '=', tariff)])
initial_time, final_time = self.initial_final_times(first_date, last_date)
first_timestamp_utc, last_timestamp_utc, winter_offset, summer_offset = self.toUtcTime(
geo_zone, initial_time, final_time)
curves_data = self._get_prices(
cursor, geo_zone, winter_offset, summer_offset, tariff_id[0],
first_timestamp_utc, last_timestamp_utc)[0][0]
keys_to_return = ['first_date', 'last_date', 'geo_zone', 'prices', 'maturity']
filtered_data = {k: v for k, v in curves_data.items() if k in keys_to_return}
json_prices = json.dumps(dict(
first_date=initial_time,
last_date=final_time,
curves=dict(
tariff=tariff,
geo_zone=filtered_data['geo_zone'],
price_euros_kwh=filtered_data['prices'],
maturity=filtered_data['maturity']
))
)
return json_prices
@www_entry_point(
expected_exceptions=exceptions.SomPolissaException,
)
def get_compensation_prices(
self, cursor, uid, geo_zone, first_date, last_date, context=None
):
self.validate_parameters(cursor, uid, geo_zone, first_date, last_date, tariff=None)
initial_time, final_time = self.initial_final_times(first_date, last_date)
first_timestamp_utc, last_timestamp_utc, winter_offset, summer_offset = self.toUtcTime(
geo_zone, initial_time, final_time)
curves_data = self._get_prices(
cursor, geo_zone, winter_offset, summer_offset, None,
first_timestamp_utc, last_timestamp_utc)[0][0]
keys_to_return = ['first_date', 'last_date', 'geo_zone', 'prices', 'maturity']
filtered_data = {k: v for k, v in curves_data.items() if k in keys_to_return}
json_prices = json.dumps(dict(
first_date=initial_time,
last_date=final_time,
curves=dict(
geo_zone=filtered_data['geo_zone'],
compensation_euros_kwh=filtered_data['prices'],
maturity=filtered_data['maturity']
))
)
return json_prices
SomIndexadaWebformsHelpers()