0

I need to get the list of jobs that was successfully ran in last 24hrs from current time of script execution. Currently i am getting only present days date successful jobs but however i am looking for last 24hrs which could be last days jobs also which fall under 24 hrs.

Have written a script and getting results for present days but not for 24 hrs

import hudson.model.*
Date date = new Date()
String present = date.format("MM-dd-yyyy")
def str_view = "GitLab_jobs"
def view = Hudson.instance.getView(str_view)
def successfulJobs = view.getItems().findAll {
  job - > job.lastBuild != null && job.lastBuild.result == hudson.model.Result.SUCCESS && !job.name.contains("maintenance")
}
def faildJobs = view.getItems().findAll {
  job - > job.lastBuild != null && job.lastBuild.result == hudson.model.Result.FAILURE && !job.name.contains("maintenance")
}
def disabledJob = view.getItems().findAll {
  job - > job.disabled == true && !job.name.contains("maintenance")
}
def enabledJob = view.getItems().findAll {
  job - > job.disabled != true && !job.name.contains("maintenance")
}

println "Total jobs: " + view.getItems().size + " Successful: " + successfulJobs.size +
  " Failed: " + faildJobs.size + " Enabled jobs: " + enabledJob.size + " Disabled jobs: " + disabledJob.size



println "Current Successful job Today:"
successfulJobs.each {
  job - > printInfo(job)
}


def printInfo(job) {
  Date date = new Date()
  String datePart = date.format("MM-dd-yyyy")
  String timePart = date.format("HH:mm:ss")


  x = "${datePart}"

  y = "${job.lastBuild.getTime().format("
  MM - dd - yyyy ")}"

  if ("$x" == "$y") {
    println "Job: ${job.name} build on ${job.getAssignedLabelString()}, " +
      "Date ${job.lastBuild.getTime().format("
    MM - dd - yyyy ")}, is disabled : ${job.disabled}"
  }

}
actual results should print the list of jobs completed successfully in last 24hrs duration

1 Answers1

0

Try

Date previousDayFromNow = new Date() - 1

if (previousDayFromNow < job.lastBuild.getTime()) {
    println "...job info..."
}
sintasy
  • 627
  • 11
  • 24
  • previousDayFromNow Date is resolving to last 24hrs from now but still the jobs are not getting with that condition. is it something to do with the formating of date? sample output: Total jobs: 3651 Successful: 1253 Failed: 459 Enabled jobs: 2193 Disabled jobs: 14 Current Successful job Today: Thu Sep 12 11:25:24 CDT 2019 Sat Apr 28 22:03:09 CDT 2018 Job: trunk_app_360Shipper, build on: snapshot, Date: Apr-28-2018, 10:03 PM, is disabled: true – Karthik Reddy Sep 13 '19 at 16:26