1+ #! /usr/bin/env bash
2+
3+ # Creates a Markdown report that contains all results of all the anomaly detection methods.
4+ # It requires an already running Neo4j graph database with already scanned and analyzed artifacts.
5+ # The results will be written into the sub directory reports/anomaly-detection.
6+
7+ # Note that "scripts/prepareAnalysis.sh" is required to run prior to this script.
8+ # Note that either "anomalyDetectionCsv.sh" or "anomalyDetectionPython.sh" is required to run prior to this script.
9+
10+ # Requires executeQueryFunctions.sh, projectionFunctions.sh, cleanupAfterReportGeneration.sh
11+
12+ # Fail on any error ("-e" = exit on first error, "-o pipefail" exist on errors within piped commands)
13+ set -o errexit -o pipefail
14+
15+ # Overrideable Constants (defaults also defined in sub scripts)
16+ REPORTS_DIRECTORY=${REPORTS_DIRECTORY:- " reports" }
17+ MARKDOWN_INCLUDES_DIRECTORY=${MARKDOWN_INCLUDES_DIRECTORY:- " includes" }
18+
19+ # # Get this "domains/anomaly-detection/summary" directory if not already set
20+ # Even if $BASH_SOURCE is made for Bourne-like shells it is also supported by others and therefore here the preferred solution.
21+ # CDPATH reduces the scope of the cd command to potentially prevent unintended directory changes.
22+ # This way non-standard tools like readlink aren't needed.
23+ ANOMALY_DETECTION_SUMMARY_DIR=${ANOMALY_DETECTION_SUMMARY_DIR:- $(CDPATH=. cd -- " $( dirname -- " ${BASH_SOURCE[0]} " ) " && pwd -P)}
24+ echo " anomalyDetectionSummary: ANOMALY_DETECTION_SUMMARY_DIR=${ANOMALY_DETECTION_SUMMARY_DIR} "
25+ # Get the "scripts" directory by taking the path of this script and going one directory up.
26+ SCRIPTS_DIR=${SCRIPTS_DIR:- " ${ANOMALY_DETECTION_SUMMARY_DIR} /../../../scripts" } # Repository directory containing the shell scripts
27+
28+ # Define functions to execute a cypher query from within a given file (first and only argument) like "execute_cypher" and "execute_cypher_summarized"
29+ source " ${SCRIPTS_DIR} /executeQueryFunctions.sh"
30+
31+ # Appends a Markdown table to an existing file and
32+ # removes redundant header + separator rows.
33+ #
34+ # Usage:
35+ # cat newTable.md | append_table myMarkdownFile.md
36+ #
37+ # append_table myMarkdownFile.md <<'EOF'
38+ # | Name | Score | Archetype |
39+ # | --- | --- | --- |
40+ # | Bar | 0.9 | Something |
41+ # EOF
42+ #
43+ # Behavior:
44+ # - Keeps the first header row and its following separator row.
45+ # - Removes all subsequent duplicate header + separator pairs.
46+ # - Leaves all data rows untouched.
47+ append_to_markdown_table () {
48+ local file=" $1 "
49+
50+ # Append stdin to the target file
51+ cat >> " ${file} "
52+
53+ # Clean up duplicate headers (header row + --- row)
54+ awk ' !seen[$0]++ || NR <= 2' " ${file} " > " ${file} .tmp" && mv " ${file} .tmp" " ${file} "
55+ }
56+
57+ # Aggregates all results in a Markdown report.
58+ #
59+ # Required Parameters:
60+ # - projection_node_label=...
61+ # Label of the nodes that will be used for the projection. Example: "Package"
62+ # - projection_language=...
63+ # Name of the associated programming language. Examples: "Java", "Typescript"
64+ anomaly_detection_summary_detail_report () {
65+ local nodeLabel
66+ nodeLabel=$( extractQueryParameter " projection_node_label" " ${@ } " )
67+
68+ local language
69+ language=$( extractQueryParameter " projection_language" " ${@ } " )
70+
71+ echo " anomalyDetectionSummary: $( date +' %Y-%m-%dT%H:%M:%S%z' ) Creating ${language} ${nodeLabel} anomaly summary Markdown report..."
72+
73+ anomaly_summary_directory=${FULL_REPORT_DIRECTORY} /anomaly_summary_${language} _${nodeLabel}
74+ mkdir -p " ${anomaly_summary_directory} "
75+ execute_cypher " ${ANOMALY_DETECTION_SUMMARY_DIR} /AnomalyDetectionReportTopArchetypes.cypher" " ${@ } " --output-markdown-table > " ${anomaly_summary_directory} /TopAnomaliesByArchetype.md"
76+ # Clean-up after report generation. Empty reports will be deleted.
77+ source " ${SCRIPTS_DIR} /cleanupAfterReportGeneration.sh" " ${anomaly_summary_directory} "
78+ }
79+
80+ # Run the anomaly detection overview report generation.
81+ anomaly_detection_overview_report () {
82+ local report_markdown_includes_directory=" ${FULL_REPORT_DIRECTORY} /${MARKDOWN_INCLUDES_DIRECTORY} "
83+ mkdir -p " ${report_markdown_includes_directory} "
84+
85+ execute_cypher " ${ANOMALY_DETECTION_SUMMARY_DIR} /AnomaliesPerAbstractionLayer.cypher" --output-markdown-table > " ${report_markdown_includes_directory} /AnomaliesPerAbstractionLayer.md"
86+ execute_cypher " ${ANOMALY_DETECTION_SUMMARY_DIR} /AnomaliesInTotal.cypher" --output-markdown-table > " ${report_markdown_includes_directory} /AnomaliesInTotal.md"
87+ }
88+
89+ # Run the anomaly detection report generation.
90+ #
91+ # Required Parameters:
92+ # - projection_node_label=...
93+ # Label of the nodes that will be used for the projection. Example: "Package"
94+ # - projection_language=...
95+ # Name of the associated programming language. Examples: "Java", "Typescript"
96+ anomaly_detection_report () {
97+ time anomaly_detection_summary_detail_report " ${@ } "
98+ }
99+
100+ # Create report directory
101+ REPORT_NAME=" anomaly-detection"
102+ FULL_REPORT_DIRECTORY=" ${REPORTS_DIRECTORY} /${REPORT_NAME} "
103+ mkdir -p " ${FULL_REPORT_DIRECTORY} "
104+
105+ # Query Parameter key pairs for projection and algorithm side
106+ ALGORITHM_NODE=" projection_node_label"
107+ ALGORITHM_LANGUAGE=" projection_language"
108+
109+ # -- Overview Report for all code type -------------------------------
110+
111+ anomaly_detection_overview_report
112+
113+ # -- Detail Reports for each code type -------------------------------
114+
115+ anomaly_detection_report " ${ALGORITHM_NODE} =Artifact" " ${ALGORITHM_LANGUAGE} =Java"
116+ anomaly_detection_report " ${ALGORITHM_NODE} =Package" " ${ALGORITHM_LANGUAGE} =Java"
117+ anomaly_detection_report " ${ALGORITHM_NODE} =Type" " ${ALGORITHM_LANGUAGE} =Java"
118+ anomaly_detection_report " ${ALGORITHM_NODE} =Module" " ${ALGORITHM_LANGUAGE} =Typescript"
119+
120+ # ---------------------------------------------------------------
121+
122+ echo " anomalyDetectionSummary: $( date +' %Y-%m-%dT%H:%M:%S%z' ) Successfully finished."
0 commit comments