Skip to content

Commit 8f1b963

Browse files
committed
Add graph visualizations to anomaly detection
1 parent ebf9aed commit 8f1b963

File tree

4 files changed

+136
-1
lines changed

4 files changed

+136
-1
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Anomaly Detection Graphs: Find top nodes marked as "hub" including their incoming dependencies and output them in Graphviz format.
2+
3+
MATCH (sourceForStatistics)-[dependencyForStatistics:DEPENDS_ON]->(targetForStatistics)
4+
WHERE $projection_node_label IN labels(sourceForStatistics)
5+
AND $projection_node_label IN labels(targetForStatistics)
6+
WITH min(coalesce(dependencyForStatistics.weight25PercentInterfaces, dependencyForStatistics.weight)) AS minWeight
7+
,max(coalesce(dependencyForStatistics.weight25PercentInterfaces, dependencyForStatistics.weight)) AS maxWeight
8+
MATCH (source)-[dependency:DEPENDS_ON]->(target)
9+
WHERE $projection_node_label IN labels(source)
10+
AND $projection_node_label IN labels(target)
11+
AND target.anomalyScore > 0
12+
AND target.anomalyHubRank = 1
13+
WITH *, coalesce(dependency.weight25PercentInterfaces, dependency.weight) AS weight
14+
WITH *, toFloat(weight - minWeight) / toFloat(maxWeight - minWeight) AS normalizedWeight
15+
WITH *, round((normalizedWeight * 5) + 1, 2) AS penWidth
16+
WITH *, source.name AS fullSourceName
17+
WITH *, target.name + "\\n(hub #" + target.anomalyHubRank + ")" AS fullTargetNode
18+
WITH *, "\" -> \"" + fullTargetNode
19+
+ "\" [label = " + weight + ";"
20+
+ " penwidth = " + penWidth + ";"
21+
+ " ];" AS graphVizDotNotationEdge
22+
WITH *, "\"" + fullSourceName + coalesce(graphVizDotNotationEdge, "\" [];") AS graphVizDotNotationLine
23+
ORDER BY target.anomalyHubRank DESC, target.name ASC
24+
RETURN DISTINCT graphVizDotNotationLine
25+
//Debugging
26+
//,source.name AS sourceName
27+
//,target.name AS targetName
28+
//,penWidth
29+
//,normalizedWeight
30+
//,dependency.weight AS weight
31+
//,minWeight
32+
//,maxWeight
33+
LIMIT 20
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// This is a GraphViz dot template file for the visualization of a anomaly archetype graph.
2+
// The main part of the template is marked by the comments "Begin-Template" and "End-Template".
3+
// It also contains a simple example graph.
4+
//
5+
strict digraph anomaly_archetype_template {
6+
//Begin-Template
7+
fontname = "Helvetica,Arial,sans-serif";
8+
layout = "fdp";
9+
sep = "0.4";
10+
node [fontname = "Helvetica,Arial,sans-serif";];
11+
edge [fontname = "Helvetica,Arial,sans-serif"; fontsize = 10;];
12+
node [style = filled; fillcolor = "0.560 0.400 0.999";];
13+
node [color = "0.560 0.900 0.700";];
14+
edge [color = "0.560 0.900 0.700";];
15+
//End-Template
16+
"A" -> "B" [penwidth = 1.0; label = 1;];
17+
"A" -> "C" [penwidth = 3.0; label = 4;];
18+
"B" -> "V" [penwidth = 2.0; label = 2;];
19+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
mkdir -p "${detail_report_directory}"
53+
54+
local queryResultFile="${detail_report_directory}/TopHubsGraphVisualization.csv"
55+
execute_cypher "${ANOMALY_DETECTION_GRAPHS_DIR}/AnomalyDetectionTopHubsGraph.cypher" "${@}" > "${queryResultFile}"
56+
57+
source "${SCRIPTS_DIR}/cleanupAfterReportGeneration.sh" "${detail_report_directory}" # Remove empty files
58+
59+
if [ -f "${queryResultFile}" ] ; then
60+
source "${VISUALIZATION_SCRIPTS_DIR}/visualizeQueryResults.sh" "${queryResultFile}" --template "${ANOMALY_DETECTION_GRAPHS_DIR}/anomaly-archetype.template.gv"
61+
fi
62+
}
63+
64+
65+
# Create report directory
66+
REPORT_NAME="anomaly-detection"
67+
FULL_REPORT_DIRECTORY="${REPORTS_DIRECTORY}/${REPORT_NAME}"
68+
mkdir -p "${FULL_REPORT_DIRECTORY}"
69+
70+
# Query Parameter key pairs for projection and algorithm side
71+
ALGORITHM_NODE="projection_node_label"
72+
ALGORITHM_LANGUAGE="projection_language"
73+
74+
# -- Detail Reports for each code type -------------------------------
75+
76+
anomaly_detection_graph_visualization "${ALGORITHM_NODE}=Artifact" "${ALGORITHM_LANGUAGE}=Java"
77+
anomaly_detection_graph_visualization "${ALGORITHM_NODE}=Package" "${ALGORITHM_LANGUAGE}=Java"
78+
anomaly_detection_graph_visualization "${ALGORITHM_NODE}=Type" "${ALGORITHM_LANGUAGE}=Java"
79+
anomaly_detection_graph_visualization "${ALGORITHM_NODE}=Module" "${ALGORITHM_LANGUAGE}=Typescript"
80+
81+
# ---------------------------------------------------------------
82+
83+
echo "anomalyDetectionSummary: $(date +'%Y-%m-%dT%H:%M:%S%z') Successfully finished."

scripts/visualization/visualizeQueryResults.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ echo "visualizeQueryResults: VISUALIZATION_SCRIPTS_DIR=${VISUALIZATION_SCRIPTS_D
1717
# Read the first unnamed input argument containing the version of the project
1818
inputCsvFileName=""
1919
case "${1}" in
20-
"--"*) ;; # Skipping named command line options to forward them later to the "analyze" command
20+
"--"*) ;; # Skipping named command line options to forward them later to the "convertQueryResultCsvToGraphVizDotFile" command
2121
*)
2222
inputCsvFileName="${1}"
2323
shift || true

0 commit comments

Comments
 (0)