lin
2025-07-30 fcd736bf35fd93b563e9bbf594f2aa7b62028cc9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#! /usr/bin/python
 
# Copyright 2015 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
 
"""Counts the number of jobs created in the last 24 hours."""
 
import argparse
from datetime import datetime, timedelta
import sys
 
import common
from autotest_lib.frontend import setup_django_environment
from autotest_lib.frontend.afe import models
from autotest_lib.server import site_utils
 
try:
    from chromite.lib import metrics
except ImportError:
    metrics = site_utils.metrics_mock
 
 
def number_of_jobs_since(delta):
    """Returns the number of jobs kicked off in the last |duration| minutes.
 
    @param delta: A timedelta which indicates the maximum age of the jobs to count
    """
    cutoff = datetime.now() - delta
    return models.Job.objects.filter(created_on__gt=cutoff).count()
 
 
def main():
    """Counts the number of AFE jobs in the last day and report to monarch."""
    parser = argparse.ArgumentParser(
        description=('A script which records the number of afe jobs run in a time interval.'))
    parser.parse_args(sys.argv[1:])
    count = number_of_jobs_since(timedelta(days=1))
 
    with site_utils.SetupTsMonGlobalState('count_jobs', short_lived=True):
        # TODO: Reporting a stat for each job created from the afe directly could be better.
        # More discussions are needed to decide whether to remove this file.
        metrics.Gauge('chromeos/autotest/experimental/jobs_rate/afe_daily_count').set(count)
 
 
if __name__ == '__main__':
    main()