UNPKG

@atomist/sdm-pack-aspect

Version:

an Atomist SDM Extension Pack for visualizing drift across an organization

170 lines 7.81 kB
"use strict"; /* * 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 treeUtils_1 = require("../../../tree/treeUtils"); const pgUtils_1 = require("./pgUtils"); /** * Return results for non-matching fingerprints */ function nonMatchingRepos(tq) { return `SELECT null as id, $3 as name, null as sha, null as data, $1 as type, ( SELECT json_agg(row_to_json(repo)) FROM ( SELECT repo_snapshots.id, repo_snapshots.owner, repo_snapshots.name, repo_snapshots.url, 1 as size FROM repo_snapshots WHERE workspace_id ${tq.workspaceId === "*" ? "<>" : "="} $1 AND repo_snapshots.id not in (select repo_fingerprints.repo_snapshot_id FROM repo_fingerprints WHERE repo_fingerprints.fingerprint_id in (SELECT id from fingerprints where fingerprints.feature_name = $2 AND fingerprints.name ${tq.byName ? "=" : "<>"} $3)) ) repo ) children`; } function fingerprintsToReposQuery(tq) { // We always select by aspect (aka feature_name, aka type), and sometimes also by fingerprint name. const sql = ` SELECT row_to_json(fingerprint_groups) FROM ( SELECT json_agg(fp) as children FROM ( SELECT fingerprints.id as id, fingerprints.name as name, fingerprints.sha as sha, fingerprints.data as data, fingerprints.feature_name as type, ( SELECT json_agg(row_to_json(repo)) FROM ( SELECT repo_snapshots.id, repo_snapshots.owner, repo_snapshots.name, repo_snapshots.url, 1 as size, repo_fingerprints.path FROM repo_fingerprints, repo_snapshots WHERE repo_fingerprints.fingerprint_id = fingerprints.id AND repo_snapshots.id = repo_fingerprints.repo_snapshot_id AND workspace_id ${tq.workspaceId === "*" ? "<>" : "="} $1 ) repo ) as children FROM fingerprints WHERE fingerprints.feature_name = $2 and fingerprints.name ${tq.byName ? "=" : "<>"} $3 ${tq.includeWithout ? ("UNION ALL " + nonMatchingRepos(tq)) : ""} ) fp WHERE children is not NULL) as fingerprint_groups `; automation_client_1.logger.debug("Running fingerprintsToRepos SQL\n%s", sql); return sql; } /** * Tree where children is one of a range of values, leaves individual repos with one of those values */ function fingerprintsToReposTreeQuery(tq, clientFactory) { return __awaiter(this, void 0, void 0, function* () { const sql = fingerprintsToReposQuery(tq); const children = yield pgUtils_1.doWithClient(sql, clientFactory, (client) => __awaiter(this, void 0, void 0, function* () { try { const results = yield client.query(sql, [tq.workspaceId, tq.aspectName, tq.rootName]); const data = results.rows[0]; return data.row_to_json.children; } catch (err) { automation_client_1.logger.error("Error running SQL %s: %s", sql, err); throw err; } }), []); const result = { tree: { name: tq.rootName, children, }, circles: [ { meaning: tq.byName ? "fingerprint name" : "aspect" }, { meaning: "fingerprint value" }, { meaning: "repo" }, ], }; treeUtils_1.validatePlantedTree(result); return result; }); } exports.fingerprintsToReposTreeQuery = fingerprintsToReposTreeQuery; function driftTreeForAllAspects(workspaceId, percentile, clientFactory) { return __awaiter(this, void 0, void 0, function* () { const sql = driftTreeSql(workspaceId); const circles = [ { meaning: "report" }, { meaning: "aspect name" }, { meaning: "fingerprint name" }, ]; return pgUtils_1.doWithClient(sql, clientFactory, (client) => __awaiter(this, void 0, void 0, function* () { const result = yield client.query(sql, [workspaceId, percentile / 100]); const tree = { circles, tree: { name: "drift", children: result.rows.map(r => r.children), }, }; return tree; }), err => { return { circles, tree: { name: "failed drift report", children: [] }, errors: [{ message: err.message }], }; }); }); } exports.driftTreeForAllAspects = driftTreeForAllAspects; function driftTreeForSingleAspect(workspaceId, type, percentile, clientFactory) { return __awaiter(this, void 0, void 0, function* () { const sql = driftTreeSql(workspaceId, type); return pgUtils_1.doWithClient(sql, clientFactory, (client) => __awaiter(this, void 0, void 0, function* () { const result = yield client.query(sql, [workspaceId, percentile / 100, type]); const tree = { circles: [ { meaning: "type" }, { meaning: "fingerprint entropy" }, ], tree: { name: type, children: result.rows[0].children.children, }, }; return tree; })); }); } exports.driftTreeForSingleAspect = driftTreeForSingleAspect; function driftTreeSql(workspaceId, type) { return `SELECT row_to_json(data) as children FROM (SELECT f0.type as name, f0.type as type, json_agg(aspects) as children FROM (SELECT distinct feature_name as type from fingerprint_analytics) f0, ( SELECT name, name as fingerprint_name, feature_name as type, variants, count, entropy, variants as size FROM fingerprint_analytics f1 WHERE workspace_id ${workspaceId === "*" ? "<>" : "="} $1 AND entropy > (SELECT percentile_disc($2) within group (order by entropy) FROM fingerprint_analytics WHERE workspace_id ${workspaceId === "*" ? "<>" : "="} $1) ORDER BY entropy desc) as aspects WHERE aspects.type = f0.type ${type ? `AND aspects.type = $3` : ""} GROUP by f0.type) as data`; } //# sourceMappingURL=repoTree.js.map