Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion esios/archives.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def get(self, start_date, end_date, taxonomy_terms=None):
date_type = 'datos'
start_date = start_date.isoformat()
end_date = end_date.isoformat()
locale = 'en'
locale = 'es'
param_list = ('locale', 'start_date', 'end_date', 'date_type')
if taxonomy_terms:
param_list += ('taxonomy_terms',)
Expand Down
23 changes: 20 additions & 3 deletions esios/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,19 @@ def parse_cierre(self, content, program_unit):

# SeriesTemporales Node
st = e[0].getparent()

# UPEntrada/UPSalida
up_entrada_salida = st[2]
if 'UPEntrada' in up_entrada_salida.tag:
tipo_unidad = 'V'
else:
tipo_unidad = 'C'

# UnidadMedida
up_unidad_medida = st[3]
if 'UnidadMedida' in up_unidad_medida.tag:
unidad_medida = up_unidad_medida.get('v')

# Periodo
periodo = st[4]
# Curve:
Expand All @@ -117,14 +130,15 @@ def parse_cierre(self, content, program_unit):
value = e.get('v')
start_str, end_str = value.split('/')
start = UTC_TZ.localize(datetime.strptime(start_str, '%Y-%m-%dT%H:%MZ'))
end = UTC_TZ.localize(datetime.strptime(end_str, '%Y-%m-%dT%H:%MZ'))
# print("P48 for UP {} from {} to {}".format(program_unit, start, end))
elif 'Resolucion' in e.tag:
integrity = 'p' if e.get('v') == 'PT60M' else 'p4'
minutes = 60 if integrity == 'p' else 15
elif 'Intervalo' not in e.tag:
continue
else:
hour = int(e[0].get('v'))
value = float(e[1].get('v'))
utc_timestamp = start + relativedelta(hours=hour)
utc_timestamp = start + relativedelta(minutes=minutes*hour)
local_timestamp = LOCAL_TZ.normalize(utc_timestamp.astimezone(LOCAL_TZ))
curve.append({
'up': program_unit,
Expand All @@ -133,6 +147,9 @@ def parse_cierre(self, content, program_unit):
'utc_timestamp': utc_timestamp,
'local_timestamp': local_timestamp,
'cierre': is_cierre,
'type': integrity,
'tipo_unidad': tipo_unidad,
'unidad_medida': unidad_medida
})

return curve
Expand Down
37 changes: 26 additions & 11 deletions spec/parsers_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,21 @@ def validate_json(result):
expect(local_ts).to(equal(expected_local_ts))


def validate_data(result, start, end, cierre=None):
def validate_data(result, start, end, cierre=None, qh=True):
data = json.loads(result)

target_data = [x for x in data
if start <= LOCAL_TZ.normalize(LOCAL_TZ.localize(
datetime.strptime(x['local_timestamp'][:18], '%Y-%m-%d %H:%M:%S'))) <= end]

max_date = max([d['local_timestamp'] for d in target_data])
min_date = min([d['local_timestamp'] for d in target_data])

hours = int(((end - start).total_seconds() / 3600) + 1)
expect(len(target_data)).to(be_above_or_equal(hours)) # can include True and False values for "cierre" field
if qh:
qhs = int((end - start).total_seconds() / 3600 * 4) + 1
expect(len(target_data)).to(be_above_or_equal(qhs)) # can include True and False values for "cierre" field
else:
hours = int(((end - start).total_seconds() / 3600) + 1)
expect(len(target_data)).to(be_above_or_equal(hours)) # can include True and False values for "cierre" field

expect(min_date).to(equal(str(start)))
expect(max_date).to(equal(str(end)))
Expand Down Expand Up @@ -80,16 +83,16 @@ def validate_data(result, start, end, cierre=None):

today = datetime.now()
start = LOCAL_TZ.localize(
today.replace(hour=0, minute=0, second=0, microsecond=0) - relativedelta(days=1)
today.replace(hour=0, minute=0, second=0, microsecond=0) - relativedelta(days=2)
)
end = LOCAL_TZ.localize(
today.replace(hour=23, minute=59, second=59, microsecond=0)
today.replace(hour=23, minute=59, second=59, microsecond=0) - relativedelta(days=1)
)

result = parser.get_data_json('SOMEC01', start, end)

validate_json(result)
validate_data(result, start + relativedelta(hours=1), end + relativedelta(seconds=1))
validate_data(result, start + relativedelta(minutes=15), end + relativedelta(seconds=1))

with context('parses local files'):

Expand Down Expand Up @@ -130,7 +133,10 @@ def validate_data(result, start, end, cierre=None):

validate_json(result)
validate_data(
result, LOCAL_TZ.localize(datetime(2020, 9, 15, 1, 0)), LOCAL_TZ.localize(datetime(2020, 9, 16, 0, 0), True)
result,
LOCAL_TZ.localize(datetime(2020, 9, 15, 1, 0)),
LOCAL_TZ.localize(datetime(2020, 9, 16, 0, 0), True),
qh=False
)


Expand All @@ -140,7 +146,10 @@ def validate_data(result, start, end, cierre=None):

validate_json(result)
validate_data(
result, LOCAL_TZ.localize(datetime(2020, 9, 17, 1, 0)), LOCAL_TZ.localize(datetime(2020, 9, 18, 0, 0), False)
result,
LOCAL_TZ.localize(datetime(2020, 9, 17, 1, 0)),
LOCAL_TZ.localize(datetime(2020, 9, 18, 0, 0), False),
qh=False
)

with it('gets 25 registers for a p48cierre xml file from October saving time day'):
Expand All @@ -149,7 +158,10 @@ def validate_data(result, start, end, cierre=None):

validate_json(result)
validate_data(
result, LOCAL_TZ.localize(datetime(2019, 10, 27, 1, 0)), LOCAL_TZ.localize(datetime(2019, 10, 28, 0, 0)), True
result,
LOCAL_TZ.localize(datetime(2019, 10, 27, 1, 0)),
LOCAL_TZ.localize(datetime(2019, 10, 28, 0, 0)), True,
qh=False
)
data = json.loads(result)
expect(len(data)).to(equal(25))
Expand All @@ -160,7 +172,10 @@ def validate_data(result, start, end, cierre=None):

validate_json(result)
validate_data(
result, LOCAL_TZ.localize(datetime(2020, 3, 29, 1, 0)), LOCAL_TZ.localize(datetime(2020, 3, 30, 0, 0)), True
result,
LOCAL_TZ.localize(datetime(2020, 3, 29, 1, 0)),
LOCAL_TZ.localize(datetime(2020, 3, 30, 0, 0)), True,
qh=False
)

data = json.loads(result)
Expand Down