Skip to content

Commit 810ec7b

Browse files
Pablo Carmonastarpit
authored andcommitted
add sorting by most recent ones for job runs on tray menu
1 parent dc91129 commit 810ec7b

File tree

2 files changed

+16
-24
lines changed
  • plugins/plugin-codeflare/src/tray

2 files changed

+16
-24
lines changed

plugins/plugin-codeflare/src/tray/menus/profiles/runs.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,18 @@ import ProfileRunWatcher, { RUNS_ERROR } from "../../watchers/profile/run"
2323
type RunOpener = (profile: string, runId: string) => void
2424

2525
/**
26-
* TODO sort the runs by start time
2726
*
2827
* @return a menu for all runs of a profile
2928
*/
30-
export function runMenuItems(profile: string, open: RunOpener, runs: string[]): MenuItemConstructorOptions[] {
31-
return runs.slice(0, 10).map((run) => ({ label: run, click: () => open(profile, run) }))
29+
export function runMenuItems(
30+
profile: string,
31+
open: RunOpener,
32+
runs: { runId: string; timestamp: number }[]
33+
): MenuItemConstructorOptions[] {
34+
return runs
35+
.slice(0, 10)
36+
.sort((a, b) => b.timestamp - a.timestamp)
37+
.map((run) => ({ label: run.runId, click: () => open(profile, run.runId) }))
3238
}
3339

3440
/** Memo of `ProfileStatusWatcher`, keyed by profile name */
@@ -50,7 +56,7 @@ export default async function submenuForRuns(
5056
await watchers[profile].init()
5157

5258
const runs = watchers[profile].runs
53-
return runs.length && runs[0] !== RUNS_ERROR
59+
return runs.length && runs[0].runId !== RUNS_ERROR
5460
? runMenuItems(profile, open, runs)
5561
: [{ label: RUNS_ERROR, enabled: false }]
5662
}

plugins/plugin-codeflare/src/tray/watchers/profile/run.ts

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import chokidar from "chokidar"
1818
import { basename } from "path"
1919

20-
import { readdir } from "fs/promises"
2120
import { Profiles } from "madwizard"
2221

2322
import UpdateFunction from "../../update"
@@ -31,7 +30,7 @@ export const RUNS_ERROR = "No runs found"
3130
*/
3231
export default class ProfileRunWatcher {
3332
/** Our model */
34-
private _runs: string[] = []
33+
private _runs: { runId: string; timestamp: number }[] = []
3534

3635
/** Have we already performed the on-time init? */
3736
private _initDone = false
@@ -57,7 +56,6 @@ export default class ProfileRunWatcher {
5756
/** Initialize `this._runs` model */
5857
public async init(): Promise<ProfileRunWatcher> {
5958
if (!this._initDone) {
60-
// await this.readOnce() no need, since chokidar gives us an initial read
6159
this.initWatcher()
6260
this._initDone = true
6361
}
@@ -66,18 +64,18 @@ export default class ProfileRunWatcher {
6664

6765
/** Initialize the filesystem watcher to notify us of new or removed profiles */
6866
private initWatcher() {
69-
this.watcher.on("addDir", async (path) => {
67+
this.watcher.on("addDir", async (path, stats) => {
7068
const runId = basename(path)
71-
const idx = this.runs.findIndex((_) => _ === runId)
69+
const idx = this.runs.findIndex((_) => _.runId === runId)
7270
if (idx < 0) {
73-
this._runs.push(runId)
71+
this._runs.push({ runId, timestamp: stats?.mtimeMs || stats?.ctimeMs || 0 })
7472
this.updateFn()
7573
}
7674
})
7775

7876
this.watcher.on("unlink", (path) => {
7977
const runId = basename(path)
80-
const idx = this.runs.findIndex((_) => _ === runId)
78+
const idx = this.runs.findIndex((_) => _.runId === runId)
8179
if (idx >= 0) {
8280
this._runs.splice(idx, 1)
8381
this.updateFn()
@@ -91,19 +89,7 @@ export default class ProfileRunWatcher {
9189
}
9290

9391
/** Overwrite the run model state */
94-
private set runs(runs: string[]) {
92+
private set runs(runs: { runId: string; timestamp: number }[]) {
9593
this._runs = runs
9694
}
97-
98-
/** @return files of the directory of job runs for a given profile */
99-
private async readOnce() {
100-
try {
101-
// TODO do a "full" read with Dirents, so that we have filesystem
102-
// timestamps, and sort, so that the `.slice(0, 10)` below pulls
103-
// out the most recent runs
104-
this.runs = await readdir(ProfileRunWatcher.path(this.profile))
105-
} catch (err) {
106-
this.runs = [RUNS_ERROR]
107-
}
108-
}
10995
}

0 commit comments

Comments
 (0)