-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvertQueryResultCsvToGraphVizDotFile.sh
executable file
·119 lines (101 loc) · 4.23 KB
/
convertQueryResultCsvToGraphVizDotFile.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env bash
# Converts a Cypher query result in CSV format to a GraphViz DOT (https://graphviz.org/doc/info/lang.html) file for Visualization including layout templates.
#
# The template file is used to define the layout of the graph.
# It needs to contain the markers //Begin-Template and //End-Template to indicate the actual template content.
# If no template is specified, "templates/default.template.gv" is used.
# Fail on any error ("-e" = exit on first error, "-o pipefail" exist on errors within piped commands)
set -o errexit -o pipefail
## Get this "scripts/visualization" directory if not already set
# Even if $BASH_SOURCE is made for Bourne-like shells it is also supported by others and therefore here the preferred solution.
# CDPATH reduces the scope of the cd command to potentially prevent unintended directory changes.
# This way non-standard tools like readlink aren't needed.
VISUALIZATION_SCRIPTS_DIR=${VISUALIZATION_SCRIPTS_DIR:-$( CDPATH=. cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P )} # Repository directory containing the shell scripts for visualization
echo "convertQueryResultCsvToGraphVizDotFile: VISUALIZATION_SCRIPTS_DIR=${VISUALIZATION_SCRIPTS_DIR}"
# Internal constants
DEFAULT_TEMPLATE_FILE="templates/default.template.gv"
# Function to display script usage
usage() {
echo "Usage: $0 --filename path/to/query/result/file.csv [--name name-of-the-graph (default=name of the file)] [--template path/to/the/template/file (default=${DEFAULT_TEMPLATE_FILE})]"
exit 1
}
# Default values
inputFilename=""
graphName=""
templateFile=""
# Parse command line arguments
while [ $# -gt 0 ]; do
key="$1"
case $key in
--filename)
inputFilename="$2"
shift
;;
--name)
graphName="$2"
shift
;;
--template)
templateFile="$2"
shift
;;
*)
echo "convertQueryResultCsvToGraphVizDotFile: Error: Unknown option: ${key}"
usage
;;
esac
shift
done
if [ -z "${inputFilename}" ]; then
echo "convertQueryResultCsvToGraphVizDotFile: Error: Missing required option: --filename"
echo "${USAGE}"
exit 1
fi
if [ ! -f "${inputFilename}" ]; then
echo "convertQueryResultCsvToGraphVizDotFile: Error: CSV file not found: ${inputFilename}"
echo "${USAGE}"
exit 1
fi
number_of_input_file_lines=$(wc -l < "${inputFilename}" | awk '{print $1}')
if [ "${number_of_input_file_lines}" -le 1 ]; then
echo "convertQueryResultCsvToGraphVizDotFile: Info: Input file is empty. Skipping *.dot and *.svg file generation."
return 0
fi
if [ -z "${graphName}" ]; then
graphName=$(basename -- "${inputFilename}")
graphName="${graphName%.*}"
echo "convertQueryResultCsvToGraphVizDotFile: Info: Using default graph name: ${graphName}"
fi
# Replace all dashes in the graphName by underscores
graphName=${graphName//-/_}
if [ -z "${templateFile}" ]; then
templateFile="${VISUALIZATION_SCRIPTS_DIR}/${DEFAULT_TEMPLATE_FILE}"
echo "convertQueryResultCsvToGraphVizDotFile: Info: Using default template file: ${templateFile}"
fi
if [ ! -f "${templateFile}" ]; then
echo "convertQueryResultCsvToGraphVizDotFile: Error: Template file not found: ${templateFile}"
echo "${USAGE}"
exit 1
fi
templateFileName=$(basename -- "${templateFile}")
inputFilePath=$(dirname "${inputFilename}")
templateFileNameWithoutExtension="${templateFileName%.*}"
outputFilename="${inputFilePath}/${graphName}.gv"
{
# Add a comment to the beginning of the file to indicate that it was generated by this script
echo "// This GraphViz dot file was generated by the script convertQueryResultCsvToGraphVizDotFile.sh with ${templateFileNameWithoutExtension}"
echo ""
# Start the graph definition with the graph name
echo "strict digraph ${graphName} {"
# Extract the template content from the template file and remove the begin and end markers
sed -n '/\/\/Begin-Template/,/\/\/End-Template/{//!p;}' "${templateFile}"
# Remove the first (header) line of the CSV file, remove the enclosing double quotes and replace the escaped double quotes by double quotes
awk -F ',' 'NR>1 {print "\t" $1}' "${inputFilename}" \
| sed 's/^\t\"\"\"/\t"/' \
| sed 's/^\t\"\\\"\"/\t"/' \
| sed 's/\\\"\"/"/g' \
| sed 's/\"\"/"/g' \
| sed 's/\"$//'
# End the graph definition
echo "}"
} > "${outputFilename}"