Skip to content

Commit

Permalink
changed internal format for alerts and queries
Browse files Browse the repository at this point in the history
  • Loading branch information
boristane committed Oct 13, 2023
1 parent ada40bb commit 0881135
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 46 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [Unreleased]

### Changed
- The internal format for alerts and queries

## [0.0.44] 2023-08-27
### Added
Expand Down
10 changes: 4 additions & 6 deletions src/resources/alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ import { getServiceName } from "../utils/service-name";
export class Alert<TKey extends string> extends CfnResource {

constructor(id: string, props: AlertProps<TKey>) {
const defaultFrequency = "1hour";
const defaultWindow = "1hour";
const defaultFrequency = "1h";
const defaultWindow = "1h";

let Parameters: DeploymentAlertParameters | undefined = undefined;

if ("ref" in props.parameters.query) {
Parameters = {
...props.parameters,
threshold: `${props.parameters.threshold?.operation || ">"} ${props.parameters.threshold?.value
}`,
threshold: props.parameters.threshold || { operation: "=", value: 0 },
query: props.parameters.query.ref,
frequency: props.parameters.frequency || defaultFrequency,
window: props.parameters.window || defaultWindow,
Expand All @@ -35,8 +34,7 @@ export class Alert<TKey extends string> extends CfnResource {

Parameters = {
...props.parameters,
threshold: `${props.parameters.threshold?.operation || ">"} ${props.parameters.threshold?.value || 0
}`,
threshold: props.parameters.threshold || { operation: "=", value: 0 },
query: query.ref,
frequency: props.parameters.frequency || defaultFrequency,
window: props.parameters.window || defaultWindow,
Expand Down
24 changes: 3 additions & 21 deletions src/resources/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import { AlertProps } from "../types/alert";
import { Alert } from './alert';
import { getServiceName } from '../utils/service-name';

function buildCalculation(cal: { alias?: string; operation: string; key?: string }) {
const short = buildShortCalculation(cal);
return `${short}${cal.alias ? ` as ${cal.alias}` : ""}`;
}

function hasDuplicates<T>(array: T[]) {
return (new Set(array)).size !== array.length;
Expand All @@ -26,20 +22,6 @@ function getCalculationAlias(cal: { alias?: string; operation: string; key?: str
return cal.alias ? cal.alias : buildShortCalculation(cal);
}

export function stringifyFilter(filter: Filter): string {
const { key, operation, value } = filter;
if (!operation) {
return `${key} = ${value}`;
}
if (["EXISTS", "DOES_NOT_EXIST"].includes(operation)) {
return `${key} ${operation}`;
}
if (["IN", "NOT_IN"].some(o => o === operation)) {
return `${key} ${operation} (${value})`;
}
return `${key} ${operation} ${value}`;
}

/**
*
*/
Expand Down Expand Up @@ -68,9 +50,9 @@ export class Query<TKey extends string> extends CfnResource {

const Parameters: DeploymentQueryParameters = {
...props.parameters,
datasets: props.parameters.datasets || ['lambda-logs'],
calculations: props.parameters.calculations ? props.parameters.calculations.map(buildCalculation) : [],
filters: props.parameters.filters?.map(stringifyFilter),
datasets: props.parameters.datasets || [],
calculations: props.parameters.calculations || [],
filters: props.parameters.filters,
groupBys: props.parameters.groupBys?.map(groupBy => {
return {
...groupBy,
Expand Down
5 changes: 4 additions & 1 deletion src/types/alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ type ChannelTypes = "slack" | "webhook" | "email";

export type DeploymentAlertParameters = {
query: string;
threshold: string;
threshold: {
operation?: QueryOperationString,
value: string | number
};
frequency: string;
window: string;
};
18 changes: 1 addition & 17 deletions src/types/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,4 @@ type CountCalculation<TKey> = {
key?: never;
};

export type DeploymentQueryParameters = {
datasets: string[];
calculations?: string[];
filterCombination: "AND" | "OR";
filters?: string[];
groupBys?: QueryGroupBy[];
orderBy?: {
value: string;
order?: "ASC" | "DESC";
};
limit?: number;
needle?: {
value: string;
isRegex?: boolean;
matchCase?: boolean;
}
};
export type DeploymentQueryParameters = QueryParameters<string>;

0 comments on commit 0881135

Please sign in to comment.