Created
November 26, 2014 15:26
-
-
Save gmaxey/06c94c62c7449bf36709 to your computer and use it in GitHub Desktop.
Electric Commander job search example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Electric Commander job search example | |
# Searches jobs that: | |
# - Are completed | |
# - Are in an Error state | |
# - Job name has a particular format | |
# - Job name contains a particular application name | |
# - Job completed before particular time | |
# | |
# Usage: ec-perl searchJobs.pl | |
# Assumes that a Commander session ID already has been established. Use line 17 if this is not the case. | |
use ElectricCommander; | |
use DateTime; | |
use DateTime::Duration; | |
my $ec = new ElectricCommander(); | |
#$ec->login("admin", "changeme"); | |
my $MAXJOBS = 100; # Number of jobs to search over for each application name | |
my $endDate = DateTime->new( | |
year => 2014, | |
month => 11, | |
day => 25, | |
hour => 16, | |
minute => 12, | |
second => 47 | |
); | |
#my $TIMESTAMP = "2014-11-30T00:00:00.000Z"; | |
my $TIMESTAMP = $endDate->iso8601(); | |
my @applications = ("app1","app2","app3"); | |
my @unitJobs; | |
my @integrationJobs; | |
foreach my $application (@applications) { | |
# create unitFilterList for unit test job names CONTAINS trunk-unit-buildNum-{app name goes here} | |
# LAST 1 unit-test JOB which had ERROR | |
my @unitFilterList; | |
# only finished jobs | |
push (@unitFilterList, {"propertyName" => "status", | |
"operator" => "equals", | |
"operand1" => "completed"}); | |
# only ERROR jobs | |
push (@unitFilterList, {"propertyName" => "outcome", | |
"operator" => "equals", | |
"operand1" => "error"}); | |
# job name is like trunk-unit- | |
push (@unitFilterList, {"propertyName" => "jobName", | |
"operator" => "like", | |
"operand1" => "trunk-unit-%"}); | |
# job name contains application name | |
push (@unitFilterList, {"propertyName" => "jobName", | |
"operator" => "contains", | |
"operand1" => $application}); | |
my $unitResults= $ec->findObjects("job",{ | |
maxIds => $MAXJOBS, | |
numObjects => $MAXJOBS, | |
filter => [{ | |
operator => 'and', | |
filter => \@unitFilterList}], | |
sort => [ { | |
propertyName => "finish", | |
order => "descending"} ] | |
}); | |
my @allUnitJobs = $unitResults->findnodes("//job"); | |
if (scalar(@allUnitJobs)>0) { # Only process if there are jobs | |
my $unitJob = @allUnitJobs[0]; | |
my $unitName = $unitJob->findvalue("jobName"),"\n"; | |
"1" =~ /1/; # reset regex value | |
$unitName =~ m/trunk-unit-(\d+)-$application/; | |
my $unitBuildNumber = $1; | |
#print "unit: $application,$unitBuildNumber,error\n" if ($unitBuildNumber>0); | |
push (@unitJobs, "$application,$unitBuildNumber,error") if ($unitBuildNumber>0); | |
} | |
# create integrationFilterList integration test job names CONTAINS trunk-integration-buildNum-{app name goes here}. | |
# LAST 1 integration-test JOB which had ERROR, BEFORE a given TIMESTAMP | |
my @integrationFilterList; | |
# only finished jobs | |
push (@integrationFilterList, {"propertyName" => "status", | |
"operator" => "equals", | |
"operand1" => "completed"}); | |
# only ERROR jobs | |
push (@integrationFilterList, {"propertyName" => "outcome", | |
"operator" => "equals", | |
"operand1" => "error"}); | |
# older than | |
push (@integrationFilterList, {"propertyName" => "finish", | |
"operator" => "lessThan", | |
"operand1" => $TIMESTAMP}); | |
# job name is like trunk-integration- | |
push (@integrationFilterList, {"propertyName" => "jobName", | |
"operator" => "like", | |
"operand1" => "trunk-integration-%"}); | |
# job name contains application name | |
push (@integrationFilterList, {"propertyName" => "jobName", | |
"operator" => "contains", | |
"operand1" => $application}); | |
my $integrationResults= $ec->findObjects("job",{ | |
maxIds => $MAXJOBS, | |
numObjects => $MAXJOBS, | |
filter => [{ | |
operator => 'and', | |
filter => \@integrationFilterList}], | |
sort => [ { | |
propertyName => "finish", | |
order => "descending"} ] | |
}); | |
my @allIntegrationJobs = $integrationResults->findnodes("//job"); | |
if (scalar(@allIntegrationJobs)>0) { # Only process if there are jobs | |
my $integrationJob = @allIntegrationJobs[0]; | |
my $integrationName = $integrationJob->findvalue("jobName"),"\n"; | |
"1" =~ /1/; # reset regex value | |
$integrationName =~ m/trunk-integration-(\d+)-$application/; | |
my $integrationBuildNumber = $1; | |
#print "integration: $application,$integrationBuildNumber,error\n" if ($integrationBuildNumber>0); | |
push (@integrationJobs, "$application,$integrationBuildNumber,error") if ($integrationBuildNumber>0); | |
} | |
} | |
print "\nUNIT\n"; | |
foreach my $unitJob (@unitJobs) { | |
print $unitJob,"\n"; | |
} | |
print "\nINTEGRATION\n"; | |
foreach my $integrationJob (@integrationJobs) { | |
print $integrationJob,"\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment