async def aget_history(server, token, metric):
    client = metricq.HistoryClient(token=token,
                                   management_url=server,
                                   event_loop=asyncio.get_running_loop())
    await client.connect()

    click.echo("connected")

    total_begin = metricq.Timestamp.from_iso8601("2020-01-01T00:00:00.0Z")
    total_end = metricq.Timestamp.now()
    chunk_duration = metricq.Timedelta.from_timedelta(timedelta(days=1))
    interval_max_raw = metricq.Timedelta(0)

    chunk_begin = total_begin
    while chunk_begin < total_end:
        chunk_end = chunk_begin + chunk_duration
        chunk_end = min(chunk_end, total_end)
        click.echo(f"Requesting chunk from {chunk_begin} to {chunk_end}")

        result = await client.history_data_request(
            metric,
            start_time=chunk_begin,
            end_time=chunk_end,
            interval_max=interval_max_raw,
            request_type=HistoryRequestType.FLEX_TIMELINE,
        )
        for tv in result.values():
            # The DB can give you one value before the requested begin timestamp
            if tv.timestamp < chunk_begin:
                continue
            click.echo(f"{tv.timestamp} {tv.value}")

        chunk_begin = chunk_end

    await client.stop(None)
Exemplo n.º 2
0
async def aget_history(server, token, metric, list_metrics, list_metadata):
    client = metricq.HistoryClient(token=token,
                                   management_url=server,
                                   event_loop=asyncio.get_running_loop())
    await client.connect()
    if list_metrics:
        metrics = await client.history_metric_list(metric)
        click.echo(
            click.style("metrics matching {}:\n{}".format(metric, metrics),
                        fg="bright_blue"))
        await client.stop()
        return

    if list_metadata:
        metadata = await client.history_metric_metadata(metric)
        pp = pprint.PrettyPrinter(indent=4)
        click.echo(
            click.style(
                "metrics matching {}:\n{}".format(metric,
                                                  pp.pformat(metadata)),
                fg="bright_blue",
            ))
        await client.stop()
        return

    now = metricq.Timestamp.now()
    last_timevalue = await client.history_last_value(metric)
    click.echo(
        click.style(
            "Last entry: {} ({} ago) value: {}".format(
                last_timevalue.timestamp,
                now - last_timevalue.timestamp,
                last_timevalue.value,
            ),
            fg="bright_blue",
        ))

    delta = metricq.Timedelta.from_timedelta(timedelta(seconds=600))
    start_time = now - delta
    interval_max = metricq.Timedelta.from_timedelta(timedelta(seconds=10))
    result = await client.history_data_request(metric,
                                               start_time=start_time,
                                               end_time=now,
                                               interval_max=interval_max)

    click.echo("Values in the last {}".format(delta))
    for aggregate in result.aggregates():
        click.echo(aggregate)

    await client.stop()