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:
parent
1dc98a7dd9
commit
19a57ba731
@ -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;
|
||||||
|
|
||||||
@ -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;
|
||||||
|
|
||||||
@ -5,38 +5,24 @@
|
|||||||
import { getInjectable } from "@ogre-tools/injectable";
|
import { getInjectable } from "@ogre-tools/injectable";
|
||||||
import { workloadInjectionToken } from "../workload-injection-token";
|
import { workloadInjectionToken } from "../workload-injection-token";
|
||||||
import { ResourceNames } from "../../../../utils/rbac";
|
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 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({
|
const jobsWorkloadInjectable = getInjectable({
|
||||||
id: "jobs-workload",
|
id: "jobs-workload",
|
||||||
|
|
||||||
instantiate: (di) => {
|
instantiate: (di) => ({
|
||||||
const navigate = di.inject(navigateToJobsInjectable);
|
resource: {
|
||||||
const namespaceStore = di.inject(namespaceStoreInjectable);
|
apiName: "jobs",
|
||||||
const store = di.inject(jobStoreInjectable);
|
group: "batch",
|
||||||
|
},
|
||||||
return {
|
open: di.inject(navigateToJobsInjectable),
|
||||||
resource: {
|
amountOfItems: di.inject(totalCountOfJobsInSelectedNamespacesInjectable),
|
||||||
apiName: "jobs",
|
status: di.inject(statusCountsForAllJobsInSelectedNamespacesInjectable),
|
||||||
group: "batch",
|
title: ResourceNames.jobs,
|
||||||
},
|
orderNumber: 60,
|
||||||
open: navigate,
|
}),
|
||||||
|
|
||||||
amountOfItems: computed(
|
|
||||||
() => store.getAllByNs(namespaceStore.contextNamespaces).length,
|
|
||||||
),
|
|
||||||
|
|
||||||
status: computed(() =>
|
|
||||||
store.getStatuses(store.getAllByNs(namespaceStore.contextNamespaces)),
|
|
||||||
),
|
|
||||||
|
|
||||||
title: ResourceNames.jobs,
|
|
||||||
orderNumber: 60,
|
|
||||||
};
|
|
||||||
},
|
|
||||||
|
|
||||||
injectionToken: workloadInjectionToken,
|
injectionToken: workloadInjectionToken,
|
||||||
});
|
});
|
||||||
|
|||||||
@ -5,44 +5,16 @@
|
|||||||
|
|
||||||
import { getInjectable } from "@ogre-tools/injectable";
|
import { getInjectable } from "@ogre-tools/injectable";
|
||||||
import { computed } from "mobx";
|
import { computed } from "mobx";
|
||||||
import podStoreInjectable from "../+workloads-pods/store.injectable";
|
import computeStatusCountsForOwnersInjectable from "../../utils/compute-status-counts.injectable";
|
||||||
import type { PodStatusPhase } from "../../../common/k8s-api/endpoints";
|
|
||||||
import { getOrInsert } from "../../utils";
|
|
||||||
import { foldPodStatusPhase } from "../../utils/fold-pod-status-phase";
|
|
||||||
import replicaSetStoreInjectable from "./store.injectable";
|
import replicaSetStoreInjectable from "./store.injectable";
|
||||||
|
|
||||||
interface PodData {
|
|
||||||
status: PodStatusPhase;
|
|
||||||
}
|
|
||||||
|
|
||||||
const statusCountsForAllReplicaSetsInSelectedNamespacesInjectable = getInjectable({
|
const statusCountsForAllReplicaSetsInSelectedNamespacesInjectable = getInjectable({
|
||||||
id: "status-counts-for-all-replica-sets-in-selected-namespaces",
|
id: "status-counts-for-all-replica-sets-in-selected-namespaces",
|
||||||
instantiate: (di) => {
|
instantiate: (di) => {
|
||||||
const podStore = di.inject(podStoreInjectable);
|
|
||||||
const replicaSetStore = di.inject(replicaSetStoreInjectable);
|
const replicaSetStore = di.inject(replicaSetStoreInjectable);
|
||||||
|
const computeStatusCountsForOwners = di.inject(computeStatusCountsForOwnersInjectable);
|
||||||
|
|
||||||
return computed(() => {
|
return computed(() => computeStatusCountsForOwners(replicaSetStore.contextItems));
|
||||||
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;
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -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;
|
||||||
|
|
||||||
Loading…
Reference in New Issue
Block a user