From 237ee74cfaaf35d6571c1e0647554f3170e51c0e Mon Sep 17 00:00:00 2001 From: Alex Andreev Date: Wed, 14 Oct 2020 15:34:43 +0300 Subject: [PATCH 1/4] Remove notiifications on log loading errors Signed-off-by: Alex Andreev --- src/renderer/components/dock/pod-logs.store.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/renderer/components/dock/pod-logs.store.ts b/src/renderer/components/dock/pod-logs.store.ts index 1cff25c80c..ebf958d431 100644 --- a/src/renderer/components/dock/pod-logs.store.ts +++ b/src/renderer/components/dock/pod-logs.store.ts @@ -72,7 +72,6 @@ export class PodLogsStore extends DockTabStore { _i18n._(t`Reason: ${error.reason} (${error.code})`) ].join("\n"); this.refresher.stop(); - Notifications.error(message); this.logs.set(tabId, message); } } From e85f975e4b0cec58cfc70d009d57054a9c313526 Mon Sep 17 00:00:00 2001 From: Alex Andreev Date: Thu, 15 Oct 2020 14:04:16 +0300 Subject: [PATCH 2/4] Transform logs into array in store Signed-off-by: Alex Andreev --- .../components/dock/pod-logs.store.ts | 21 +++++++------ src/renderer/components/dock/pod-logs.tsx | 30 ++++++++++--------- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/src/renderer/components/dock/pod-logs.store.ts b/src/renderer/components/dock/pod-logs.store.ts index ebf958d431..f859d1a667 100644 --- a/src/renderer/components/dock/pod-logs.store.ts +++ b/src/renderer/components/dock/pod-logs.store.ts @@ -5,7 +5,6 @@ import { DockTabStore } from "./dock-tab.store"; import { dockStore, IDockTab, TabKind } from "./dock.store"; import { t } from "@lingui/macro"; import { _i18n } from "../../i18n"; -import { Notifications } from "../notifications"; import { isDevelopment } from "../../../common/vars"; export interface IPodLogsData { @@ -18,7 +17,7 @@ export interface IPodLogsData { } type TabId = string; -type PodLogs = string; +type PodLogLine = string; // Number for log lines to load export const logRange = isDevelopment ? 100 : 1000; @@ -31,7 +30,7 @@ export class PodLogsStore extends DockTabStore { this.loadMore(id) }); - @observable logs = observable.map(); + @observable logs = observable.map(); @observable newLogSince = observable.map(); // Timestamp after which all logs are considered to be new constructor() { @@ -64,13 +63,13 @@ export class PodLogsStore extends DockTabStore { const logs = await this.loadLogs(tabId, { tailLines: this.lines + logRange }); - if (!this.refresher.isRunning) this.refresher.start(); + this.refresher.start(); this.logs.set(tabId, logs); } catch ({error}) { const message = [ _i18n._(t`Failed to load logs: ${error.message}`), _i18n._(t`Reason: ${error.reason} (${error.code})`) - ].join("\n"); + ]; this.refresher.stop(); this.logs.set(tabId, message); } @@ -88,7 +87,7 @@ export class PodLogsStore extends DockTabStore { sinceTime: this.getLastSinceTime(tabId) }); // Add newly received logs to bottom - this.logs.set(tabId, oldLogs + logs); + this.logs.set(tabId, [...oldLogs, ...logs]); } /** @@ -104,11 +103,15 @@ export class PodLogsStore extends DockTabStore { const pod = new Pod(data.pod); const namespace = pod.getNs(); const name = pod.getName(); - return await podsApi.getLogs({ namespace, name }, { + return podsApi.getLogs({ namespace, name }, { ...params, timestamps: true, // Always setting timestampt 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; }); } @@ -130,7 +133,7 @@ export class PodLogsStore extends DockTabStore { get lines() { const id = dockStore.selectedTabId; const logs = this.logs.get(id); - return logs ? logs.split("\n").length : 0; + return logs ? logs.length : 0; } /** @@ -139,7 +142,7 @@ export class PodLogsStore extends DockTabStore { * @param tabId */ getLastSinceTime(tabId: TabId) { - const timestamps = this.getTimestamps(this.logs.get(tabId)); + const timestamps = this.getTimestamps(this.logs.get(tabId).join("\n")); let stamp = new Date(0); if (timestamps) { stamp = new Date(timestamps.slice(-1)[0]); diff --git a/src/renderer/components/dock/pod-logs.tsx b/src/renderer/components/dock/pod-logs.tsx index efc3efb30a..e000922bb4 100644 --- a/src/renderer/components/dock/pod-logs.tsx +++ b/src/renderer/components/dock/pod-logs.tsx @@ -80,7 +80,7 @@ export class PodLogs extends React.Component { * scrolling position * @param scrollHeight previous scrollHeight position before adding new lines */ - preload = async (scrollHeight: number) => { + loadMore = async (scrollHeight: number) => { if (podLogsStore.lines < logRange) return; this.preloading = true; await podLogsStore.load(this.tabId).then(() => this.preloading = false); @@ -101,19 +101,21 @@ export class PodLogs extends React.Component { const logs = podLogsStore.logs.get(this.tabId); const { getData, removeTimestamps, newLogSince } = podLogsStore; const { showTimestamps } = getData(this.tabId); - let oldLogs = logs; - let newLogs = ""; + let oldLogs: string[] = logs; + let newLogs: string[] = []; if (newLogSince.has(this.tabId)) { // Finding separator timestamp in logs - const index = logs.indexOf(newLogSince.get(this.tabId)); + const index = logs.findIndex(item => item.includes(newLogSince.get(this.tabId))); if (index !== -1) { // Splitting logs to old and new ones - oldLogs = logs.substring(0, index); - newLogs = logs.substring(index); + oldLogs = logs.slice(0, index); + newLogs = logs.slice(index); + + } } if (!showTimestamps) { - return [removeTimestamps(oldLogs), removeTimestamps(newLogs)]; + return [oldLogs, newLogs].map(logs => logs.map(item => removeTimestamps(item))) } return [oldLogs, newLogs]; } @@ -135,7 +137,7 @@ export class PodLogs extends React.Component { const toBottomOffset = 100 * 16; // 100 lines * 16px (height of each line) const { scrollHeight, clientHeight, scrollTop } = logsArea; if (scrollTop === 0) { - this.preload(scrollHeight); + this.loadMore(scrollHeight); } if (scrollHeight - scrollTop > toBottomOffset) { this.showJumpToBottom = true; @@ -149,7 +151,7 @@ export class PodLogs extends React.Component { const { pod, selectedContainer } = this.tabData; const fileName = selectedContainer ? selectedContainer.name : pod.getName(); const [oldLogs, newLogs] = this.logs; - downloadFile(fileName + ".log", oldLogs + newLogs, "text/plain"); + downloadFile(fileName + ".log", [...oldLogs, ...newLogs].join("\n"), "text/plain"); } onContainerChange = (option: SelectOption) => { @@ -208,7 +210,7 @@ export class PodLogs extends React.Component { renderControls() { if (!this.ready) return null; const { selectedContainer, showTimestamps, previous } = this.tabData; - const timestamps = podLogsStore.getTimestamps(podLogsStore.logs.get(this.tabId)); + const timestamps = podLogsStore.getTimestamps(podLogsStore.logs.get(this.tabId).join("\n")); return (
Container @@ -255,7 +257,7 @@ export class PodLogs extends React.Component { if (!this.ready) { return ; } - if (!oldLogs && !newLogs) { + if (!oldLogs.length && !newLogs.length) { return (
There are no logs available for container. @@ -269,11 +271,11 @@ export class PodLogs extends React.Component {
)} -
- {newLogs && ( +
+ {newLogs.length > 0 && ( <>

-

+
)} From dd8efbc7de507be7427f3407cf85fd35d51b4442 Mon Sep 17 00:00:00 2001 From: Alex Andreev Date: Thu, 15 Oct 2020 14:32:21 +0300 Subject: [PATCH 3/4] Clean up Signed-off-by: Alex Andreev --- src/renderer/components/dock/pod-logs.tsx | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/renderer/components/dock/pod-logs.tsx b/src/renderer/components/dock/pod-logs.tsx index e000922bb4..2092b601ad 100644 --- a/src/renderer/components/dock/pod-logs.tsx +++ b/src/renderer/components/dock/pod-logs.tsx @@ -110,8 +110,6 @@ export class PodLogs extends React.Component { // Splitting logs to old and new ones oldLogs = logs.slice(0, index); newLogs = logs.slice(index); - - } } if (!showTimestamps) { From 0aee5554b00f1d5d0a214cf9316a08978ad92d9c Mon Sep 17 00:00:00 2001 From: Alex Andreev Date: Thu, 15 Oct 2020 14:41:42 +0300 Subject: [PATCH 4/4] Remove excessive join('\n\)s Signed-off-by: Alex Andreev --- src/renderer/components/dock/pod-logs.store.ts | 10 ++++------ src/renderer/components/dock/pod-logs.tsx | 6 +++--- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/renderer/components/dock/pod-logs.store.ts b/src/renderer/components/dock/pod-logs.store.ts index f859d1a667..6c7cba8aea 100644 --- a/src/renderer/components/dock/pod-logs.store.ts +++ b/src/renderer/components/dock/pod-logs.store.ts @@ -142,12 +142,10 @@ export class PodLogsStore extends DockTabStore { * @param tabId */ getLastSinceTime(tabId: TabId) { - const timestamps = this.getTimestamps(this.logs.get(tabId).join("\n")); - let stamp = new Date(0); - if (timestamps) { - stamp = new Date(timestamps.slice(-1)[0]); - stamp.setSeconds(stamp.getSeconds() + 1); // avoid duplicates from last second - } + const logs = this.logs.get(tabId); + const timestamps = this.getTimestamps(logs[logs.length - 1]); + const stamp = new Date(timestamps[0]); + stamp.setSeconds(stamp.getSeconds() + 1); // avoid duplicates from last second return stamp.toISOString(); } diff --git a/src/renderer/components/dock/pod-logs.tsx b/src/renderer/components/dock/pod-logs.tsx index 2092b601ad..2a69fa7f2e 100644 --- a/src/renderer/components/dock/pod-logs.tsx +++ b/src/renderer/components/dock/pod-logs.tsx @@ -208,7 +208,7 @@ export class PodLogs extends React.Component { renderControls() { if (!this.ready) return null; const { selectedContainer, showTimestamps, previous } = this.tabData; - const timestamps = podLogsStore.getTimestamps(podLogsStore.logs.get(this.tabId).join("\n")); + const since = podLogsStore.getTimestamps(podLogsStore.logs.get(this.tabId)[0]); return (
Container @@ -220,10 +220,10 @@ export class PodLogs extends React.Component { autoConvertOptions={false} />
- {timestamps && ( + {since && ( <> Since{" "} - {new Date(timestamps[0]).toLocaleString()} + {new Date(since[0]).toLocaleString()} )}