Skip to content

Commit

Permalink
Merge branch 'master' of git@github.com:maluco/CMD
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagorondon committed Jul 29, 2011
2 parents 6fc5457 + c7839a7 commit 7292f39
Show file tree
Hide file tree
Showing 21 changed files with 328 additions and 112 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
META.yml
Makefile
inc/
pm_to_blib
blib
*~
.*
!.gitignore
!.shipit
2 changes: 2 additions & 0 deletions .shipit
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
steps = FindVersion, ChangeAllVersions, CheckChangeLog, DistTest, Commit, Tag, MakeDist, UploadCPAN
git.push_to = origin
44 changes: 33 additions & 11 deletions INSTALL
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,44 @@
INSTALL
-------

1. Comente as linhas 35 e 36, do arquivo lib/CMD/Schema/Node.pm.

2. Crie um database para a aplicação. A sugestão, e para fins de documentação vamos chamar de 'cmd'.

3. Execute o comando abaixo para importar a base de 2010 para o banco de dados.
1. Crie um database para a aplicação. A sugestão, e para fins
de documentação vamos chamar de 'cmd'.
$ mysql
$ create database cmd;
$ grant all on cmd.* to usuario@localhost indentified by 'senha';

2 Crie o arquivo de configuracao db_config.json com o conteudo:
$ cd My_app_dir
$ vim db_config.json
{
"db_config" : {
"dsn": "dbi:mysql:db=cmd",
# "dsn":"dbi:mysql:db=cmd", # to install on mysql
# "dsn":"dbi:Pg:dbname=twitter", # to install on postgres
"user": "usuario",
"password": "senha",
"install": "yes",
}
}

OBSERVACOES

1 instale o "DBD::Mysql"( intale antes libmysqlclient-dev ) via cpanm ou "apt-get install libmysqlclient-dev libdbd-mysql-perl"

2. instale as dependencias perl e execute o Makefile
$ cd CMD
$ cpanm inc::Module::Install
$ cpanm Module::Install::Catalyst
$ perl Makefile.PL test
$ make installdeps


3. Execute o comando abaixo para importar a base de 2010 ( ou outras ) para o banco de dados.

$ perl -Ilib raw2db/federal.pl 2010 data/raw/federal/diretas/2010.csv data/raw/federal/transferencia/2010.csv

4. Descomente as linhas 35 e 36, no arquivo lib/CMD/Schema/Node.pm.

6. Levante o servidor web:

$ script/cmd_server.pl

7. Agora, pode acessar o site pelo endereço: http://localhost:3000/




1 change: 1 addition & 0 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ requires 'Catalyst::Runtime' => '5.80032';
requires 'Catalyst::Plugin::ConfigLoader';
requires 'Catalyst::Plugin::Static::Simple';
requires 'Catalyst::Action::RenderView';
requires 'DBIx::Class::Tree::AdjacencyList';
requires 'Moose';
requires 'namespace::autoclean';

Expand Down
11 changes: 11 additions & 0 deletions db_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

{
"db_config":{
"password":"bar",
# "dsn":"dbi:mysql:db=cmd", # to install on mysql
# "dsn":"dbi:Pg:dbname=twitter", # to install on postgres
"dsn":"dbi:SQLite:dbname=cmd", # to install on sqlite
"install":"yes",
"user":"foo"
}
}
9 changes: 9 additions & 0 deletions deploy.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use strict;
use warnings;
use Config::Any;

my $config = Config::Any->load_files( { files => [ 'db_config.json' ], use_ext => 5 } );

dbicadmin -Ilib --schema CMD::Schema \
--connect='[ $config->[0]{ 'db_config.json' }->{ db_config }->{ dsn } , $config->[0]{ 'db_config.json' }->{ db_config }->{ password }, $config->[0]{ 'db_config.json' }->{ db_config }->{ user }]' \
--deploy
60 changes: 60 additions & 0 deletions lib/CMD.pm
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,69 @@ __PACKAGE__->config(
},
);

# Load database config:
__PACKAGE__->config( 'Plugin::ConfigLoader' => { file => 'db_config.json' } );

# Start the application
__PACKAGE__->setup();

sub update_config_status {
my ( $self, ) = @_;
use JSON::XS;
my $json = { };
$json->{ db_config } = __PACKAGE__->config->{ db_config };
$json->{ db_config }->{ install } = 'no'; #UPDATE INSTALL VAL
my $json_text = encode_json( $json );
open( FHOUT , '>', 'db_config.json' );
print FHOUT $json_text;
close FHOUT;
}

