// File generated from our OpenAPI spec by Stainless.

import * as Core from 'cloudflare/core';
import { APIResource } from 'cloudflare/resource';
import { isRequestOptions } from 'cloudflare/core';
import * as AnalyticsAPI from 'cloudflare/resources/storage/analytics';

export class Analytics extends APIResource {
  /**
   * Retrieves Workers KV request metrics for the given account.
   */
  list(
    accountId: string,
    query?: AnalyticsListParams,
    options?: Core.RequestOptions,
  ): Core.APIPromise<AnalyticsListResponse>;
  list(accountId: string, options?: Core.RequestOptions): Core.APIPromise<AnalyticsListResponse>;
  list(
    accountId: string,
    query: AnalyticsListParams | Core.RequestOptions = {},
    options?: Core.RequestOptions,
  ): Core.APIPromise<AnalyticsListResponse> {
    if (isRequestOptions(query)) {
      return this.list(accountId, {}, query);
    }
    return (
      this._client.get(`/accounts/${accountId}/storage/analytics`, { query, ...options }) as Core.APIPromise<{
        result: AnalyticsListResponse;
      }>
    )._thenUnwrap((obj) => obj.result);
  }

  /**
   * Retrieves Workers KV stored data metrics for the given account.
   */
  stored(
    accountId: string,
    query?: AnalyticsStoredParams,
    options?: Core.RequestOptions,
  ): Core.APIPromise<AnalyticsStoredResponse>;
  stored(accountId: string, options?: Core.RequestOptions): Core.APIPromise<AnalyticsStoredResponse>;
  stored(
    accountId: string,
    query: AnalyticsStoredParams | Core.RequestOptions = {},
    options?: Core.RequestOptions,
  ): Core.APIPromise<AnalyticsStoredResponse> {
    if (isRequestOptions(query)) {
      return this.stored(accountId, {}, query);
    }
    return (
      this._client.get(`/accounts/${accountId}/storage/analytics/stored`, {
        query,
        ...options,
      }) as Core.APIPromise<{ result: AnalyticsStoredResponse }>
    )._thenUnwrap((obj) => obj.result);
  }
}

/**
 * Metrics on Workers KV requests.
 */
export interface AnalyticsListResponse {
  data: Array<AnalyticsListResponse.Data> | null;

  /**
   * Number of seconds between current time and last processed event, i.e. how many
   * seconds of data could be missing.
   */
  data_lag: number;

  /**
   * Maximum results for each metric.
   */
  max: unknown;

  /**
   * Minimum results for each metric.
   */
  min: unknown;

  /**
   * For specifying result metrics.
   */
  query: AnalyticsListResponse.Query;

  /**
   * Total number of rows in the result.
   */
  rows: number;

  /**
   * Total results for metrics across all data.
   */
  totals: unknown;
}

export namespace AnalyticsListResponse {
  export interface Data {
    /**
     * List of metrics returned by the query.
     */
    metrics: Array<unknown>;
  }

  /**
   * For specifying result metrics.
   */
  export interface Query {
    /**
     * Can be used to break down the data by given attributes.
     */
    dimensions?: Array<string>;

    /**
     * Used to filter rows by one or more dimensions. Filters can be combined using OR
     * and AND boolean logic. AND takes precedence over OR in all the expressions. The
     * OR operator is defined using a comma (,) or OR keyword surrounded by whitespace.
     * The AND operator is defined using a semicolon (;) or AND keyword surrounded by
     * whitespace. Note that the semicolon is a reserved character in URLs (rfc1738)
     * and needs to be percent-encoded as %3B. Comparison options are:
     *
     * | Operator | Name                     | URL Encoded |
     * | -------- | ------------------------ | ----------- |
     * | ==       | Equals                   | %3D%3D      |
     * | !=       | Does not equals          | !%3D        |
     * | >        | Greater Than             | %3E         |
     * | <        | Less Than                | %3C         |
     * | >=       | Greater than or equal to | %3E%3D      |
     * | <=       | Less than or equal to    | %3C%3D .    |
     */
    filters?: string;

    /**
     * Limit number of returned metrics.
     */
    limit?: number;

    /**
     * One or more metrics to compute.
     */
    metrics?: Array<string>;

    /**
     * Start of time interval to query, defaults to 6 hours before request received.
     */
    since?: string;

    /**
     * Array of dimensions or metrics to sort by, each dimension/metric may be prefixed
     * by - (descending) or + (ascending).
     */
    sort?: Array<unknown>;

    /**
     * End of time interval to query, defaults to current time.
     */
    until?: string;
  }
}

/**
 * Metrics on Workers KV requests.
 */
export interface AnalyticsStoredResponse {
  data: Array<AnalyticsStoredResponse.Data> | null;

  /**
   * Number of seconds between current time and last processed event, i.e. how many
   * seconds of data could be missing.
   */
  data_lag: number;

  /**
   * Maximum results for each metric.
   */
  max: unknown;

  /**
   * Minimum results for each metric.
   */
  min: unknown;

  /**
   * For specifying result metrics.
   */
  query: AnalyticsStoredResponse.Query;

  /**
   * Total number of rows in the result.
   */
  rows: number;

  /**
   * Total results for metrics across all data.
   */
  totals: unknown;
}

export namespace AnalyticsStoredResponse {
  export interface Data {
    /**
     * List of metrics returned by the query.
     */
    metrics: Array<unknown>;
  }

  /**
   * For specifying result metrics.
   */
  export interface Query {
    /**
     * Can be used to break down the data by given attributes.
     */
    dimensions?: Array<string>;

