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