@atomist/sdm-pack-aspect
Version:
an Atomist SDM Extension Pack for visualizing drift across an organization
97 lines • 5.37 kB
JavaScript
;
/*
* Copyright © 2019 Atomist, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const automation_client_1 = require("@atomist/automation-client");
const sdm_pack_fingerprint_1 = require("@atomist/sdm-pack-fingerprint");
const _ = require("lodash");
const overview_1 = require("../../../views/overview");
const topLevelPage_1 = require("../../../views/topLevelPage");
const pgClientFactory_1 = require("../../analysis/offline/persist/pgClientFactory");
const DefaultAspectRegistry_1 = require("../../aspect/DefaultAspectRegistry");
function exposeOverviewPage(conf, topLevelRoute) {
conf.express.get(topLevelRoute, ...conf.handlers, (req, res, next) => __awaiter(this, void 0, void 0, function* () {
try {
const repos = yield conf.store.loadInWorkspace(req.query.workspace || req.params.workspace_id, false);
const workspaceId = "*";
const fingerprintUsage = yield conf.store.fingerprintUsageForType(workspaceId);
const ideals = yield conf.aspectRegistry.idealStore.loadIdeals(workspaceId);
const aspectsEligibleForDisplay = conf.aspectRegistry.aspects
.filter(a => !!a.displayName)
.filter(a => fingerprintUsage.some(fu => fu.type === a.name));
const foundAspects = _.sortBy(aspectsEligibleForDisplay, a => a.displayName)
.map(aspect => {
const fingerprintsForThisAspect = fingerprintUsage.filter(fu => fu.type === aspect.name);
return {
aspect,
fingerprints: fingerprintsForThisAspect
.map(fp => formatFingerprintUsageForDisplay(aspect, ideals, fp)),
};
});
const unfoundAspects = conf.aspectRegistry.aspects
.filter(f => !!f.displayName)
.filter(f => !fingerprintUsage.some(fu => fu.type === f.name));
const virtualProjectCount = yield conf.store.virtualProjectCount(workspaceId);
res.send(topLevelPage_1.renderStaticReactNode(overview_1.Overview({
projectsAnalyzed: repos.length,
foundAspects,
unfoundAspects,
repos: repos.map(r => ({
id: r.id,
repo: r.repoRef.repo,
owner: r.repoRef.owner,
url: r.repoRef.url,
})),
virtualProjectCount,
}), `Atomist Visualizer (${repos.length} repositories)`, conf.instanceMetadata));
}
catch (e) {
automation_client_1.logger.error(e.stack);
res.status(500).send(cleverlyExplainError(conf, e));
}
}));
}
exports.exposeOverviewPage = exposeOverviewPage;
const ReadmeLink = "https://github.com/atomist/org-visualizer/#database-setup";
function cleverlyExplainError(conf, e) {
if (e.message.includes(pgClientFactory_1.ConnectionErrorHeading) || e.message.includes("ENOCONNECT")) {
return `This page cannot load without a database connection.<br>
Please check <a href="${ReadmeLink}">the org-visualizer README</a> for how to set up a database.<p>
Error:<br>${e.message}`;
}
return `Failed to load page. Please check the log output of ${conf.instanceMetadata.name}`;
}
function idealMatchesFingerprint(id, fp) {
const c = sdm_pack_fingerprint_1.idealCoordinates(id);
return c.type === fp.type && c.name === fp.name;
}
function formatFingerprintUsageForDisplay(aspect, ideals, fp) {
const foundIdeal = ideals.find(ide => idealMatchesFingerprint(ide, fp));
const ideal = foundIdeal && sdm_pack_fingerprint_1.isConcreteIdeal(foundIdeal) && aspect.toDisplayableFingerprint ?
{ displayValue: aspect.toDisplayableFingerprint(foundIdeal.ideal) }
: undefined;
return Object.assign(Object.assign({}, fp), { ideal, displayName: DefaultAspectRegistry_1.defaultedToDisplayableFingerprintName(aspect)(fp.name), entropy: sdm_pack_fingerprint_1.supportsEntropy(aspect) ? fp.entropy : undefined });
}
//# sourceMappingURL=overviewPage.js.map