Skip to content

Instantly share code, notes, and snippets.

@pjlsergeant
Created November 27, 2012 09:23
Show Gist options
  • Save pjlsergeant/4153299 to your computer and use it in GitHub Desktop.
Save pjlsergeant/4153299 to your computer and use it in GitHub Desktop.
BubbleCharts of DB Tables
#!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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment