@@ -8,8 +8,12 @@ import { TailLogGroupWizard } from '../wizard/tailLogGroupWizard'
88import { CancellationError } from '../../../shared/utilities/timeoutUtils'
99import { LiveTailSession , LiveTailSessionConfiguration } from '../registry/liveTailSession'
1010import { LiveTailSessionRegistry } from '../registry/liveTailSessionRegistry'
11- import { LiveTailSessionLogEvent , StartLiveTailResponseStream } from '@aws-sdk/client-cloudwatch-logs'
12- import { ToolkitError } from '../../../shared'
11+ import {
12+ LiveTailSessionLogEvent ,
13+ LiveTailSessionUpdate ,
14+ StartLiveTailResponseStream ,
15+ } from '@aws-sdk/client-cloudwatch-logs'
16+ import { convertToTimeString , globals , ToolkitError } from '../../../shared'
1317
1418export async function tailLogGroup (
1519 registry : LiveTailSessionRegistry ,
@@ -35,13 +39,17 @@ export async function tailLogGroup(
3539 registry . set ( session . uri , session )
3640
3741 const document = await prepareDocument ( session )
38- registerTabChangeCallback ( session , registry , document )
42+ const timer = startSessionTimer ( session )
43+ hideShowStatusBarItemsOnActiveEditor ( session , document )
44+ registerTabChangeCallback ( session , registry , document , timer )
45+
3946 const stream = await session . startLiveTailSession ( )
4047
41- await handleSessionStream ( stream , document , session )
48+ await handleSessionStream ( stream , document , session , timer )
4249}
4350
44- export function closeSession ( sessionUri : vscode . Uri , registry : LiveTailSessionRegistry ) {
51+ export function closeSession ( sessionUri : vscode . Uri , registry : LiveTailSessionRegistry , timer : NodeJS . Timer ) {
52+ globals . clock . clearInterval ( timer )
4553 const session = registry . get ( sessionUri )
4654 if ( session === undefined ) {
4755 throw new ToolkitError ( `No LiveTail session found for URI: ${ sessionUri . toString ( ) } ` )
@@ -63,27 +71,35 @@ async function prepareDocument(session: LiveTailSession): Promise<vscode.TextDoc
6371 await clearDocument ( textDocument )
6472 await vscode . window . showTextDocument ( textDocument , { preview : false } )
6573 await vscode . languages . setTextDocumentLanguage ( textDocument , 'log' )
74+ session . showStatusBarItems ( )
6675 return textDocument
6776}
6877
6978async function handleSessionStream (
7079 stream : AsyncIterable < StartLiveTailResponseStream > ,
7180 document : vscode . TextDocument ,
72- session : LiveTailSession
81+ session : LiveTailSession ,
82+ timer : NodeJS . Timer
7383) {
74- for await ( const event of stream ) {
75- if ( event . sessionUpdate !== undefined && event . sessionUpdate . sessionResults !== undefined ) {
76- const formattedLogEvents = event . sessionUpdate . sessionResults . map < string > ( ( logEvent ) =>
77- formatLogEvent ( logEvent )
78- )
79- if ( formattedLogEvents . length !== 0 ) {
80- //Determine should scroll before adding new lines to doc because adding large
81- //amount of new lines can push bottom of file out of view before scrolling.
82- const editorsToScroll = getTextEditorsToScroll ( document )
83- await updateTextDocumentWithNewLogEvents ( formattedLogEvents , document , session . maxLines )
84- editorsToScroll . forEach ( scrollTextEditorToBottom )
84+ try {
85+ for await ( const event of stream ) {
86+ if ( event . sessionUpdate !== undefined && event . sessionUpdate . sessionResults !== undefined ) {
87+ const formattedLogEvents = event . sessionUpdate . sessionResults . map < string > ( ( logEvent ) =>
88+ formatLogEvent ( logEvent )
89+ )
90+ if ( formattedLogEvents . length !== 0 ) {
91+ //Determine should scroll before adding new lines to doc because adding large
92+ //amount of new lines can push bottom of file out of view before scrolling.
93+ const editorsToScroll = getTextEditorsToScroll ( document )
94+ await updateTextDocumentWithNewLogEvents ( formattedLogEvents , document , session . maxLines )
95+ editorsToScroll . forEach ( scrollTextEditorToBottom )
96+ }
97+ updateStatusBarItemsOnStreamEvent ( session , event . sessionUpdate )
8598 }
8699 }
100+ } catch ( err ) {
101+ globals . clock . clearInterval ( timer )
102+ throw err
87103 }
88104}
89105
@@ -147,6 +163,52 @@ function trimOldestLines(
147163 edit . delete ( document . uri , range )
148164}
149165
166+ function updateStatusBarItemsOnStreamEvent ( session : LiveTailSession , event : LiveTailSessionUpdate ) {
167+ updateIsSampledStatusBar ( session . statusBarItems . isSampled , event )
168+ updateEventRateStatusBar ( session . statusBarItems . eventRate , event )
169+ }
170+
171+ function updateIsSampledStatusBar ( isSampledStatusBarItem : vscode . StatusBarItem , event : LiveTailSessionUpdate ) {
172+ let isSampled : boolean
173+ //sessionMetadata is expected to always be present on a LiveTail stream update
174+ if ( event . sessionMetadata === undefined || event . sessionMetadata . sampled === undefined ) {
175+ isSampled = false
176+ } else {
177+ isSampled = event . sessionMetadata . sampled
178+ }
179+ isSampledStatusBarItem . text = `Sampled: ${ isSampled ? 'Yes' : 'No' } `
180+ return isSampledStatusBarItem
181+ }
182+
183+ function updateEventRateStatusBar ( eventRateStatusBarItem : vscode . StatusBarItem , event : LiveTailSessionUpdate ) {
184+ let numEvents
185+ if ( event . sessionResults === undefined ) {
186+ numEvents = 0
187+ } else {
188+ numEvents = event . sessionResults . length
189+ }
190+ eventRateStatusBarItem . text = `${ numEvents } events/sec.`
191+ return eventRateStatusBarItem
192+ }
193+
194+ function hideShowStatusBarItemsOnActiveEditor ( session : LiveTailSession , document : vscode . TextDocument ) {
195+ vscode . window . onDidChangeActiveTextEditor ( ( editor ) => {
196+ if ( editor ?. document === document ) {
197+ session . showStatusBarItems ( )
198+ } else {
199+ session . hideStatusBarItems ( )
200+ }
201+ } )
202+ }
203+
204+ function startSessionTimer ( session : LiveTailSession ) : NodeJS . Timer {
205+ return globals . clock . setInterval ( ( ) => {
206+ const elapsedTime = session . getLiveTailSessionDuration ( )
207+ const timeString = convertToTimeString ( elapsedTime )
208+ session . statusBarItems . sessionTimer . text = `LiveTail Session Timer: ${ timeString } `
209+ } , 500 )
210+ }
211+
150212/**
151213 * The LiveTail session should be automatically closed if the user does not have the session's
152214 * document in any Tab in their editor.
@@ -162,12 +224,13 @@ function trimOldestLines(
162224function registerTabChangeCallback (
163225 session : LiveTailSession ,
164226 registry : LiveTailSessionRegistry ,
165- document : vscode . TextDocument
227+ document : vscode . TextDocument ,
228+ timer : NodeJS . Timer
166229) {
167230 vscode . window . tabGroups . onDidChangeTabs ( ( tabEvent ) => {
168231 const isOpen = isLiveTailSessionOpenInAnyTab ( session )
169232 if ( ! isOpen ) {
170- closeSession ( session . uri , registry )
233+ closeSession ( session . uri , registry , timer )
171234 void clearDocument ( document )
172235 }
173236 } )
0 commit comments