示例#1
0
def org_timeseries(obj, org_id=None, metrics_lookup=None, commit=True):
    """
    Ingest Timeseries Metrics for an organization.
    """
    cmd_kwargs = {"org_id": org_id}

    # parse datetime.
    if "datetime" not in obj:
        cmd_kwargs["datetime"] = dates.floor_now(unit="hour", value=1).isoformat()

    else:
        ds = obj.pop("datetime")
        dt = dates.parse_iso(ds)
        cmd_kwargs["datetime"] = dates.floor(dt, unit="hour", value=1).isoformat()

    metrics = ingest_util.prepare_metrics(obj, metrics_lookup, valid_levels=["org", "all"], check_timeseries=True)

    # upsert command
    cmd = """SELECT upsert_org_metric_timeseries(
                 {org_id},
                '{datetime}',
                '{metrics}')
           """.format(
        metrics=obj_to_json(metrics), **cmd_kwargs
    )

    if commit:
        try:
            db.session.execute(cmd)
        except Exception as err:
            raise RequestError(err.message)
        cmd_kwargs["metrics"] = metrics
        return cmd_kwargs
    return cmd
示例#2
0
def _prepare_metric_date(obj):
    """
    Round a metric to a configurable unit.
    """
    u = settings.METRICS_MIN_DATE_UNIT
    v = settings.METRICS_MIN_DATE_VALUE

    # set current time if no time exists.
    if 'datetime' not in obj:
        return dates.floor_now(unit=u, value=v).isoformat()

    ds = obj.pop('datetime')
    dt = dates.parse_iso(ds, enforce_tz=True)
    return dates.floor(dt, unit=u, value=v).isoformat()
示例#3
0
def content_timeseries(obj,
                       org_id=None,
                       metrics_lookup=None,
                       content_item_ids=None,
                       commit=True):
    """
    Ingest Timeseries Metrics for a content item.
    """
    # if not content_item_id or not org or not metrics_lookup:
    #     raise RequestError('Missing required kwargs.')
    content_item_id = obj.pop('content_item_id')
    if not content_item_id:
        raise RequestError('Object is missing a "content_item_id"')
    if not content_item_id in content_item_ids:
        raise RequestError(
            'Content Item with ID {} doesnt exist'.format(content_item_id))

    cmd_kwargs = {"org_id": org_id, "content_item_id": content_item_id}

    # parse datetime.
    if 'datetime' not in obj:
        cmd_kwargs['datetime'] = dates.floor_now(unit='hour',
                                                 value=1).isoformat()

    else:
        ds = obj.pop('datetime')
        dt = dates.parse_iso(ds)
        cmd_kwargs['datetime'] = dates.floor(dt, unit='hour',
                                             value=1).isoformat()

    metrics = ingest_util.prepare_metrics(obj,
                                          metrics_lookup,
                                          valid_levels=['content_item', 'all'],
                                          check_timeseries=True)

    # upsert command
    cmd = """SELECT upsert_content_metric_timeseries(
                {org_id},
                {content_item_id},
                '{datetime}',
                '{metrics}')
           """.format(metrics=obj_to_json(metrics), **cmd_kwargs)

    if commit:
        try:
            db.session.execute(cmd)
        except Exception as err:
            raise RequestError(err.message)
        cmd_kwargs['metrics'] = metrics
    return cmd
示例#4
0
def _prepare_metric_date(obj):
    """
    Round a metric to a configurable unit.
    """
    u = settings.METRICS_MIN_DATE_UNIT
    v = settings.METRICS_MIN_DATE_VALUE

    # set current time if no time exists.
    if 'datetime' not in obj:
        return dates.floor_now(unit=u, value=v).isoformat()

    ds = obj.pop('datetime')
    dt = dates.parse_iso(ds, enforce_tz=True)
    return dates.floor(dt, unit=u, value=v).isoformat()
示例#5
0
def content_timeseries(obj, org_id=None, metrics_lookup=None, content_item_ids=None, commit=True):
    """
    Ingest Timeseries Metrics for a content item.
    """
    # if not content_item_id or not org or not metrics_lookup:
    #     raise RequestError('Missing required kwargs.')
    content_item_id = obj.pop("content_item_id")
    if not content_item_id:
        raise RequestError('Object is missing a "content_item_id"')
    if not content_item_id in content_item_ids:
        raise RequestError("Content Item with ID {} doesnt exist".format(content_item_id))

    cmd_kwargs = {"org_id": org_id, "content_item_id": content_item_id}

    # parse datetime.
    if "datetime" not in obj:
        cmd_kwargs["datetime"] = dates.floor_now(unit="hour", value=1).isoformat()

    else:
        ds = obj.pop("datetime")
        dt = dates.parse_iso(ds)
        cmd_kwargs["datetime"] = dates.floor(dt, unit="hour", value=1).isoformat()

    metrics = ingest_util.prepare_metrics(
        obj, metrics_lookup, valid_levels=["content_item", "all"], check_timeseries=True
    )

    # upsert command
    cmd = """SELECT upsert_content_metric_timeseries(
                {org_id},
                {content_item_id},
                '{datetime}',
                '{metrics}')
           """.format(
        metrics=obj_to_json(metrics), **cmd_kwargs
    )

    if commit:
        try:
            db.session.execute(cmd)
        except Exception as err:
            raise RequestError(err.message)
        cmd_kwargs["metrics"] = metrics
    return cmd
示例#6
0
def org_timeseries(obj, org_id=None, metrics_lookup=None, commit=True):
    """
    Ingest Timeseries Metrics for an organization.
    """
    cmd_kwargs = {'org_id': org_id}

    # parse datetime.
    if 'datetime' not in obj:
        cmd_kwargs['datetime'] = dates.floor_now(unit='hour',
                                                 value=1).isoformat()

    else:
        ds = obj.pop('datetime')
        dt = dates.parse_iso(ds)
        cmd_kwargs['datetime'] = dates.floor(dt, unit='hour',
                                             value=1).isoformat()

    metrics = ingest_util.prepare_metrics(obj,
                                          metrics_lookup,
                                          valid_levels=['org', 'all'],
                                          check_timeseries=True)

    # upsert command
    cmd = """SELECT upsert_org_metric_timeseries(
                 {org_id},
                '{datetime}',
                '{metrics}')
           """.format(metrics=obj_to_json(metrics), **cmd_kwargs)

    if commit:
        try:
            db.session.execute(cmd)
        except Exception as err:
            raise RequestError(err.message)
        cmd_kwargs['metrics'] = metrics
        return cmd_kwargs
    return cmd