Skip to content

Commit 2d2569b

Browse files
committed
fix: use time ago format and fix issues with selectCloudWatchLogStream
1 parent 086557f commit 2d2569b

7 files changed

Lines changed: 60 additions & 40 deletions

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,13 @@
6060
"typescript": "^5.1.0"
6161
},
6262
"dependencies": {
63-
"@aws-sdk/client-ec2": "^3.499.0",
6463
"@aws-sdk/client-cloudwatch-logs": "^3.499.0",
64+
"@aws-sdk/client-ec2": "^3.499.0",
6565
"@babel/runtime": "^7.18.6",
6666
"async-autocomplete-cli": "^1.0.0",
6767
"chalk": "^4.0.0",
6868
"fs-extra": "^9.1.0",
69+
"javascript-time-ago": "^2.5.10",
6970
"jmespath": "^0.16.0",
7071
"yargs": "^17.7.2"
7172
},

pnpm-lock.yaml

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/selectCloudWatchLogGroup.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import {
1717
import { Readable, Writable } from 'stream'
1818
import { loadRecents, addRecent } from './recents'
1919
import stripAnsi from 'strip-ansi'
20-
import { formatDate } from './formatDate'
2120
import { column } from './column'
21+
import timeAgo from './timeAgo'
2222

2323
export type LogGroupForChoice = Pick<
2424
LogGroup,
@@ -36,10 +36,7 @@ function createChoice(
3636
options?.recent
3737
? chalk.magentaBright('(recent)')
3838
: ' '.repeat('(recent)'.length)
39-
} ${column(
40-
formatDate(new Date(creationTime ?? NaN)),
41-
'2022/03/17 17:37'.length
42-
)}`
39+
} ${column(timeAgo(creationTime ?? NaN), '2022/03/17 17:37'.length)}`
4340
return {
4441
title:
4542
column(

src/selectCloudWatchLogStream.ts

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ import {
1919
import { Readable, Writable } from 'stream'
2020
import { loadRecents, addRecent } from './recents'
2121
import stripAnsi from 'strip-ansi'
22-
import { formatDate } from './formatDate'
2322
import { column } from './column'
2423
import selectCloudWatchLogGroup from './selectCloudWatchLogGroup'
24+
import timeAgo from './timeAgo'
2525

2626
export type LogStreamForChoice = Pick<
2727
LogStream,
28-
'arn' | 'logStreamName' | 'creationTime'
28+
'arn' | 'logStreamName' | 'creationTime' | 'lastEventTimestamp'
2929
>
3030

3131
type 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) {

src/selectEBSSnapshot.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import {
2121
} from '@aws-sdk/client-ec2'
2222

2323
import { loadRecents, addRecent } from './recents'
24-
import { formatDate } from './formatDate'
2524
import { column } from './column'
25+
import timeAgo from './timeAgo'
2626

2727
function formatState(State: SnapshotState | null | undefined): string {
2828
if (!State) return ''
@@ -58,7 +58,7 @@ function createChoice(
5858
)} ${column(
5959
options?.recent ? chalk.magentaBright('(recent)') : formatState(State),
6060
stateLength
61-
)} ${column(formatDate(StartTime), '2022/03/17 17:37'.length)}`
61+
)} ${column(timeAgo(StartTime ?? NaN), '59 minutes ago'.length)}`
6262
return {
6363
title:
6464
column(

src/selectEC2Instance.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import {
1818
import { Readable, Writable } from 'stream'
1919
import { loadRecents, addRecent } from './recents'
2020
import stripAnsi from 'strip-ansi'
21-
import { formatDate } from './formatDate'
2221
import { column } from './column'
22+
import timeAgo from './timeAgo'
2323

2424
function formatState(State: InstanceState | null | undefined): string {
2525
if (!State) return ''
@@ -58,7 +58,7 @@ function createChoice(
5858
const rest = ` ${column(InstanceId, 19)} ${column(
5959
options?.recent ? chalk.magentaBright('(recent)') : formatState(State),
6060
stateLength
61-
)} ${column(formatDate(LaunchTime), '2022/03/17 17:37'.length)}`
61+
)} ${column(timeAgo(LaunchTime ?? NaN), '59 minutes ago'.length)}`
6262
return {
6363
title:
6464
column(

src/timeAgo.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import TimeAgo from 'javascript-time-ago'
2+
3+
import en from 'javascript-time-ago/locale/en'
4+
5+
TimeAgo.addLocale(en)
6+
7+
const _timeAgo = new TimeAgo('en-US')
8+
9+
export default function timeAgo(input: number | Date): string {
10+
const result = _timeAgo.format(input)
11+
return Array.isArray(result) ? result[0] : result
12+
}

0 commit comments

Comments
 (0)