|
| 1 | +const debug = require("debug")("action-dashboard:actions"); |
| 2 | +const _ = require("lodash"); |
| 3 | +const dayjs = require("dayjs"); |
| 4 | +const pLimit = require("p-limit"); |
| 5 | + |
| 6 | +class Actions { |
| 7 | + constructor(gitHub, runStatus, lookbackDays) { |
| 8 | + this._gitHub = gitHub; |
| 9 | + this._runStatus = runStatus; |
| 10 | + // Cache all workflows to speed up refresh |
| 11 | + this._runs = []; |
| 12 | + this._refreshingRuns = false; |
| 13 | + this._lookbackDays = lookbackDays; |
| 14 | + } |
| 15 | + |
| 16 | + start() { |
| 17 | + debug("Performing initial refreshRuns"); |
| 18 | + // Load the initial set |
| 19 | + this.refreshRuns(); |
| 20 | + |
| 21 | + debug("Setting interval to refreshRuns at 15m"); |
| 22 | + // Refresh by default every fifteeen minutes |
| 23 | + setInterval(this.refreshRuns, 1000 * 60 * 15); |
| 24 | + } |
| 25 | + |
| 26 | + async getMostRecentRuns(repoOwner, repoName, workflowId) { |
| 27 | + try { |
| 28 | + const daysAgo = dayjs().subtract(this._lookbackDays, "day"); |
| 29 | + const runs = await this._gitHub.listWorkflowRuns( |
| 30 | + repoOwner, |
| 31 | + repoName, |
| 32 | + workflowId |
| 33 | + ); |
| 34 | + if (runs.length > 0) { |
| 35 | + const groupedRuns = _.groupBy(runs, "head_branch"); |
| 36 | + const rows = _.reduce( |
| 37 | + groupedRuns, |
| 38 | + (result, runs, branch) => { |
| 39 | + debug(`branch`, branch); |
| 40 | + if (daysAgo.isBefore(dayjs(runs[0].created_at))) { |
| 41 | + debug(`adding run.id: ${runs[0].id}`); |
| 42 | + result.push({ |
| 43 | + runId: runs[0].id, |
| 44 | + repo: runs[0].repository.name, |
| 45 | + owner: repoOwner, |
| 46 | + workflowId: workflowId, |
| 47 | + runNumber: runs[0].run_number, |
| 48 | + workflow: runs[0].name, |
| 49 | + branch: runs[0].head_branch, |
| 50 | + sha: runs[0].head_sha, |
| 51 | + message: runs[0].head_commit.message, |
| 52 | + committer: runs[0].head_commit.committer.name, |
| 53 | + status: |
| 54 | + runs[0].status === "completed" |
| 55 | + ? runs[0].conclusion |
| 56 | + : runs[0].status, |
| 57 | + createdAt: runs[0].created_at, |
| 58 | + updatedAt: runs[0].updated_at, |
| 59 | + }); |
| 60 | + } else { |
| 61 | + debug( |
| 62 | + `skipping run.id: ${runs[0].id} created_at: ${runs[0].created_at}` |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + return result; |
| 67 | + }, |
| 68 | + [] |
| 69 | + ); |
| 70 | + |
| 71 | + debug( |
| 72 | + `getting duration of runs owner: ${repoOwner}, repo: ${repoName}, workflowId: ${workflowId}` |
| 73 | + ); |
| 74 | + |
| 75 | + // Get durations of runs |
| 76 | + const limit = pLimit(10); |
| 77 | + const getUsagePromises = rows.map((row) => { |
| 78 | + return limit(async () => { |
| 79 | + const usage = await this._gitHub.getUsage( |
| 80 | + repoOwner, |
| 81 | + repoName, |
| 82 | + workflowId, |
| 83 | + row.runId |
| 84 | + ); |
| 85 | + if (usage?.run_duration_ms) { |
| 86 | + row.durationMs = usage.run_duration_ms; |
| 87 | + } |
| 88 | + |
| 89 | + return row; |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + const rowsWithDuration = await Promise.all(getUsagePromises); |
| 94 | + |
| 95 | + debug( |
| 96 | + `most recent runs owner: ${repoOwner}, repo: ${repoName}, workflowId: ${workflowId}`, |
| 97 | + rowsWithDuration |
| 98 | + ); |
| 99 | + return rows; |
| 100 | + } else { |
| 101 | + return []; |
| 102 | + } |
| 103 | + } catch (e) { |
| 104 | + console.error("Error getting runs", e); |
| 105 | + return []; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + mergeRuns(runs) { |
| 110 | + // Merge into cache |
| 111 | + runs.forEach((run) => { |
| 112 | + debug(`merging run`, run); |
| 113 | + const index = _.findIndex(this._runs, { |
| 114 | + workflowId: run.workflowId, |
| 115 | + branch: run.branch, |
| 116 | + }); |
| 117 | + if (index >= 0) { |
| 118 | + this._runs[index] = run; |
| 119 | + } else { |
| 120 | + this._runs.push(run); |
| 121 | + } |
| 122 | + this._runStatus.updatedRun(run); |
| 123 | + }); |
| 124 | + |
| 125 | + debug("merged runs", this._runs); |
| 126 | + } |
| 127 | + |
| 128 | + async refreshRuns() { |
| 129 | + // Prevent re-entrant calls |
| 130 | + if (this._refreshingRuns) { |
| 131 | + return; |
| 132 | + } |
| 133 | + |
| 134 | + debug("Starting refreshing runs"); |
| 135 | + try { |
| 136 | + this._refreshingRuns = true; |
| 137 | + const repos = await this._gitHub.listRepos(); |
| 138 | + for (const repo of repos) { |
| 139 | + debug(`repo: ${repo.name}`); |
| 140 | + const workflows = await this._gitHub.listWorkflowsForRepo( |
| 141 | + repo.name, |
| 142 | + repo.owner.login |
| 143 | + ); |
| 144 | + if (workflows.length > 0) { |
| 145 | + for (const workflow of workflows) { |
| 146 | + debug(`workflow: ${workflow.name}`); |
| 147 | + const runs = await this.getMostRecentRuns( |
| 148 | + repo.owner.login, |
| 149 | + repo.name, |
| 150 | + workflow.id |
| 151 | + ); |
| 152 | + // Not using apply or spread in case there are a large number of runs returned |
| 153 | + this.mergeRuns(runs); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + } catch (e) { |
| 158 | + console.error("Error getting initial data", e); |
| 159 | + } finally { |
| 160 | + debug("Finished refreshing runs"); |
| 161 | + this._refreshingRuns = false; |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + async refreshWorkflow(repoOwner, repoName, workflowId) { |
| 166 | + const runs = await this.getMostRecentRuns(repoOwner, repoName, workflowId); |
| 167 | + this.mergeRuns(runs); |
| 168 | + } |
| 169 | + |
| 170 | + getInitialData() { |
| 171 | + debug(`getInitialData this._runs.length: ${this._runs.length}`); |
| 172 | + if (this._runs.length === 0 && !this._refreshingRuns) { |
| 173 | + debug("getInitialData calling refreshRuns"); |
| 174 | + this.refreshRuns(); |
| 175 | + } |
| 176 | + |
| 177 | + return this._runs; |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +module.exports = Actions; |
0 commit comments