1+ #! /usr/bin/env bash
2+
3+ # Executes selected anomaly detection Cypher queries for GraphViz visualization.
4+ # Visualizes top ranked anomaly archetypes.
5+ # Requires an already running Neo4j graph database with already scanned and analyzed artifacts.
6+ # The reports (csv, dot and svg files) will be written into the sub directory reports/anomaly-detection/{language}_{codeUnit}.
7+
8+ # Requires executeQueryFunctions.sh, visualizeQueryResults.sh, cleanupAfterReportGeneration.sh
9+
10+ # Fail on any error ("-e" = exit on first error, "-o pipefail" exist on errors within piped commands)
11+ set -o errexit -o pipefail
12+
13+ # Overrideable Constants (defaults also defined in sub scripts)
14+ REPORTS_DIRECTORY=${REPORTS_DIRECTORY:- " reports" }
15+
16+ # # Get this "scripts/reports" directory if not already set
17+ # Even if $BASH_SOURCE is made for Bourne-like shells it is also supported by others and therefore here the preferred solution.
18+ # CDPATH reduces the scope of the cd command to potentially prevent unintended directory changes.
19+ # This way non-standard tools like readlink aren't needed.
20+ ANOMALY_DETECTION_GRAPHS_DIR=${REPORTS_SCRIPT_DIR:- $( CDPATH=. cd -- " $( dirname -- " ${BASH_SOURCE[0]} " ) " && pwd -P )}
21+ # echo "anomalyDetectionGraphVisualization: ANOMALY_DETECTION_GRAPHS_DIR=${ANOMALY_DETECTION_GRAPHS_DIR}"
22+
23+ # Get the "scripts" directory by taking the path of this script and going one directory up.
24+ SCRIPTS_DIR=${SCRIPTS_DIR:- " ${ANOMALY_DETECTION_GRAPHS_DIR} /../../../scripts" } # Repository directory containing the shell scripts
25+ # echo "anomalyDetectionGraphVisualization: SCRIPTS_DIR=${SCRIPTS_DIR}"
26+
27+ # Get the "scripts/visualization" directory.
28+ VISUALIZATION_SCRIPTS_DIR=${VISUALIZATION_SCRIPTS_DIR:- " ${SCRIPTS_DIR} /visualization" } # Repository directory containing the shell scripts for visualization
29+ # echo "anomalyDetectionGraphVisualization: VISUALIZATION_SCRIPTS_DIR=${VISUALIZATION_SCRIPTS_DIR}"
30+
31+ # Define functions to execute cypher queries from within a given file
32+ source " ${SCRIPTS_DIR} /executeQueryFunctions.sh"
33+
34+ # Runs a parametrized query, converts their results in GraphViz format and creates a Graph visualization.
35+ # Outputs (at most) 10 indexed files (for report_name="TopHub" then TopHub1, TopHub2,...) with a focused visualization of one selected node and its surroundings.
36+ #
37+ # Required Parameters:
38+ # - report_name=...
39+ # Name of the query and then also the resulting visualization file.
40+ # - template_name=...
41+ # Name of the GraphViz template gv file.
42+ # - projection_language=...
43+ # Name of the associated programming language. Examples: "Java", "Typescript"
44+ # - projection_node_label=...
45+ # Label of the nodes that will be used for the projection. Example: "Package"
46+ create_graph_visualization () {
47+ local nodeLabel
48+ nodeLabel=$( extractQueryParameter " projection_node_label" " ${@ } " )
49+
50+ local language
51+ language=$( extractQueryParameter " projection_language" " ${@ } " )
52+
53+ local report_name
54+ report_name=$( extractQueryParameter " report_name" " ${@ } " )
55+
56+ local template_name
57+ template_name=$( extractQueryParameter " template_name" " ${@ } " )
58+
59+ echo " anomalyDetectionGraphVisualization: $( date +' %Y-%m-%dT%H:%M:%S%z' ) Creating ${language} ${nodeLabel} ${report_name} visualizations with template ${template_name} ..."
60+
61+ local detail_report_directory_name=" ${language} _${nodeLabel} "
62+ local detail_report_directory=" ${FULL_REPORT_DIRECTORY} /${detail_report_directory_name} /GraphVisualizations"
63+ mkdir -p " ${detail_report_directory} "
64+
65+ for index in {1..5}; do
66+ # Query Graph data
67+ local resultFileName=" ${detail_report_directory} /${report_name}${index} "
68+ local queryResultFile=" ${resultFileName} .csv"
69+ execute_cypher " ${ANOMALY_DETECTION_GRAPHS_DIR} /${report_name} .cypher" " ${@ } " " projection_node_rank=${index} " > " ${queryResultFile} " || true
70+
71+ # Remove empty files
72+ # Note: Afterwards, detail_report_directory might be deleted as well.
73+ # In that case the image generation is finished and the loop needs to be terminated.
74+ source " ${SCRIPTS_DIR} /cleanupAfterReportGeneration.sh" " ${detail_report_directory} "
75+ # Stop generation as soon as the first query result is empty or the directory is deleted.
76+ if [ ! -f " ${queryResultFile} " ] ; then
77+ break ;
78+ fi
79+
80+ # Generate svg image using GraphViz
81+ source " ${VISUALIZATION_SCRIPTS_DIR} /visualizeQueryResults.sh" " ${queryResultFile} " --template " ${ANOMALY_DETECTION_GRAPHS_DIR} /${template_name} .template.gv"
82+
83+ # Clean up after graph visualization image generation:
84+ rm -rf " ${queryResultFile} " # Remove query result
85+ # Collect graphviz files in a "graphviz" sub directory
86+ mkdir -p " ${detail_report_directory} /graphviz"
87+ mv -f " ${resultFileName} .gv" " ${detail_report_directory} /graphviz"
88+
89+ # Create visualization reference Markdown file to be embeddable in main Markdown report
90+ if [ " ${index} " == " 1" ]; then
91+ {
92+ echo " "
93+ echo " ##### ${language} ${nodeLabel} - ${report_name} Graph Visualizations"
94+ echo " "
95+ } > " ${detail_report_directory} /VisualizationsReference.md"
96+ fi
97+ echo " " >> " ${detail_report_directory} /VisualizationsReference.md"
98+ done
99+ }
100+
101+ # Run queries, outputs their results in GraphViz format and create Graph visualizations.
102+ #
103+ # Required Parameters:
104+ # - projection_language=...
105+ # Name of the associated programming language. Examples: "Java", "Typescript"
106+ # - projection_node_label=...
107+ # Label of the nodes that will be used for the projection. Example: "Package"
108+ anomaly_detection_graph_visualization () {
109+ create_graph_visualization " report_name=TopHub" " template_name=TopCentral" " ${@ } "
110+ create_graph_visualization " report_name=TopBottleneck" " template_name=TopCentral" " ${@ } "
111+ }
112+
113+
114+ # Create report directory
115+ REPORT_NAME=" anomaly-detection"
116+ FULL_REPORT_DIRECTORY=" ${REPORTS_DIRECTORY} /${REPORT_NAME} "
117+ mkdir -p " ${FULL_REPORT_DIRECTORY} "
118+
119+ # Query Parameter key pairs for projection and algorithm side
120+ QUERY_NODE=" projection_node_label"
121+ QUERY_LANGUAGE=" projection_language"
122+
123+ # -- Detail Reports for each code type -------------------------------
124+
125+ anomaly_detection_graph_visualization " ${QUERY_NODE} =Artifact" " ${QUERY_LANGUAGE} =Java"
126+ anomaly_detection_graph_visualization " ${QUERY_NODE} =Package" " ${QUERY_LANGUAGE} =Java"
127+ anomaly_detection_graph_visualization " ${QUERY_NODE} =Type" " ${QUERY_LANGUAGE} =Java"
128+ anomaly_detection_graph_visualization " ${QUERY_NODE} =Module" " ${QUERY_LANGUAGE} =Typescript"
129+
130+ # ---------------------------------------------------------------
131+
132+ echo " anomalyDetectionGraphVisualization: $( date +' %Y-%m-%dT%H:%M:%S%z' ) Successfully finished."
0 commit comments