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