From f16845b21f9513d298f10a54e3b4c45079f75497 Mon Sep 17 00:00:00 2001 From: Alex Andreev Date: Wed, 14 Oct 2020 10:00:50 +0300 Subject: [PATCH] Moving newLogsSince determination out of component Signed-off-by: Alex Andreev --- .../components/dock/pod-logs.store.ts | 55 ++++++++++++------- src/renderer/components/dock/pod-logs.tsx | 15 ++--- 2 files changed, 40 insertions(+), 30 deletions(-) diff --git a/src/renderer/components/dock/pod-logs.store.ts b/src/renderer/components/dock/pod-logs.store.ts index a86de9f6c2..421bfeac27 100644 --- a/src/renderer/components/dock/pod-logs.store.ts +++ b/src/renderer/components/dock/pod-logs.store.ts @@ -1,4 +1,4 @@ -import { autorun, computed, observable } from "mobx"; +import { autorun, computed, observable, reaction } from "mobx"; import { Pod, IPodContainer, podsApi, IPodLogsQuery } from "../../api/endpoints"; import { autobind, interval } from "../../utils"; import { DockTabStore } from "./dock-tab.store"; @@ -6,6 +6,7 @@ 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 { pod: Pod; @@ -14,13 +15,14 @@ export interface IPodLogsData { initContainers: IPodContainer[] showTimestamps: boolean previous: boolean + newLogsSince?: string // Timestamp after which all logs are considered to be new } type TabId = string; type PodLogs = string; // Number for log lines to load -export const logRange = 100; // TODO: Change to 1000 for production +export const logRange = isDevelopment ? 100 : 1000; @autobind() export class PodLogsStore extends DockTabStore { @@ -44,6 +46,10 @@ export class PodLogsStore extends DockTabStore { this.refresher.stop(); } }, { delay: 500 }); + + reaction(() => this.logs.get(dockStore.selectedTabId), () => { + this.setNewLogSince(dockStore.selectedTabId); + }) } /** @@ -54,23 +60,21 @@ export class PodLogsStore extends DockTabStore { * @param tabId */ load = async (tabId: TabId) => { - const logs = await this.loadLogs(tabId, { - tailLines: this.lines + logRange - }) - .then(logs => { - if (!this.refresher.isRunning) this.refresher.start(); - return 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(); - Notifications.error(message); - return message; + try { + const logs = await this.loadLogs(tabId, { + tailLines: this.lines + logRange }); - this.logs.set(tabId, logs); + if (!this.refresher.isRunning) 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(); + Notifications.error(message); + this.logs.set(tabId, message); + } } /** @@ -85,7 +89,6 @@ export class PodLogsStore extends DockTabStore { sinceTime: this.getLastSinceTime(tabId) }); // Add newly received logs to bottom - // TODO: set a new log separator here this.logs.set(tabId, oldLogs + logs); } @@ -110,6 +113,20 @@ export class PodLogsStore extends DockTabStore { }); } + /** + * Sets newLogSince separator timestamp to split old logs from new ones + * @param tabId + */ + setNewLogSince(tabId: TabId) { + const data = this.data.get(tabId); + if (data.newLogsSince || !this.logs.has(tabId)) return; + const timestamp = podLogsStore.getLastSinceTime(tabId); + this.setData(tabId, { + ...data, + newLogsSince: timestamp.split(".")[0] // Removing milliseconds from string + }) + } + /** * Converts logs into a string array * @returns {number} Length of log lines diff --git a/src/renderer/components/dock/pod-logs.tsx b/src/renderer/components/dock/pod-logs.tsx index 7b10074b1f..d00809231a 100644 --- a/src/renderer/components/dock/pod-logs.tsx +++ b/src/renderer/components/dock/pod-logs.tsx @@ -25,7 +25,6 @@ export class PodLogs extends React.Component { @observable ready = false; @observable preloading = false; // Indicator for setting Spinner (loader) at the top of the logs @observable showJumpToBottom = false; - @observable newLogsSince = ""; // The time after which the logs are considered to be new private logsElement: HTMLDivElement; private lastLineIsShown = true; // used for proper auto-scroll content after refresh @@ -39,13 +38,7 @@ export class PodLogs extends React.Component { return; } await this.load(); - }, { fireImmediately: true }), - // Setting newLogSince separator timestamp to split old logs from new ones - reaction(() => podLogsStore.logs.get(this.tabId), () => { - if (this.newLogsSince) return; - const timestamp = podLogsStore.getLastSinceTime(this.tabId); - this.newLogsSince = timestamp.split(".")[0]; // Removing milliseconds from string - }) + }, { fireImmediately: true }) ]); } @@ -107,12 +100,12 @@ export class PodLogs extends React.Component { if (!podLogsStore.logs.has(this.tabId)) return []; const logs = podLogsStore.logs.get(this.tabId); const { getData, removeTimestamps } = podLogsStore; - const { showTimestamps } = getData(this.tabId); + const { showTimestamps, newLogsSince } = getData(this.tabId); let oldLogs = logs; let newLogs = ""; - if (this.newLogsSince) { + if (newLogsSince) { // Finding separator timestamp in logs - const index = logs.indexOf(this.newLogsSince); + const index = logs.indexOf(newLogsSince); if (index !== -1) { // Splitting logs to old and new ones oldLogs = logs.substring(0, index);