showgraph.in 4.02 KB
Newer Older
1
#!/usr/bin/perl -w
Mac Newbold's avatar
Mac Newbold committed
2

Leigh Stoller's avatar
Leigh Stoller committed
3 4 5 6 7 8
#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2002 University of Utah and the Flux Group.
# All rights reserved.
#

9
if ($#ARGV >= 1 || $#ARGV < 0) {
Mac Newbold's avatar
Mac Newbold committed
10
    #Help mode
11
    print "Usage: schemagraph [output]\n";
Mac Newbold's avatar
Mac Newbold committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25
    print "\n";
    print "This shows a graph of the relations in the Testbed Database.\n";
    print "Fields or columns are shown as ellipses, and entities or tables\n";
    print "are shown as boxes. Direction of arrows is insignificant.\n";
    print "\n";
    print "Fields that connect to only one table are only found in that\n";
    print "table. Fields that connect to two or more tables can be used\n";
    print "to use information from both tables. By making a chain from one\n";
    print "field to a field in another table, you can relate the two\n";
    print "fields together.\n";
    print "\n";
    print "This graph is generated from the current table and column setup\n";
    print "in the actual database, and is always current.\n";
    print "\n";
26
    print "If you get an 'Unable to open display' message, give a display \n";
27 28
    print "parameter (like 'myhost:0') on the command line, and it will \n";
    print "attempt to open it.\n";
Mac Newbold's avatar
Mac Newbold committed
29 30 31 32 33 34
    die("\n");
} else  {
    use Mysql;
    require "ctime.pl";
}

35 36
my $display = shift;

Mac Newbold's avatar
Mac Newbold committed
37 38
my $v = 0; # Verbose

39
my $dbh = Mysql->connect("localhost","@TBDBNAME@","script","none");
Mac Newbold's avatar
Mac Newbold committed
40 41 42 43 44 45 46 47

my @tbls = $dbh->listtables;

my $date = &ctime(time);

$date =~ s/[ \t]+/ /;
$date =~ s/\n//;

48 49
my $filename="/tmp/showgraph$$";
my $filename2="/tmp/showgraphneato$$";
Mac Newbold's avatar
Mac Newbold committed
50
open(TMP,">$filename");
51
open(TMPN,">$filename2");
Mac Newbold's avatar
Mac Newbold committed
52 53 54

print TMP "graph: {\n  orientation: left_to_right\n".
    "  title: \"Testbed Database Relational Graph - ".$date."\"\n";
55
print TMP "  height: 1000\n  width: 500\n";
Mac Newbold's avatar
Mac Newbold committed
56
print TMP "  priority_phase: yes\n  straight_phase: yes\n";
57
print TMP "  arrowmode: fixed\n";
Mac Newbold's avatar
Mac Newbold committed
58

59 60 61 62 63 64 65
# Adjust the scale here...
print TMP "  display_edge_labels: yes\n  scaling: 0.37\n";

# Use one of the following to control layout... whichever works better...
print TMP "  crossingweight: medianbary\n  splines: yes\n";
#print TMP "  crossingweight: barymedian\n  splines: yes\n";

66 67
print TMPN "graph G {\n";

Mac Newbold's avatar
Mac Newbold committed
68 69 70 71 72 73 74 75 76
my $sth ="";
my $cmd ="";
my @data=();
my $tbl ="";
my $dest="";
my $col ="";
my @tables=();
my %tablecols=();
my @tablelist =();
77
my %forkeys=();
Mac Newbold's avatar
Mac Newbold committed
78 79 80 81 82 83 84 85 86 87

foreach $tbl ( @tbls ) {
    push(@tablelist,$tbl);
    print "Found $tbl:\n" if $v;
    if ($sth = $dbh->query("describe $tbl") ) {
	while ( @data = $sth->fetchrow()) {
	    $col = $data[0];
	    $col =~ s/[0-9\-]+$//;
	    if (!defined($tablecols{$col})) {
		$tablecols{$col}=$tbl;
88 89
		#print TMP "  node: { title: \"$col\" shape: ellipse }\n";
		#print TMPN "{node [label=\"$col\", color=skyblue, shape=ellipse] $col}\n";
Mac Newbold's avatar
Mac Newbold committed
90 91 92 93
		print "  Found $col\n" if $v;
	    } else {
		if (!($tablecols{$col} =~ /^$tbl\b|\.$tbl\.|\.$tbl$/) ) {
		    $tablecols{$col}.=".".$tbl;
94 95 96 97 98
		    if (!defined($forkeys{$col})) {
			print TMP "  node: { title: \"$col\" shape: ellipse }\n";
			print TMPN "{node [label=\"$col\", color=skyblue, shape=ellipse] $col}\n";
		    }
		    $forkeys{$col}=1;
Mac Newbold's avatar
Mac Newbold committed
99 100 101 102 103 104 105
		    print "  Linked $col\n" if $v;
		} #else { print STDERR "It was in there"; }
	    }
	}
    }
}

106
foreach $tbl ( sort @tablelist) {
107 108
    print TMP "  node: { title: \"table_$tbl\" }\n";
    print TMPN "{node [label=\"table_$tbl\", color=green, shape=box] table_$tbl}\n";
Mac Newbold's avatar
Mac Newbold committed
109 110 111
}

foreach $col ( sort keys %tablecols) {
112 113
    @tables= sort split('\.',$tablecols{$col});
    foreach $tbl (sort @tables) {
114 115 116 117 118 119 120 121 122 123 124
	if (defined($forkeys{$col}) && $forkeys{$col}==1) {
	    print TMPN "$col -- table_$tbl [label=\"\"];\n";
	
	    if (@tables < 2 || 
		$tbl=~/^delta_|proj_|dela|rel|nodes|log|exper|res|part|int/i ) {
		print TMP "  edge: { sourcename: \"$col\" ".
		  "targetname: \"table_$tbl\" }\n";
	    } else {
		print TMP "  edge: { sourcename: \"table_$tbl\" ".
		  "targetname: \"$col\" }\n";
	    }
Mac Newbold's avatar
Mac Newbold committed
125 126 127 128 129 130
	}
    }
}

print TMP "}\n";
close TMP;
131 132
print TMPN "}\n";
close TMPN;
Mac Newbold's avatar
Mac Newbold committed
133

134
if ($display ne "") {
135
  system("/usr/X11R6/bin/xvcg -silent -display $display $filename");
136
} else {
137
  system("/usr/X11R6/bin/xvcg -silent $filename");
138
}
139
#exec "/bin/rm $filename";