@@ -19,13 +19,13 @@ import {
1919import { Readable , Writable } from 'stream'
2020import { loadRecents , addRecent } from './recents'
2121import stripAnsi from 'strip-ansi'
22- import { formatDate } from './formatDate'
2322import { column } from './column'
2423import selectCloudWatchLogGroup from './selectCloudWatchLogGroup'
24+ import timeAgo from './timeAgo'
2525
2626export type LogStreamForChoice = Pick <
2727 LogStream ,
28- 'arn' | 'logStreamName' | 'creationTime'
28+ 'arn' | 'logStreamName' | 'creationTime' | 'lastEventTimestamp'
2929>
3030
3131type ChoiceProps = {
@@ -38,15 +38,12 @@ function createChoice(
3838 LogStream : LogStreamForChoice ,
3939 options ?: { recent ?: boolean }
4040) : Choice < ChoiceProps > {
41- const { arn, logStreamName, creationTime } = LogStream
41+ const { arn, logStreamName, lastEventTimestamp } = LogStream
4242 const rest = ` ${
4343 options ?. recent
4444 ? chalk . magentaBright ( '(recent)' )
4545 : ' ' . repeat ( '(recent)' . length )
46- } ${ column (
47- formatDate ( new Date ( creationTime ?? NaN ) ) ,
48- '2022/03/17 17:37' . length
49- ) } `
46+ } ${ column ( timeAgo ( lastEventTimestamp ?? NaN ) , '59 minutes ago' . length ) } `
5047 return {
5148 title :
5249 column (
@@ -103,6 +100,28 @@ export default async function selectCloudWatchLogStream({
103100 }
104101
105102 const cachedPages : DescribeLogStreamsCommandOutput [ ] = [ ]
103+ let loadedCount = 0
104+
105+ const args : DescribeLogStreamsRequest = {
106+ limit : 50 ,
107+ logGroupName,
108+ logGroupIdentifier,
109+ orderBy : 'LastEventTime' ,
110+ descending : true ,
111+ }
112+ async function * paginateWithCache ( ) {
113+ yield * cachedPages
114+ if ( loadedCount >= MaxLoad ) return
115+ for await ( const page of paginateDescribeLogStreams (
116+ { client : logs } ,
117+ { ...args , nextToken : cachedPages [ cachedPages . length - 1 ] ?. nextToken }
118+ ) ) {
119+ cachedPages . push ( page )
120+ yield page
121+ loadedCount += page . logStreams ?. length ?? 0
122+ if ( loadedCount >= MaxLoad ) break
123+ }
124+ }
106125
107126 const selected = await asyncAutocomplete ( {
108127 ...autocompleteOpts ,
@@ -129,14 +148,6 @@ export default async function selectCloudWatchLogStream({
129148 yieldChoices ( choices )
130149 }
131150
132- let loadedCount = 0
133- const args : DescribeLogStreamsRequest = {
134- limit : 50 ,
135- logGroupName,
136- logGroupIdentifier,
137- orderBy : 'LastEventTime' ,
138- descending : true ,
139- }
140151 let regex
141152 try {
142153 regex = new RegExp ( input . trim ( ) , 'i' )
@@ -150,19 +161,7 @@ export default async function selectCloudWatchLogStream({
150161 const ac = new AbortController ( )
151162 cancelationToken . once ( 'canceled' , ( ) => ac . abort ( ) )
152163
153- async function * paginateWithLoaded ( ) {
154- yield * cachedPages
155- for await ( const page of paginateDescribeLogStreams (
156- { client : logs } ,
157- { ...args , nextToken : cachedPages [ cachedPages . length - 1 ] ?. nextToken }
158- ) ) {
159- cachedPages . push ( page )
160- yield page
161- }
162- }
163-
164- for await ( const page of paginateWithLoaded ( ) ) {
165- cachedPages . push ( page )
164+ for await ( const page of paginateWithCache ( ) ) {
166165 const { logStreams = [ ] } = page
167166 if ( ac . signal . aborted ) break
168167 for ( const LogStream of logStreams ) {
@@ -175,8 +174,6 @@ export default async function selectCloudWatchLogStream({
175174 if ( choices . length >= MaxResults ) break
176175 }
177176 if ( choices . length >= MaxResults ) break
178- loadedCount += logStreams . length
179- if ( loadedCount >= MaxLoad ) break
180177 }
181178
182179 if ( ! choices . length ) {
0 commit comments