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 get_registration(registration_id): try: registration = Registration.get(Registration.id == registration_id) return registration._data except Registration.DoesNotExist: return create_error(status_code=404, message=('A registration with the ID %s does not ' 'exist') % (registration_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 delete_registration(registration_id, user): try: registration = Registration.get(Registration.id == registration_id, Registration.creator == user) id = registration.id registration.delete_instance() return {'id': id} except Registration.DoesNotExist: return create_error(status_code=404, message=('A registration with the ID %s does not ' ' exist') % (registration_id))