Skip to content

Commit

Permalink
maybe some good things with indexing our results
Browse files Browse the repository at this point in the history
  • Loading branch information
mint-thompson committed Feb 7, 2024
1 parent 041a8fd commit c4602a8
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 73 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 52 additions & 12 deletions src/DockerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import { logger } from './logger';

export class DockerManager {
containerId = '';
processedUrls: { uri: string; schema: string }[] = [];

constructor(public outputPath = '') {}
constructor(public outputPath = './') {}

private async initContainerId(): Promise<void> {
this.containerId = await util
Expand All @@ -24,7 +25,7 @@ export class DockerManager {
schemaPath: string,
schemaName: string,
dataPath: string,
outputPath = this.outputPath
dataUri: string
): Promise<ContainerResult> {
try {
if (this.containerId.length === 0) {
Expand All @@ -35,36 +36,75 @@ export class DockerManager {
temp.track();
const outputDir = temp.mkdirSync('output');
const containerOutputPath = path.join(outputDir, 'output.txt');
const containerLocationPath = path.join(outputDir, 'locations.json');
const containerReportsPath = path.join(outputDir, 'reports.json');
// copy output files after it finishes
const runCommand = this.buildRunCommand(schemaPath, dataPath, outputDir, schemaName);
logger.info('Running validator container...');
logger.debug(runCommand);
return util
.promisify(exec)(runCommand)
.then(() => {
this.processedUrls.push({ uri: dataUri, schema: schemaName });
const containerResult: ContainerResult = { pass: true };
if (fs.existsSync(containerOutputPath)) {
if (outputPath) {
fs.copySync(containerOutputPath, outputPath);
if (this.outputPath) {
fs.copySync(
containerOutputPath,
path.join(this.outputPath, `output${this.processedUrls.length}.txt`)
);
} else {
const outputText = fs.readFileSync(containerOutputPath, 'utf-8');
logger.info(outputText);
}
}
if (fs.existsSync(containerLocationPath)) {
if (fs.existsSync(containerReportsPath)) {
if (this.outputPath) {
fs.copySync(
containerReportsPath,
path.join(this.outputPath, `reports${this.processedUrls.length}.json`)
);
}
try {
containerResult.locations = fs.readJsonSync(containerLocationPath);
if (schemaName === 'table-of-contents') {
// if key ends with .allowed_amount_file: it's an object, grab .location
// if key ends with .in_network_files: it's an array, foreach grab .location
const reports = fs.readJsonSync(containerReportsPath);
containerResult.locations = { allowedAmount: [], inNetwork: [] };
Object.keys(reports).forEach((key: string) => {
if (key.endsWith('.allowed_amount_file') && reports[key].location != null) {
containerResult.locations.allowedAmount.push(reports[key].location);
} else if (key.endsWith('.in_network_files')) {
reports[key]?.forEach((inNetwork: any) => {
if (inNetwork?.location != null) {
containerResult.locations.inNetwork.push(inNetwork.location);
}
});
}
});
} else if (schemaName === 'in-network-rates') {
// if key ends with .location: it's a string, grab it
const reports = fs.readJsonSync(containerReportsPath);
containerResult.locations = { providerReference: [] };
Object.keys(reports).forEach((key: string) => {
if (key.endsWith('.location')) {
containerResult.locations.providerReference.push(reports[key]);
}
});
}
} catch (err) {
// something went wrong when reading the location file that the validator produced
// don't know either
}
}
return containerResult;
})
.catch(() => {
.catch((..._zagwo) => {
this.processedUrls.push({ uri: dataUri, schema: schemaName });
if (fs.existsSync(containerOutputPath)) {
if (outputPath) {
fs.copySync(containerOutputPath, outputPath);
if (this.outputPath) {
fs.copySync(
containerOutputPath,
path.join(this.outputPath, `output${this.processedUrls.length}.txt`)
);
} else {
const outputText = fs.readFileSync(containerOutputPath, 'utf-8');
logger.info(outputText);
Expand Down Expand Up @@ -103,7 +143,7 @@ export class DockerManager {
outputDir
)}":/output/ ${
this.containerId
} "schema/${schemaFile}" "data/${dataFile}" -o "output/" -s ${schemaName}`;
} "schema/${schemaFile}" "data/${dataFile}" "output/" -s ${schemaName}`;
}
}

Expand Down
30 changes: 25 additions & 5 deletions src/commands.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import util from 'util';
import path from 'path';
import { exec } from 'child_process';
import os from 'os';
import fs from 'fs-extra';
import readlineSync from 'readline-sync';
import { OptionValues } from 'commander';
Expand All @@ -25,6 +26,7 @@ export async function validate(dataFile: string, options: OptionValues) {
process.exitCode = 1;
return;
}
fs.ensureDirSync(options.out);
const schemaManager = new SchemaManager();
await schemaManager.ensureRepo();
schemaManager.strict = options.strict;
Expand Down Expand Up @@ -65,9 +67,10 @@ export async function validate(dataFile: string, options: OptionValues) {
const containerResult = await dockerManager.runContainer(
schemaPath,
options.target,
dataFile
dataFile,
`file://${dataFile}`
);
if (containerResult.pass) {
if (containerResult.pass || true) {
if (options.target === 'table-of-contents') {
const providerReferences = await assessTocContents(
containerResult.locations,
Expand All @@ -92,6 +95,11 @@ export async function validate(dataFile: string, options: OptionValues) {
downloadManager
);
}
// make index file
const indexContents = dockerManager.processedUrls
.map(({ uri, schema }, index) => `${index + 1}\t\t${schema}\t\t${uri}`)
.join(os.EOL);
fs.writeFileSync(path.join(options.out, 'result-index.txt'), indexContents);
}
} else {
logger.error('No schema available - not validating.');
Expand All @@ -107,6 +115,7 @@ export async function validate(dataFile: string, options: OptionValues) {

export async function validateFromUrl(dataUrl: string, options: OptionValues) {
temp.track();
fs.ensureDirSync(options.out);
const downloadManager = new DownloadManager(options.yesAll);
if (await downloadManager.checkDataUrl(dataUrl)) {
const schemaManager = new SchemaManager();
Expand All @@ -127,9 +136,10 @@ export async function validateFromUrl(dataUrl: string, options: OptionValues) {
const containerResult = await dockerManager.runContainer(
schemaPath,
options.target,
dataFile
dataFile,
dataUrl
);
if (containerResult.pass) {
if (containerResult.pass || true) {
if (options.target === 'table-of-contents') {
const providerReferences = await assessTocContents(
containerResult.locations,
Expand Down Expand Up @@ -162,13 +172,23 @@ export async function validateFromUrl(dataUrl: string, options: OptionValues) {
while (continuation === true) {
const chosenEntry = chooseJsonFile(dataFile.jsonEntries);
await getEntryFromZip(dataFile.zipFile, chosenEntry, dataFile.dataPath);
await dockerManager.runContainer(schemaPath, options.target, dataFile.dataPath);
await dockerManager.runContainer(
schemaPath,
options.target,
dataFile.dataPath,
`${dataUrl}:${chosenEntry.fileName}` // TODO see if this is actually useful
);
continuation = readlineSync.keyInYNStrict(
'Would you like to validate another file in the ZIP?'
);
}
dataFile.zipFile.close();
}
// make index file
const indexContents = dockerManager.processedUrls
.map(({ uri, schema }, index) => `${index + 1}\t\t${schema}\t\t${uri}`)
.join(os.EOL);
fs.writeFileSync(path.join(options.out, 'result-index.txt'), indexContents);
} else {
logger.error('No schema available - not validating.');
process.exitCode = 1;
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async function main() {
.usage('<data-file> [options]')
.argument('<data-file>', 'path to data file to validate')
.option('--schema-version <version>', 'version of schema to use for validation')
.option('-o, --out <out>', 'output path')
.option('-o, --out <out>', 'output directory', './')
.addOption(
new Option('-t, --target <schema>', 'name of schema to use')
.choices(config.AVAILABLE_SCHEMAS)
Expand All @@ -46,7 +46,7 @@ async function main() {
.usage('<data-url> [options]')
.argument('<data-url>', 'URL to data file to validate')
.option('--schema-version <version>', 'version of schema to use for validation')
.option('-o, --out <out>', 'output path')
.option('-o, --out <out>', 'output directory', './')
.addOption(
new Option('-t, --target <schema>', 'name of schema to use')
.choices(config.AVAILABLE_SCHEMAS)
Expand Down
Loading

0 comments on commit c4602a8

Please sign in to comment.