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

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

+2-2
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 */

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

+1-1
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

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

+1-1
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 = {

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

+9-62
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),
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+
}
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

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

+1-1
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"

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

+3-3
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

Diff for: plugins/plugin-codeflare-dashboard/src/controller/dashboard/status/index.ts renamed to plugins/plugin-codeflare-dashboard/src/controller/dashboard/job/status/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616

1717
import type { TextProps } from "ink"
1818

19-
import type { Tail } from "../tailf.js"
19+
import type { Tail } from "../../tailf.js"
2020
import type Options from "../options.js"
2121
import type HistoryConfig from "../history.js"
2222
import type { WorkerState } from "./states.js"
23-
import type { OnData, GridSpec } from "../../../components/Job/types.js"
23+
import type { OnData, GridSpec } from "../../../../components/Job/types.js"
2424

2525
import Demo from "./Demo.js"
2626
import Live from "./Live.js"

Diff for: plugins/plugin-codeflare-dashboard/src/controller/dashboard/utilization/Demo.ts renamed to plugins/plugin-codeflare-dashboard/src/controller/dashboard/job/utilization/Demo.ts

+1-1
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"

Diff for: plugins/plugin-codeflare-dashboard/src/controller/dashboard/utilization/Live.ts renamed to plugins/plugin-codeflare-dashboard/src/controller/dashboard/job/utilization/Live.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
import stripAnsi from "strip-ansi"
1818
import type { TextProps } from "ink"
1919

20-
import type { Tail } from "../tailf.js"
20+
import type { Tail } from "../../tailf.js"
2121
import type { WorkerState } from "./states.js"
2222
import type HistoryConfig from "../history.js"
23-
import type { OnData, Worker } from "../../../components/Job/types.js"
23+
import type { OnData, Worker } from "../../../../components/Job/types.js"
2424

2525
import { states } from "./states.js"
2626
import { update as updateHistory } from "../history.js"

Diff for: plugins/plugin-codeflare-dashboard/src/controller/dashboard/utilization/index.ts renamed to plugins/plugin-codeflare-dashboard/src/controller/dashboard/job/utilization/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
import type { TextProps } from "ink"
1818

1919
import type Kind from "../kinds.js"
20-
import type { Tail } from "../tailf.js"
20+
import type { Tail } from "../../tailf.js"
2121
import type Options from "../options.js"
2222
import type HistoryConfig from "../history.js"
2323
import type { WorkerState } from "./states.js"
24-
import type { OnData, GridSpec } from "../../../components/Job/types.js"
24+
import type { OnData, GridSpec } from "../../../../components/Job/types.js"
2525
import { SupportedUtilizationGrid, defaultUtilizationThemes, providerFor } from "../grids.js"
2626

2727
import { states } from "./states.js"

Diff for: plugins/plugin-codeflare-dashboard/src/controller/dashboard/tailf.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import split2 from "split2"
1919
import chokidar from "chokidar"
2020
import TailFile from "@logdna/tail-file"
2121

22-
import Kind, { KindedSource, resourcePaths } from "./kinds.js"
22+
import Kind, { KindedSource, resourcePaths } from "./job/kinds.js"
2323

2424
export type Tail = {
2525
kind: Kind

Diff for: plugins/plugin-codeflare-dashboard/src/controller/dashboard/top.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import type { Arguments } from "@kui-shell/core"
1818

19-
import type { Options } from "./job.js"
19+
import type Options from "./job/options.js"
2020
import type { OnData, HostRec, PodRec, ResourceSpec, UpdatePayload } from "../../components/Top/types.js"
2121

2222
import { enterAltBufferMode } from "./term.js"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 { validKinds } from "./job/kinds.js"
18+
19+
export default function usage(cmd: string, extraKinds: string[] = []) {
20+
return `Usage: codeflare ${cmd} ${extraKinds.concat(validKinds()).join("|")} [<jobId>|-N]`
21+
}

Diff for: plugins/plugin-codeflare-dashboard/src/controller/dump.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { Arguments } from "@kui-shell/core"
17+
import type { Arguments } from "@kui-shell/core"
18+
import type DashboardOptions from "./dashboard/job/options.js"
1819

19-
import { pathsFor } from "./dashboard/tailf.js"
20-
import { filepathOf, isValidKind } from "./dashboard/kinds.js"
21-
import { Options as DashboardOptions, jobIdFrom, usage as dbUsage } from "./dashboard/job.js"
20+
import dbUsage from "./dashboard/usage.js"
2221

2322
export type Options = DashboardOptions & {
2423
f: boolean
@@ -36,6 +35,12 @@ function usage() {
3635

3736
/** Dump raw info, rather than nicely formatted into a dashboard */
3837
export default async function dump(args: Arguments<Options>) {
38+
const [{ pathsFor }, { filepathOf, isValidKind }, { default: jobIdFrom }] = await Promise.all([
39+
import("./dashboard/tailf.js"),
40+
import("./dashboard/job/kinds.js"),
41+
import("./dashboard/job/jobid.js"),
42+
])
43+
3944
// what kind of data are we being asked to show
4045
const kind = args.argvNoOptions[args.argvNoOptions.indexOf("dump") + 1]
4146

Diff for: plugins/plugin-codeflare-dashboard/src/controller/env.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import Debug from "debug"
1818
import { join } from "path"
1919

20-
import { resourcePaths, filepathOf } from "./dashboard/kinds.js"
20+
import { resourcePaths, filepathOf } from "./dashboard/job/kinds.js"
2121

2222
type NameValue = { name: string; value: unknown }
2323

Diff for: plugins/plugin-codeflare-dashboard/src/controller/path.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import type Kind from "./dashboard/kinds.js"
17+
import type Kind from "./dashboard/job/kinds.js"
1818
import { pathsFor } from "./dashboard/tailf.js"
1919

2020
/** @return path to the data captured for the given jobId in the given profile */

Diff for: plugins/plugin-codeflare-dashboard/src/plugin.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { KResponse, Registrar } from "@kui-shell/core"
18-
17+
import type { KResponse, Registrar } from "@kui-shell/core"
1918
import type { MyOptions as TopOptions } from "./controller/dashboard/top.js"
20-
import type { Options as DashboardOptions } from "./controller/dashboard/job.js"
19+
import type DashboardOptions from "./controller/dashboard/job/options.js"
2120

2221
import { flags } from "./controller/dashboard/options.js"
2322
import { Options as DumpOptions, flags as dumpFlags } from "./controller/dump.js"
@@ -26,7 +25,7 @@ import { Options as DumpOptions, flags as dumpFlags } from "./controller/dump.js
2625
export default function registerCodeflareCommands(registrar: Registrar) {
2726
registrar.listen<KResponse, DashboardOptions>(
2827
`/codeflare/top/job`,
29-
(args) => import("./controller/dashboard/job.js").then((_) => _.default(args)),
28+
(args) => import("./controller/dashboard/job/index.js").then((_) => _.default(args)),
3029
{ flags }
3130
)
3231

0 commit comments

Comments
 (0)