/
query-definition.ts
165 lines (145 loc) · 3.78 KB
/
query-definition.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { Resource } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { CfnQueryDefinition } from '.';
import { ILogGroup } from './log-group';
/**
* Properties for a QueryString
*/
export interface QueryStringProps {
/**
* Retrieves the specified fields from log events for display.
*
* @default - no fields in QueryString
*/
readonly fields?: string[];
/**
* Extracts data from a log field and creates one or more ephemeral fields that you can process further in the query.
*
* @default - no parse in QueryString
*/
readonly parse?: string;
/**
* Filters the results of a query that's based on one or more conditions.
*
* @default - no filter in QueryString
*/
readonly filter?: string;
/**
* Uses log field values to calculate aggregate statistics.
*
* @default - no stats in QueryString
*/
readonly stats?: string;
/**
* Sorts the retrieved log events.
*
* @default - no sort in QueryString
*/
readonly sort?: string;
/**
* Specifies the number of log events returned by the query.
*
* @default - no limit in QueryString
*/
readonly limit?: Number;
/**
* Specifies which fields to display in the query results.
*
* @default - no display in QueryString
*/
readonly display?: string;
}
interface QueryStringMap {
readonly fields?: string,
readonly parse?: string,
readonly filter?: string,
readonly stats?: string,
readonly sort?: string,
readonly limit?: Number,
readonly display?: string,
}
/**
* Define a QueryString
*/
export class QueryString {
private readonly fields?: string[];
private readonly parse?: string;
private readonly filter?: string;
private readonly stats?: string;
private readonly sort?: string;
private readonly limit?: Number;
private readonly display?: string;
constructor(props: QueryStringProps = {}) {
this.fields = props.fields;
this.parse = props.parse;
this.filter = props.filter;
this.stats = props.stats;
this.sort = props.sort;
this.limit = props.limit;
this.display = props.display;
}
/**
* String representation of this QueryString.
*/
public toString(): string {
return noUndef({
fields: this.fields !== undefined ? this.fields.join(', ') : this.fields,
parse: this.parse,
filter: this.filter,
stats: this.stats,
sort: this.sort,
limit: this.limit,
display: this.display,
}).join(' | ');
}
}
function noUndef(x: QueryStringMap): string[] {
const ret: string[] = [];
for (const [key, value] of Object.entries(x)) {
if (value !== undefined) {
ret.push(`${key} ${value}`);
}
}
return ret;
}
/**
* Properties for a QueryDefinition
*/
export interface QueryDefinitionProps {
/**
* Name of the query definition.
*/
readonly queryDefinitionName: string;
/**
* The query string to use for this query definition.
*/
readonly queryString: QueryString;
/**
* Specify certain log groups for the query definition.
*
* @default - no specified log groups
*/
readonly logGroups?: ILogGroup[];
}
/**
* Define a query definition for CloudWatch Logs Insights
*/
export class QueryDefinition extends Resource {
/**
* The ID of the query definition.
*
* @attribute
*/
public readonly queryDefinitionId: string;
constructor(scope: Construct, id: string, props: QueryDefinitionProps) {
super(scope, id, {
physicalName: props.queryDefinitionName,
});
const queryDefinition = new CfnQueryDefinition(this, 'Resource', {
name: props.queryDefinitionName,
queryString: props.queryString.toString(),
logGroupNames: typeof props.logGroups === 'undefined' ? [] : props.logGroups.flatMap(logGroup => logGroup.logGroupName),
});
this.queryDefinitionId = queryDefinition.attrQueryDefinitionId;
}
}