KBEC-00324 - How can I get a list of jobs started within the last 30 minutes?

Article ID:360033190531
2 minute readKnowledge base
On this page

Problem

What is the syntax for ectool getJobSummaries with a greater than date time stamp filter?

We want to query using ectool for all jobs started in the last 30 minutes.

Solution

The following sample code was provided by one our Professional Services Engineers as a courtesy, and can be used to return jobs run in the last 30 minutes

#############################################################################
#
# Copyright 2013-2015 Electric-Cloud Inc.
#
#############################################################################
use strict;
use English;
use ElectricCommander;
use Data::Dumper;
use DateTime;

$| = 1;

my $DEBUG=0;

# Create a single instance of the Perl access to ElectricCommander
my $ec = new ElectricCommander({'format' => "json"});

# Check for the OS Type
my $osIsWindows = $^O =~ /MSWin/;

my $timeLimit       =  30;      # 30 minutes ago
# create filterList
my @filterList;
# only finished jobs
push (@filterList, {"propertyName" => "status",
                    "operator" => "equals",
                    "operand1" => "completed"});
# older than
push (@filterList, {"propertyName" => "createTime",
                    "operator" => "greaterThan",
                    "operand1" => calculateDate($timeLimit)});

my ($success, $xPath) = InvokeCommander("SuppressLog", "findObjects", "job",
                                        {filter => \@filterList});
# Loop over all returned jobs
my @nodeset=$xPath->findnodes('//job');
my $nbObjs=scalar(@nodeset);
printf("Search Status: %s.\n%s objects returned.\n", $success?"success":"failure", $nbObjs);

JOB: foreach my $node (@nodeset) {
    my $jobId   = $node->{jobId};
    my $jobName = $node->{jobName};

    print "Job: $jobName ($jobId)\n";
}

#############################################################################
#
#  Calculate the Date based on now and the number of minutes
#
#############################################################################
sub calculateDate {
    my $nbMin=shift;
    return DateTime->now()->subtract(minutes => $nbMin)->iso8601() . ".000Z";
}

#############################################################################
#
# Invoke a API call
#
#############################################################################
sub InvokeCommander {

    my $optionFlags = shift;
    my $commanderFunction = shift;
    my $result;
    my $success = 1;
    my $errMsg;
    my $errCode;

    my $bSuppressLog = $optionFlags =~ /SuppressLog/i;
    my $bSuppressResult = $bSuppressLog || $optionFlags =~ /SuppressResult/i;
    my $bIgnoreError = $optionFlags =~ /IgnoreError/i;

    # Run the command
    # print "Request to Commander: $commanderFunction\n" unless ($bSuppressLog);

    $ec->abortOnError(0) if $bIgnoreError;
    $result = $ec->$commanderFunction(@_);
    $ec->abortOnError(1) if $bIgnoreError;

    # Check for error return
    if (defined ($result->{responses}->[0]->{error})) {
        $errCode=$result->{responses}->[0]->{error}->{code};
        $errMsg=$result->{responses}->[0]->{error}->{message};
    }

    if ($errMsg ne "") {
        $success = 0;
    }
    if ($result) {
        print "Return data from Commander:\n" .
               Dumper($result) . "\n"
            unless $bSuppressResult;
    }

    # Return the result
    return ($success, $result, $errMsg, $errCode);
}

This code is provided as a starting point for your work, please modify it as you see fit. If you find any errors in the code, please feel free to send us your fixes and we will incorporate them to this article.