Skip to content

Commit 66dd77d

Browse files
committed
fix: improvements for deferring load of ink npm
1 parent b20be34 commit 66dd77d

File tree

25 files changed

+150
-89
lines changed

25 files changed

+150
-89
lines changed

plugins/plugin-codeflare-dashboard/src/components/Top/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import type { JobRec, HostRec, PodRec, OnData, UpdatePayload, Resource, Resource
2525
import defaultValueFor from "./defaults.js"
2626
import { Breakdown, ValidResources } from "./types.js"
2727

28-
import { themes } from "../../controller/dashboard/utilization/theme.js"
29-
import { defaultUtilizationThemes } from "../../controller/dashboard/grids.js"
28+
import { themes } from "../../controller/dashboard/job/utilization/theme.js"
29+
import { defaultUtilizationThemes } from "../../controller/dashboard/job/grids.js"
3030

3131
type UI = {
3232
/** Force a refresh */

plugins/plugin-codeflare-dashboard/src/controller/dashboard/generic/Demo.ts renamed to plugins/plugin-codeflare-dashboard/src/controller/dashboard/job/generic/Demo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import type { TextProps } from "ink"
1818

1919
import type HistoryConfig from "../history.js"
20-
import type { OnData } from "../../../components/Job/types.js"
20+
import type { OnData } from "../../../../components/Job/types.js"
2121

2222
import { update } from "../history.js"
2323

plugins/plugin-codeflare-dashboard/src/controller/dashboard/history.ts renamed to plugins/plugin-codeflare-dashboard/src/controller/dashboard/job/history.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import type { Worker } from "../../components/Job/types.js"
17+
import type { Worker } from "../../../components/Job/types.js"
1818

1919
/** Configuration governining the history model of states per worker */
2020
type HistoryConfig = {

plugins/plugin-codeflare-dashboard/src/controller/dashboard/job.ts renamed to plugins/plugin-codeflare-dashboard/src/controller/dashboard/job/index.ts

Lines changed: 9 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -16,74 +16,21 @@
1616

1717
import Debug from "debug"
1818
import type { Arguments } from "@kui-shell/core"
19+
import type Options from "./options.js"
1920

20-
import tailf from "./tailf.js"
21-
import dashboardUI from "./db.js"
21+
import tailf from "../tailf.js"
22+
import usage from "../usage.js"
23+
import dashboardUI from "../db.js"
2224
import status from "./status/index.js"
2325
import utilization from "./utilization/index.js"
2426

25-
import { enterAltBufferMode } from "./term.js"
27+
import jobIdFrom from "./jobid.js"
28+
import { enterAltBufferMode } from "../term.js"
29+
import { KindA, isValidKindA } from "./kinds.js"
2630
import { SupportedGrid, isSupportedGrid } from "./grids.js"
27-
import { KindA, isValidKindA, validKinds } from "./kinds.js"
2831

2932
import type HistoryConfig from "./history.js"
30-
import type { GridSpec } from "../../components/Job/types.js"
31-
32-
export type Options = Arguments["parsedOptions"] & {
33-
s: number
34-
scale: number
35-
36-
demo: boolean
37-
38-
p: string
39-
profile: string
40-
theme: string
41-
42-
/** Frequency of updates to the timeline, in seconds */
43-
u: number
44-
"update-frequency": number
45-
}
46-
47-
export function usage(cmd: string, extraKinds: string[] = []) {
48-
return `Usage: codeflare ${cmd} ${extraKinds.concat(validKinds()).join("|")} [<jobId>|-N]`
49-
}
50-
51-
async function lastNJob(profile: string, N: number) {
52-
const [{ join }, { readdir, stat }, { Profiles }] = await Promise.all([
53-
import("path"),
54-
import("fs/promises"),
55-
import("madwizard"),
56-
])
57-
58-
const dir = Profiles.guidebookJobDataPath({ profile })
59-
const files = await readdir(dir)
60-
if (files.length === 0) {
61-
throw new Error("No jobs available")
62-
return
63-
}
64-
65-
const cTimes = await Promise.all(files.map((_) => stat(join(dir, _)).then((_) => _.ctime.getTime())))
66-
const sorted = files.map((file, idx) => ({ file, lastM: cTimes[idx] })).sort((a, b) => b.lastM - a.lastM)
67-
68-
if (!sorted[N]) {
69-
throw new Error("Specified historical job not available")
70-
} else {
71-
return sorted[N].file
72-
}
73-
}
74-
75-
export async function jobIdFrom(args: Arguments<Options>, cmd: string, offset = 2) {
76-
const profile = args.parsedOptions.p || (await import("madwizard").then((_) => _.Profiles.lastUsed()))
77-
78-
const jobIdFromCommandLine = args.argvNoOptions[args.argvNoOptions.indexOf(cmd) + offset]
79-
const jobId = /^-\d+$/.test(jobIdFromCommandLine)
80-
? await lastNJob(profile, -parseInt(jobIdFromCommandLine, 10))
81-
: jobIdFromCommandLine === undefined
82-
? await lastNJob(profile, 0)
83-
: jobIdFromCommandLine
84-
85-
return { jobId, profile }
86-
}
33+
import type { GridSpec } from "../../../components/Job/types.js"
8734

8835
/** @return grid model for the given `kind` for `jobId` in `profile` */
8936
async function gridFor(
@@ -104,7 +51,7 @@ async function allGridsFor(
10451
historyConfig: HistoryConfig,
10552
opts: Pick<Options, "demo" | "theme" | "events">
10653
) {
107-
const usesGpus = opts.demo || (await import("../env.js").then((_) => _.usesGpus(profile, jobId)))
54+
const usesGpus = opts.demo || (await import("../../env.js").then((_) => _.usesGpus(profile, jobId)))
10855

10956
const all = [
11057
gridFor("status", profile, jobId, historyConfig, opts),
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2023 The Kubernetes Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import type { Arguments } from "@kui-shell/core"
18+
import type Options from "./options.js"
19+
20+
async function lastNJob(profile: string, N: number) {
21+
const [{ join }, { readdir, stat }, { Profiles }] = await Promise.all([
22+
import("path"),
23+
import("fs/promises"),
24+
import("madwizard"),
25+
])
26+
27+
const dir = Profiles.guidebookJobDataPath({ profile })
28+
const files = await readdir(dir)
29+
if (files.length === 0) {
30+
throw new Error("No jobs available")
31+
return
32+
}
33+
34+
const cTimes = await Promise.all(files.map((_) => stat(join(dir, _)).then((_) => _.ctime.getTime())))
35+
const sorted = files.map((file, idx) => ({ file, lastM: cTimes[idx] })).sort((a, b) => b.lastM - a.lastM)
36+
37+
if (!sorted[N]) {
38+
throw new Error("Specified historical job not available")
39+
} else {
40+
return sorted[N].file
41+
}
42+
}
43+
44+
export default async function jobIdFrom(args: Arguments<Options>, cmd: string, offset = 2) {
45+
const profile = args.parsedOptions.p || (await import("madwizard").then((_) => _.Profiles.lastUsed()))
46+
47+
const jobIdFromCommandLine = args.argvNoOptions[args.argvNoOptions.indexOf(cmd) + offset]
48+
const jobId = /^-\d+$/.test(jobIdFromCommandLine)
49+
? await lastNJob(profile, -parseInt(jobIdFromCommandLine, 10))
50+
: jobIdFromCommandLine === undefined
51+
? await lastNJob(profile, 0)
52+
: jobIdFromCommandLine
53+
54+
return { jobId, profile }
55+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2023 The Kubernetes Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import type { ParsedOptions } from "@kui-shell/core"
18+
19+
type Options = ParsedOptions & {
20+
s: number
21+
scale: number
22+
23+
demo: boolean
24+
25+
p: string
26+
profile: string
27+
theme: string
28+
29+
/** Frequency of updates to the timeline, in seconds */
30+
u: number
31+
"update-frequency": number
32+
}
33+
34+
export default Options

plugins/plugin-codeflare-dashboard/src/controller/dashboard/status/Demo.ts renamed to plugins/plugin-codeflare-dashboard/src/controller/dashboard/job/status/Demo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import type { TextProps } from "ink"
1818

1919
import type HistoryConfig from "../history.js"
2020
import type { WorkerState } from "./states.js"
21-
import type { OnData } from "../../../components/Job/types.js"
21+
import type { OnData } from "../../../../components/Job/types.js"
2222

2323
import { states } from "./states.js"
2424
import GenericDemo from "../generic/Demo.js"

plugins/plugin-codeflare-dashboard/src/controller/dashboard/status/Live.ts renamed to plugins/plugin-codeflare-dashboard/src/controller/dashboard/job/status/Live.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ import ansiRegex from "ansi-regex"
1919
import stripAnsi from "strip-ansi"
2020
import type { TextProps } from "ink"
2121

22-
import stripColors from "../stripColors.js"
22+
import stripColors from "../../stripColors.js"
2323
import type Options from "../options.js"
24-
import type { Tail } from "../tailf.js"
24+
import type { Tail } from "../../tailf.js"
2525
import type HistoryConfig from "../history.js"
2626
import type { WorkerState } from "./states.js"
27-
import type { OnData, Worker } from "../../../components/Job/types.js"
27+
import type { OnData, Worker } from "../../../../components/Job/types.js"
2828

2929
import { rankFor, stateFor } from "./states.js"
3030

0 commit comments

Comments
 (0)