@atomist/sdm-pack-aspect
Version:
an Atomist SDM Extension Pack for visualizing drift across an organization
384 lines • 18.5 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) {
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) : new P(function (resolve) { resolve(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_core_1 = require("@atomist/sdm-core");
const sdm_pack_fingerprints_1 = require("@atomist/sdm-pack-fingerprints");
const bodyParser = require("body-parser");
const _ = require("lodash");
const path = require("path");
const swaggerUi = require("swagger-ui-express");
const yaml = require("yamljs");
const analytics_1 = require("../analysis/offline/spider/analytics");
const categories_1 = require("../customize/categories");
const customReporters_1 = require("../customize/customReporters");
const sunburst_1 = require("../tree/sunburst");
const treeUtils_1 = require("../tree/treeUtils");
const bands_1 = require("../util/bands");
const commonBands_1 = require("../util/commonBands");
const auth_1 = require("./auth");
const buildFingerprintTree_1 = require("./buildFingerprintTree");
const tagUtils_1 = require("./support/tagUtils");
const treeMunging_1 = require("./support/treeMunging");
/**
* Expose the public API routes, returning JSON.
* Also expose Swagger API documentation.
*/
function api(projectAnalysisResultStore, aspectRegistry) {
const serveSwagger = sdm_core_1.isInLocalMode();
const docRoute = "/api-docs";
const routesToSuggestOnStartup = serveSwagger ? [{ title: "Swagger", route: docRoute }] : [];
return {
routesToSuggestOnStartup,
customizer: (express, ...handlers) => {
express.use(bodyParser.json()); // to support JSON-encoded bodies
express.use(bodyParser.urlencoded({
extended: true,
}));
if (serveSwagger) {
exposeSwaggerDoc(express, docRoute);
}
auth_1.configureAuth(express);
exposeIdealAndProblemSetting(express, aspectRegistry);
exposeAspectMetadata(express, projectAnalysisResultStore);
exposeListFingerprints(express, projectAnalysisResultStore);
exposeFingerprintByType(express, aspectRegistry, projectAnalysisResultStore);
exposeExplore(express, aspectRegistry, projectAnalysisResultStore);
exposeFingerprintByTypeAndName(express, aspectRegistry, projectAnalysisResultStore);
exposeDrift(express, aspectRegistry, projectAnalysisResultStore);
exposeCustomReports(express, projectAnalysisResultStore);
exposePersistEntropy(express, projectAnalysisResultStore, handlers);
},
};
}
exports.api = api;
function exposeSwaggerDoc(express, docRoute) {
const swaggerDocPath = path.join(__dirname, "..", "..", "swagger.yaml");
const swaggerDocument = yaml.load(swaggerDocPath);
express.use(docRoute, swaggerUi.serve, swaggerUi.setup(swaggerDocument));
}
function exposeAspectMetadata(express, store) {
// Return the aspects metadata
express.options("/api/v1/:workspace_id/aspects", auth_1.corsHandler());
express.get("/api/v1/:workspace_id/aspects", [auth_1.corsHandler(), ...auth_1.authHandlers()], (req, res) => __awaiter(this, void 0, void 0, function* () {
try {
const workspaceId = req.params.workspace_id || "local";
const fingerprintUsage = yield store.fingerprintUsageForType(workspaceId);
const reports = categories_1.getAspectReports(fingerprintUsage, workspaceId);
automation_client_1.logger.debug("Returning aspect reports for '%s': %j", workspaceId, reports);
const count = yield store.distinctRepoCount(workspaceId);
const at = yield store.latestTimestamp(workspaceId);
res.json({
list: reports,
analyzed: {
repo_count: count,
at,
},
});
}
catch (e) {
automation_client_1.logger.warn("Error occurred getting aspect metadata: %s %s", e.message, e.stack);
res.sendStatus(500);
}
}));
}
function exposeListFingerprints(express, store) {
// Return all fingerprints
express.options("/api/v1/:workspace_id/fingerprints", auth_1.corsHandler());
express.get("/api/v1/:workspace_id/fingerprints", [auth_1.corsHandler(), ...auth_1.authHandlers()], (req, res) => __awaiter(this, void 0, void 0, function* () {
try {
const workspaceId = req.params.workspace_id || "local";
const fingerprintUsage = yield store.fingerprintUsageForType(workspaceId);
automation_client_1.logger.debug("Returning fingerprints for '%s': %j", workspaceId, fingerprintUsage);
res.json({ list: fingerprintUsage });
}
catch (e) {
automation_client_1.logger.warn("Error occurred getting fingerprints: %s %s", e.message, e.stack);
res.sendStatus(500);
}
}));
}
function exposeFingerprintByType(express, aspectRegistry, store) {
express.options("/api/v1/:workspace_id/fingerprint/:type", auth_1.corsHandler());
express.get("/api/v1/:workspace_id/fingerprint/:type", [auth_1.corsHandler(), ...auth_1.authHandlers()], (req, res) => __awaiter(this, void 0, void 0, function* () {
try {
const workspaceId = req.params.workspace_id || "*";
const type = req.params.type;
const fps = yield store.fingerprintUsageForType(workspaceId, type);
fillInAspectNamesInList(aspectRegistry, fps);
automation_client_1.logger.debug("Returning fingerprints of type for '%s': %j", workspaceId, fps);
res.json({
list: fps,
analyzed: {
count: fps.length,
variants: _.sumBy(fps, "variants"),
},
});
}
catch (e) {
automation_client_1.logger.warn("Error occurred getting fingerprints: %s %s", e.message, e.stack);
res.sendStatus(500);
}
}));
}
function exposeFingerprintByTypeAndName(express, aspectRegistry, store) {
express.options("/api/v1/:workspace_id/fingerprint/:type/:name", auth_1.corsHandler());
express.get("/api/v1/:workspace_id/fingerprint/:type/:name", [auth_1.corsHandler(), ...auth_1.authHandlers()], (req, res) => __awaiter(this, void 0, void 0, function* () {
const workspaceId = req.params.workspace_id;
const fingerprintType = req.params.type;
const fingerprintName = req.params.name;
const byName = req.params.name !== "*";
const showPresence = req.query.presence === "true";
const showProgress = req.query.progress === "true";
const trim = req.query.trim === "true";
const byOrg = req.query.byOrg === "true";
const otherLabel = req.query.otherLabel === "true";
try {
const pt = yield buildFingerprintTree_1.buildFingerprintTree({ aspectRegistry, store }, {
showPresence,
otherLabel,
showProgress,
byOrg,
trim,
fingerprintType,
fingerprintName,
workspaceId,
byName,
});
const ideal = yield aspectRegistry.idealStore.loadIdeal(workspaceId, fingerprintType, fingerprintName);
let target;
if (sdm_pack_fingerprints_1.isConcreteIdeal(ideal)) {
const aspect = aspectRegistry.aspectOf(fingerprintType);
if (!!aspect && !!aspect.toDisplayableFingerprint) {
target = Object.assign({}, ideal.ideal, { value: aspect.toDisplayableFingerprint(ideal.ideal) });
}
}
res.json(Object.assign({}, pt, { target }));
}
catch (e) {
automation_client_1.logger.warn("Error occurred getting one fingerprint: %s %s", e.message, e.stack);
res.sendStatus(500);
}
}));
}
/**
* Drift report, sizing aspects and fingerprints by entropy
*/
function exposeDrift(express, aspectRegistry, store) {
express.options("/api/v1/:workspace_id/drift", auth_1.corsHandler());
express.get("/api/v1/:workspace_id/drift", [auth_1.corsHandler(), ...auth_1.authHandlers()], (req, res) => __awaiter(this, void 0, void 0, function* () {
try {
const type = req.query.type;
const band = req.query.band === "true";
const percentile = req.query.percentile ? parseFloat(req.query.percentile) : 0;
automation_client_1.logger.info("Entropy query: query.percentile='%s', percentile=%d, type=%s", req.query.percentile, percentile, type);
let driftTree = yield store.aspectDriftTree(req.params.workspace_id, percentile, type);
fillInAspectNames(aspectRegistry, driftTree.tree);
if (!type) {
driftTree = removeAspectsWithoutMeaningfulEntropy(aspectRegistry, driftTree);
}
if (band) {
driftTree = treeUtils_1.introduceClassificationLayer(driftTree, {
newLayerMeaning: "entropy band",
newLayerDepth: 1,
descendantClassifier: fp => bands_1.bandFor(commonBands_1.EntropySizeBands, fp.entropy, {
casing: bands_1.BandCasing.Sentence,
includeNumber: false,
}),
});
}
// driftTree.tree = flattenSoleFingerprints(driftTree.tree);
fillInDriftTreeAspectNames(aspectRegistry, driftTree.tree);
return res.json(driftTree);
}
catch (err) {
automation_client_1.logger.warn("Error occurred getting drift report: %s %s", err.message, err.stack);
res.sendStatus(500);
}
}));
}
function exposeIdealAndProblemSetting(express, aspectRegistry) {
// Set an ideal
express.options("/api/v1/:workspace_id/ideal/:id", auth_1.corsHandler());
express.put("/api/v1/:workspace_id/ideal/:id", [auth_1.corsHandler(), ...auth_1.authHandlers()], (req, res) => __awaiter(this, void 0, void 0, function* () {
yield aspectRegistry.idealStore.setIdeal(req.params.workspace_id, req.params.id);
automation_client_1.logger.info(`Set ideal to ${req.params.id}`);
res.sendStatus(201);
}));
// Note this fingerprint as a problem
express.options("/api/v1/:workspace_id/problem/:id", auth_1.corsHandler());
express.put("/api/v1/:workspace_id/problem/:id", [auth_1.corsHandler(), ...auth_1.authHandlers()], (req, res) => __awaiter(this, void 0, void 0, function* () {
yield aspectRegistry.problemStore.noteProblem(req.params.workspace_id, req.params.id);
automation_client_1.logger.info(`Set problem at ${req.params.id}`);
res.sendStatus(201);
}));
}
/**
* Explore by tags
*/
function exposeExplore(express, aspectRegistry, store) {
express.options("/api/v1/:workspace_id/explore", auth_1.corsHandler());
express.get("/api/v1/:workspace_id/explore", [auth_1.corsHandler(), ...auth_1.authHandlers()], (req, res) => __awaiter(this, void 0, void 0, function* () {
const workspaceId = req.params.workspace_id || "*";
const repos = yield store.loadInWorkspace(workspaceId, true);
const selectedTags = req.query.tags ? req.query.tags.split(",") : [];
const taggedRepos = yield aspectRegistry.tagAndScoreRepos(workspaceId, repos);
const relevantRepos = taggedRepos.filter(repo => selectedTags.every(tag => relevant(tag, repo)));
automation_client_1.logger.info("Found %d relevant repos of %d", relevantRepos.length, repos.length);
const allTags = tagUtils_1.tagUsageIn(aspectRegistry, relevantRepos);
let repoTree = {
circles: [{ meaning: "tag filter" }, { meaning: "repo" }],
tree: {
name: describeSelectedTagsToAnimals(selectedTags),
children: relevantRepos.map(r => {
return {
id: r.id,
owner: r.repoRef.owner,
repo: r.repoRef.repo,
name: r.repoRef.repo,
url: r.repoRef.url,
size: r.analysis.fingerprints.length,
tags: r.tags,
weightedScore: r.weightedScore,
};
}),
},
};
if (req.query.byOrg !== "false") {
repoTree = treeMunging_1.splitByOrg(repoTree);
}
repoTree.tree = treeMunging_1.addRepositoryViewUrl(repoTree.tree);
const tagTree = Object.assign({ tags: allTags, selectedTags, repoCount: repos.length, matchingRepoCount: relevantRepos.length,
// TODO fix this
averageFingerprintCount: -1 }, repoTree, { workspaceId });
res.send(tagTree);
}));
}
/**
* Any nodes that have type and name should be given the fingerprint name from the aspect if possible
*/
function fillInAspectNames(aspectRegistry, tree) {
treeUtils_1.visit(tree, n => {
const t = n;
if (t.name && t.type) {
if (t.name && t.type) {
const aspect = aspectRegistry.aspectOf(t.type);
if (aspect) {
if (aspect.toDisplayableFingerprintName) {
n.name = aspect.toDisplayableFingerprintName(n.name);
}
else if (aspect.displayName) {
n.name = aspect.displayName;
}
}
}
}
return true;
});
}
/**
* If the aspect says entropy isn't significant, reduce it.
*/
function removeAspectsWithoutMeaningfulEntropy(aspectRegistry, driftTree) {
driftTree.tree = treeUtils_1.killChildren(driftTree.tree, child => {
if (sunburst_1.isSunburstTree(child)) {
return false;
}
const t = child;
if (t.type) {
const aspect = aspectRegistry.aspectOf(t.type);
return !!aspect && !!aspect.stats && aspect.stats.defaultStatStatus.entropy === false;
}
return false;
});
return driftTree;
}
function flattenSoleFingerprints(tree) {
// Remove anything where entropy isn't meaningful
return treeUtils_1.trimOuterRim(tree, container => container.children.length === 1);
}
/**
* Fill in aspect names
*/
function fillInAspectNamesInList(aspectRegistry, fingerprints) {
fingerprints.forEach(fp => {
const aspect = aspectRegistry.aspectOf(fp.type);
if (!!aspect && !!aspect.toDisplayableFingerprintName) {
fp.displayName = aspect.toDisplayableFingerprintName(fp.name);
}
// This is going to be needed for the invocation of the command handlers to set targets
fp.fingerprint = `${fp.type}::${fp.name}`;
});
}
function fillInDriftTreeAspectNames(aspectRegistry, driftTree) {
treeUtils_1.visit(driftTree, (n, depth) => {
if (depth === 2) {
const aspect = aspectRegistry.aspectOf(n.name);
if (aspect && aspect.displayName) {
n.name = aspect.displayName;
}
}
return true;
});
}
function exposeCustomReports(express, store) {
// In memory queries against returns
express.options("/api/v1/:workspace_id/report/:name", auth_1.corsHandler());
express.get("/api/v1/:workspace_id/report/:name", [auth_1.corsHandler(), ...auth_1.authHandlers()], (req, res) => __awaiter(this, void 0, void 0, function* () {
try {
const q = customReporters_1.CustomReporters[req.params.name];
if (!q) {
throw new Error(`No report named '${req.params.name}'`);
}
const repos = yield store.loadInWorkspace(req.query.workspace || req.params.workspace_id, true);
const relevantRepos = repos.filter(ar => req.query.owner ? ar.analysis.id.owner === req.params.owner : true);
let pt = yield q.builder.toPlantedTree(() => relevantRepos.map(r => r.analysis));
if (req.query.byOrg !== "false") {
pt = treeMunging_1.splitByOrg(pt);
}
return res.json(pt);
}
catch (e) {
automation_client_1.logger.warn("Error occurred getting report: %s %s", e.message, e.stack);
res.sendStatus(500);
}
}));
}
function exposePersistEntropy(express, store, handlers) {
// Calculate and persist entropy for this fingerprint
express.put("/api/v1/:workspace/entropy/:type/:name", ...handlers, (req, res) => __awaiter(this, void 0, void 0, function* () {
yield analytics_1.computeAnalyticsForFingerprintKind(store, req.params.workspace, req.params.type, req.params.name);
res.sendStatus(201);
}));
}
function relevant(selectedTag, repo) {
const repoTags = repo.tags.map(tag => tag.name);
return selectedTag.startsWith("!") ? !repoTags.includes(selectedTag.substr(1)) : repoTags.includes(selectedTag);
}
function describeSelectedTagsToAnimals(selectedTags) {
return selectedTags.map(t => t.replace("!", "not ")).join(" and ") || "All";
}
exports.describeSelectedTagsToAnimals = describeSelectedTagsToAnimals;
//# sourceMappingURL=api.js.map