sub is_db_configured {
my ( $self, $c ) = @_;
warn "INICIANDO VERIFICACOES DE INSTALACAO DO BANCO DE DADOS";
my $schema = CMD::Schema->connect(
__PACKAGE__->config->{ db_config }->{ dsn },
__PACKAGE__->config->{ db_config }->{ user },
__PACKAGE__->config->{ db_config }->{ password },
);
my $database = eval { $schema->resultset( 'Node' )->search()->first; };
if ( ! $database ) {
if ( defined __PACKAGE__->config->{ db_config }->{ user }
and defined __PACKAGE__->config->{ db_config }->{ password }
and defined __PACKAGE__->config->{ db_config }->{ dsn }
) {
warn "Tentarei criar as tabelas no banco " ;
my $schema = CMD::Schema->connect(
__PACKAGE__->config->{ db_config }->{ dsn },
__PACKAGE__->config->{ db_config }->{ user },
__PACKAGE__->config->{ db_config }->{ password },
);
$schema->deploy;
my $cmd = "perl -Ilib raw2db/federal.pl 2010 data/raw/federal/diretas/2010.csv data/raw/federal/transferencia/2010.csv";
warn "Iniciando instalacao do banco de dados.";
warn `$cmd` . "\n\n";
warn "dados inseridos no banco";
$self->update_config_status();

warn <<HELPADD
Agora:
1. Reinicie o servidor
HELPADD
;
} else {
#nao foi encontrado usuario e senha
my $help = `cat ../INSTALL`;
warn $help;
}
}
}

__PACKAGE__->is_db_configured();

=head1 NAME
CMD - Catalyst based application
Expand Down
21 changes: 8 additions & 13 deletions lib/CMD/Controller/Data.pm
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ sub node2base : Chained('base') Args(1) {
my ( $self, $c, $node ) = @_;
my $sc = $c->model('DB::Node')->find( { node_id => $node } );
my $root = get_root_by_nodeid($sc);
my $obj = $c->model('DB::BaseNode')->search( { node_id => $root->id } )->first;
my $obj =
$c->model('DB::BaseNode')->search( { node_id => $root->id } )->first;
$c->stash->{data} = { base_id => $obj->base_id } if $obj;
$c->forward('View::JSON');
}


sub node : Chained('base') Args(1) {
my ( $self, $c, $id ) = @_;
my $rs = $c->model('DB::Node');
my $tree = $c->stash->{tree} = $rs->search( { parent_id => $id } );
my $rs = $c->model('DB::Node');
my $tree = $c->stash->{tree} = $rs->search( { parent_id => $id } );
my $total = 0;
map {
$total += $_->valor
Expand All @@ -49,17 +49,14 @@ sub node : Chained('base') Args(1) {
sub handle_TREE : Private {
my ( $self, $c ) = @_;

my $tree = $c->stash->{tree};
my $tt = $c->stash->{tt};
my $total_collection = $c->stash->{total_collection};
my $tree = $c->stash->{tree};

# I don't want this in JSON output.
delete $c->stash->{collection};
delete $c->stash->{tree};
delete $c->stash->{tt};
delete $c->stash->{total_collection};

my @levels;
my @children;
my @zones;
my @bgcolor = bgcolor;
Expand Down Expand Up @@ -91,11 +88,10 @@ sub handle_TREE : Private {
my $color = shift(@bgcolor) || $bgcolor_default;
my $valor_print = formata_valor( $item->valor );
my $porcentagem = formata_float( $valor_porcentagem, 3 );
my $zone = $item->children->count ? '/node' : '/programa';
my $link = join( '/', $zone, $item->id );
my $zone = $item->children->count ? '/node' : '/programa';
my $link = join( '/', $zone, $item->id );

#push( @levels, $item->level ) unless grep ( $item->level, @levels );
# Fix content with 'repasse' in db. Fix DB ?
# Fix content with 'repasse' in db. Fix DB ?
my $title =
$item->content eq 'repasse'
? 'Repasse para estados e mun&iacute;cipios'
Expand Down Expand Up @@ -138,7 +134,6 @@ sub handle_TREE : Private {
warn $c->stash->{zones};
$c->stash->{children} = [@children];

#$c->stash->{levels} = [@levels];
$c->forward('View::JSON');
}

Expand Down
Loading

0 comments on commit 7292f39

Please sign in to comment.