@@ -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 { 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,34 @@ 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 . showStatusBarItem ( true )
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+ } finally {
101+ globals . clock . clearInterval ( timer )
87102 }
88103}
89104
@@ -147,6 +162,38 @@ function trimOldestLines(
147162 edit . delete ( document . uri , range )
148163}
149164
165+ function updateStatusBarItemsOnStreamEvent ( session : LiveTailSession , event : LiveTailSessionUpdate ) {
166+ updateIsSampled ( session , event )
167+ updateEventRate ( session , event )
168+ }
169+
170+ function updateIsSampled ( session : LiveTailSession , event : LiveTailSessionUpdate ) {
171+ session . isSampled =
172+ event . sessionMetadata === undefined || event . sessionMetadata . sampled === undefined
173+ ? false
174+ : event . sessionMetadata . sampled
175+ }
176+
177+ function updateEventRate ( session : LiveTailSession , event : LiveTailSessionUpdate ) {
178+ session . eventRate = event . sessionResults === undefined ? 0 : event . sessionResults . length
179+ }
180+
181+ function hideShowStatusBarItemsOnActiveEditor ( session : LiveTailSession , document : vscode . TextDocument ) {
182+ vscode . window . onDidChangeActiveTextEditor ( ( editor ) => {
183+ if ( editor ?. document === document ) {
184+ session . showStatusBarItem ( true )
185+ } else {
186+ session . showStatusBarItem ( false )
187+ }
188+ } )
189+ }
190+
191+ function startSessionTimer ( session : LiveTailSession ) : NodeJS . Timer {
192+ return globals . clock . setInterval ( ( ) => {
193+ session . updateStatusBarItemText ( )
194+ } , 500 )
195+ }
196+
150197/**
151198 * The LiveTail session should be automatically closed if the user does not have the session's
152199 * document in any Tab in their editor.
@@ -162,12 +209,13 @@ function trimOldestLines(
162209function registerTabChangeCallback (
163210 session : LiveTailSession ,
164211 registry : LiveTailSessionRegistry ,
165- document : vscode . TextDocument
212+ document : vscode . TextDocument ,
213+ timer : NodeJS . Timer
166214) {
167215 vscode . window . tabGroups . onDidChangeTabs ( ( tabEvent ) => {
168216 const isOpen = isLiveTailSessionOpenInAnyTab ( session )
169217 if ( ! isOpen ) {
170- closeSession ( session . uri , registry )
218+ closeSession ( session . uri , registry , timer )
171219 void clearDocument ( document )
172220 }
173221 } )
0 commit comments