Magpie/Action/WebStatic.pm

filter: Statementstmt 93.7% 30 / 32 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod n/a totaltotal 95.3% 41 / 43 CC 1 SCAR 0.3
CC
1
Cov
94%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Magpie/Action/Old/Module.pm ↑ Summary Magpie/Constants.pm »
1 #
2 # This file is part of App-Magpie
3 #
4 # This software is copyright (c) 2011 by Jerome Quelin.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 1 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 package App::Magpie::Action::WebStatic;
14 # ABSTRACT: webstatic command implementation
15 $App::Magpie::Action::WebStatic::VERSION = '2.010';
16 1 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use LWP::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use ORDB::CPAN::Mageia;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Parse::CPAN::Packages::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 0 ▶use RRDTool::OO;
Subroutine BEGIN: called CC=1 SCAR=2.6
24 use Readonly;
25 use Template;
26  
27 use App::Magpie::Constants qw{ $SHAREDIR };
28  
29  
30 with 'App::Magpie::Role::Logging';
31  
32 my $datadir = path( File::HomeDir->my_dist_data( "App-Magpie", { create=>1 } ) );
33 my $rrdsdir = $datadir->child( "rrds" );
34  
35 my $rrdvers = $rrdsdir->child( "version" );
36 my %rrdfile = (
37 mga_mods => $rrdsdir->child( "mageia-modules.rrd" ),
38 mga_dists => $rrdsdir->child( "mageia-dists.rrd" ),
39 cpan_mods => $rrdsdir->child( "cpan-modules.rrd" ),
40 cpan_dists => $rrdsdir->child( "cpan-dists.rrd" ),
41 );
42 my %rrd;
43  
44  
45  
46  
47 sub run {
48 my ($self, $opts) = @_;
49 $self->_migrate_and_create_rrds_if_needed;
50  
51 # -- first, update the rrd files
52 $self->log( "** updating rrd files" );
53  
54 my $mgamods = ORDB::CPAN::Mageia::Module->count;
55 $rrd{mga_mods}->update( $mgamods );
56 $self->log_debug( "mageia modules: $mgamods" );
57  
58 my $mgadists = ORDB::CPAN::Mageia->selectcol_arrayref(
59 'SELECT DISTINCT dist FROM module ORDER BY dist'
60 );
61 my $nbmgadists = scalar @$mgadists;
62 $rrd{mga_dists}->update( $nbmgadists );
63 $self->log_debug( "mageia dists: $nbmgadists" );
64  
65 my $modpkg = $datadir->child( "02packages.details.txt.gz" );
66 my $src = "http://cpan.cpantesters.org/modules/02packages.details.txt.gz";
67 mirror( $src, $modpkg->stringify );
68 my $p = Parse::CPAN::Packages::Fast->new($modpkg->stringify);
69 my $cpanmods = $p->package_count;
70 my $cpandists = $p->distribution_count;
71 $rrd{cpan_mods}->update( $cpanmods );
72 $self->log_debug( "cpan modules: $cpanmods" );
73 $rrd{cpan_dists}->update( $cpandists );
74 $self->log_debug( "cpan dists: $cpandists" );
75  
76  
77 # -- create the web site
78 $self->log( "** creating web site" );
79 $opts->{directory} =~ s!/$!!;
80 my $dir = path( $opts->{directory} . ".new" );
81 $dir->remove_tree( { safe => 0 } ); $dir->mkpath;
82  
83 # images
84 $self->log_debug( "images:" );
85 my $imgdir = $dir->child( "images" );
86 $imgdir->mkpath;
87 $self->log_debug( " - mageia modules" );
88 $rrd{mga_mods}->graph(
89 image => $imgdir->child("mgamods.png"),
90 width => 800,
91 title => 'Number of available Perl modules in Mageia Linux',
92 start => DateTime->new(year=>2012)->epoch,
93 draw => {
94 thickness => 2,
95 color => '0000FF',
96 },
97 units_exponent => 0,
98 );
99  
100 # template toolkit
101 $self->log_debug( "template toolkit processing" );
102 my $tt = Template->new({
103 INCLUDE_PATH => $SHAREDIR->child("webstatic"),
104 INTERPOLATE => 1,
105 }) or die "$Template::ERROR\n";
106  
107 my $vars = {
108 mgamods => $mgamods,
109 mgadists => $nbmgadists,
110 date => scalar localtime,
111 };
112 $tt->process('index.tt2', $vars, $dir->child("index.html")->stringify)
113 or die $tt->error(), "\n";
114  
115 # rrd files
116 $self->log_debug( "copying rrd files" );
117 my $rrdsubdir = $dir->child( "rrds" );
118 $rrdsubdir->mkpath;
119 foreach my $f ( keys %rrdfile ) {
120 copy( $rrdfile{$f}->stringify, $rrdsubdir->stringify );
121 }
122  
123 # update website in one pass: remove previous version, replace it by new one
124 $self->log( "** updating web site" );
125 my $olddir = path( $opts->{directory} );
126 $olddir->remove_tree( { safe => 0 } );
127 move( $dir->stringify, $olddir->stringify );
128 }
129  
130  
131 # -- private methods
132  
133 sub _migrate_and_create_rrds_if_needed {
134 my $self = shift;
135 $rrdsdir->mkpath;
136  
137 # v0 - too bad, drop existing files
138 my $rrdfile = $datadir->child( "modules.rrd" );
139 if ( -e $rrdfile ) {
140 $self->log( "converting from v0" );
141 $self->log_debug( "removing $rrdfile" );
142 $rrdfile->remove
143 }
144  
145 # create rrds
146 $self->log("creating rrd files");
147 foreach my $f ( keys %rrdfile ) {
148 $rrd{$f} = RRDTool::OO->new( file=>$rrdfile{$f} );
149 next if -f $rrdfile{$f};
150 $self->log_debug( "creating $rrdfile{$f}" );
151 $rrd{$f}->create(
152 step => 60*60*24, # 1 measure per day
153 data_source => {
154 name => "nb",
155 type => "GAUGE",
156 },
157 archive => { rows => 365 * 100 }, # data kept for 100 years (!)
158 );
159 }
160  
161 # saving rrd schema version
162 $self->log_debug( "saving schema version" );
163 my $fh = $rrdvers->openw;
164 $fh->print( "1" );
165 $fh->close;
166 }
167  
168 1;
169  
170 __END__
« Magpie/Action/Old/Module.pm ↑ Summary Magpie/Constants.pm »