-
Notifications
You must be signed in to change notification settings - Fork 113
Json stdout #2044
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Json stdout #2044
Conversation
Enables JSON reports to be optionally output directly to stdout for piping to other programs, rather than being written to file system. Log output is suppressed. Involves changing how output directory is handled, but not intended to change existing default functionality in the CLI or the GUI. closes MobilityData#1194
|
Thanks for opening this pull request! You're awesome. We use semantic commit messages to streamline the release process. Before your pull request can be merged, you should update your pull request title to start with a semantic prefix. Examples of titles with semantic prefixes:
|
|
|
|
Hi @drewda! Thanks for this great contribution and apologies for it slipping our notice in the midst of getting on top of emails after summer vacations. We plan to take a look soon. |
davidgamez
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @drewda, great work!
I added a few minor comments that increase the readability of the code. I tested locally and it works as expected.
As a separate thought, I'm thinking of a fatal error scenario. In this case, as expected, the error is flushed to stderr, and nothing is output to stdout. I'm wondering if it makes sense to flush the "content" of the system_errors.json to the stdout so that the pipe processing can make decisions in case of errors. Otherwise, the pipe will get an empty string with no context of the error. This is an edge case where no human is looking at the screen. cc: @emmambd
| } | ||
|
|
||
| try { | ||
| Desktop.getDesktop().browse(reportPath.toUri()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick]: As reportPath can be null now, even currently, the CLI parameters are not supported while running the GUI app; it would be nice to log here a message when reportPath is null and return without executing reportPath.toUri() that will raise a NullPointerException.
| config != null ? config.systemErrorsReportFileName() : null, | ||
| config != null ? config.validationReportFileName() : null, | ||
| config != null ? config.htmlReportFileName() : null, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To keep consistency, we can set the exported filenames as they will not be created with the stdout parameter. Please take the suggested code snippet as a guide, I'm not sure if it has the right alignment or any other issue, GitHub editor has its limitations ;-)
| config != null ? config.systemErrorsReportFileName() : null, | |
| config != null ? config.validationReportFileName() : null, | |
| config != null ? config.htmlReportFileName() : null, | |
| config != null && !config.stdoutOutput() ? config.systemErrorsReportFileName() : null, | |
| config != null && !config.stdoutOutput() ? config.validationReportFileName() : null, | |
| config != null && !config.stdoutOutput() ? config.htmlReportFileName() : null, |
| if (config.outputDirectory().isPresent()) { | ||
| Path outputDir = config.outputDirectory().get(); | ||
| if (!Files.exists(outputDir)) { | ||
| try { | ||
| Files.createDirectories(outputDir); | ||
| } catch (IOException ex) { | ||
| logger.atSevere().withCause(ex).log("Error creating output directory: %s", outputDir); | ||
| } | ||
| } | ||
| } | ||
| boolean is_different_date = !now.toLocalDate().equals(config.dateForValidation()); | ||
|
|
||
| HtmlReportGenerator htmlGenerator = new HtmlReportGenerator(); | ||
| if (config.outputDirectory().isPresent()) { | ||
| Path outputDir = config.outputDirectory().get(); | ||
| try { | ||
| Files.write( | ||
| outputDir.resolve(config.validationReportFileName()), | ||
| gson.toJson(jsonReport).getBytes(StandardCharsets.UTF_8)); | ||
| } catch (Exception ex) { | ||
| logger.atSevere().withCause(ex).log("Error creating JSON report"); | ||
| } | ||
|
|
||
| try { | ||
| htmlGenerator.generateReport( | ||
| feedMetadata, | ||
| noticeContainer, | ||
| config, | ||
| versionInfo, | ||
| outputDir.resolve(config.htmlReportFileName()), | ||
| date, | ||
| is_different_date); | ||
| Files.write( | ||
| outputDir.resolve(config.systemErrorsReportFileName()), | ||
| gson.toJson(noticeContainer.exportSystemErrors()).getBytes(StandardCharsets.UTF_8)); | ||
| } catch (IOException e) { | ||
| logger.atSevere().withCause(e).log("Cannot store report files"); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[suggestion]: if the execution reached L340, then the previous conditional config.stdoutOutput() is false(if true, the function would return in line L336). In this case, we can remove the two IF conditions as the output directory is expected to be populated. Please take the suggested code snippet as a guide, I'm not sure if it has the right alignment or any other issue, GitHub editor has its limitations ;-)
| if (config.outputDirectory().isPresent()) { | |
| Path outputDir = config.outputDirectory().get(); | |
| if (!Files.exists(outputDir)) { | |
| try { | |
| Files.createDirectories(outputDir); | |
| } catch (IOException ex) { | |
| logger.atSevere().withCause(ex).log("Error creating output directory: %s", outputDir); | |
| } | |
| } | |
| } | |
| boolean is_different_date = !now.toLocalDate().equals(config.dateForValidation()); | |
| HtmlReportGenerator htmlGenerator = new HtmlReportGenerator(); | |
| if (config.outputDirectory().isPresent()) { | |
| Path outputDir = config.outputDirectory().get(); | |
| try { | |
| Files.write( | |
| outputDir.resolve(config.validationReportFileName()), | |
| gson.toJson(jsonReport).getBytes(StandardCharsets.UTF_8)); | |
| } catch (Exception ex) { | |
| logger.atSevere().withCause(ex).log("Error creating JSON report"); | |
| } | |
| try { | |
| htmlGenerator.generateReport( | |
| feedMetadata, | |
| noticeContainer, | |
| config, | |
| versionInfo, | |
| outputDir.resolve(config.htmlReportFileName()), | |
| date, | |
| is_different_date); | |
| Files.write( | |
| outputDir.resolve(config.systemErrorsReportFileName()), | |
| gson.toJson(noticeContainer.exportSystemErrors()).getBytes(StandardCharsets.UTF_8)); | |
| } catch (IOException e) { | |
| logger.atSevere().withCause(e).log("Cannot store report files"); | |
| } | |
| } | |
| } | |
| Path outputDir = config.outputDirectory().get(); | |
| if (!Files.exists(outputDir)) { | |
| try { | |
| Files.createDirectories(outputDir); | |
| } catch (IOException ex) { | |
| logger.atSevere().withCause(ex).log("Error creating output directory: %s", outputDir); | |
| } | |
| } | |
| boolean is_different_date = !now.toLocalDate().equals(config.dateForValidation()); | |
| HtmlReportGenerator htmlGenerator = new HtmlReportGenerator(); | |
| try { | |
| Files.write( | |
| outputDir.resolve(config.validationReportFileName()), | |
| gson.toJson(jsonReport).getBytes(StandardCharsets.UTF_8)); | |
| } catch (Exception ex) { | |
| logger.atSevere().withCause(ex).log("Error creating JSON report"); | |
| } | |
| try { | |
| htmlGenerator.generateReport( | |
| feedMetadata, | |
| noticeContainer, | |
| config, | |
| versionInfo, | |
| outputDir.resolve(config.htmlReportFileName()), | |
| date, | |
| is_different_date); | |
| Files.write( | |
| outputDir.resolve(config.systemErrorsReportFileName()), | |
| gson.toJson(noticeContainer.exportSystemErrors()).getBytes(StandardCharsets.UTF_8)); | |
| } catch (IOException e) { | |
| logger.atSevere().withCause(e).log("Cannot store report files"); | |
| } | |
| } |
Hi, here's a PR to add some functionality that is useful for developers chaining this together with other tools on the command line. This is related to at least one issue that has requested some aspects of this in the past. This is my first PR ever for this repo, so please do feel free to revise as needed or to hold if it conflicts with other planned functionality. Thanks.
Summary:
Enables JSON reports to be optionally output directly to stdout for piping to other programs, rather than being written to file system. Log output is suppressed. Involves changing how output directory is handled, but not intended to change existing default functionality in the CLI or the GUI.
closes #1194
Expected behavior:
Please make sure these boxes are checked before submitting your pull request - thanks!
gradle testto make sure you didn't break anythingscreenshot(s)a shell example showing how this pull request works and fixes the issue(s)