def test_json_facturas(self): t_0, t_f = '2016-01-01', '2017-01-09' f1 = FacturaElec(t_0, t_f, consumo=[500, 500], tipo_peaje=TIPO_PEAJE_NOC) print(json.dumps(f1.to_dict(include_text_repr=True, include_html_repr=True))) t_0, t_f = '2016-11-01', '2016-12-09' f1 = FacturaElec(t_0, t_f, consumo=[500, 500], tipo_peaje=TIPO_PEAJE_NOC) print(json.dumps(f1.to_dict(include_text_repr=True, include_html_repr=True))) t_0, t_f = '2016-11-01', '2016-12-09' f1 = FacturaElec(t_0, t_f, tipo_peaje=TIPO_PEAJE_NOC) print(json.dumps(f1.to_dict(include_text_repr=True, include_html_repr=True))) t_0, t_f = '2016-01-01', '2017-01-09' f1 = FacturaElec(t_0, t_f, tipo_peaje=TIPO_PEAJE_NOC) print(json.dumps(f1.to_dict(include_text_repr=True, include_html_repr=True)))
def _gen_stream_data_factura(start=None, end=None, **kwargs_factura): tic = time() if start is None: # Inicio de mes actual hasta instante actual start = dt.datetime.now(tz=SENSORS.TZ).replace(day=1, hour=0, minute=0, second=0, microsecond=0) end = dt.datetime.now(tz=SENSORS.TZ) cat = enerpi_data_catalog(check_integrity=False) df = cat.get_summary(start=start, end=end) toc_df = time() if (df is not None) and not df.empty and ('kWh' in df): consumption = df['kWh'] # Fix timezone (ya en esiosdata) # try: # consumption.index = consumption.index.tz_localize(SENSORS.TZ, ambiguous='infer') # except AmbiguousTimeError as e: # consumption.index = consumption.index.tz_localize(SENSORS.TZ, ambiguous='NaT') # consumption = consumption.reindex(DatetimeIndex(start=consumption.index[0], end=consumption.index[-1], # freq='1h', tz=SENSORS.TZ)).interpolate() # log('AmbiguousTimeError ({}) en elec_bill. Se reindexa e interpola el índice.'.format(e), 'error') factura = FacturaElec(consumo=consumption, **kwargs_factura) data_factura = factura.to_dict(include_text_repr=True, include_html_repr=True) toc_p = time() msg = 'Factura generada en {:.3f} s; datos en {:.3f} s.'.format( toc_p - toc_df, toc_df - tic) log(msg, 'debug', False) log( 'stream_data_factura: STREAM BILL from "{}" to "{}"'.format( start, end), 'debug', False) yield _format_event_stream( dict(success=True, factura=data_factura, took=round(toc_p - tic, 3), took_df=round(toc_df - tic, 3))) elif df is not None: factura = FacturaElec(start, end, **kwargs_factura) data_factura = factura.to_dict(include_text_repr=True, include_html_repr=True) toc_p = time() msg = 'Factura vacía generada en {:.3f} s; datos en {:.3f} s.'.format( toc_p - toc_df, toc_df - tic) log(msg, 'debug', False) log( 'stream_data_factura: STREAM EMPTY BILL from "{}" to "{}"'.format( start, end), 'debug', False) yield _format_event_stream( dict(success=True, factura=data_factura, error=msg, took=round(toc_p - tic, 3), took_df=round(toc_df - tic, 3))) else: msg = 'No data from {} to {}. CATALOG:\n{}'.format(start, end, cat) log(msg, 'debug', False) log( 'stream_data_factura: STREAM ERR NO DATA from "{}" to "{}"'.format( start, end), 'debug', False) yield _format_event_stream( dict(success=False, error=msg, took=round(time() - tic, 3), took_df=round(toc_df - tic, 3))) log( 'CLOSING stream_data_factura from "{}" to "{}" with args={}'.format( start, end, kwargs_factura), 'debug', False) yield _format_event_stream('CLOSE')