From de050375cf549ad9ef70bb6bfe3a16fc84f73774 Mon Sep 17 00:00:00 2001 From: Azu Barraza <41596861+azu-b@users.noreply.github.com> Date: Tue, 29 Mar 2022 16:31:36 -0600 Subject: [PATCH] Modify /compiler-info to show both active and inactive compilers (#16) --- lib/CompilerManager.js | 2 +- lib/constants.js | 2 + lib/middleware.js | 13 +++++- test/lib/CompilerManager.test.js | 71 +++++++++++++++++++++++++++++++- 4 files changed, 85 insertions(+), 3 deletions(-) diff --git a/lib/CompilerManager.js b/lib/CompilerManager.js index add0dc7..9d077e7 100644 --- a/lib/CompilerManager.js +++ b/lib/CompilerManager.js @@ -147,7 +147,7 @@ class CompilerManager { */ getAllCompilerInfo() { const result = { compilers: {}, leastUsedCompiler: null }; - Object.entries(this.activeCompilers).forEach(([name, compiler]) => { + Object.keys(this.activeCompilers).forEach((name) => { result.compilers[name] = this.getInfoForCompiler(name); }); result.leastUsedCompiler = this.getLeastUsedCompiler(); diff --git a/lib/constants.js b/lib/constants.js index ef21763..9448d9a 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -6,6 +6,7 @@ const FIRST_BUILD = "first-build"; const BUILDING = "building"; const ERROR = "error"; const DONE = "done"; +const NOT_BUILT = "not-built"; module.exports = { PLUGIN_NAME, @@ -14,4 +15,5 @@ module.exports = { BUILDING, ERROR, DONE, + NOT_BUILT, }; diff --git a/lib/middleware.js b/lib/middleware.js index 068496b..0bc5226 100644 --- a/lib/middleware.js +++ b/lib/middleware.js @@ -29,6 +29,7 @@ const { FIRST_BUILD, ERROR, DONE, + NOT_BUILT, } = require("./constants"); // we're leaving manager detached from Kevin to keep it and its methods private @@ -357,7 +358,17 @@ class Kevin { // this endpoint shows general details about each compiler, // particularly metrics around its use and whether it may // be eligible for eviction - res.json(manager.getAllCompilerInfo()); + // 2022 note: adding config names so this endpoint + // also shows the inactive compilers + const { compilers, leastUsedCompiler } = manager.getAllCompilerInfo(); + this.configs.forEach(({ name }) => { + if (Object.prototype.hasOwnProperty.call(compilers, name)) { + return; + } + compilers[name] = { status: NOT_BUILT }; + }); + + res.json({ compilers, leastUsedCompiler }); return; } diff --git a/test/lib/CompilerManager.test.js b/test/lib/CompilerManager.test.js index 1667f9a..d577f9f 100644 --- a/test/lib/CompilerManager.test.js +++ b/test/lib/CompilerManager.test.js @@ -94,7 +94,7 @@ describe("getStatus", () => { }); describe("getAllBuildStatuses", () => { - it("should show the build states of all compilers", () => { + it("should show the build states of active compilers", () => { const manager = new CompilerManager(); manager.manageCompiler("nick", getMockCompiler(), {}); manager.manageCompiler("elback", getMockCompiler(), {}, BUILDING); @@ -118,3 +118,72 @@ describe("getAllBuildStatuses", () => { }); }); }); + +describe("getAllCompilerInfo", () => { + it("should return an object with active compilers", () => { + const manager = new CompilerManager(); + // Active compilers + manager.manageCompiler("iam", getMockCompiler(), {}); + manager.manageCompiler("an", getMockCompiler(), {}, BUILDING); + manager.manageCompiler("active", getMockCompiler(), {}, DONE); + manager.manageCompiler("region", getMockCompiler(), {}, ERROR); + + expect(manager.getAllCompilerInfo()).toEqual( + expect.objectContaining({ + compilers: { + iam: expect.objectContaining({ + status: FIRST_BUILD, + }), + an: expect.objectContaining({ + status: BUILDING, + }), + active: expect.objectContaining({ + status: DONE, + }), + region: expect.objectContaining({ + status: ERROR, + }), + }, + }) + ); + }); + + it("should return more information about the active compilers", () => { + const manager = new CompilerManager(); + + manager.manageCompiler("iam", getMockCompiler(), {}); + manager.manageCompiler("an", getMockCompiler(), {}, BUILDING); + manager.manageCompiler("active", getMockCompiler(), {}, DONE); + manager.manageCompiler("region", getMockCompiler(), {}, ERROR); + + const moreInfo = { + errors: expect.any(Array), + frequency: expect.any(Number), + frecency: expect.any(Number), + frequencyChecks: expect.any(Array), + pinned: expect.any(Boolean), + }; + + expect(manager.getAllCompilerInfo()).toEqual({ + compilers: expect.objectContaining({ + iam: { + status: FIRST_BUILD, + ...moreInfo, + }, + an: { + status: BUILDING, + ...moreInfo, + }, + active: { + status: DONE, + ...moreInfo, + }, + region: { + status: ERROR, + ...moreInfo, + }, + }), + leastUsedCompiler: expect.any(String), + }); + }); +});