Ejemplo n.º 1
0
def create_registration(user):
    registration_dict = request.json
    registration_dict['creator'] = user

    new_registration = Registration(**registration_dict)
    try:
        new_registration.save()
        return {'id': new_registration.id}
    except IntegrityError:
        return create_error(status_code=400,
                            message=('A registration for topic %s with '
                                     'container %s already exists') %
                            (registration_dict['topic'],
                             registration_dict['container']))
Ejemplo n.º 2
0
    def test_Orchestrator_with_threshold(self):
        Registration.create(
            topic="MyTopic1",
            container="busybox",
            creator="me",
            threshold=1,
            timeout=15
        )

        inboxer = Inboxer(os.path.join(self.TEST_DIR, "master_inbox"),
                          os.path.join(self.TEST_DIR, "container_inboxes"))
        orchestrator = Orchestrator(inboxer)
        inboxer.add_file_by_bytes("MyTopic1", "This is some data")
        self.assertEqual(len(inboxer.get_inbox_list("MyTopic1")), 0)
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
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))
Ejemplo n.º 5
0
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
Ejemplo n.º 6
0
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))
Ejemplo n.º 7
0
 def __get_registered_containers(self, topic):
     return resolve_query(
         Registration.select().where(Registration.topic == topic))
Ejemplo n.º 8
0
def get_registrations():
    query = Registration.select()
    if 'creator' in request.query:
        query = query.where(Registration.creator == request.query['creator'])
    return {'results': resolve_query(query)}
Ejemplo n.º 9
0
 def __get_registered_containers(self, topic):
     return resolve_query(Registration.select().where(Registration.topic == topic))
Ejemplo n.º 10
0
 def setUp(self):
     Registration.delete().execute()