@atomist/sdm-pack-aspect
Version:
an Atomist SDM Extension Pack for visualizing drift across an organization
397 lines • 19.9 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_core_1 = require("@atomist/sdm-core");
const sdm_pack_fingerprint_1 = require("@atomist/sdm-pack-fingerprint");
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 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 categories_1 = require("./categories");
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, secure) {
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, secure);
exposeAspectMetadata(express, projectAnalysisResultStore, aspectRegistry, secure);
exposeListTags(express, projectAnalysisResultStore, secure);
exposeListFingerprints(express, projectAnalysisResultStore, secure);
exposeFingerprintByType(express, aspectRegistry, projectAnalysisResultStore, secure);
exposeExplore(express, aspectRegistry, projectAnalysisResultStore, secure);
exposeFingerprintByTypeAndName(express, aspectRegistry, projectAnalysisResultStore, secure);
exposeDrift(express, aspectRegistry, projectAnalysisResultStore, secure);
exposeCustomReports(express, projectAnalysisResultStore, secure);
exposePersistEntropy(express, projectAnalysisResultStore, handlers, secure);
},
};
}
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, aspectRegistry, secure) {
// 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(secure)], (req, res, next) => __awaiter(this, void 0, void 0, function* () {
try {
const workspaceId = req.params.workspace_id || "local";
const fingerprintKinds = yield store.distinctRepoFingerprintKinds(workspaceId);
const reports = yield categories_1.getAspectReports(fingerprintKinds, aspectRegistry, 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);
next(e);
}
}));
}
function exposeListFingerprints(express, store, secure) {
// 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(secure)], (req, res, next) => store.fingerprintUsageForType(req.params.workspace_id || "local").then(fingerprintUsage => {
automation_client_1.logger.debug("Returning fingerprints: %j", fingerprintUsage);
res.json({ list: fingerprintUsage });
}, next));
}
function exposeListTags(express, store, secure) {
express.options("/api/v1/:workspace_id/tags", auth_1.corsHandler());
express.get("/api/v1/:workspace_id/tags", [auth_1.corsHandler(), ...auth_1.authHandlers(secure)], (req, res, next) => store.tags(req.params.workspace_id || "local").then(tags => {
automation_client_1.logger.debug("Returning tags: %j", tags);
res.json({ list: tags });
}, next));
}
function exposeFingerprintByType(express, aspectRegistry, store, secure) {
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(secure)], (req, res, next) => __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);
next(e);
}
}));
}
function exposeFingerprintByTypeAndName(express, aspectRegistry, store, secure) {
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(secure)], (req, res, next) => __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 showProgress = req.query.progress === "true";
const trim = req.query.trim === "true";
const byOrg = req.query.byOrg === "true";
const otherLabel = req.query.otherLabel;
try {
const pt = yield buildFingerprintTree_1.buildFingerprintTree({ aspectRegistry, store }, {
otherLabel,
showProgress,
byOrg,
trim,
fingerprintType,
fingerprintName,
workspaceId,
byName,
});
const ideal = yield aspectRegistry.idealStore.loadIdeal(workspaceId, fingerprintType, fingerprintName);
let target;
if (sdm_pack_fingerprint_1.isConcreteIdeal(ideal)) {
const aspect = aspectRegistry.aspectOf(fingerprintType);
if (!!aspect && !!aspect.toDisplayableFingerprint) {
target = Object.assign(Object.assign({}, ideal.ideal), { value: aspect.toDisplayableFingerprint(ideal.ideal) });
}
else if (!!ideal.ideal.data && !!ideal.ideal.data.displayValue) {
target = Object.assign(Object.assign({}, ideal.ideal), { value: ideal.ideal.data.displayValue });
}
else if (!!ideal.ideal.displayValue) {
target = Object.assign(Object.assign({}, ideal.ideal), { value: ideal.ideal.displayValue });
}
}
res.json(Object.assign(Object.assign({}, pt), { target }));
}
catch (e) {
automation_client_1.logger.warn("Error occurred getting one fingerprint: %s %s", e.message, e.stack);
next(e);
}
}));
}
/**
* Drift report, sizing aspects and fingerprints by entropy
*/
function exposeDrift(express, aspectRegistry, store, secure) {
express.options("/api/v1/:workspace_id/drift", auth_1.corsHandler());
express.get("/api/v1/:workspace_id/drift", [auth_1.corsHandler(), ...auth_1.authHandlers(secure)], (req, res, next) => __awaiter(this, void 0, void 0, function* () {
try {
const type = req.query.type;
const band = req.query.band === "true";
const repos = req.query.repos === "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, { repos, 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 => {
if (!!fp.entropy) {
return bands_1.bandFor(commonBands_1.EntropySizeBands, fp.entropy, {
casing: bands_1.BandCasing.Sentence,
includeNumber: false,
});
}
else {
return undefined;
}
},
});
}
// 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);
next(err);
}
}));
}
function exposeIdealAndProblemSetting(express, aspectRegistry, secure) {
// 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(secure)], (req, res, next) => aspectRegistry.idealStore.setIdeal(req.params.workspace_id, req.params.id).then(() => {
automation_client_1.logger.info(`Set ideal to ${req.params.id}`);
res.sendStatus(201);
}, next));
// 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(secure)], (req, res, next) => aspectRegistry.problemStore.noteProblem(req.params.workspace_id, req.params.id).then(() => {
automation_client_1.logger.info(`Set problem at ${req.params.id}`);
res.sendStatus(201);
}, next));
}
/**
* Explore by tags
*/
function exposeExplore(express, aspectRegistry, store, secure) {
express.options("/api/v1/:workspace_id/explore", auth_1.corsHandler());
express.get("/api/v1/:workspace_id/explore", [auth_1.corsHandler(), ...auth_1.authHandlers(secure)], (req, res, next) => __awaiter(this, void 0, void 0, function* () {
try {
const workspaceId = req.params.workspace_id || "*";
const repos = yield store.loadInWorkspace(workspaceId, true);
const selectedTags = req.query.tags ? req.query.tags.split(",") : [];
const category = req.query.category;
const taggedRepos = yield aspectRegistry.tagAndScoreRepos(workspaceId, repos, { category });
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);
// await store.tags(workspaceId)
let repoTree = {
circles: [{ meaning: "tag filter" }, { meaning: "repo" }],
tree: {
name: describeSelectedTagsToAnimals(selectedTags),
children: relevantRepos.map(r => {
return {
id: r.id,
owner: r.analysis.id.owner,
repo: r.analysis.id.repo,
name: r.analysis.id.repo,
url: r.analysis.id.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(Object.assign({ tags: allTags, selectedTags, repoCount: repos.length, matchingRepoCount: relevantRepos.length,
// TODO fix this
averageFingerprintCount: -1 }, repoTree), { workspaceId });
res.send(tagTree);
}
catch (err) {
next(err);
}
}));
}
/**
* 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);
}
}
}
}
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, secure) {
// 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(secure)], (req, res, next) => __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);
next(e);
}
}));
}
function exposePersistEntropy(express, store, handlers, secure) {
// Calculate and persist entropy for this fingerprint
express.options("/api/v1/:workspace_id/entropy/:type/:name", auth_1.corsHandler());
express.put("/api/v1/:workspace_id/entropy/:type/:name", [auth_1.corsHandler(), ...auth_1.authHandlers(secure)], (req, res, next) => __awaiter(this, void 0, void 0, function* () { return analytics_1.computeAnalyticsForFingerprintKind(store, req.params.workspace_id, req.params.type, req.params.name).then(() => res.sendStatus(201), next); }));
}
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