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 });
}
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
* Each time it increasing it's number, caused to fetch more logs.
@ -70,14 +84,8 @@ export class LogStore {
this.refresher.start();
this.podLogs.set(tabId, logs);
} catch ({error}) {
const message = [
`Failed to load logs: ${error.message}`,
`Reason: ${error.reason} (${error.code})`
];
this.refresher.stop();
this.podLogs.set(tabId, message);
} catch (error) {
this.handlerError(tabId, error);
}
};
@ -88,7 +96,11 @@ export class LogStore {
* @param 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 logs = await this.loadLogs(tabId, {
sinceTime: this.getLastSinceTime(tabId)
@ -96,6 +108,9 @@ export class LogStore {
// Add newly received logs to bottom
this.podLogs.set(tabId, [...oldLogs, ...logs]);
} catch (error) {
this.handlerError(tabId, error);
}
};
/**
@ -103,45 +118,40 @@ export class LogStore {
* an API request
* @param tabId
* @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 { selectedContainer, previous } = data;
const pod = new Pod(data.selectedPod);
const namespace = pod.getNs();
const name = pod.getName();
return podsApi.getLogs({ namespace, name }, {
const result = await podsApi.getLogs({ namespace, name }, {
...params,
timestamps: true, // Always setting timestamp to separate old logs from new ones
container: selectedContainer.name,
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
* @returns {number} Length of log lines
* @returns Length of log lines
*/
@computed
get lines() {
const id = dockStore.selectedTabId;
const logs = this.podLogs.get(id);
return logs ? logs.length : 0;
get lines(): number {
return this.logs.length;
}
/**
* Returns logs with timestamps for selected tab
*/
@computed
get logs() {
return this.podLogs.get(dockStore.selectedTabId) ?? [];
}
@ -150,6 +160,7 @@ export class LogStore {
* Removes timestamps from each log line and returns changed logs
* @returns Logs without timestamps
*/
@computed
get logsWithoutTimestamps() {
return this.logs.map(item => this.removeTimestamps(item));
}