-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
store.ts
231 lines (190 loc) · 6.7 KB
/
store.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { IComputedValue } from "mobx";
import { observable } from "mobx";
import type { PodLogsQuery, Pod } from "../../../../common/k8s-api/endpoints";
import { waitUntilDefined } from "../../../../common/utils/wait";
import type { IntervalFn } from "../../../utils";
import { getOrInsertWith, interval } from "../../../utils";
import type { TabId } from "../dock/store";
import type { CallForLogs } from "./call-for-logs.injectable";
import type { LogTabData } from "./tab-store";
type PodLogLine = string;
const logLinesToLoad = 500;
interface Dependencies {
callForLogs: CallForLogs;
}
export class LogStore {
protected podLogs = observable.map<TabId, PodLogLine[]>();
protected refreshers = new Map<TabId, IntervalFn>();
constructor(private dependencies: Dependencies) {}
protected handlerError(tabId: TabId, error: any): void {
if (error.error && !(error.message || error.reason || error.code)) {
error = error.error;
}
const message = [
`Failed to load logs: ${error.message}`,
`Reason: ${error.reason} (${error.code})`,
];
this.stopLoadingLogs(tabId);
this.podLogs.set(tabId, message);
}
/**
* Function prepares tailLines param for passing to API request
* Each time it increasing it's number, caused to fetch more logs.
* Also, it handles loading errors, rewriting whole logs with error
* messages
*/
public async load(tabId: TabId, computedPod: IComputedValue<Pod | undefined>, logTabData: IComputedValue<LogTabData | undefined>): Promise<void> {
try {
const logs = await this.loadLogs(computedPod, logTabData, {
tailLines: this.getLogLines(tabId) + logLinesToLoad,
});
this.getRefresher(tabId, computedPod, logTabData).start();
this.podLogs.set(tabId, logs);
} catch (error) {
this.handlerError(tabId, error);
}
}
private getRefresher(tabId: TabId, computedPod: IComputedValue<Pod | undefined>, logTabData: IComputedValue<LogTabData | undefined>): IntervalFn {
return getOrInsertWith(this.refreshers, tabId, () => (
interval(10, () => {
if (this.podLogs.has(tabId)) {
this.loadMore(tabId, computedPod, logTabData);
}
})
));
}
/**
* Stop loading more logs for a given tab
* @param tabId The ID of the logs tab to stop loading more logs for
*/
public stopLoadingLogs(tabId: TabId): void {
this.refreshers.get(tabId)?.stop();
}
/**
* Function is used to refresher/stream-like requests.
* It changes 'sinceTime' param each time allowing to fetch logs
* starting from last line received.
* @param tabId
*/
public async loadMore(tabId: TabId, computedPod: IComputedValue<Pod | undefined>, logTabData: IComputedValue<LogTabData | undefined>): Promise<void> {
const oldLogs = this.podLogs.get(tabId);
if (!oldLogs?.length) {
return;
}
try {
const logs = await this.loadLogs(computedPod, logTabData, {
sinceTime: this.getLastSinceTime(tabId),
});
// Add newly received logs to bottom
this.podLogs.set(tabId, [...oldLogs, ...logs.filter(Boolean)]);
} catch (error) {
this.handlerError(tabId, error);
}
}
/**
* Main logs loading function adds necessary data to payload and makes
* an API request
* @param tabId
* @param params request parameters described in IPodLogsQuery interface
* @returns A fetch request promise
*/
private async loadLogs(computedPod: IComputedValue<Pod | undefined>, logTabData: IComputedValue<LogTabData | undefined>, params: Partial<PodLogsQuery>): Promise<string[]> {
const {
pod,
tabData: {
selectedContainer,
showPrevious,
},
} = await waitUntilDefined(() => {
const pod = computedPod.get();
const tabData = logTabData.get();
if (pod && tabData) {
return { pod, tabData };
}
return undefined;
});
const namespace = pod.getNs();
const name = pod.getName();
const result = await this.dependencies.callForLogs({ namespace, name }, {
...params,
timestamps: true, // Always setting timestamp to separate old logs from new ones
container: selectedContainer,
previous: showPrevious,
});
return result.trimEnd().replace(/\r/g, "\n").split("\n");
}
/**
* @deprecated This depends on dockStore, which should be removed
* Converts logs into a string array
* @returns Length of log lines
*/
get lines(): number {
return this.logs.length;
}
getLogLines(tabId: TabId): number{
return this.getLogs(tabId).length;
}
areLogsPresent(tabId: TabId): boolean {
return !this.podLogs.has(tabId);
}
getLogs(tabId: TabId): string[]{
return this.podLogs.get(tabId) ?? [];
}
getLogsWithoutTimestamps(tabId: TabId): string[]{
return this.getLogs(tabId).map(this.removeTimestamps);
}
getTimestampSplitLogs(tabId: TabId): [string, string][]{
return this.getLogs(tabId).map(this.splitOutTimestamp);
}
/**
* @deprecated This now only returns the empty array
* Returns logs with timestamps for selected tab
*/
get logs(): string[] {
return [];
}
/**
* @deprecated This now only returns the empty array
* Removes timestamps from each log line and returns changed logs
* @returns Logs without timestamps
*/
get logsWithoutTimestamps(): string[] {
return this.logs.map(item => this.removeTimestamps(item));
}
/**
* It gets timestamps from all logs then returns last one + 1 second
* (this allows to avoid getting the last stamp in the selection)
* @param tabId
*/
getLastSinceTime(tabId: TabId): string {
const logs = this.podLogs.get(tabId) ?? [];
const timestamps = this.getTimestamps(logs[logs.length - 1]);
const stamp = timestamps[0] ? new Date(timestamps[0]) : new Date();
stamp.setSeconds(stamp.getSeconds() + 1); // avoid duplicates from last second
return stamp.toISOString();
}
splitOutTimestamp(logs: string): [string, string] {
const extraction = /^(\d+\S+)(.*)/m.exec(logs);
if (!extraction || extraction.length < 3) {
return ["", logs];
}
return [extraction[1], extraction[2]];
}
getTimestamps(logs: string): RegExpMatchArray {
return logs.match(/^\d+\S+/gm) ?? [];
}
removeTimestamps(logs: string): string {
return logs.replace(/^\d+.*?\s/gm, "");
}
clearLogs(tabId: TabId): void {
this.podLogs.delete(tabId);
}
reload(tabId: TabId, computedPod: IComputedValue<Pod | undefined>, logTabData: IComputedValue<LogTabData | undefined>): Promise<void> {
this.clearLogs(tabId);
return this.load(tabId, computedPod, logTabData);
}
}