1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Cleanup pod log store (#2260)

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2021-05-27 10:13:24 -04:00 committed by GitHub
parent 652319ac89
commit 6104572fea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -55,6 +55,20 @@ export class LogStore {
}, { delay: 500 }); }, { delay: 500 });
} }
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.refresher.stop();
this.podLogs.set(tabId, message);
}
/** /**
* Function prepares tailLines param for passing to API request * Function prepares tailLines param for passing to API request
* Each time it increasing it's number, caused to fetch more logs. * Each time it increasing it's number, caused to fetch more logs.
@ -70,14 +84,8 @@ export class LogStore {
this.refresher.start(); this.refresher.start();
this.podLogs.set(tabId, logs); this.podLogs.set(tabId, logs);
} catch ({error}) { } catch (error) {
const message = [ this.handlerError(tabId, error);
`Failed to load logs: ${error.message}`,
`Reason: ${error.reason} (${error.code})`
];
this.refresher.stop();
this.podLogs.set(tabId, message);
} }
}; };
@ -88,7 +96,11 @@ export class LogStore {
* @param tabId * @param tabId
*/ */
loadMore = async (tabId: TabId) => { loadMore = async (tabId: TabId) => {
if (!this.podLogs.get(tabId).length) return; if (!this.podLogs.get(tabId).length) {
return;
}
try {
const oldLogs = this.podLogs.get(tabId); const oldLogs = this.podLogs.get(tabId);
const logs = await this.loadLogs(tabId, { const logs = await this.loadLogs(tabId, {
sinceTime: this.getLastSinceTime(tabId) sinceTime: this.getLastSinceTime(tabId)
@ -96,6 +108,9 @@ export class LogStore {
// Add newly received logs to bottom // Add newly received logs to bottom
this.podLogs.set(tabId, [...oldLogs, ...logs]); this.podLogs.set(tabId, [...oldLogs, ...logs]);
} catch (error) {
this.handlerError(tabId, error);
}
}; };
/** /**
@ -103,45 +118,40 @@ export class LogStore {
* an API request * an API request
* @param tabId * @param tabId
* @param params request parameters described in IPodLogsQuery interface * @param params request parameters described in IPodLogsQuery interface
* @returns {Promise} A fetch request promise * @returns A fetch request promise
*/ */
loadLogs = async (tabId: TabId, params: Partial<IPodLogsQuery>) => { @autobind()
async loadLogs(tabId: TabId, params: Partial<IPodLogsQuery>): Promise<string[]> {
const data = logTabStore.getData(tabId); const data = logTabStore.getData(tabId);
const { selectedContainer, previous } = data; const { selectedContainer, previous } = data;
const pod = new Pod(data.selectedPod); const pod = new Pod(data.selectedPod);
const namespace = pod.getNs(); const namespace = pod.getNs();
const name = pod.getName(); const name = pod.getName();
return podsApi.getLogs({ namespace, name }, { const result = await podsApi.getLogs({ namespace, name }, {
...params, ...params,
timestamps: true, // Always setting timestamp to separate old logs from new ones timestamps: true, // Always setting timestamp to separate old logs from new ones
container: selectedContainer.name, container: selectedContainer.name,
previous previous
}).then(result => {
const logs = [...result.split("\n")]; // Transform them into array
logs.pop(); // Remove last empty element
return logs;
}); });
};
return result.trimEnd().split("\n");
}
/** /**
* Converts logs into a string array * Converts logs into a string array
* @returns {number} Length of log lines * @returns Length of log lines
*/ */
@computed @computed
get lines() { get lines(): number {
const id = dockStore.selectedTabId; return this.logs.length;
const logs = this.podLogs.get(id);
return logs ? logs.length : 0;
} }
/** /**
* Returns logs with timestamps for selected tab * Returns logs with timestamps for selected tab
*/ */
@computed
get logs() { get logs() {
return this.podLogs.get(dockStore.selectedTabId) ?? []; return this.podLogs.get(dockStore.selectedTabId) ?? [];
} }
@ -150,6 +160,7 @@ export class LogStore {
* Removes timestamps from each log line and returns changed logs * Removes timestamps from each log line and returns changed logs
* @returns Logs without timestamps * @returns Logs without timestamps
*/ */
@computed
get logsWithoutTimestamps() { get logsWithoutTimestamps() {
return this.logs.map(item => this.removeTimestamps(item)); return this.logs.map(item => this.removeTimestamps(item));
} }