def _process_jenkins_jobs()

in services/jenkins-run-statistics/statistics.py [0:0]


def _process_jenkins_jobs(dynamo_db, cloudwatch, jenkins_jobs):
    """
    Process the passed Jenkins Jobs and record metrics of the underlying runs
    :param dynamo_db: Handle to Boto DynamoDB
    :param cloudwatch: Handle to Boto CloudWatch
    :param jenkins_jobs: List of Jenkins Jobs
    :return: Nothing
    """
    def generate_metric_dimensions():
        job_name = jenkins_job.get_job_hierarchy()['job_name']
        branch_name = jenkins_job.get_job_hierarchy()['branch_name']
        if branch_name and 'PR-' in branch_name:
            # Replace pull request branch names with a generalized name. We don't want to track PR branches individually
            branch_name = 'Pull Request'

        metric_dimensions = {'Job': job_name}
        if branch_name:
            metric_dimensions['Branch'] = branch_name

        return metric_dimensions

    table = dynamo_db.Table(DYNAMODB_TABLE)

    for jenkins_job in jenkins_jobs:
        time_diff = datetime.now(tz=timezone.utc) - jenkins_job.last_build_time
        if time_diff.total_seconds() >= MAXIMUM_LOOKBACK_TIMEFRAME_SECONDS:
            logging.debug('%s has last been run %d days ago, skipping since its more than two weeks', jenkins_job,
                          time_diff.days)
            continue

        last_processed_run_id = _dynamo_get_last_processed_jenkins_run_id(dynamo_table=table,
                                                                          jenkins_job_name=jenkins_job.full_job_name)
        if last_processed_run_id:
            jenkins_job.update_last_scanned_run_id(last_processed_run_id)

        outstanding_jenkins_runs = jenkins_job.get_outstanding_jenkins_runs()
        if not outstanding_jenkins_runs:
            logging.debug('%s has no outstanding runs', jenkins_job)
            continue

        metric_dimensions = generate_metric_dimensions()

        for jenkins_run in outstanding_jenkins_runs:
            if _process_jenkins_run(cloudwatch=cloudwatch, jenkins_run=jenkins_run,
                                    metric_dimensions=dict(metric_dimensions)):
                logging.info('%s has been processed, saving state in database', jenkins_run)
                _dynamo_set_last_processed_jenkins_run_id(dynamo_db=dynamo_db, jenkins_run=jenkins_run)
            else:
                logging.info('%s requested to not be processed further, aborting scan of job', jenkins_run)
                break