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

Optimize Job workloads status computation

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2023-03-09 09:16:09 -05:00
parent 1dc98a7dd9
commit 19a57ba731
5 changed files with 112 additions and 58 deletions

View File

@ -0,0 +1,21 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { computed } from "mobx";
import computeStatusCountsForOwnersInjectable from "../../utils/compute-status-counts.injectable";
import jobStoreInjectable from "./store.injectable";
const statusCountsForAllJobsInSelectedNamespacesInjectable = getInjectable({
id: "status-counts-for-all-jobs-in-selected-namespaces",
instantiate: (di) => {
const jobStore = di.inject(jobStoreInjectable);
const computeStatusCountsForOwners = di.inject(computeStatusCountsForOwnersInjectable);
return computed(() => computeStatusCountsForOwners(jobStore.contextItems));
},
});
export default statusCountsForAllJobsInSelectedNamespacesInjectable;

View File

@ -0,0 +1,19 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { computed } from "mobx";
import jobStoreInjectable from "./store.injectable";
const totalCountOfJobsInSelectedNamespacesInjectable = getInjectable({
id: "total-count-of-jobs-in-selected-namespaces",
instantiate: (di) => {
const jobStore = di.inject(jobStoreInjectable);
return computed(() => jobStore.getTotalCount());
},
});
export default totalCountOfJobsInSelectedNamespacesInjectable;

View File

@ -5,38 +5,24 @@
import { getInjectable } from "@ogre-tools/injectable";
import { workloadInjectionToken } from "../workload-injection-token";
import { ResourceNames } from "../../../../utils/rbac";
import namespaceStoreInjectable from "../../../+namespaces/store.injectable";
import jobStoreInjectable from "../../../+workloads-jobs/store.injectable";
import navigateToJobsInjectable from "../../../../../common/front-end-routing/routes/cluster/workloads/jobs/navigate-to-jobs.injectable";
import { computed } from "mobx";
import totalCountOfJobsInSelectedNamespacesInjectable from "../../../+workloads-jobs/total-count.injectable";
import statusCountsForAllJobsInSelectedNamespacesInjectable from "../../../+workloads-jobs/statuses.injectable";
const jobsWorkloadInjectable = getInjectable({
id: "jobs-workload",
instantiate: (di) => {
const navigate = di.inject(navigateToJobsInjectable);
const namespaceStore = di.inject(namespaceStoreInjectable);
const store = di.inject(jobStoreInjectable);
return {
instantiate: (di) => ({
resource: {
apiName: "jobs",
group: "batch",
},
open: navigate,
amountOfItems: computed(
() => store.getAllByNs(namespaceStore.contextNamespaces).length,
),
status: computed(() =>
store.getStatuses(store.getAllByNs(namespaceStore.contextNamespaces)),
),
open: di.inject(navigateToJobsInjectable),
amountOfItems: di.inject(totalCountOfJobsInSelectedNamespacesInjectable),
status: di.inject(statusCountsForAllJobsInSelectedNamespacesInjectable),
title: ResourceNames.jobs,
orderNumber: 60,
};
},
}),
injectionToken: workloadInjectionToken,
});

View File

@ -5,44 +5,16 @@
import { getInjectable } from "@ogre-tools/injectable";
import { computed } from "mobx";
import podStoreInjectable from "../+workloads-pods/store.injectable";
import type { PodStatusPhase } from "../../../common/k8s-api/endpoints";
import { getOrInsert } from "../../utils";
import { foldPodStatusPhase } from "../../utils/fold-pod-status-phase";
import computeStatusCountsForOwnersInjectable from "../../utils/compute-status-counts.injectable";
import replicaSetStoreInjectable from "./store.injectable";
interface PodData {
status: PodStatusPhase;
}
const statusCountsForAllReplicaSetsInSelectedNamespacesInjectable = getInjectable({
id: "status-counts-for-all-replica-sets-in-selected-namespaces",
instantiate: (di) => {
const podStore = di.inject(podStoreInjectable);
const replicaSetStore = di.inject(replicaSetStoreInjectable);
const computeStatusCountsForOwners = di.inject(computeStatusCountsForOwnersInjectable);
return computed(() => {
const podsByOwnerId = new Map<string, PodData[]>();
const statuses = { running: 0, failed: 0, pending: 0 };
for (const pod of podStore.contextItems) {
for (const ownerRef of pod.getOwnerRefs()) {
getOrInsert(podsByOwnerId, ownerRef.uid, []).push({
status: pod.getStatus(),
});
}
}
for (const replicaSet of replicaSetStore.contextItems) {
const status = (podsByOwnerId.get(replicaSet.getId()) ?? [])
.map(pod => pod.status)
.reduce(foldPodStatusPhase, "running");
statuses[status]++;
}
return statuses;
});
return computed(() => computeStatusCountsForOwners(replicaSetStore.contextItems));
},
});

View File

@ -0,0 +1,56 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { PodStatusPhase } from "../../common/k8s-api/endpoints";
import { getOrInsert } from "../../common/utils";
import { foldPodStatusPhase } from "./fold-pod-status-phase";
import { getInjectable } from "@ogre-tools/injectable";
import podStoreInjectable from "../components/+workloads-pods/store.injectable";
import { computed } from "mobx";
import type { KubeObject } from "../../common/k8s-api/kube-object";
export interface StatusCounts {
running: number;
failed: number;
pending: number;
}
const computeStatusCountsForOwnersInjectable = getInjectable({
id: "compute-status-counts-for-owners",
instantiate: (di) => {
const podStore = di.inject(podStoreInjectable);
const podsByOwnerId = computed(() => {
const podsByOwnerId = new Map<string, ({ status: PodStatusPhase })[]>();
for (const pod of podStore.contextItems) {
for (const ownerRef of pod.getOwnerRefs()) {
getOrInsert(podsByOwnerId, ownerRef.uid, []).push({
status: pod.getStatus(),
});
}
}
return podsByOwnerId;
});
return (possibleOwners: KubeObject[]): StatusCounts => {
const statuses = { running: 0, failed: 0, pending: 0 };
for (const possibleOwner of possibleOwners) {
const status = (podsByOwnerId.get().get(possibleOwner.getId()) ?? [])
.map(pod => pod.status)
.reduce(foldPodStatusPhase, "running");
statuses[status]++;
}
return statuses;
};
},
});
export default computeStatusCountsForOwnersInjectable;