#! /usr/bin/perl
# Automatically enables "strict", "warnings", "utf8" and
# Perl 5.10 features

use Mojolicious::Lite;
use DBI;

use Munin::Common::Defaults;

my $datafilename = "$Munin::Common::Defaults::MUNIN_DBDIR/datafile.sqlite";

get '/grp' => sub {
	my $self = shift;
	my $id = $self->param('grp_id');
	my $p_id = $self->param('p_id');

	my @params;
	# Actually, the more specific ID *overrides* the other ones, therefore
	# the order is important
	if ($id) {
		@params = ("id = ?", $id);
	} elsif ($p_id) {
		@params = ("p_id = ?", $p_id);
	}

	$self->render(json => get_table_data("grp", @params));
};

get '/nodes' => sub {
	my $self = shift;
	my $id = $self->param('node_id');
	my $grp_id = $self->param('grp_id');

	my @params;
	# Actually, the more specific ID *overrides* the other ones, therefore
	# the order is important
	if ($id) {
		@params = ("id = ?", $id);
	} elsif ($grp_id) {
		@params = ("grp_id = ?", $grp_id);
	}

	$self->render(json => get_table_data("node", @params));
};

get '/services' => sub {
	my $self = shift;
	my $id = $self->param('service_id');
	my $node_id = $self->param('node_id');

	my @params;
	# Actually, the more specific ID *overrides* the other ones, therefore
	# the order is important
	if ($id) {
		@params = ("id = ?", $id);
	} elsif ($node_id) {
		@params = ("node_id = ?", $node_id);
	}

	$self->render(json => get_table_data("service", @params));
};

get '/ds' => sub {
	my $self = shift;
	my $id = $self->param('ds_id');
	my $service_id = $self->param('service_id');

	my @params;
	# Actually, the more specific ID *overrides* the other ones, therefore
	# the order is important
	if ($id) {
		@params = ("id = ?", $id);
	} elsif ($service_id) {
		@params = ("service_id = ?", $service_id);
	}

	$self->render(json => get_table_data("ds", @params));
};

get '/states' => sub {
	my $self = shift;
	my $id = $self->param('state_id');
	my $type = $self->param('state_type');

	my $where = "";
	my @params;
	if ($id) {
		$where .= " AND id = ?";
		push @params, $id;
	}
	if ($type) {
		$where .= " AND type = ?";
		push @params, $type;
	}

	# We cannot use the generic db accessor
	$self->render(json => get_state_data($where, @params));
};

# Start the Mojolicious command system
app->start;

sub get_table_data
{
	my ($table, $where_clause, @params) = @_;
	$where_clause = (! $where_clause) ? "" : "WHERE $where_clause";

	my $dbh = DBI->connect_cached("dbi:SQLite:dbname=$datafilename","","") or die $DBI::errstr;

	my $sth = $dbh->prepare_cached( "SELECT * FROM $table $where_clause");
	$sth->execute(@params);

	my $nodes = $sth->fetchall_arrayref(
		{}
		#	{ Slice => {} }, # Each row is stored as a hashref
	);

	my $sth_attr = $dbh->prepare_cached(
		"SELECT name, value FROM $table"."_attr WHERE id = ?",
	);
	foreach my $node (@$nodes) {
		$sth_attr->execute($node->{id});
		while (my ($_name, $_value) = $sth_attr->fetchrow_array) {
			$node->{attr}{$_name} = $_value;
		}
	}

	return $nodes;
}

# We cannot use the same DB accessor, as the state table is special
sub get_state_data
{
	my ($where_clause, @params) = @_;

	my $dbh = DBI->connect_cached("dbi:SQLite:dbname=$datafilename","","") or die $DBI::errstr;

	my $sth = $dbh->prepare_cached( "SELECT * FROM state WHERE 1 = 1 $where_clause");
	$sth->execute(@params);

	my $nodes = $sth->fetchall_arrayref(
		{}
		#	{ Slice => {} }, # Each row is stored as a hashref
	);

	return $nodes;
}
