blob: 798f431ce9384bdb732c9ca41ef6ec1630e6b3ee [file] [log] [blame]
Mike Frysinger0e2cb7a2019-08-20 17:04:52 -04001#!/usr/bin/python2
Paul Hobbs29c37b32015-12-11 16:24:27 -08002
3# Copyright 2015 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7"""Counts the number of jobs created in the last 24 hours."""
8
9import argparse
Paul Hobbsebc34602015-12-16 14:52:48 -080010from datetime import datetime, timedelta
Paul Hobbs29c37b32015-12-11 16:24:27 -080011import sys
12
13import common
14from autotest_lib.frontend import setup_django_environment
15from autotest_lib.frontend.afe import models
Ningning Xiac0c10612016-11-23 13:15:26 -080016from autotest_lib.server import site_utils
Dan Shi5e2efb72017-02-07 11:40:23 -080017
18try:
19 from chromite.lib import metrics
20except ImportError:
21 metrics = site_utils.metrics_mock
22
Paul Hobbs29c37b32015-12-11 16:24:27 -080023
24def number_of_jobs_since(delta):
25 """Returns the number of jobs kicked off in the last |duration| minutes.
26
27 @param delta: A timedelta which indicates the maximum age of the jobs to count
28 """
Paul Hobbsebc34602015-12-16 14:52:48 -080029 cutoff = datetime.now() - delta
Paul Hobbs29c37b32015-12-11 16:24:27 -080030 return models.Job.objects.filter(created_on__gt=cutoff).count()
31
32
33def main():
Aviv Keshete70fed82017-07-13 16:50:58 -070034 """Counts the number of AFE jobs in the last day and report to monarch."""
Paul Hobbs29c37b32015-12-11 16:24:27 -080035 parser = argparse.ArgumentParser(
36 description=('A script which records the number of afe jobs run in a time interval.'))
37 parser.parse_args(sys.argv[1:])
Paul Hobbsebc34602015-12-16 14:52:48 -080038 count = number_of_jobs_since(timedelta(days=1))
Ningning Xiac0c10612016-11-23 13:15:26 -080039
40 with site_utils.SetupTsMonGlobalState('count_jobs', short_lived=True):
41 # TODO: Reporting a stat for each job created from the afe directly could be better.
42 # More discussions are needed to decide whether to remove this file.
43 metrics.Gauge('chromeos/autotest/experimental/jobs_rate/afe_daily_count').set(count)
Paul Hobbs29c37b32015-12-11 16:24:27 -080044
45
46if __name__ == '__main__':
47 main()