示例#1
0
async def get_history_data(app, request):
    time_begin = timer()
    targets = []
    for target_dict in request["targets"]:
        metrics = [target_dict["metric"]]
        # guess if this is a pattern (regex) to expand by sending it to the manager
        if "(" in target_dict["metric"] and ")" in target_dict["metric"]:
            metrics = await app["history_client"].get_metrics(
                metadata=False, historic=True, selector=target_dict["metric"])
        for metric in metrics:
            targets.append(
                Target(
                    metric=metric,
                    name=target_dict.get("name", None),
                    functions=list(parse_functions(target_dict)),
                    scaling_factor=float(target_dict.get(
                        "scaling_factor", "1")),
                ))

    start_time = Timestamp.from_iso8601(request["range"]["from"])
    end_time = Timestamp.from_iso8601(request["range"]["to"])
    # Grafana gives inconsistent information here:
    #    intervalMs is very coarse grained
    #    maxDataPoints is not really the number of pixels, usually less
    # interval = Timedelta.from_ms(request["intervalMs"])
    interval = ((end_time - start_time) / request["maxDataPoints"]) * 2
    results = await asyncio.gather(*[
        target.get_response(app, start_time, end_time, interval)
        for target in targets
    ])
    rv = functools.reduce(operator.iconcat, results, [])
    time_diff = timer() - time_begin
    logger.log(
        logging.DEBUG if time_diff < 1 else logging.INFO,
        "get_history_data for {} targets took {} s",
        len(targets),
        time_diff,
    )

    return rv
示例#2
0
 def convert(self, value: str, param: Optional[Parameter],
             ctx: Optional[Context]) -> Any:
     if value is None:
         return None
     elif isinstance(value, str):
         try:
             return Timestamp.from_iso8601(value)
         except ValueError:
             self.fail(
                 "expected an ISO-8601 timestamp (e.g. 2012-12-21T00:00:00Z)",
                 param=param,
                 ctx=ctx,
             )
     else:
         return value
示例#3
0
def test_timestamp_from_iso8601(date_string: str, expected: Timestamp):
    assert Timestamp.from_iso8601(date_string) == expected
示例#4
0
def test_timestamp_param():
    value = "2021-05-02T00:00:00Z"
    assert TIMESTAMP.convert(value, param=None,
                             ctx=None) == Timestamp.from_iso8601(value)
示例#5
0
def timestamp():
    return Timestamp.from_iso8601("2021-03-03T18:00:00Z")