def blacklist_update(self): """ Black list update (change expires to new value in existing blacklist event). """ # add event records from RecordDict for event_record in self.record_dict.iter_db_items(): self.records['event'].append(event_record) self.json_to_record(self.records['event']) id_event = self.records['event'][0]["id"] expires = self.records['event'][0]["expires"] LOGGER.debug("ID: %r NEW_EXPIRES: %r", id_event, expires) with transact: rec_count = (self.session_db.query(n6NormalizedData). filter(n6NormalizedData.id == id_event). update({'expires': expires, 'modified': datetime.datetime.utcnow().replace(microsecond=0), })) if rec_count: LOGGER.debug("records with the same id %r exist: %r", id_event, rec_count) else: items = [] for record in self.records['event']: record["status"] = "active" event = n6NormalizedData(**record) items.append(event) for record in self.records['client']: client = n6ClientToEvent(**record) items.append(client) LOGGER.debug("bl-update, records with id %r DO NOT EXIST!", id_event) LOGGER.debug("insert new events,::count:: %r", len(items)) self.insert_new_event(items, with_transact=False)
def suppressed_update(self): """ Agregated event update(change fields: until and count, to the value of suppressed event). """ LOGGER.debug('* suppressed_update() %a', self.record_dict) # add event records from RecordDict for event_record in self.record_dict.iter_db_items(): self.records['event'].append(event_record) self.json_to_record(self.records['event']) id_event = self.records['event'][0]["id"] until = self.records['event'][0]["until"] count = self.records['event'][0]["count"] # optimization: we can limit time => searching within one partition, not all; # it seems that mysql (and/or sqlalchemy?) truncates times to seconds, # we are also not 100% sure if other time data micro-distortions are not done # -- that's why here we use a 1-second-range instead of an exact value first_time_min = parse_iso_datetime_to_utc( self.record_dict["_first_time"]).replace(microsecond=0) first_time_max = first_time_min + datetime.timedelta(days=0, seconds=1) with transact: rec_count = (self.session_db.query(n6NormalizedData).filter( n6NormalizedData.time >= first_time_min, n6NormalizedData.time <= first_time_max, n6NormalizedData.id == id_event).update({ 'until': until, 'count': count })) if rec_count: LOGGER.debug("records with the same id %a exist: %a", id_event, rec_count) else: items = [] for record in self.records['event']: event = n6NormalizedData(**record) items.append(event) for record in self.records['client']: client = n6ClientToEvent(**record) items.append(client) LOGGER.warning( "suppressed_update, records with id %a DO NOT EXIST!", id_event) LOGGER.debug("insert new events,,::count:: %a", len(items)) self.insert_new_event(items, with_transact=False)
def blacklist_change(self): """ Black list change(change status to replaced in existing blacklist event, and add new event in changing values(new id, and old replaces give comparator)). """ # add event records from RecordDict for event_record in self.record_dict.iter_db_items(): self.records['event'].append(event_record) self.json_to_record(self.records['event']) id_db = self.records['event'][0]["id"] id_replaces = self.records['event'][0]["replaces"] LOGGER.debug("ID: %a REPLACES: %a", id_db, id_replaces) try: with transact: rec_count = (self.session_db.query(n6NormalizedData).filter( n6NormalizedData.id == id_replaces).update({ 'status': 'replaced', 'modified': datetime.datetime.utcnow().replace(microsecond=0) })) with transact: items = [] for record in self.records['event']: record["status"] = "active" event = n6NormalizedData(**record) items.append(event) for record in self.records['client']: client = n6ClientToEvent(**record) items.append(client) if rec_count: LOGGER.debug("insert new events, count.: %a", len(items)) else: LOGGER.debug("bl-change, records with id %a DO NOT EXIST!", id_replaces) LOGGER.debug("inserting new events anyway, count.: %a", len(items)) self.insert_new_event(items, with_transact=False, recorded=True) except IntegrityError as exc: LOGGER.warning("IntegrityError: %a", exc)
def new_event(self, _is_blacklist=False): """ Add new event to n6 database. """ LOGGER.debug('* new_event() %r', self.record_dict) # add event records from RecordDict for event_record in self.record_dict.iter_db_items(): if _is_blacklist: event_record["status"] = "active" self.records['event'].append(event_record) self.json_to_record(self.records['event']) items = [] for record in self.records['event']: event = n6NormalizedData(**record) items.append(event) for record in self.records['client']: client = n6ClientToEvent(**record) items.append(client) LOGGER.debug("insert new events, count.: %r", len(items)) self.insert_new_event(items, recorded=True)