CSV to GRAPH

De Wiki Clusterlab.com.br
Revisão de 13h23min de 10 de setembro de 2019 por Damato (discussão | contribs)
(dif) ← Edição anterior | Revisão atual (dif) | Versão posterior → (dif)
Ir para navegação Ir para pesquisar
#!/bin/bash 
function TEMPFILE() {
	case $1 in
	criar)
		mktemp -p /tmp --suffix azure
		;;
	apagar)
		rm  -f $2
		;;
	*)
		EXITNOW "could not create temporary file"
		;;
	esac
}
function DRAWDIGRAPH() {
    case $1 in
        open)
            echo "digraph G {"
            echo "ratio = \"auto\" ;"
            echo "mincross = 2.0 ;"
            echo "label = \"Graphic\" ;"
            echo "overlap=false;"
            echo "splines=true;"
            # echo "rankdir = LR;"
            ;;
        close)
            echo "}"
            ;;
        *)
            exit 1
            ;;
    esac
}
function DRAWCLUSTER() {
    case $1 in
        open)
            echo "subgraph cluster_$3 {"
            echo "label = \"$2\";"
            echo "node [style=filled];"
            echo "color=blue"
            
            ;;
        close)
            echo "}"
            ;;
        *)
            exit 1
            ;;
    esac
}
function DRAWITEM() {
    if [ "$2" == "nothing" ]
    then
        echo $1
    else
        echo "$1 -> $2 [$4,label=\"$3\"""]"
        # echo "$1 -> $2 [dir=none, label=\"$3\"""]"
    fi
}
function DRAW() {
    DRAWDIGRAPH open
    export DATASOURCE=$1
    export CLUSTERCOUNT=0

    #Generates the clusters and create entities
    while IFS=';' read CLUSTER ORIGIN DESCRIPTION DESTARRAY ATTRIBUTE
    do
        echo $CLUSTER
    done < $DATASOURCE | sort -u | \
    while read CLUSTERS
    do
        export CLUSTERCOUNT=$(expr $CLUSTERCOUNT + 1)
        DRAWCLUSTER open $CLUSTERS $CLUSTERCOUNT
        
        while IFS=';' read CLUSTER ORIGIN DESCRIPTION DESTARRAY ATTRIBUTE
        do
            ARRAYLENGH=$(echo $DESTARRAY | tr ',' '\n' | wc -l)
            if [ $ARRAYLENGH -eq 1 ]
            then
                if [ "$DESTARRAY" == "" ]
                then
                    ARRAYLENGH=0
                fi
            fi
            if [ "$CLUSTERS" == "$CLUSTER" ]
            then
                DRAWITEM $ORIGIN nothing
                # seq 1 $(tput cols) | while read COLS; do echo -en "\ "; done; echo -en "\r" >&2
                echo -ne "\r$ORIGIN                                                            " >&2

            fi
        done < $DATASOURCE | sort -u 
        DRAWCLUSTER close $CLUSTERS
    done
    #Populate graph with directions
    while IFS=';' read CLUSTER ORIGIN DESCRIPTION DESTARRAY ATTRIBUTE
    do
        echo $CLUSTER
    done < $DATASOURCE | sort -u | \
    while read CLUSTERS
    do
        export CLUSTERCOUNT=$(expr $CLUSTERCOUNT + 1)
        
        while IFS=';' read CLUSTER ORIGIN DESCRIPTION DESTARRAY ATTRIBUTE
        do
            ARRAYLENGH=$(echo $DESTARRAY | tr ',' '\n' | wc -l)
            if [ $ARRAYLENGH -eq 1 ]
            then
                if [ "$DESTARRAY" == "" ]
                then
                    ARRAYLENGH=0
                fi
            fi
            if [ "$CLUSTERS" == "$CLUSTER" ]
            then
                if [ $ARRAYLENGH -eq 0 ]
                then
                    echo ""
                else
                    echo $DESTARRAY | tr ',' '\n' | \
                    while read ARRAYITEM
                    do
                        DRAWITEM $ORIGIN $ARRAYITEM "$DESCRIPTION" "$ATTRIBUTE"
                        # seq 1 $(tput cols) | while read COLS; do echo -en " "; done; echo -en "\r"  >&2
                        echo -en "\r$ORIGIN $ARRAYITEM $DESCRIPTION                                                  " >&2
                    done
                fi
            fi
        done < $DATASOURCE
    done
    DRAWDIGRAPH close

}
if [ $# -eq 2 ]
then
    export DATAIN=$1
    export DATAOUT=$2
    export DATAFORMATED=$(TEMPFILE criar)
    echo Converting dots to underscore
    cat $DATAIN | grep -v ^\#| tr '.' '_' > $DATAFORMATED
    echo Converting DOS to UNIX
    dos2unix $DATAFORMATED
    echo Start Drawing
    DRAW $DATAFORMATED |grep -v "^$"| tee $DATAOUT.dot | cat -n
    echo Generating image
    echo -en "\rDOT"
    dot -Gsize=9,15\! -Gdpi=1500 -Tpng $DATAOUT.dot -odot_$DATAOUT
    echo -en "\rNEATO"
    neato -Gsize=9,15\! -Gdpi=1500 -Tpng $DATAOUT.dot -oneato_$DATAOUT
    echo -en "\rCIRCO"
    circo -Gsize=9,15\! -Gdpi=1500 -Tpng $DATAOUT.dot -oncirco_$DATAOUT
    echo -en "\r\n"
else   
    echo Error
    exit 1
fi