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

Move downloadLogs logic to the model

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
Alex Andreev 2022-08-12 11:11:50 +03:00
parent 298c8a1145
commit bafcf4ef50
6 changed files with 38 additions and 22 deletions

View File

@ -26,7 +26,7 @@ interface Dependencies {
downloadAllLogs: (params: ResourceDescriptor, query: PodLogsQuery) => Promise<void>; downloadAllLogs: (params: ResourceDescriptor, query: PodLogsQuery) => Promise<void>;
} }
const NonInjectedLogControls = observer(({ openSaveFileDialog, model, downloadAllLogs }: Dependencies & LogControlsProps) => { const NonInjectedLogControls = observer(({ model, downloadAllLogs }: Dependencies & LogControlsProps) => {
const tabData = model.logTabData.get(); const tabData = model.logTabData.get();
const pod = model.pod.get(); const pod = model.pod.get();
@ -47,18 +47,6 @@ const NonInjectedLogControls = observer(({ openSaveFileDialog, model, downloadAl
model.reloadLogs(); model.reloadLogs();
}; };
const downloadLogs = () => {
return new Promise((resolve) => {
const fileName = pod.getName();
const logsToDownload: string[] = showTimestamps
? model.logs.get()
: model.logsWithoutTimestamps.get();
openSaveFileDialog(`${fileName}.log`, logsToDownload.join("\n"), "text/plain");
resolve(true);
});
};
return ( return (
<div className={styles.controls}> <div className={styles.controls}>
<div> <div>
@ -85,7 +73,7 @@ const NonInjectedLogControls = observer(({ openSaveFileDialog, model, downloadAl
/> />
<DownloadLogsDropdown <DownloadLogsDropdown
downloadVisibleLogs={downloadLogs} downloadVisibleLogs={model.downloadLogs}
downloadAllLogs={() => downloadAllLogs( downloadAllLogs={() => downloadAllLogs(
{ name: pod.getName(), namespace: pod.getNs() }, { name: pod.getName(), namespace: pod.getNs() },
{ timestamps: showTimestamps, previous } { timestamps: showTimestamps, previous }

View File

@ -1,14 +1,9 @@
import { getInjectable } from "@ogre-tools/injectable"; import { getInjectable } from "@ogre-tools/injectable";
import type { PodContainer, Pod, PodLogsQuery } from "../../../../common/k8s-api/endpoints"; import type { PodLogsQuery } from "../../../../common/k8s-api/endpoints";
import type { ResourceDescriptor } from "../../../../common/k8s-api/kube-api"; import type { ResourceDescriptor } from "../../../../common/k8s-api/kube-api";
import openSaveFileDialogInjectable from "../../../utils/save-file.injectable"; import openSaveFileDialogInjectable from "../../../utils/save-file.injectable";
import callForLogsInjectable from "./call-for-logs.injectable"; import callForLogsInjectable from "./call-for-logs.injectable";
export interface PodLogsTabData {
selectedPod: Pod;
selectedContainer: PodContainer;
}
const downloadAllLogsInjectable = getInjectable({ const downloadAllLogsInjectable = getInjectable({
id: "download-all-logs", id: "download-all-logs",

View File

@ -10,7 +10,7 @@ import { Icon } from "../../icon";
import { Menu, MenuItem } from "../../menu"; import { Menu, MenuItem } from "../../menu";
interface DownloadLogsDropdownProps { interface DownloadLogsDropdownProps {
downloadVisibleLogs: () => Promise<any>; downloadVisibleLogs: () => void;
downloadAllLogs: () => Promise<any>; downloadAllLogs: () => Promise<any>;
} }
@ -51,7 +51,7 @@ export function DownloadLogsDropdown({ downloadAllLogs, downloadVisibleLogs }: D
open={toggle} open={toggle}
> >
<MenuItem <MenuItem
onClick={() => downloadLogs(downloadVisibleLogs)} onClick={downloadVisibleLogs}
data-testid="download-visible-logs" data-testid="download-visible-logs"
> >
Visible logs Visible logs

View File

@ -0,0 +1,16 @@
import { getInjectable } from "@ogre-tools/injectable";
import openSaveFileDialogInjectable from "../../../utils/save-file.injectable";
const downloadLogsInjectable = getInjectable({
id: "download-logs",
instantiate: (di) => {
const openSaveFileDialog = di.inject(openSaveFileDialogInjectable)
return (filename: string, logs: string[]) => {
openSaveFileDialog(filename, logs.join("/n"), "text/plain");
}
},
});
export default downloadLogsInjectable;

View File

@ -18,6 +18,7 @@ import areLogsPresentInjectable from "./are-logs-present.injectable";
import searchStoreInjectable from "../../../search-store/search-store.injectable"; import searchStoreInjectable from "../../../search-store/search-store.injectable";
import getPodsByOwnerIdInjectable from "../../+workloads-pods/get-pods-by-owner-id.injectable"; import getPodsByOwnerIdInjectable from "../../+workloads-pods/get-pods-by-owner-id.injectable";
import getPodByIdInjectable from "../../+workloads-pods/get-pod-by-id.injectable"; import getPodByIdInjectable from "../../+workloads-pods/get-pod-by-id.injectable";
import downloadLogsInjectable from "./download-logs.injectable";
export interface InstantiateArgs { export interface InstantiateArgs {
tabId: TabId; tabId: TabId;
@ -39,6 +40,7 @@ const logsViewModelInjectable = getInjectable({
areLogsPresent: di.inject(areLogsPresentInjectable), areLogsPresent: di.inject(areLogsPresentInjectable),
getPodById: di.inject(getPodByIdInjectable), getPodById: di.inject(getPodByIdInjectable),
getPodsByOwnerId: di.inject(getPodsByOwnerIdInjectable), getPodsByOwnerId: di.inject(getPodsByOwnerIdInjectable),
downloadLogs: di.inject(downloadLogsInjectable),
searchStore: di.inject(searchStoreInjectable), searchStore: di.inject(searchStoreInjectable),
}), }),
lifecycle: lifecycleEnum.transient, lifecycle: lifecycleEnum.transient,

View File

@ -27,6 +27,7 @@ export interface LogTabViewModelDependencies {
getPodById: GetPodById; getPodById: GetPodById;
getPodsByOwnerId: GetPodsByOwnerId; getPodsByOwnerId: GetPodsByOwnerId;
areLogsPresent: (tabId: TabId) => boolean; areLogsPresent: (tabId: TabId) => boolean;
downloadLogs: (filename: string, logs: string[]) => void;
searchStore: SearchStore; searchStore: SearchStore;
} }
@ -77,4 +78,18 @@ export class LogTabViewModel {
reloadLogs = () => this.dependencies.reloadLogs(this.tabId, this.pod, this.logTabData); reloadLogs = () => this.dependencies.reloadLogs(this.tabId, this.pod, this.logTabData);
renameTab = (title: string) => this.dependencies.renameTab(this.tabId, title); renameTab = (title: string) => this.dependencies.renameTab(this.tabId, title);
stopLoadingLogs = () => this.dependencies.stopLoadingLogs(this.tabId); stopLoadingLogs = () => this.dependencies.stopLoadingLogs(this.tabId);
downloadLogs = () => {
const pod = this.pod.get();
const tabData = this.logTabData.get();
if (pod && tabData) {
const fileName = pod.getName();
const logsToDownload: string[] = tabData.showTimestamps
? this.logs.get()
: this.logsWithoutTimestamps.get();
this.dependencies.downloadLogs(`${fileName}.log`, logsToDownload);
}
};
} }