1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/dashboard/client/components/+workloads-jobs/job.store.ts
Sebastian Malton b1ff34879a cleanup Lens repo with tighter linting
Signed-off-by: Sebastian Malton <smalton@mirantis.com>
2020-07-09 17:00:23 -04:00

44 lines
1.4 KiB
TypeScript

import { KubeObjectStore } from "../../kube-object.store";
import { autobind } from "../../utils";
import { Job, jobApi } from "../../api/endpoints/job.api";
import { CronJob, Pod, PodStatus } from "../../api/endpoints";
import { podsStore } from "../+workloads-pods/pods.store";
import { apiManager } from "../../api/api-manager";
import { Dictionary } from "lodash";
@autobind()
export class JobStore extends KubeObjectStore<Job> {
api = jobApi
getChildPods(job: Job): Pod[] {
return podsStore.getPodsByOwner(job);
}
getJobsByOwner(cronJob: CronJob): Job[] {
return this.items.filter(job =>
job.getNs() == cronJob.getNs() &&
job.getOwnerRefs().find(ref => ref.name === cronJob.getName() && ref.kind === cronJob.kind)
);
}
getStatuses(jobs?: Job[]): Dictionary<number> {
const status = { failed: 0, pending: 0, running: 0, succeeded: 0 };
jobs.forEach(job => {
const pods = this.getChildPods(job);
if (pods.some(pod => pod.getStatus() === PodStatus.FAILED)) {
status.failed++;
} else if (pods.some(pod => pod.getStatus() === PodStatus.PENDING)) {
status.pending++;
} else if (pods.some(pod => pod.getStatus() === PodStatus.RUNNING)) {
status.running++;
} else {
status.succeeded++;
}
});
return status;
}
}
export const jobStore = new JobStore();
apiManager.registerStore(jobApi, jobStore);