github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/ui/src/views/shared/components/metricQuery/index.tsx (about)

     1  // Copyright 2018 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  /**
    12   * MetricQuery Components
    13   *
    14   * These react-like components are intended to express metric queries for graphs
    15   * in a declarative, HTML-like syntax.  For example, a query for a graph that
    16   * displays the non-negative rate of change of two metrics on a shared axis:
    17   *
    18   * <Axis units={AxisUnits.Duration}>
    19   *  <Metric name="cr.node.sys.cpu.user.ns" title="User CPU Time" nonNegativeRate />
    20   *  <Metric name="cr.node.sys.cpu.sys.ns" title="Sys CPU Time" nonNegativeRate />
    21   * </Axis>
    22   *
    23   * This information is used to construct a query to the backend for metrics; it
    24   * is also used by the parent component to render the query response correctly.
    25   *
    26   * While these are react components, they are not intended to be rendered
    27   * directly and will throw an error if rendered. Instead, it is intended that
    28   * parent components to read the information expressed by these components and
    29   * combine it with the result of a query to create some renderable output.
    30   */
    31  
    32  import React from "react";
    33  import * as protos from  "src/js/protos";
    34  
    35  type TSResponse = protos.cockroach.ts.tspb.TimeSeriesQueryResponse;
    36  import TimeSeriesQueryAggregator = protos.cockroach.ts.tspb.TimeSeriesQueryAggregator;
    37  import TimeSeriesQueryDerivative = protos.cockroach.ts.tspb.TimeSeriesQueryDerivative;
    38  
    39  /**
    40   * AxisUnits is an enumeration used to specify the type of units being displayed
    41   * on an Axis.
    42   */
    43  export enum AxisUnits {
    44    /**
    45     * Units are a simple count.
    46     */
    47    Count,
    48    /**
    49     * Units are a count of bytes.
    50     */
    51    Bytes,
    52    /**
    53     * Units are durations expressed in nanoseconds.
    54     */
    55    Duration,
    56    /**
    57     * Units are percentages expressed as fractional values of 1 (1.0 = 100%).
    58     */
    59    Percentage,
    60  }
    61  
    62  /**
    63   * AxisProps represents the properties of an Axis being specified as part of a
    64   * query for metrics.
    65   */
    66  export interface AxisProps {
    67    label?: string;
    68    format?: (n: number) => string;
    69    range?: number[];
    70    units?: AxisUnits;
    71  }
    72  
    73  /**
    74   * Axis is a React component which describes an Axis of a metrics query.
    75   *
    76   * This component should not be rendered directly; rather, a renderable
    77   * component should contain axes as children and use them only informationally
    78   * without rendering them.
    79   */
    80  export class Axis extends React.Component<AxisProps, {}> {
    81    static defaultProps: AxisProps = {
    82      units: AxisUnits.Count,
    83    };
    84  
    85    render(): React.ReactElement<any> {
    86      throw new Error("Component <Axis /> should never render.");
    87    }
    88  }
    89  
    90  /**
    91   * MetricProps reperesents the properties of a Metric being selected as part of
    92   * a query.
    93   *
    94   * Note that there are redundant specifiers for several of the options
    95   * (derivatives, aggregators, downsamplers). These exist because, while the
    96   * exact specifiers (e.g. "aggregator") are convenient when constructing metrics
    97   * programmatically, the boolean specifiers (e.g. "aggregateMax") are convenient
    98   * when writing JSX directly. This is purely a syntactic helper.
    99   *
   100   * Only one option should be specified for each of the (derivative, aggregator,
   101   * downsampler); if multiple options are specified, the exact specifier takes
   102   * precedence.
   103   */
   104  export interface MetricProps {
   105    name: string;
   106    sources?: string[];
   107    title?: string;
   108    rate?: boolean;
   109    nonNegativeRate?: boolean;
   110    aggregateMax?: boolean;
   111    aggregateMin?: boolean;
   112    aggregateAvg?: boolean;
   113    downsampleMax?: boolean;
   114    downsampleMin?: boolean;
   115    derivative?: TimeSeriesQueryDerivative;
   116    aggregator?: TimeSeriesQueryAggregator;
   117    downsampler?: TimeSeriesQueryAggregator;
   118  }
   119  
   120  /**
   121   * Metric is a React component which describes a Metric in a metrics query.
   122   *
   123   * This component should not be rendered directly; rather, a renderable
   124   * component should contain axes as children and use them only informationally
   125   * without rendering them.
   126   */
   127  export class Metric extends React.Component<MetricProps> {
   128    render(): React.ReactElement<any> {
   129      throw new Error("Component <Metric /> should never render.");
   130    }
   131  }
   132  
   133  /**
   134   * QueryTimeInfo is a convenience structure which contains information about
   135   * the time range of a metrics query.
   136   */
   137  export interface QueryTimeInfo {
   138    // The start time of the query, expressed as a unix timestamp in nanoseconds.
   139    start: Long;
   140    // The end time of the query, expressed as a unix timestamp in nanoseconds.
   141    end: Long;
   142    // The duration of individual samples in the query, expressed in nanoseconds.
   143    sampleDuration: Long;
   144  }
   145  
   146  /**
   147   * MetricsDataComponentProps is an interface that should be implemented by any
   148   * components expecting to receive a metrics query result.
   149   */
   150  export interface MetricsDataComponentProps {
   151    data?: TSResponse;
   152    timeInfo?: QueryTimeInfo;
   153    // Allow graphs to declare a single source for all metrics. This is a
   154    // convenient syntax for a common use case where all metrics on a graph are
   155    // are from the same source set.
   156    sources?: string[];
   157  }