    /**
     * Used to filter rows by one or more dimensions. Filters can be combined using OR
     * and AND boolean logic. AND takes precedence over OR in all the expressions. The
     * OR operator is defined using a comma (,) or OR keyword surrounded by whitespace.
     * The AND operator is defined using a semicolon (;) or AND keyword surrounded by
     * whitespace. Note that the semicolon is a reserved character in URLs (rfc1738)
     * and needs to be percent-encoded as %3B. Comparison options are:
     *
     * | Operator | Name                     | URL Encoded |
     * | -------- | ------------------------ | ----------- |
     * | ==       | Equals                   | %3D%3D      |
     * | !=       | Does not equals          | !%3D        |
     * | >        | Greater Than             | %3E         |
     * | <        | Less Than                | %3C         |
     * | >=       | Greater than or equal to | %3E%3D      |
     * | <=       | Less than or equal to    | %3C%3D .    |
     */
    filters?: string;

    /**
     * Limit number of returned metrics.
     */
    limit?: number;

    /**
     * One or more metrics to compute.
     */
    metrics?: Array<string>;

    /**
     * Start of time interval to query, defaults to 6 hours before request received.
     */
    since?: string;

    /**
     * Array of dimensions or metrics to sort by, each dimension/metric may be prefixed
     * by - (descending) or + (ascending).
     */
    sort?: Array<unknown>;

    /**
     * End of time interval to query, defaults to current time.
     */
    until?: string;
  }
}

export interface AnalyticsListParams {
  /**
   * For specifying result metrics.
   */
  query?: AnalyticsListParams.Query;
}

export namespace AnalyticsListParams {
  /**
   * For specifying result metrics.
   */
  export interface Query {
    /**
     * Can be used to break down the data by given attributes.
     */
    dimensions?: Array<'accountId' | 'responseCode' | 'requestType'>;

    /**
     * Used to filter rows by one or more dimensions. Filters can be combined using OR
     * and AND boolean logic. AND takes precedence over OR in all the expressions. The
     * OR operator is defined using a comma (,) or OR keyword surrounded by whitespace.
     * The AND operator is defined using a semicolon (;) or AND keyword surrounded by
     * whitespace. Note that the semicolon is a reserved character in URLs (rfc1738)
     * and needs to be percent-encoded as %3B. Comparison options are:
     *
     * | Operator | Name                     | URL Encoded |
     * | -------- | ------------------------ | ----------- |
     * | ==       | Equals                   | %3D%3D      |
     * | !=       | Does not equals          | !%3D        |
     * | >        | Greater Than             | %3E         |
     * | <        | Less Than                | %3C         |
     * | >=       | Greater than or equal to | %3E%3D      |
     * | <=       | Less than or equal to    | %3C%3D .    |
     */
    filters?: string;

    /**
     * Limit number of returned metrics.
     */
    limit?: number;

    /**
     * One or more metrics to compute.
     */
    metrics?: Array<'requests' | 'writeKiB' | 'readKiB'>;

    /**
     * Start of time interval to query, defaults to 6 hours before request received.
     */
    since?: string;

    /**
     * Array of dimensions or metrics to sort by, each dimension/metric may be prefixed
     * by - (descending) or + (ascending).
     */
    sort?: Array<unknown>;

    /**
     * End of time interval to query, defaults to current time.
     */
    until?: string;
  }
}

export interface AnalyticsStoredParams {
  /**
   * For specifying result metrics.
   */
  query?: AnalyticsStoredParams.Query;
}

export namespace AnalyticsStoredParams {
  /**
   * For specifying result metrics.
   */
  export interface Query {
    /**
     * Can be used to break down the data by given attributes.
     */
    dimensions?: Array<'namespaceId'>;

    /**
     * Used to filter rows by one or more dimensions. Filters can be combined using OR
     * and AND boolean logic. AND takes precedence over OR in all the expressions. The
     * OR operator is defined using a comma (,) or OR keyword surrounded by whitespace.
     * The AND operator is defined using a semicolon (;) or AND keyword surrounded by
     * whitespace. Note that the semicolon is a reserved character in URLs (rfc1738)
     * and needs to be percent-encoded as %3B. Comparison options are:
     *
     * | Operator | Name                     | URL Encoded |
     * | -------- | ------------------------ | ----------- |
     * | ==       | Equals                   | %3D%3D      |
     * | !=       | Does not equals          | !%3D        |
     * | >        | Greater Than             | %3E         |
     * | <        | Less Than                | %3C         |
     * | >=       | Greater than or equal to | %3E%3D      |
     * | <=       | Less than or equal to    | %3C%3D .    |
     */
    filters?: string;

    /**
     * Limit number of returned metrics.
     */
    limit?: number;

    /**
     * One or more metrics to compute.
     */
    metrics?: Array<'storedBytes' | 'storedKeys'>;

    /**
     * Start of time interval to query, defaults to 6 hours before request received.
     */
    since?: string;

    /**
     * Array of dimensions or metrics to sort by, each dimension/metric may be prefixed
     * by - (descending) or + (ascending).
     */
    sort?: Array<unknown>;

    /**
     * End of time interval to query, defaults to current time.
     */
    until?: string;
  }
}

export namespace Analytics {
  export import AnalyticsListResponse = AnalyticsAPI.AnalyticsListResponse;
  export import AnalyticsStoredResponse = AnalyticsAPI.AnalyticsStoredResponse;
  export import AnalyticsListParams = AnalyticsAPI.AnalyticsListParams;
  export import AnalyticsStoredParams = AnalyticsAPI.AnalyticsStoredParams;
}
