Skip to content

Commit

Permalink
Prettify provenance output
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Woodward committed Sep 8, 2023
1 parent 05e9d9f commit d186b59
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"typescript.tsc.autoDetect": "off",
"cSpell.words": [
"eopa",
"norc",
"picomatch",
"tsandall"
]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[![slack](https://img.shields.io/badge/slack-styra-24b6e0.svg?logo=slack)](https://styracommunity.slack.com/)
[![Apache License](https://img.shields.io/badge/license-Apache%202.0-orange.svg)](https://www.apache.org/licenses/LICENSE-2.0)
[![Visual Studio Marketplace Version](https://img.shields.io/visual-studio-marketplace/v/Styra.vscode-styra?color=24b6e0)](#)
[![Coverage](https://img.shields.io/badge/Coverage-74%25-brightgreen)](#)
[![Coverage](https://img.shields.io/badge/Coverage-73%25-brightgreen)](#)
[![CI status](https://github.com/StyraInc/vscode-styra/actions/workflows/main.yaml/badge.svg)](https://github.com/StyraInc/vscode-styra/actions/workflows/main.yaml)
[![closed PRs](https://img.shields.io/github/issues-pr-closed-raw/StyraInc/vscode-styra)](https://github.com/StyraInc/vscode-styra/pulls?q=is%3Apr+is%3Aclosed)
<!--
Expand Down
29 changes: 25 additions & 4 deletions src/lib/eopaPreview/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,21 +160,20 @@ export function formatResults(results: object, selection:boolean, raw?: boolean)
if (raw) {
return JSON.stringify(results, null, 2);
}
const r = results as {result: object, metrics: {[key: string]: number}, printed: string, provenance: object};
const r = results as {result: object, metrics: {[key: string]: number}, printed: string, provenance: {[key: string]: string | {[key: string]: object}}};

let output = '';

if (r.provenance) {
output += `PROVENANCE\n==========\n${JSON.stringify(r.provenance, null, 2)}\n\n`;
output += `PROVENANCE\n==========\n${getPrettyProvenance(r.provenance)}`;
}

if (r.metrics?.timer_query_compile_stage_resolve_refs_ns) {
output += `METRICS\n=======\n${metricTables(r.metrics)}`;
// output += `METRICS\n=======\n${Object.keys(r.metrics).map((k: string) => formatMetric(k, r.metrics[k])).join('\n')}\n\n`;
}

if (r.printed) {
output += `PRINTED\n=======\n${r.printed}\n\n`;
output += `PRINTED\n=======\n${r.printed}\n`;
}

output += 'RESULT\n======\n';
Expand Down Expand Up @@ -353,6 +352,28 @@ export function getPrettyTime(ns: number): string {
return (ns / 1e3).toString() + 'µs';
}

function getPrettyProvenance(provenance: {[key: string]: string | {[key: string]: object}}): string {
let output = '';
Object.keys(provenance).forEach((k: string) => {
if (typeof provenance[k] !== 'string') {
return;
}
const niceKey = k.split('_').map((part: string) => part[0].toUpperCase() + part.substring(1)).join(' ');
output += `${niceKey}: ${provenance[k]}\n`;
});
output += '\n';

if (provenance.bundles && typeof provenance.bundles === 'object' && Object.keys(provenance.bundles).length > 0) {
const bundleData: Array<[string,string]> = [['Name', 'Revision']];

Check warning on line 367 in src/lib/eopaPreview/utils.ts

View workflow job for this annotation

GitHub Actions / lint

A space is required after ','
Object.keys(provenance.bundles).forEach((bundle) => {
const revision = (provenance.bundles as {[key: string]: {revision?: string}})[bundle].revision;
bundleData.push([bundle, revision || '']);
});
output += table(bundleData, {border: getBorderCharacters('norc'), columns: [{}, {width: 65, wrapWord: true}], header: {alignment: 'center', content: 'BUNDLES'}}) + '\n';
}
return output;
}

/**
* Reports a result to the Enterprise OPA Preview pane
*/
Expand Down

0 comments on commit d186b59

Please sign in to comment.