Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

3.7 KB
Raw Blame
import type {
  TestAggregationSpec,
  TestBreakoutWithNameSpec,
  TestExpressionSpec,
  TestNamedAggregationSpec,
  TestOperatorSpec,
  TestStageSpec,
  TestStageWithSourceSpec,
} from "metabase-types/api";
import { isObject } from "metabase-types/guards";

import type {
  MeasureSchema,
  MetricSchema,
  QuestionSchema,
  SegmentSchema,
  TableSchema,
} from "./schema";

export type TableQueryInput = Omit<TestStageWithSourceSpec, "source"> & {
  source: TableSchema;
  limit?: number;
  enabled?: boolean;
  /** The entity ID of the question a data app runs in production. */
  entityId?: string;
};

/**
 * The dynamic clauses applied on top of a static query. They become a stage of
 * their own, and a stage above the source sees only the previous stage's result
 * columns — so it takes the same clause set as a card stage.
 */
export type DynamicQueryInput = Pick<
  TestStageSpec,
  "breakouts" | "orderBys" | "limit"
> & {
  filters?: readonly TestExpressionSpec[];
  aggregations?: readonly TestExpressionSpec[];
  enabled?: boolean;
};

export type QuestionQueryInput = DynamicQueryInput & {
  source: QuestionSchema;
};

export type QueryInput = TableQueryInput | QuestionQueryInput;

export const isTableInput = (input: unknown): input is TableQueryInput =>
  isObject(input) && "source" in input && isTableReference(input.source);

export const isTableReference = (value: unknown): value is TableSchema =>
  isObject(value) && typeof value.id === "number" && value.type === "table";

export const isMetricReference = (value: unknown): value is MetricSchema =>
  isObject(value) && typeof value.id === "number" && value.type === "metric";

export const isSegmentReference = (value: unknown): value is SegmentSchema =>
  isObject(value) && typeof value.id === "number" && value.type === "segment";

export const isMeasureReference = (value: unknown): value is MeasureSchema =>
  isObject(value) && typeof value.id === "number" && value.type === "measure";

export const isNamedBreakout = (
  value: unknown,
): value is TestBreakoutWithNameSpec =>
  isObject(value) &&
  value.type === "breakout" &&
  typeof value.name === "string" &&
  isObject(value.column);

export const isNamedAggregation = (
  value: unknown,
): value is TestNamedAggregationSpec =>
  isObject(value) &&
  typeof value.name === "string" &&
  "value" in value &&
  !("type" in value);

export const unwrapNamedAggregation = (aggregation: TestAggregationSpec) =>
  isNamedAggregation(aggregation) ? aggregation.value : aggregation;

export const isQuestionInput = (input: unknown): input is QuestionQueryInput =>
  isObject(input) && "source" in input && isQuestionReference(input.source);

export const isQuestionReference = (value: unknown): value is QuestionSchema =>
  isObject(value) &&
  value.type === "card" &&
  (typeof value.id === "number" || typeof value.entityId === "string");

export const isQueryInput = (input: unknown): input is QueryInput =>
  isTableInput(input) || isQuestionInput(input);

const isOperator = (value: unknown): value is TestOperatorSpec =>
  isObject(value) &&
  value.type === "operator" &&
  typeof value.operator === "string";

export const isAggregationSpec = (
  value: unknown,
): value is TestAggregationSpec =>
  isOperator(value) ||
  isNamedAggregation(value) ||
  isMetricReference(value) ||
  isMeasureReference(value);

export const isCountAggregation = (aggregation: TestAggregationSpec) =>
  !isNamedAggregation(aggregation) &&
  aggregation.type === "operator" &&
  aggregation.operator === "count" &&
  aggregation.args?.length === 0;

export const isUnaryOperator = (operator: string) =>
  operator === "is-empty" ||
  operator === "not-empty" ||
  operator === "is-null" ||
  operator === "not-null";