@tduniec/backstage-plugin-time-saver-backend
Version:
This plugin provides an implementation of charts and statistics related to your time savings that are coming from usage of your templates. Plugins is built from frontend and backend part. Backend plugin is responsible for scheduled stats parsing process a
75 lines (71 loc) • 2.49 kB
JavaScript
;
var scaffolderClient = require('../api/scaffolderClient.cjs.js');
var luxon = require('luxon');
class TimeSaverHandler {
constructor(logger, config, auth, db) {
this.logger = logger;
this.config = config;
this.auth = auth;
this.db = db;
}
async fetchTemplates() {
const pageSize = this.config.getOptionalNumber("ts.scheduler.parallelProcessing") ?? 100;
this.logger.debug(`SET parallelProcessing of tasks to: ${pageSize}`);
const client = new scaffolderClient.ScaffolderClient(this.logger, this.config, this.auth);
this.logger.info("START \u2013 Collecting Time Savings data from templates");
let excludedSet = /* @__PURE__ */ new Set();
try {
const excluded = await this.db.getTasksToExclude();
if (Array.isArray(excluded)) excludedSet = new Set(excluded);
} catch (e) {
this.logger.error("Failed to load exclusion list", e);
return "FAIL";
}
await this.db.truncate();
for (let page = 0; ; page++) {
this.logger.debug(`Fetching page ${page} (size=${pageSize})`);
const tasks = await client.fetchTemplatesFromScaffolder({
page,
pageSize
});
if (tasks.length === 0) break;
const rows = [];
for (const tpl of tasks) {
if (tpl.status !== "completed" || excludedSet.has(tpl.id)) {
continue;
}
const subs = tpl.spec.templateInfo.entity.metadata.substitute?.engineering;
if (!subs) {
continue;
}
const createdAt = luxon.DateTime.fromISO(tpl.createdAt, { setZone: true });
if (!createdAt.isValid) {
this.logger.error(
`Invalid createdAt for template ${tpl.id}: ${tpl.createdAt}`
);
continue;
}
for (const [team, timeSaved] of Object.entries(subs)) {
rows.push({
team,
role: "",
timeSaved,
createdAt,
createdBy: tpl.createdBy,
templateName: tpl.spec.templateInfo.entityRef,
templateTaskStatus: tpl.status,
templateTaskId: tpl.id
});
}
}
if (rows.length) {
this.logger.debug(`Inserting ${rows.length} rows`);
await this.db.bulkInsertTimeSavings(rows);
}
}
this.logger.info("STOP \u2013 Collecting Time Savings data from templates");
return "SUCCESS";
}
}
exports.TimeSaverHandler = TimeSaverHandler;
//# sourceMappingURL=handler.cjs.js.map