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>;
}
const NonInjectedLogControls = observer(({ openSaveFileDialog, model, downloadAllLogs }: Dependencies & LogControlsProps) => {
const NonInjectedLogControls = observer(({ model, downloadAllLogs }: Dependencies & LogControlsProps) => {
const tabData = model.logTabData.get();
const pod = model.pod.get();
@ -47,18 +47,6 @@ const NonInjectedLogControls = observer(({ openSaveFileDialog, model, downloadAl
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 (
<div className={styles.controls}>
<div>
@ -85,7 +73,7 @@ const NonInjectedLogControls = observer(({ openSaveFileDialog, model, downloadAl
/>
<DownloadLogsDropdown
downloadVisibleLogs={downloadLogs}
downloadVisibleLogs={model.downloadLogs}
downloadAllLogs={() => downloadAllLogs(
{ name: pod.getName(), namespace: pod.getNs() },
{ timestamps: showTimestamps, previous }

View File

@ -1,14 +1,9 @@
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 openSaveFileDialogInjectable from "../../../utils/save-file.injectable";
import callForLogsInjectable from "./call-for-logs.injectable";
export interface PodLogsTabData {
selectedPod: Pod;
selectedContainer: PodContainer;
}
const downloadAllLogsInjectable = getInjectable({
id: "download-all-logs",

View File

@ -10,7 +10,7 @@ import { Icon } from "../../icon";
import { Menu, MenuItem } from "../../menu";
interface DownloadLogsDropdownProps {
downloadVisibleLogs: () => Promise<any>;
downloadVisibleLogs: () => void;
downloadAllLogs: () => Promise<any>;
}
@ -51,7 +51,7 @@ export function DownloadLogsDropdown({ downloadAllLogs, downloadVisibleLogs }: D
open={toggle}
>
<MenuItem
onClick={() => downloadLogs(downloadVisibleLogs)}
onClick={downloadVisibleLogs}
data-testid="download-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 getPodsByOwnerIdInjectable from "../../+workloads-pods/get-pods-by-owner-id.injectable";
import getPodByIdInjectable from "../../+workloads-pods/get-pod-by-id.injectable";
import downloadLogsInjectable from "./download-logs.injectable";
export interface InstantiateArgs {
tabId: TabId;
@ -39,6 +40,7 @@ const logsViewModelInjectable = getInjectable({
areLogsPresent: di.inject(areLogsPresentInjectable),
getPodById: di.inject(getPodByIdInjectable),
getPodsByOwnerId: di.inject(getPodsByOwnerIdInjectable),
downloadLogs: di.inject(downloadLogsInjectable),
searchStore: di.inject(searchStoreInjectable),
}),
lifecycle: lifecycleEnum.transient,

View File

@ -27,6 +27,7 @@ export interface LogTabViewModelDependencies {
getPodById: GetPodById;
getPodsByOwnerId: GetPodsByOwnerId;
areLogsPresent: (tabId: TabId) => boolean;
downloadLogs: (filename: string, logs: string[]) => void;
searchStore: SearchStore;
}
@ -77,4 +78,18 @@ export class LogTabViewModel {
reloadLogs = () => this.dependencies.reloadLogs(this.tabId, this.pod, this.logTabData);
renameTab = (title: string) => this.dependencies.renameTab(this.tabId, title);
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);
}
};
}