|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +import { Prompter, PromptResult } from '../../shared/ui/prompter' |
| 6 | +import { DefaultCloudWatchLogsClient } from '../../shared/clients/cloudWatchLogsClient' |
| 7 | +import { createCommonButtons } from '../../shared/ui/buttons' |
| 8 | +import { createInputBox, InputBoxPrompter } from '../../shared/ui/inputPrompter' |
| 9 | +import { createQuickPick, DataQuickPickItem, QuickPickPrompter } from '../../shared/ui/pickerPrompter' |
| 10 | +import { pageableToCollection } from '../../shared/utilities/collectionUtils' |
| 11 | +import { CloudWatchLogs } from 'aws-sdk' |
| 12 | +import { isValidResponse, StepEstimator } from '../../shared/wizards/wizard' |
| 13 | +import { isNonNullable } from '../../shared/utilities/tsUtils' |
| 14 | +import { |
| 15 | + startLiveTailHelpUrl, |
| 16 | + startLiveTailLogStreamNamesHelpUrl, |
| 17 | + startLiveTailLogStreamPrefixHelpUrl, |
| 18 | +} from '../../shared/constants' |
| 19 | + |
| 20 | +export type LogStreamFilterType = 'menu' | 'prefix' | 'specific' | 'all' |
| 21 | + |
| 22 | +export interface LogStreamFilterResponse { |
| 23 | + readonly filter?: string |
| 24 | + readonly type: LogStreamFilterType |
| 25 | +} |
| 26 | + |
| 27 | +export class LogStreamFilterSubmenu extends Prompter<LogStreamFilterResponse> { |
| 28 | + private logStreamPrefixRegEx = /^[^:*]*$/ |
| 29 | + private currentState: LogStreamFilterType = 'menu' |
| 30 | + private steps?: [current: number, total: number] |
| 31 | + private region: string |
| 32 | + private logGroupArn: string |
| 33 | + public defaultPrompter: QuickPickPrompter<LogStreamFilterType> = this.createMenuPrompter() |
| 34 | + |
| 35 | + public constructor(logGroupArn: string, region: string) { |
| 36 | + super() |
| 37 | + this.region = region |
| 38 | + this.logGroupArn = logGroupArn |
| 39 | + } |
| 40 | + |
| 41 | + public createMenuPrompter() { |
| 42 | + const helpUri = startLiveTailHelpUrl |
| 43 | + const prompter = createQuickPick(this.menuOptions, { |
| 44 | + title: 'Select LogStream filter type', |
| 45 | + buttons: createCommonButtons(helpUri), |
| 46 | + }) |
| 47 | + return prompter |
| 48 | + } |
| 49 | + |
| 50 | + private get menuOptions(): DataQuickPickItem<LogStreamFilterType>[] { |
| 51 | + const options: DataQuickPickItem<LogStreamFilterType>[] = [] |
| 52 | + options.push({ |
| 53 | + label: 'All', |
| 54 | + detail: 'Include log events from all LogStreams in the selected LogGroup', |
| 55 | + data: 'all', |
| 56 | + }) |
| 57 | + options.push({ |
| 58 | + label: 'Specific', |
| 59 | + detail: 'Include log events from only a specific LogStream', |
| 60 | + data: 'specific', |
| 61 | + }) |
| 62 | + options.push({ |
| 63 | + label: 'Prefix', |
| 64 | + detail: 'Include log events from LogStreams that begin with a provided prefix', |
| 65 | + data: 'prefix', |
| 66 | + }) |
| 67 | + return options |
| 68 | + } |
| 69 | + |
| 70 | + public createLogStreamPrefixBox(): InputBoxPrompter { |
| 71 | + const helpUri = startLiveTailLogStreamPrefixHelpUrl |
| 72 | + return createInputBox({ |
| 73 | + title: 'Enter LogStream prefix', |
| 74 | + placeholder: 'logStream prefix (case sensitive; empty matches all)', |
| 75 | + prompt: 'Only log events in the LogStreams that have names that start with the prefix that you specify here are included in the Live Tail session', |
| 76 | + validateInput: (input) => this.validateLogStreamPrefix(input), |
| 77 | + buttons: createCommonButtons(helpUri), |
| 78 | + }) |
| 79 | + } |
| 80 | + |
| 81 | + public validateLogStreamPrefix(prefix: string) { |
| 82 | + if (prefix.length > 512) { |
| 83 | + return 'LogStream prefix cannot be longer than 512 characters' |
| 84 | + } |
| 85 | + |
| 86 | + if (!this.logStreamPrefixRegEx.test(prefix)) { |
| 87 | + return 'LogStream prefix must match pattern: [^:*]*' |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + public createLogStreamSelector(): QuickPickPrompter<string> { |
| 92 | + const helpUri = startLiveTailLogStreamNamesHelpUrl |
| 93 | + const client = new DefaultCloudWatchLogsClient(this.region) |
| 94 | + const request: CloudWatchLogs.DescribeLogStreamsRequest = { |
| 95 | + logGroupIdentifier: this.logGroupArn, |
| 96 | + orderBy: 'LastEventTime', |
| 97 | + descending: true, |
| 98 | + } |
| 99 | + const requester = (request: CloudWatchLogs.DescribeLogStreamsRequest) => client.describeLogStreams(request) |
| 100 | + const collection = pageableToCollection(requester, request, 'nextToken', 'logStreams') |
| 101 | + |
| 102 | + const items = collection |
| 103 | + .filter(isNonNullable) |
| 104 | + .map((streams) => streams!.map((stream) => ({ data: stream.logStreamName!, label: stream.logStreamName! }))) |
| 105 | + |
| 106 | + return createQuickPick(items, { |
| 107 | + title: 'Select LogStream', |
| 108 | + buttons: createCommonButtons(helpUri), |
| 109 | + }) |
| 110 | + } |
| 111 | + |
| 112 | + private switchState(newState: LogStreamFilterType) { |
| 113 | + this.currentState = newState |
| 114 | + } |
| 115 | + |
| 116 | + protected async promptUser(): Promise<PromptResult<LogStreamFilterResponse>> { |
| 117 | + while (true) { |
| 118 | + switch (this.currentState) { |
| 119 | + case 'menu': { |
| 120 | + const prompter = this.createMenuPrompter() |
| 121 | + this.steps && prompter.setSteps(this.steps[0], this.steps[1]) |
| 122 | + |
| 123 | + const resp = await prompter.prompt() |
| 124 | + if (resp === 'prefix') { |
| 125 | + this.switchState('prefix') |
| 126 | + } else if (resp === 'specific') { |
| 127 | + this.switchState('specific') |
| 128 | + } else if (resp === 'all') { |
| 129 | + return { filter: undefined, type: resp } |
| 130 | + } else { |
| 131 | + return undefined |
| 132 | + } |
| 133 | + |
| 134 | + break |
| 135 | + } |
| 136 | + case 'prefix': { |
| 137 | + const resp = await this.createLogStreamPrefixBox().prompt() |
| 138 | + if (isValidResponse(resp)) { |
| 139 | + return { filter: resp, type: 'prefix' } |
| 140 | + } |
| 141 | + this.switchState('menu') |
| 142 | + break |
| 143 | + } |
| 144 | + case 'specific': { |
| 145 | + const resp = await this.createLogStreamSelector().prompt() |
| 146 | + if (isValidResponse(resp)) { |
| 147 | + return { filter: resp, type: 'specific' } |
| 148 | + } |
| 149 | + this.switchState('menu') |
| 150 | + break |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + public setSteps(current: number, total: number): void { |
| 157 | + this.steps = [current, total] |
| 158 | + } |
| 159 | + |
| 160 | + // Unused |
| 161 | + public get recentItem(): any { |
| 162 | + return |
| 163 | + } |
| 164 | + public set recentItem(response: any) {} |
| 165 | + public setStepEstimator(estimator: StepEstimator<LogStreamFilterResponse>): void {} |
| 166 | +} |
0 commit comments