public
Created

BubbleCharts of DB Tables

  • Download Gist
bubble.pl
Perl
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
#!perl
 
# Display a bubble chart of DB tables, with rows and relationships to other tables
# Either run directly: perl bubble.pl
# Or with Plack: plackup bubble.pl
 
use strict; use warnings;
 
# cpanm Dancer Template DBIx::Class::Schema::Loader Data::Google::Visualization::DataTable
use Dancer;
use Template;
use DBIx::Class::Schema::Loader;
use Data::Google::Visualization::DataTable;
 
# Enter your DB connection details. You will need the corresponding DBD::* module installed
# for the DB. `mysql`, `Pg`, `Oracle` should all work. (eg: cpanm DBD::Pg).
my $db_driver = 'Pg';
my $db_name = 'mydatabase';
my $db_user = 'username';
my $db_pass = 'password';
 
# Disable irritating DBIx::Class::Schema::Loader warning.
$ENV{SCHEMA_LOADER_BACKCOMPAT}='current';
 
# Connect to and investigate the database
my $schema = DBIx::Class::Schema::Loader->connect(
sprintf('dbi:%s:dbname="%s"', $db_driver, $db_name),
$db_user, $db_pass
);
 
# We'll store our output in a Google DataTable
my $datatable = Data::Google::Visualization::DataTable->new();
$datatable->add_columns(
{ id => 'table', label => "Database Table Name", type => 'string' },
{ id => 'rowcount', label => "Row Count", type => 'number' },
{ id => 'relationships', label => "Relationships", type => 'number' },
);
 
# Record the table data
for my $source ( map { $schema->source($_) } $schema->sources ) {
$datatable->add_rows({
table => $source->from,
rowcount => $source->resultset->count,
relationships => (scalar $source->relationships ),
});
}
 
# Render HTML
my $html;
my $template = join '', (<DATA>);
Template->new->process(\$template, { datatable => $datatable }, \$html);
 
get '/' => sub { return $html };
dance;
 
__DATA__
<html><head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable([% datatable.output_json %]);
var options = {
hAxis: {title: 'Rows'},
vAxis: {title: 'Relationships'},
bubble: {textStyle: {fontSize: 11}}
};
var chart = new google.visualization.BubbleChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
<body>
<div id="chart_div" style="width: 900px; height: 500px;"></div>
</body>
</html>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.