UNPKG

2.31 kBTypeScriptView Raw
1import { Duration } from '@aws-cdk/core';
2import { IMetric, MetricConfig, MetricExpressionConfig, MetricStatConfig } from '../metric-types';
3/**
4 * Return a unique string representation for this metric.
5 *
6 * Can be used to determine as a hash key to determine if 2 Metric objects
7 * represent the same metric. Excludes rendering properties.
8 */
9export declare function metricKey(metric: IMetric): string;
10/**
11 * Return the period of a metric
12 *
13 * For a stat metric, return the immediate period.
14 *
15 * For an expression metric, all metrics used in it have been made to have the
16 * same period, so we return the period of the first inner metric.
17 */
18export declare function metricPeriod(metric: IMetric): Duration;
19/**
20 * Given a metric object, inspect it and call the correct function for the type of output
21 *
22 * In addition to the metric object itself, takes a callback object with two
23 * methods, to be invoked for the particular type of metric.
24 *
25 * If the metric represent a metric query (nominally generated through an
26 * instantiation of `Metric` but can be generated by any class that implements
27 * `IMetric`) a particular field in its `toMetricConfig()` output will be set
28 * (to wit, `metricStat`) and the `withStat()` callback will be called with
29 * that object.
30 *
31 * If the metric represents an expression (usually by instantiating `MathExpression`
32 * but users can implement `IMetric` arbitrarily) the `mathExpression` field
33 * will be set in the object returned from `toMetricConfig` and the callback
34 * called `withExpression` will be applied to that object.
35 *
36 * Will return the values returned by the callbacks.
37 *
38 * To be used as such:
39 *
40 * ```ts
41 * const ret = dispatchMetric(someMetric, {
42 * withStat(stat) {
43 * // do something with stat
44 * return 1;
45 * },
46 * withExpression(expr) {
47 * // do something with expr
48 * return 2;
49 * },
50 * });
51 * ```
52 *
53 * This function encapsulates some type analysis that would otherwise have to be
54 * repeated in all places where code needs to make a distinction on the type
55 * of metric object that is being passed.
56 */
57export declare function dispatchMetric<A, B>(metric: IMetric, fns: {
58 withStat: (x: MetricStatConfig, c: MetricConfig) => A;
59 withExpression: (x: MetricExpressionConfig, c: MetricConfig) => B;
60}): A | B;