-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhistory.ts
151 lines (126 loc) · 4.57 KB
/
history.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import {Flags} from '@oclif/core'
import * as Ably from 'ably'
import chalk from 'chalk'
import {AblyBaseCommand} from '../../../base-command.js'
import { formatJson, isJsonData } from '../../../utils/json-formatter.js'
export default class LogsPushHistory extends AblyBaseCommand {
static override description = 'Retrieve push notification log history'
static override examples = [
'$ ably logs push history',
'$ ably logs push history --limit 20',
'$ ably logs push history --direction forwards',
'$ ably logs push history --json',
'$ ably logs push history --pretty-json']
static override flags = {
...AblyBaseCommand.globalFlags,
direction: Flags.string({
default: 'backwards',
description: 'Direction of log retrieval',
options: ['backwards', 'forwards'],
}),
limit: Flags.integer({
default: 100,
description: 'Maximum number of logs to retrieve',
}),
}
async run(): Promise<void> {
const {flags} = await this.parse(LogsPushHistory)
try {
// Get API key from flags or config
const apiKey = flags['api-key'] || await this.configManager.getApiKey()
if (!apiKey) {
await this.ensureAppAndKey(flags)
return
}
// Create the Ably REST client
const options: Ably.ClientOptions = this.getClientOptions(flags)
const client = new Ably.Rest(options)
const channelName = '[meta]log:push'
const channel = client.channels.get(channelName)
// Get message history
const historyOptions = {
direction: flags.direction as 'backwards' | 'forwards',
limit: flags.limit,
}
const historyPage = await channel.history(historyOptions)
const messages = historyPage.items
// Output results based on format
if (this.shouldOutputJson(flags)) {
this.log(this.formatJsonOutput({
messages: messages.map(msg => ({
channel: channelName,
clientId: msg.clientId,
connectionId: msg.connectionId,
data: msg.data,
encoding: msg.encoding,
id: msg.id,
name: msg.name,
timestamp: msg.timestamp ? new Date(msg.timestamp).toISOString() : new Date().toISOString()
})),
success: true
}, flags))
} else {
if (messages.length === 0) {
this.log('No push log messages found in history.')
return
}
this.log(`Found ${chalk.cyan(messages.length.toString())} push log messages:`)
this.log('')
for (const message of messages) {
const timestamp = message.timestamp
? new Date(message.timestamp).toISOString()
: 'Unknown timestamp'
const event = message.name || 'unknown'
// Color-code different event types based on severity
let eventColor = chalk.blue
// For push log events - based on examples and severity
if (message.data && typeof message.data === 'object' && 'severity' in message.data) {
const severity = message.data.severity as string
switch (severity) {
case 'error': {
eventColor = chalk.red
break;
}
case 'warning': {
eventColor = chalk.yellow
break;
}
case 'info': {
eventColor = chalk.green
break;
}
case 'debug': {
eventColor = chalk.blue
break;
}
// No default
}
}
// Format the log output
this.log(`${chalk.dim(`[${timestamp}]`)} Channel: ${chalk.cyan(channelName)} | Event: ${eventColor(event)}`)
if (message.data) {
this.log('Data:')
if (isJsonData(message.data)) {
this.log(formatJson(message.data))
} else {
this.log(String(message.data))
}
}
this.log('')
}
if (messages.length === flags.limit) {
this.log(chalk.yellow(`Showing maximum of ${flags.limit} logs. Use --limit to show more.`))
}
}
} catch (error) {
if (this.shouldOutputJson(flags)) {
this.log(this.formatJsonOutput({
error: error instanceof Error ? error.message : String(error),
success: false
}, flags))
} else {
this.error(`Error retrieving push notification logs: ${error instanceof Error ? error.message : String(error)}`)
}
}
}
}