def get_metrics(registration_id): # Collect and return the following metrics: # - Current number of containers running for this registration # - Average CPU usage for this registration # - Average memory usage for this registration # - Average processing time for this registration # - Number of events per day for the last week result = {} registration = Registration.get(Registration.id == registration_id) stats = Metric.select( Metric.name, fn.AVG(Metric.value).alias('average') ).where( Metric.topic == registration.topic, Metric.container == registration.container ).group_by(Metric.name) # Add to the result object for stat in stats: result[stat.name] = stat.average # Use the beginning of the day as a starting point (for full # days) today_dt = datetime.datetime.combine( datetime.date.today(), datetime.time.min ) today = int(time.mktime(today_dt.timetuple()) * 1000) # Get the starting point (one week ago) one_week_ago = today - (7 * MILLISECONDS_IN_DAY) events = Event.select( # In order to query by date intervals, we need to divide by one day # worth of milliseconds, round to the nearest whole number, then # multiply the milliseconds back in. This will create intervals and # normalize the returned values. (fn.ROUND(Event.timestamp/MILLISECONDS_IN_DAY) * MILLISECONDS_IN_DAY).alias('date'), fn.COUNT(Event.id).alias('events') ).where( Event.topic == registration.topic, Event.container == registration.container, Event.timestamp >= one_week_ago ).group_by('date') # Add to the result object result['events'] = {} for event in events: result['events'][int(event.date)] = event.events avg_duration = Event.select(fn.AVG(Event.duration)).where( Event.topic == registration.topic, Event.container == registration.container ).scalar() result['duration'] = avg_duration result['containers'] = docker_agent.container_count(registration.topic, registration.container) return result
def __clean_container(self, container_id): if container_id in self.id_to_metadata: meta = self.id_to_metadata[container_id] Event.create(topic=meta['topic'], container=meta['container'], timestamp=meta['timestamp'], duration=(int(time() * 1000) - meta['timestamp'])) del self.id_to_metadata[container_id] if container_id in self.id_to_cpu: del self.id_to_cpu[container_id]
def get_metrics(registration_id): # Collect and return the following metrics: # - Current number of containers running for this registration # - Average CPU usage for this registration # - Average memory usage for this registration # - Average processing time for this registration # - Number of events per day for the last week result = {} registration = Registration.get(Registration.id == registration_id) stats = Metric.select( Metric.name, fn.AVG(Metric.value).alias('average')).where( Metric.topic == registration.topic, Metric.container == registration.container).group_by(Metric.name) # Add to the result object for stat in stats: result[stat.name] = stat.average # Use the beginning of the day as a starting point (for full # days) today_dt = datetime.datetime.combine(datetime.date.today(), datetime.time.min) today = int(time.mktime(today_dt.timetuple()) * 1000) # Get the starting point (one week ago) one_week_ago = today - (7 * MILLISECONDS_IN_DAY) events = Event.select( # In order to query by date intervals, we need to divide by one day # worth of milliseconds, round to the nearest whole number, then # multiply the milliseconds back in. This will create intervals and # normalize the returned values. (fn.ROUND(Event.timestamp / MILLISECONDS_IN_DAY) * MILLISECONDS_IN_DAY ).alias('date'), fn.COUNT(Event.id).alias('events')).where( Event.topic == registration.topic, Event.container == registration.container, Event.timestamp >= one_week_ago).group_by('date') # Add to the result object result['events'] = {} for event in events: result['events'][int(event.date)] = event.events avg_duration = Event.select(fn.AVG(Event.duration)).where( Event.topic == registration.topic, Event.container == registration.container).scalar() result['duration'] = avg_duration result['containers'] = docker_agent.container_count( registration.topic, registration.container) return result
def __clean_container(self, container_id): if container_id in self.id_to_metadata: meta = self.id_to_metadata[container_id] Event.create( topic=meta["topic"], container=meta["container"], timestamp=meta["timestamp"], duration=(int(time() * 1000) - meta["timestamp"]), ) del self.id_to_metadata[container_id] if container_id in self.id_to_cpu: del self.id_to_cpu[container_id]