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+ # Run queries, outputs their results in GraphViz format and create Graph visualizations.
35+ #
36+ # Required Parameters:
37+ # - projection_node_label=...
38+ # Label of the nodes that will be used for the projection. Example: "Package"
39+ # - projection_language=...
40+ # Name of the associated programming language. Examples: "Java", "Typescript"
41+ anomaly_detection_graph_visualization () {
42+ local nodeLabel
43+ nodeLabel=$( extractQueryParameter " projection_node_label" " ${@ } " )
44+
45+ local language
46+ language=$( extractQueryParameter " projection_language" " ${@ } " )
47+
48+ echo " anomalyDetectionSummary: $( date +' %Y-%m-%dT%H:%M:%S%z' ) Creating ${language} ${nodeLabel} anomaly summary Markdown report..."
49+
50+ local detail_report_directory_name=" ${language} _${nodeLabel} "
51+ local detail_report_directory=" ${FULL_REPORT_DIRECTORY} /${detail_report_directory_name} "
52+
53+ reportName=" ${detail_report_directory} /TopHubsGraphVisualization"
54+ execute_cypher " ${ANOMALY_DETECTION_GRAPHS_DIR} /AnomalyDetectionTopHubsGraph.cypher" " ${@ } " > " ${reportName} .csv"
55+ source " ${VISUALIZATION_SCRIPTS_DIR} /visualizeQueryResults.sh" " ${reportName} .csv"
56+
57+ # Remove empty Markdown includes
58+ source " ${SCRIPTS_DIR} /cleanupAfterReportGeneration.sh" " ${detail_report_directory} "
59+ }
60+
61+
62+ # Create report directory
63+ REPORT_NAME=" anomaly-detection"
64+ FULL_REPORT_DIRECTORY=" ${REPORTS_DIRECTORY} /${REPORT_NAME} "
65+ mkdir -p " ${FULL_REPORT_DIRECTORY} "
66+
67+ # Query Parameter key pairs for projection and algorithm side
68+ ALGORITHM_NODE=" projection_node_label"
69+ ALGORITHM_LANGUAGE=" projection_language"
70+
71+ # -- Detail Reports for each code type -------------------------------
72+
73+ anomaly_detection_graph_visualization " ${ALGORITHM_NODE} =Artifact" " ${ALGORITHM_LANGUAGE} =Java"
74+ anomaly_detection_graph_visualization " ${ALGORITHM_NODE} =Package" " ${ALGORITHM_LANGUAGE} =Java"
75+ anomaly_detection_graph_visualization " ${ALGORITHM_NODE} =Type" " ${ALGORITHM_LANGUAGE} =Java"
76+ anomaly_detection_graph_visualization " ${ALGORITHM_NODE} =Module" " ${ALGORITHM_LANGUAGE} =Typescript"
77+
78+ # ---------------------------------------------------------------
79+
80+ echo " anomalyDetectionSummary: $( date +' %Y-%m-%dT%H:%M:%S%z' ) Successfully finished."
0 commit comments