Exemple #1
0
def get_series_values_after(series_def, report, after,
                            limit=mqeconfig.MAX_SERIES_POINTS_IN_TILE,
                            latest_instance_id=None):
    """Retrieves a list of :class:`SeriesValue` created after the specified report instance ID
    (``after``). The function inserts new data series values if they haven't been already created.

    :param SeriesDef series_def: a series definition for which to get data
    :param ~mqe.report.Report report: a report for which to get data
    :param ~uuid.UUID after: a ``report_instance_id`` that specifies the starting point to fetch
        (and possibly create) data series values.
    :param int limit: the limit of the series values to fetch/create
    :param latest_instance_id: (optional) a latest report instance ID of the report and tags.
        Passing this parameter ensures multiple calls of the functions for the same report
        will return consistent data (ie. coming from the same report instances).
    :return: a list of :class:`SeriesValue` objects in the order of creation time of the corresponding report instances
    """
    if series_def['from_rid'] is None or series_def['to_rid'] is None:
        insert_after = after
    elif util.uuid_lt(after, series_def['from_rid']):
        insert_after = after
    else:
        insert_after = series_def['to_rid']
    insert_series_values(series_def, report, None, None, after=insert_after)

    if latest_instance_id:
        max_report_instance_id = util.uuid_for_next_dt(latest_instance_id)
    else:
        max_report_instance_id = None

    rows = c.dao.SeriesValueDAO.select_multi(series_def.series_id, after, max_report_instance_id, limit)
    log.debug('Selected %d series_values after series_id=%s report_name=%r',
              len(rows) if isinstance(rows, list) else '?',
              series_def.series_id, report.report_name)
    return list(reversed([SeriesValue(row) for row in rows]))
Exemple #2
0
 def _min_max_uuid_from_args(self, from_dt, to_dt, before, after):
     if after is not None or before is not None:
         min_uuid = after or util.MIN_UUID
         max_uuid = before or util.MAX_UUID
     else:
         min_uuid = util.uuid_for_prev_dt(util.uuid_with_dt(from_dt)) \
             if from_dt is not None else util.MIN_UUID
         max_uuid = util.uuid_for_next_dt(util.uuid_with_dt(to_dt)) \
             if to_dt is not None else util.MAX_UUID
     return min_uuid, max_uuid
Exemple #3
0
def get_report_instances(name):
    from_dt = parse_datetime(request.args.get('from'))
    to_dt = parse_datetime(request.args.get('to'))
    tags = parse_tags(request.args.get('tags'))

    expand = parse_bool(request.args.get('expand'))
    if expand is None:
        expand = True

    expand_input = parse_bool(request.args.get('expandInput')) or False
    limit = get_limit()
    from_id = parse_id(request.args.get('fromId'))
    last_id = parse_id(request.args.get('lastId'))
    order = parse_enum(request.args.get('order'), ('asc', 'desc')) or 'asc'

    report = get_report(name)

    after = None
    before = None
    if last_id:
        if order == 'asc':
            after = last_id
            before = None
        elif order == 'desc':
            after = None
            before = last_id
    elif from_id:
        if order == 'asc':
            after = uuid_for_prev_dt(from_id)
            before = None
        elif order == 'desc':
            after = None
            before = uuid_for_next_dt(from_id)

    instances = report.fetch_instances(from_dt=from_dt,
                                       to_dt=to_dt,
                                       limit=limit,
                                       tags=tags,
                                       order=order,
                                       after=after,
                                       before=before)

    res = [
        _report_instance_desc(name, ri, expand, expand_input)
        for ri in instances
    ]
    r = ApiResponse(200, result=res)
    if len(instances) == limit:
        r.set_detail(
            'next',
            set_query_param(request.url, 'lastId',
                            to_id(instances[-1].report_instance_id)))
    else:
        r.set_detail('next', None)
    return r.get()
