2008-10-28 10:19:31 +00:00
package HydraFrontend::Controller::Root ;
use strict ;
use warnings ;
use parent 'Catalyst::Controller' ;
#
# Sets the actions in this controller to be registered with no prefix
# so they function identically to actions created in MyApp.pm
#
__PACKAGE__ - > config - > { namespace } = '' ;
sub error {
my ( $ c , $ msg ) = @ _ ;
$ c - > stash - > { template } = 'error.tt' ;
$ c - > stash - > { error } = $ msg ;
$ c - > response - > status ( 404 ) ;
}
sub getBuild {
my ( $ c , $ id ) = @ _ ;
( my $ build ) = $ c - > model ( 'DB::Builds' ) - > search ( { id = > $ id } ) ;
return $ build ;
}
sub index :Path :Args(0) {
my ( $ self , $ c ) = @ _ ;
$ c - > stash - > { template } = 'index.tt' ;
2008-11-10 10:18:50 +00:00
$ c - > stash - > { jobs } = [ $ c - > model ( 'DB::Jobs' ) - > all ] ;
2008-11-09 00:48:36 +00:00
$ c - > stash - > { projects } = [ $ c - > model ( 'DB::Projects' ) - > all ] ;
2008-11-06 18:26:29 +00:00
$ c - > stash - > { allBuilds } = [ $ c - > model ( 'DB::Builds' ) - > search ( undef , { order_by = > "timestamp DESC" } ) ] ;
2008-10-28 12:44:36 +00:00
# Get the latest build for each unique job.
2008-10-28 17:08:29 +00:00
# select * from builds as x where timestamp == (select max(timestamp) from builds where jobName == x.jobName);
2008-11-05 03:25:48 +00:00
$ c - > stash - > { latestBuilds } = [ $ c - > model ( 'DB::Builds' ) - > search ( undef , { order_by = > "project, attrName" , where = > "timestamp == (select max(timestamp) from builds where project == me.project and attrName == me.attrName)" } ) ] ;
2008-10-28 10:19:31 +00:00
}
2008-11-06 23:17:46 +00:00
sub project :Local {
my ( $ self , $ c , $ projectName ) = @ _ ;
$ c - > stash - > { template } = 'project.tt' ;
( my $ project ) = $ c - > model ( 'DB::Projects' ) - > search ( { name = > $ projectName } ) ;
return error ( $ c , "Project <tt>$projectName</tt> doesn't exist." ) if ! defined $ project ;
$ c - > stash - > { project } = $ project ;
$ c - > stash - > { jobNames } =
[ $ c - > model ( 'DB::Builds' ) - > search ( { project = > $ projectName } , { select = > [ { distinct = > 'attrname' } ] , as = > [ 'attrname' ] } ) ] ;
}
2008-10-28 15:34:29 +00:00
sub job :Local {
2008-11-05 03:25:48 +00:00
my ( $ self , $ c , $ project , $ jobName ) = @ _ ;
2008-10-28 15:34:29 +00:00
$ c - > stash - > { template } = 'job.tt' ;
2008-11-06 23:17:46 +00:00
$ c - > stash - > { projectName } = $ project ;
2008-10-28 15:34:29 +00:00
$ c - > stash - > { jobName } = $ jobName ;
2008-11-05 03:25:48 +00:00
$ c - > stash - > { builds } = [ $ c - > model ( 'DB::Builds' ) - > search ( { project = > $ project , attrName = > $ jobName } , { order_by = > "timestamp DESC" } ) ] ;
2008-10-28 15:34:29 +00:00
}
2008-10-28 10:19:31 +00:00
sub default :Path {
my ( $ self , $ c ) = @ _ ;
error ( $ c , "Page not found." ) ;
}
sub build :Local {
my ( $ self , $ c , $ id ) = @ _ ;
my $ build = getBuild ( $ c , $ id ) ;
return error ( $ c , "Build with ID $id doesn't exist." ) if ! defined $ build ;
2008-10-28 12:44:36 +00:00
2008-10-28 10:19:31 +00:00
$ c - > stash - > { template } = 'build.tt' ;
$ c - > stash - > { build } = $ build ;
$ c - > stash - > { id } = $ id ;
}
sub log :Local {
my ( $ self , $ c , $ id , $ logPhase ) = @ _ ;
my $ build = getBuild ( $ c , $ id ) ;
return error ( $ c , "Build with ID $id doesn't exist." ) if ! defined $ build ;
my $ log = $ build - > buildlogs - > find ( { logphase = > $ logPhase } ) ;
return error ( $ c , "Build $id doesn't have a log phase named <tt>$logPhase</tt>." ) if ! defined $ log ;
$ c - > stash - > { template } = 'log.tt' ;
$ c - > stash - > { id } = $ id ;
$ c - > stash - > { log } = $ log ;
# !!! should be done in the view (as a TT plugin).
$ c - > stash - > { logtext } = loadLog ( $ log - > path ) ;
}
sub loadLog {
my ( $ path ) = @ _ ;
# !!! all a quick hack
if ( $ path =~ /.bz2$/ ) {
return `cat $path | bzip2 -d` ;
} else {
return `cat $path` ;
}
}
sub end : ActionClass('RenderView') { }
1 ;