def get_message_log(self, source=None, user=None, log_level=logging.INFO, since_timestamp=None): """Retrieve log messages from source at or above log_level since timestamp. If source is omitted, retrieve from all sources. If log_level is omitted, retrieve at all levels. If since_timestamp is omitted, only retrieve most recent message. """ with self.config_rlock: while True: try: logs = LogMessage.objects.filter(log_level__gte=log_level) if source: logs = logs.filter(source=source) if user: logs = logs.filter(user=user) if since_timestamp is None: message = logs.latest('timestamp') return [(message.timestamp.timestamp(), message.source, message.user, message.log_level, message.message)] else: since_datetime = datetime_obj_from_timestamp(since_timestamp) logs = logs.filter(timestamp__gt=since_datetime).order_by('timestamp') return [(message.timestamp.timestamp(), message.source, message.user, message.log_level, message.message) for message in logs] except django.db.utils.OperationalError as e: logging.warning('get_message_log() ' 'Got DjangoOperationalError. Trying again: %s', e) connection.close() time.sleep(0.1)
def get_status(self, cruise_id, since_timestamp=None): """Retrieve a dict of the most-recent status report from each logger. If since_timestamp is specified, retrieve all status reports since that time.""" status = {} def _add_lcs_to_status(lcs): """Helper function - add retrieved record to the status report.""" # May be loggers that haven't been run yet if not lcs.last_checked: return lcs_timestamp = lcs.last_checked.timestamp() # Add entry to our status report, indexed by timestamp if not lcs_timestamp in status: status[lcs_timestamp] = {} id = cruise_id + ID_SEPARATOR + lcs.logger.name status[lcs_timestamp][id] = { 'config':lcs.config.name, 'running':lcs.running, 'failed':lcs.failed, 'pid':lcs.pid, 'errors':lcs.errors.split('\n') } if since_timestamp is None: # We just want the latest config state message from each logger for logger in Logger.objects.filter(cruise__id=cruise_id): try: lcs = LoggerConfigState.objects.filter( logger=logger).latest('last_checked') _add_lcs_to_status(lcs) except LoggerConfigState.DoesNotExist: continue else: # We want all status updates since specified timestamp since_datetime = datetime_obj_from_timestamp(since_timestamp) for lcs in LoggerConfigState.objects.filter( logger__cruise_id=cruise_id, last_checked__gt=since_datetime): _add_lcs_to_status(lcs) return status
def get_message_log(self, source=None, user=None, log_level=logging.INFO, since_timestamp=None): """Retrieve log messages from source at or above log_level since timestamp. If source is omitted, retrieve from all sources. If log_level is omitted, retrieve at all levels. If since_timestamp is omitted, only retrieve most recent message. """ logs = LogMessage.objects.filter(log_level__gte=log_level) if source is not None: logs = logs.filter(source=source) if user is not None: logs = logs.filter(user=user) if since_timestamp is None: message = logs.latest('timestamp') return [(message.timestamp.timestamp(), message.source, message.user, message.log_level, message.message)] else: since_datetime = datetime_obj_from_timestamp(since_timestamp) logs = logs.filter(timestamp__gt=since_datetime).order_by('timestamp') return [(message.timestamp.timestamp(), message.source, message.user, message.log_level, message.message) for message in logs]
def get_status(self, since_timestamp=None): """Retrieve a dict of the most-recent status report from each logger. If since_timestamp is specified, retrieve all status reports since that time.""" with self.config_rlock: status = {} while True: try: if since_timestamp is None: # If they just want the latest status and our cache is good, # return it. last_update = self._last_config_update_time() if (self.retrieved_status and self.retrieved_status_time >= last_update ) and None: logging.debug('Returning cached status') return self.retrieved_status # If here, cache was suspect - retrieve fresh logging.debug('Cache is stale, retrieving status') self.retrieved_status_time = time.time() for logger in Logger.objects.all(): try: lcs = LoggerConfigState.objects.filter( logger=logger).latest('last_checked') if not lcs.last_checked: continue lcs_timestamp = lcs.last_checked.timestamp() # Add entry to our status report, indexed by timestamp if not lcs_timestamp in status: status[lcs_timestamp] = {} id = lcs.logger.name status[lcs_timestamp][id] = { 'config': lcs.config.name if lcs.config else None, 'running': lcs.running, 'failed': lcs.failed, 'pid': lcs.pid, 'errors': lcs.errors.split('\n') } except (KeyError, Logger.DoesNotExist, LoggerConfig.DoesNotExist, LoggerConfigState.DoesNotExist): logging.warning('no LoggerConfigState for %s', logger) continue self.retrieved_status = status return status # If here, we want all status updates since specified timestamp since_datetime = datetime_obj_from_timestamp( since_timestamp) for lcs in LoggerConfigState.objects.filter( last_checked__gt=since_datetime): _add_lcs_to_status(lcs) return status except django.db.utils.OperationalError as e: logging.warning( 'set_active_logger_config() ' 'Got DjangoOperationalError. Trying again: %s', e) connection.close() time.sleep(0.1)