Exemple #4
0
def get_series_values(series_def,
                      report,
                      from_dt,
                      to_dt,
                      limit=mqeconfig.MAX_SERIES_POINTS_IN_TILE,
                      latest_instance_id=None):
    """Retrieves a list of :class:`SeriesValue` objects for a given time range.
    The function inserts new data series values if they haven't been already created
    for the requested time period.

    :param SeriesDef series_def: a series definition for which to get data
    :param ~mqe.report.Report report: a report for which to get data
    :param ~datetime.datetime from_dt: starting datetime
    :param ~datetime.datetime to_dt: ending datetime
    :param int limit: the limit of the series values to fetch/create
    :param latest_instance_id: (optional) a latest report instance ID of the report and tags
        (if not passed, the value will be fetched)
    :return: a list of :class:`SeriesValue` objects in the order of creation time of the corresponding report instances
    """
    assert from_dt is not None and to_dt is not None
    if series_def.from_dt is None or series_def.to_dt is None:
        insert_series_values(series_def, report, from_dt, to_dt, limit=limit)
    else:
        if from_dt < series_def.from_dt:
            insert_series_values(series_def,
                                 report,
                                 from_dt,
                                 prev_dt(series_def.from_dt),
                                 limit=limit)

        if not latest_instance_id:
            latest_instance_id = report.fetch_latest_instance_id(
                series_def.tags)
        if latest_instance_id is not None \
                and util.uuid_lt(series_def['to_rid'], latest_instance_id) \
                and to_dt >= series_def.to_dt:
            insert_series_values(series_def,
                                 report,
                                 None,
                                 None,
                                 after=series_def['to_rid'],
                                 limit=limit)

    min_report_instance_id = util.uuid_for_prev_dt(util.uuid_with_dt(from_dt))
    max_report_instance_id = util.uuid_for_next_dt(util.uuid_with_dt(to_dt))
    rows = c.dao.SeriesValueDAO.select_multi(series_def.series_id,
                                             min_report_instance_id,
                                             max_report_instance_id, limit)

    log.debug('Selected %d series_values by dates series_id=%s report_name=%r',
              len(rows), series_def.series_id, report.report_name)
    return list(reversed([SeriesValue(row) for row in rows]))
Exemple #5
0
    def fetch_instances(self,
                        from_dt=None,
                        to_dt=None,
                        before=None,
                        after=None,
                        tags=None,
                        columns=None,
                        order='asc',
                        limit=100):
        """Fetch a list of report instances. The time range can be specified as either
        datetimes (``from_dt``, ``to_dt``) or report instance IDs (``before``, ``after``).

        :param ~datetime.datetime|None from_dt: fetch report instances created on the datetime or
            later
        :param ~datetime.datetime|None to_dt: fetch report instances created on the datetime or
            earlier
        :param ~uuid.UUID|None before: fetch instances created before the given report instance ID
        :param ~uuid.UUID|None after: fetch instances created after the given report instance ID
        :param list tags: a list of tags the returned instances must have attached
        :param str order: ``asc`` (ascending) or ``desc`` (descending) order wrt. creation datetime
        :param str columns: a list of :class:`ReportInstance` attributes to select
        :param int limit: the limit of the number of report instances to fetch
            fetch
        :return: a list of :class:`ReportInstance` objects
        """
        if after is not None or before is not None:
            min_uuid = after or util.MIN_UUID
            max_uuid = before or util.MAX_UUID
        else:
            min_uuid = util.uuid_for_prev_dt(util.uuid_with_dt(from_dt)) \
                       if from_dt is not None else util.MIN_UUID
            max_uuid = util.uuid_for_next_dt(util.uuid_with_dt(to_dt)) \
                       if to_dt is not None else util.MAX_UUID

        rows = c.dao.ReportInstanceDAO.select_multi(
            report_id=self.report_id,
            tags=tags,
            min_report_instance_id=min_uuid,
            max_report_instance_id=max_uuid,
            columns=columns,
            order=order,
            limit=limit)
        return [ReportInstance(row) for row in rows]