@atomist/sdm-pack-aspect
Version:
an Atomist SDM Extension Pack for visualizing drift across an organization
201 lines • 9.13 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());
});
};
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
Object.defineProperty(exports, "__esModule", { value: true });
const _ = require("lodash");
class DefaultTreeBuilder {
/**
*
* @param {string} rootName
*/
constructor(rootName) {
this.rootName = rootName;
this.steps = [];
}
split(splitStep) {
this.steps.push(Object.assign(Object.assign({}, splitStep), { kind: "split" }));
return this;
}
group(groupStep) {
this.steps.push(Object.assign(Object.assign({}, groupStep), { kind: "group" }));
return this;
}
customGroup(customGroupStep) {
this.steps.push(Object.assign(Object.assign({}, customGroupStep), { kind: "customGroup" }));
return this;
}
map(mapStep) {
this.steps.push(Object.assign(Object.assign({}, mapStep), { kind: "map" }));
return this;
}
/**
* Creates the final tree
* @param {(t: T) => SunburstLeaf} renderer
* @return {SunburstTree}
*/
renderWith(renderer) {
return {
toPlantedTree: (originalQuery) => __awaiter(this, void 0, void 0, function* () {
var e_1, _a;
const data = [];
try {
for (var _b = __asyncValues(originalQuery()), _c; _c = yield _b.next(), !_c.done;) {
const root = _c.value;
data.push(root);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) yield _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
const tree = yield this.treeify(data, renderer);
const circles = [
...this.steps
.filter(step => !!step.name)
.map(step => ({
meaning: step.name,
})),
{ meaning: "render" },
];
return { tree, circles };
}),
};
}
// Make a single tree from materialized data
treeify(data, renderer) {
return __awaiter(this, void 0, void 0, function* () {
return {
name: this.rootName,
children: yield layer(() => data, data, this.steps, renderer),
};
});
}
}
function treeBuilder(rootName) {
return new DefaultTreeBuilder(rootName);
}
exports.treeBuilder = treeBuilder;
function layer(originalQuery, currentLayerData, steps, renderer) {
var currentLayerData_1, currentLayerData_1_1, currentLayerData_2, currentLayerData_2_1, currentLayerData_3, currentLayerData_3_1;
var e_2, _a, e_3, _b, e_4, _c;
return __awaiter(this, void 0, void 0, function* () {
if (steps.length === 0) {
const results = [];
try {
for (currentLayerData_1 = __asyncValues(currentLayerData); currentLayerData_1_1 = yield currentLayerData_1.next(), !currentLayerData_1_1.done;) {
const l = currentLayerData_1_1.value;
results.push(renderer(l));
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (currentLayerData_1_1 && !currentLayerData_1_1.done && (_a = currentLayerData_1.return)) yield _a.call(currentLayerData_1);
}
finally { if (e_2) throw e_2.error; }
}
return results;
}
const step = steps[0];
switch (step.kind) {
case "customGroup":
case "group":
let groups;
if (step.kind === "customGroup") {
groups = yield step.to(currentLayerData);
}
else {
const evaluations = [];
try {
for (currentLayerData_2 = __asyncValues(currentLayerData); currentLayerData_2_1 = yield currentLayerData_2.next(), !currentLayerData_2_1.done;) {
const e = currentLayerData_2_1.value;
evaluations.push(yield Promise.resolve(step.by(e)).then(result => ({
e,
result,
})));
}
}
catch (e_3_1) { e_3 = { error: e_3_1 }; }
finally {
try {
if (currentLayerData_2_1 && !currentLayerData_2_1.done && (_b = currentLayerData_2.return)) yield _b.call(currentLayerData_2);
}
finally { if (e_3) throw e_3.error; }
}
groups = _.groupBy(currentLayerData, e => evaluations.find(ev => ev.e === e).result);
}
// Lodash returns the name as the string "undefined"
const groupNames = Object.getOwnPropertyNames(groups).filter(name => name !== "undefined");
if (groupNames.length === 1 && step.flattenSingle) {
return layer(originalQuery, currentLayerData, steps.slice(1), renderer);
}
else {
return Promise.all(groupNames.map((name) => __awaiter(this, void 0, void 0, function* () {
return {
name,
children: yield layer(originalQuery, yield groups[name], steps.slice(1), renderer),
};
})));
}
case "split":
const splitStep = step;
const kids = [];
try {
for (currentLayerData_3 = __asyncValues(currentLayerData); currentLayerData_3_1 = yield currentLayerData_3.next(), !currentLayerData_3_1.done;) {
const t = currentLayerData_3_1.value;
kids.push({
name: splitStep.namer(t),
children: yield layer(originalQuery, (yield splitStep.splitter(t))
.filter(x => !!x), steps.slice(1), renderer),
});
}
}
catch (e_4_1) { e_4 = { error: e_4_1 }; }
finally {
try {
if (currentLayerData_3_1 && !currentLayerData_3_1.done && (_c = currentLayerData_3.return)) yield _c.call(currentLayerData_3);
}
finally { if (e_4) throw e_4.error; }
}
return kids;
case "map":
const mapStep = step;
return layer(originalQuery, mapStep.mapping(currentLayerData, originalQuery), steps.slice(1), renderer);
default:
throw new Error(`Unknown step type '${step.kind}'`);
}
});
}
//# sourceMappingURL=TreeBuilder.js.map