mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Adding LogTabStore
Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
1 parent
83ed44f670
commit
3ed0173d48
14 files changed
+792
-109
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import { action, computed, observable } from "mobx";
|
||||
import { action, computed, observable,reaction } from "mobx";
|
||||
import { dockStore } from "../renderer/components/dock/dock.store";
|
||||
import { autobind } from "../renderer/utils";
|
||||
|
||||
export class SearchStore {
|
||||
@@ -6,6 +7,12 @@ export class SearchStore {
|
||||
@observable occurrences: number[] = []; // Array with line numbers, eg [0, 0, 10, 21, 21, 40...]
|
||||
@observable activeOverlayIndex = -1; // Index withing the occurences array. Showing where is activeOverlay currently located
|
||||
|
||||
constructor() {
|
||||
reaction(() => dockStore.selectedTabId, () => {
|
||||
searchStore.reset();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets default activeOverlayIndex
|
||||
* @param text An array of any textual data (logs, for example)
|
||||
|
||||
@@ -38,4 +38,4 @@ export * from "../../renderer/components/+events/kube-event-details";
|
||||
// specific exports
|
||||
export * from "../../renderer/components/status-brick";
|
||||
export { terminalStore, createTerminalTab } from "../../renderer/components/dock/terminal.store";
|
||||
export { createPodLogsTab } from "../../renderer/components/dock/log.store";
|
||||
export { logTabStore } from "../../renderer/components/dock/log-tab.store";
|
||||
@@ -18,7 +18,7 @@ export class DaemonSetStore extends KubeObjectStore<DaemonSet> {
|
||||
}
|
||||
|
||||
getChildPods(daemonSet: DaemonSet): Pod[] {
|
||||
return podsStore.getPodsByOwner(daemonSet);
|
||||
return podsStore.getPodsByOwnerId(daemonSet.getId());
|
||||
}
|
||||
|
||||
getStatuses(daemonSets?: DaemonSet[]) {
|
||||
|
||||
@@ -10,7 +10,7 @@ export class JobStore extends KubeObjectStore<Job> {
|
||||
api = jobApi;
|
||||
|
||||
getChildPods(job: Job): Pod[] {
|
||||
return podsStore.getPodsByOwner(job);
|
||||
return podsStore.getPodsByOwnerId(job.getId());
|
||||
}
|
||||
|
||||
getJobsByOwner(cronJob: CronJob) {
|
||||
|
||||
@@ -3,7 +3,6 @@ import { action, observable } from "mobx";
|
||||
import { KubeObjectStore } from "../../kube-object.store";
|
||||
import { autobind, cpuUnitsToNumber, unitsToBytes } from "../../utils";
|
||||
import { IPodMetrics, Pod, PodMetrics, podMetricsApi, podsApi } from "../../api/endpoints";
|
||||
import { WorkloadKubeObject } from "../../api/workload-kube-object";
|
||||
import { apiManager } from "../../api/api-manager";
|
||||
|
||||
@autobind()
|
||||
@@ -32,15 +31,15 @@ export class PodsStore extends KubeObjectStore<Pod> {
|
||||
}
|
||||
}
|
||||
|
||||
getPodsByOwner(workload: WorkloadKubeObject): Pod[] {
|
||||
if (!workload) return [];
|
||||
getPodsByOwnerId(workloadId: string): Pod[] {
|
||||
if (!workloadId) return [];
|
||||
|
||||
return this.items.filter(pod => {
|
||||
const owners = pod.getOwnerRefs();
|
||||
|
||||
if (!owners.length) return;
|
||||
|
||||
return owners.find(owner => owner.uid === workload.getId());
|
||||
return owners.find(owner => owner.uid === workloadId);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ export class ReplicaSetStore extends KubeObjectStore<ReplicaSet> {
|
||||
}
|
||||
|
||||
getChildPods(replicaSet: ReplicaSet) {
|
||||
return podsStore.getPodsByOwner(replicaSet);
|
||||
return podsStore.getPodsByOwnerId(replicaSet.getId());
|
||||
}
|
||||
|
||||
getStatuses(replicaSets: ReplicaSet[]) {
|
||||
|
||||
@@ -17,7 +17,7 @@ export class StatefulSetStore extends KubeObjectStore<StatefulSet> {
|
||||
}
|
||||
|
||||
getChildPods(statefulSet: StatefulSet) {
|
||||
return podsStore.getPodsByOwner(statefulSet);
|
||||
return podsStore.getPodsByOwnerId(statefulSet.getId());
|
||||
}
|
||||
|
||||
getStatuses(statefulSets: StatefulSet[]) {
|
||||
|
||||
@@ -7,7 +7,7 @@ import { DockTab } from "./dock-tab";
|
||||
import { IDockTab } from "./dock.store";
|
||||
import { isEditResourceTab } from "./edit-resource.store";
|
||||
import { isInstallChartTab } from "./install-chart.store";
|
||||
import { isLogsTab } from "./log.store";
|
||||
import { isLogsTab } from "./log-tab.store";
|
||||
import { TerminalTab } from "./terminal-tab";
|
||||
import { isTerminalTab } from "./terminal.store";
|
||||
import { isUpgradeChartTab } from "./upgrade-chart.store";
|
||||
|
||||
@@ -17,7 +17,7 @@ import { isEditResourceTab } from "./edit-resource.store";
|
||||
import { InstallChart } from "./install-chart";
|
||||
import { isInstallChartTab } from "./install-chart.store";
|
||||
import { Logs } from "./logs";
|
||||
import { isLogsTab } from "./log.store";
|
||||
import { isLogsTab } from "./log-tab.store";
|
||||
import { TerminalWindow } from "./terminal-window";
|
||||
import { createTerminalTab, isTerminalTab } from "./terminal.store";
|
||||
import { UpgradeChart } from "./upgrade-chart";
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
import uniqueId from "lodash/uniqueId";
|
||||
import { podsStore } from "../+workloads-pods/pods.store";
|
||||
|
||||
import { IPodContainer, Pod } from "../../api/endpoints";
|
||||
import { WorkloadKubeObject } from "../../api/workload-kube-object";
|
||||
import { DockTabStore } from "./dock-tab.store";
|
||||
import { dockStore, IDockTab, TabId, TabKind } from "./dock.store";
|
||||
|
||||
export interface LogTabData {
|
||||
pods: Pod[];
|
||||
selectedPod: Pod;
|
||||
selectedContainer: IPodContainer
|
||||
containers?: IPodContainer[]
|
||||
initContainers?: IPodContainer[]
|
||||
showTimestamps?: boolean
|
||||
previous?: boolean
|
||||
}
|
||||
|
||||
interface PodLogsTabData {
|
||||
selectedPod: Pod
|
||||
selectedContainer: IPodContainer
|
||||
}
|
||||
|
||||
interface WorkloadLogsTabData {
|
||||
workload: WorkloadKubeObject
|
||||
}
|
||||
|
||||
export class LogTabStore extends DockTabStore<LogTabData> {
|
||||
constructor() {
|
||||
super({
|
||||
storageName: "pod_logs"
|
||||
});
|
||||
}
|
||||
|
||||
public createPodTab({ selectedPod, selectedContainer }: PodLogsTabData) {
|
||||
const podOwner = selectedPod.getOwnerRefs()[0];
|
||||
const pods = podsStore.getPodsByOwnerId(podOwner?.uid);
|
||||
const title = `Pod ${selectedPod.getName()}`;
|
||||
|
||||
this.createLogsTab(title, {
|
||||
pods: pods.length ? pods : [selectedPod],
|
||||
selectedPod,
|
||||
selectedContainer
|
||||
});
|
||||
}
|
||||
|
||||
public createWorkloadTab({ workload }: WorkloadLogsTabData) {
|
||||
const pods = podsStore.getPodsByOwnerId(workload.getId());
|
||||
|
||||
if (!pods.length) return;
|
||||
|
||||
const selectedPod = pods[0];
|
||||
const selectedContainer = selectedPod.getAllContainers()[0];
|
||||
const title = `${workload.kind} ${selectedPod.getName()}`;
|
||||
|
||||
this.createLogsTab(title, {
|
||||
pods: pods.length ? pods : [selectedPod],
|
||||
selectedPod,
|
||||
selectedContainer
|
||||
});
|
||||
}
|
||||
|
||||
private createDockTab(tabParams: Partial<IDockTab>) {
|
||||
dockStore.createTab({
|
||||
kind: TabKind.POD_LOGS,
|
||||
...tabParams
|
||||
}, false);
|
||||
}
|
||||
|
||||
private saveControlsData(id: TabId, data: LogTabData) {
|
||||
this.setData(id, data);
|
||||
}
|
||||
|
||||
private addBasicControlsData(data: LogTabData): LogTabData {
|
||||
const containers = data.selectedPod.getContainers();
|
||||
const initContainers = data.selectedPod.getInitContainers();
|
||||
|
||||
return {
|
||||
...data,
|
||||
containers,
|
||||
initContainers,
|
||||
showTimestamps: false,
|
||||
previous: false
|
||||
};
|
||||
}
|
||||
|
||||
private createLogsTab(title: string, data: LogTabData) {
|
||||
const id = uniqueId("log-tab-");
|
||||
const fullData = this.addBasicControlsData(data);
|
||||
|
||||
this.createDockTab({ id, title });
|
||||
this.saveControlsData(id, fullData);
|
||||
}
|
||||
}
|
||||
|
||||
export const logTabStore = new LogTabStore();
|
||||
|
||||
export function isLogsTab(tab: IDockTab) {
|
||||
return tab && tab.kind === TabKind.POD_LOGS;
|
||||
}
|
||||
@@ -1,27 +1,16 @@
|
||||
import { autorun, computed, observable, reaction } from "mobx";
|
||||
import { Pod, IPodContainer, podsApi, IPodLogsQuery } from "../../api/endpoints";
|
||||
import { autorun, computed, observable } from "mobx";
|
||||
|
||||
import { IPodLogsQuery, Pod, podsApi } from "../../api/endpoints";
|
||||
import { autobind, interval } from "../../utils";
|
||||
import { DockTabStore } from "./dock-tab.store";
|
||||
import { dockStore, IDockTab, TabKind } from "./dock.store";
|
||||
import { searchStore } from "../../../common/search-store";
|
||||
import { dockStore, TabId } from "./dock.store";
|
||||
import { isLogsTab, logTabStore } from "./log-tab.store";
|
||||
|
||||
export interface IPodLogsData {
|
||||
pod: Pod;
|
||||
selectedContainer: IPodContainer
|
||||
containers: IPodContainer[]
|
||||
initContainers: IPodContainer[]
|
||||
showTimestamps: boolean
|
||||
previous: boolean
|
||||
}
|
||||
|
||||
type TabId = string;
|
||||
type PodLogLine = string;
|
||||
|
||||
// Number for log lines to load
|
||||
export const logRange = 500;
|
||||
const logLinesToLoad = 500;
|
||||
|
||||
@autobind()
|
||||
export class LogStore extends DockTabStore<IPodLogsData> {
|
||||
export class LogStore {
|
||||
private refresher = interval(10, () => {
|
||||
const id = dockStore.selectedTabId;
|
||||
|
||||
@@ -30,12 +19,8 @@ export class LogStore extends DockTabStore<IPodLogsData> {
|
||||
});
|
||||
|
||||
@observable podLogs = observable.map<TabId, PodLogLine[]>();
|
||||
@observable newLogSince = observable.map<TabId, string>(); // Timestamp after which all logs are considered to be new
|
||||
|
||||
constructor() {
|
||||
super({
|
||||
storageName: "pod_logs"
|
||||
});
|
||||
autorun(() => {
|
||||
const { selectedTab, isOpen } = dockStore;
|
||||
|
||||
@@ -45,15 +30,6 @@ export class LogStore extends DockTabStore<IPodLogsData> {
|
||||
this.refresher.stop();
|
||||
}
|
||||
}, { delay: 500 });
|
||||
|
||||
reaction(() => this.podLogs.get(dockStore.selectedTabId), () => {
|
||||
this.setNewLogSince(dockStore.selectedTabId);
|
||||
});
|
||||
|
||||
reaction(() => dockStore.selectedTabId, () => {
|
||||
// Clear search query on tab change
|
||||
searchStore.reset();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -66,7 +42,7 @@ export class LogStore extends DockTabStore<IPodLogsData> {
|
||||
load = async (tabId: TabId) => {
|
||||
try {
|
||||
const logs = await this.loadLogs(tabId, {
|
||||
tailLines: this.lines + logRange
|
||||
tailLines: this.lines + logLinesToLoad
|
||||
});
|
||||
|
||||
this.refresher.start();
|
||||
@@ -107,9 +83,9 @@ export class LogStore extends DockTabStore<IPodLogsData> {
|
||||
* @returns {Promise} A fetch request promise
|
||||
*/
|
||||
loadLogs = async (tabId: TabId, params: Partial<IPodLogsQuery>) => {
|
||||
const data = this.getData(tabId);
|
||||
const data = logTabStore.getData(tabId);
|
||||
const { selectedContainer, previous } = data;
|
||||
const pod = new Pod(data.pod);
|
||||
const pod = new Pod(data.selectedPod);
|
||||
const namespace = pod.getNs();
|
||||
const name = pod.getName();
|
||||
|
||||
@@ -127,17 +103,6 @@ export class LogStore extends DockTabStore<IPodLogsData> {
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets newLogSince separator timestamp to split old logs from new ones
|
||||
* @param tabId
|
||||
*/
|
||||
setNewLogSince(tabId: TabId) {
|
||||
if (!this.podLogs.has(tabId) || !this.podLogs.get(tabId).length || this.newLogSince.has(tabId)) return;
|
||||
const timestamp = this.getLastSinceTime(tabId);
|
||||
|
||||
this.newLogSince.set(tabId, timestamp.split(".")[0]); // Removing milliseconds from string
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts logs into a string array
|
||||
* @returns {number} Length of log lines
|
||||
@@ -196,37 +161,6 @@ export class LogStore extends DockTabStore<IPodLogsData> {
|
||||
clearLogs(tabId: TabId) {
|
||||
this.podLogs.delete(tabId);
|
||||
}
|
||||
|
||||
clearData(tabId: TabId) {
|
||||
this.data.delete(tabId);
|
||||
this.clearLogs(tabId);
|
||||
}
|
||||
}
|
||||
|
||||
export const podLogsStore = new LogStore();
|
||||
|
||||
export function createPodLogsTab(data: IPodLogsData, tabParams: Partial<IDockTab> = {}) {
|
||||
const podId = data.pod.getId();
|
||||
let tab = dockStore.getTabById(podId);
|
||||
|
||||
if (tab) {
|
||||
dockStore.open();
|
||||
dockStore.selectTab(tab.id);
|
||||
|
||||
return;
|
||||
}
|
||||
// If no existent tab found
|
||||
tab = dockStore.createTab({
|
||||
id: podId,
|
||||
kind: TabKind.POD_LOGS,
|
||||
title: data.pod.getName(),
|
||||
...tabParams
|
||||
}, false);
|
||||
podLogsStore.setData(tab.id, data);
|
||||
|
||||
return tab;
|
||||
}
|
||||
|
||||
export function isLogsTab(tab: IDockTab) {
|
||||
return tab && tab.kind === TabKind.POD_LOGS;
|
||||
}
|
||||
export const logStore = new LogStore();
|
||||
@@ -8,9 +8,10 @@ import { IDockTab } from "./dock.store";
|
||||
import { InfoPanel } from "./info-panel";
|
||||
import { LogResourceSelector } from "./log-resource-selector";
|
||||
import { LogList } from "./log-list";
|
||||
import { IPodLogsData, podLogsStore } from "./log.store";
|
||||
import { logStore } from "./log.store";
|
||||
import { LogSearch } from "./log-search";
|
||||
import { LogControls } from "./log-controls";
|
||||
import { LogTabData, logTabStore } from "./log-tab.store";
|
||||
|
||||
interface Props {
|
||||
className?: string
|
||||
@@ -30,7 +31,7 @@ export class Logs extends React.Component<Props> {
|
||||
}
|
||||
|
||||
get tabData() {
|
||||
return podLogsStore.getData(this.tabId);
|
||||
return logTabStore.getData(this.tabId);
|
||||
}
|
||||
|
||||
get tabId() {
|
||||
@@ -38,18 +39,18 @@ export class Logs extends React.Component<Props> {
|
||||
}
|
||||
|
||||
@autobind()
|
||||
save(data: Partial<IPodLogsData>) {
|
||||
podLogsStore.setData(this.tabId, { ...this.tabData, ...data });
|
||||
save(data: Partial<LogTabData>) {
|
||||
logTabStore.setData(this.tabId, { ...this.tabData, ...data });
|
||||
}
|
||||
|
||||
load = async () => {
|
||||
this.isLoading = true;
|
||||
await podLogsStore.load(this.tabId);
|
||||
await logStore.load(this.tabId);
|
||||
this.isLoading = false;
|
||||
};
|
||||
|
||||
reload = async () => {
|
||||
podLogsStore.clearLogs(this.tabId);
|
||||
logStore.clearLogs(this.tabId);
|
||||
await this.load();
|
||||
};
|
||||
|
||||
@@ -82,8 +83,8 @@ export class Logs extends React.Component<Props> {
|
||||
}
|
||||
|
||||
renderResourceSelector() {
|
||||
const logs = podLogsStore.logs;
|
||||
const searchLogs = this.tabData.showTimestamps ? logs : podLogsStore.logsWithoutTimestamps;
|
||||
const logs = logStore.logs;
|
||||
const searchLogs = this.tabData.showTimestamps ? logs : logStore.logsWithoutTimestamps;
|
||||
const controls = (
|
||||
<div className="flex gaps">
|
||||
<LogResourceSelector
|
||||
@@ -112,7 +113,7 @@ export class Logs extends React.Component<Props> {
|
||||
}
|
||||
|
||||
render() {
|
||||
const logs = podLogsStore.logs;
|
||||
const logs = logStore.logs;
|
||||
|
||||
return (
|
||||
<div className="PodLogs flex column">
|
||||
|
||||
Reference in new issue
Block a user