@@ -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,19 @@ 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 = globals . clock . setInterval ( ( ) => {
43+ session . updateStatusBarItemText ( )
44+ } , 500 )
45+ hideShowStatusBarItemsOnActiveEditor ( session , document )
46+ registerTabChangeCallback ( session , registry , document , timer )
47+
3948 const stream = await session . startLiveTailSession ( )
4049
41- await handleSessionStream ( stream , document , session )
50+ await handleSessionStream ( stream , document , session , timer )
4251}
4352
44- export function closeSession ( sessionUri : vscode . Uri , registry : LiveTailSessionRegistry ) {
53+ export function closeSession ( sessionUri : vscode . Uri , registry : LiveTailSessionRegistry , timer : NodeJS . Timer ) {
54+ globals . clock . clearInterval ( timer )
4555 const session = registry . get ( sessionUri )
4656 if ( session === undefined ) {
4757 throw new ToolkitError ( `No LiveTail session found for URI: ${ sessionUri . toString ( ) } ` )
@@ -63,27 +73,35 @@ async function prepareDocument(session: LiveTailSession): Promise<vscode.TextDoc
6373 await clearDocument ( textDocument )
6474 await vscode . window . showTextDocument ( textDocument , { preview : false } )
6575 await vscode . languages . setTextDocumentLanguage ( textDocument , 'log' )
76+ session . showStatusBarItem ( true )
6677 return textDocument
6778}
6879
6980async function handleSessionStream (
7081 stream : AsyncIterable < StartLiveTailResponseStream > ,
7182 document : vscode . TextDocument ,
72- session : LiveTailSession
83+ session : LiveTailSession ,
84+ timer : NodeJS . Timer
7385) {
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 )
86+ try {
87+ for await ( const event of stream ) {
88+ if ( event . sessionUpdate !== undefined && event . sessionUpdate . sessionResults !== undefined ) {
89+ const formattedLogEvents = event . sessionUpdate . sessionResults . map < string > ( ( logEvent ) =>
90+ formatLogEvent ( logEvent )
91+ )
92+ if ( formattedLogEvents . length !== 0 ) {
93+ //Determine should scroll before adding new lines to doc because adding large
94+ //amount of new lines can push bottom of file out of view before scrolling.
95+ const editorsToScroll = getTextEditorsToScroll ( document )
96+ await updateTextDocumentWithNewLogEvents ( formattedLogEvents , document , session . maxLines )
97+ editorsToScroll . forEach ( scrollTextEditorToBottom )
98+ }
99+ session . eventRate = eventRate ( event . sessionUpdate )
100+ session . isSampled = isSampled ( event . sessionUpdate )
85101 }
86102 }
103+ } finally {
104+ globals . clock . clearInterval ( timer )
87105 }
88106}
89107
@@ -147,6 +165,22 @@ function trimOldestLines(
147165 edit . delete ( document . uri , range )
148166}
149167
168+ function isSampled ( event : LiveTailSessionUpdate ) : boolean {
169+ return event . sessionMetadata === undefined || event . sessionMetadata . sampled === undefined
170+ ? false
171+ : event . sessionMetadata . sampled
172+ }
173+
174+ function eventRate ( event : LiveTailSessionUpdate ) : number {
175+ return event . sessionResults === undefined ? 0 : event . sessionResults . length
176+ }
177+
178+ function hideShowStatusBarItemsOnActiveEditor ( session : LiveTailSession , document : vscode . TextDocument ) {
179+ vscode . window . onDidChangeActiveTextEditor ( ( editor ) => {
180+ session . showStatusBarItem ( editor ?. document === document )
181+ } )
182+ }
183+
150184/**
151185 * The LiveTail session should be automatically closed if the user does not have the session's
152186 * document in any Tab in their editor.
@@ -162,12 +196,13 @@ function trimOldestLines(
162196function registerTabChangeCallback (
163197 session : LiveTailSession ,
164198 registry : LiveTailSessionRegistry ,
165- document : vscode . TextDocument
199+ document : vscode . TextDocument ,
200+ timer : NodeJS . Timer
166201) {
167202 vscode . window . tabGroups . onDidChangeTabs ( ( tabEvent ) => {
168203 const isOpen = isLiveTailSessionOpenInAnyTab ( session )
169204 if ( ! isOpen ) {
170- closeSession ( session . uri , registry )
205+ closeSession ( session . uri , registry , timer )
171206 void clearDocument ( document )
172207 }
173208 } )
0 commit comments