def handle_request(self, request, token): """ :param SetKeyRequest request: :param Token token: :rtype: bool """ self.set_key(token.subject, request.gsrn, request.key) logger.info(f'Extended key set for MeteringPoint: {request.gsrn}', extra={ 'gsrn': request.gsrn, 'subject': token.subject, }) start_import_measurements_pipeline_for(token.subject, request.gsrn) return True
def import_meteringpoints(self, sub, session): """ Imports all meteringpoints for the subject from ElOverblik. :param str sub: :param sqlalchemy.orm.Session session: :rtype: list[MeteringPoint] """ logger.info(f'Importing MeteringPoints from ElOverblik', extra={ 'subject': sub, }) # Import MeteringPoints from ElOverblik imported_meteringpoints = eloverblik_service.get_meteringpoints( scope=e.Scope.CustomerKey, identifier=sub, ) # Filter out GSRN numbers that already exists filtered_meteringpoints = (mp for mp in imported_meteringpoints if not self.gsrn_exists(mp.gsrn, session)) # Map to MeteringPoint objects mapped_meteringpoints = list( map( partial(self.map_imported_meteringpoint, sub), filtered_meteringpoints, )) # Insert to database session.add_all(mapped_meteringpoints) session.flush() logger.info( f'Imported {len(mapped_meteringpoints)} MeteringPoints from ElOverblik', extra={ 'subject': sub, 'gsrn': ', '.join(m.gsrn for m in mapped_meteringpoints), }) return mapped_meteringpoints
def handle_request(self, request, token): """ :param SubscribeRequest request: :param Token token: :rtype: bool """ self.service.subscribe( event=self.event, subject=token.subject, url=request.url, secret=request.secret, ) logger.info(f'Webhook subscribed: {self.event.value}', extra={ 'subject': token.subject, 'event': self.event.value, 'url': request.url, }) return True
def import_measurements_for(self, meteringpoint, session): """ :param MeteringPoint meteringpoint: :param sqlalchemy.orm.Session session: :rtype: list[Measurement] """ begin = self.get_begin(meteringpoint, session) end = self.get_end() measurements = self.get_measurements(meteringpoint, begin, end, session) # Save measurements to database session.add_all(measurements) # Issue GGOs if necessary if (meteringpoint.is_producer() and meteringpoint.technology_code and meteringpoint.fuel_code): emissions = self.get_emissions(meteringpoint.gsrn) session.add_all((self.issue_ggo_for(measurement, emissions) for measurement in measurements if measurement.amount > 0)) session.flush() logger.info( f'Imported {len(measurements)} measurements from ElOverblik for GSRN: {meteringpoint.gsrn}', extra={ 'subject': meteringpoint.sub, 'gsrn': meteringpoint.gsrn, 'type': meteringpoint.type.value, 'begin': str(begin), 'end': str(end), }) return measurements