Mike Frysinger | 0e2cb7a | 2019-08-20 17:04:52 -0400 | [diff] [blame] | 1 | #!/usr/bin/python2 |
Paul Hobbs | 29c37b3 | 2015-12-11 16:24:27 -0800 | [diff] [blame] | 2 | |
| 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 | |
| 9 | import argparse |
Paul Hobbs | ebc3460 | 2015-12-16 14:52:48 -0800 | [diff] [blame] | 10 | from datetime import datetime, timedelta |
Paul Hobbs | 29c37b3 | 2015-12-11 16:24:27 -0800 | [diff] [blame] | 11 | import sys |
| 12 | |
| 13 | import common |
| 14 | from autotest_lib.frontend import setup_django_environment |
| 15 | from autotest_lib.frontend.afe import models |
Ningning Xia | c0c1061 | 2016-11-23 13:15:26 -0800 | [diff] [blame] | 16 | from autotest_lib.server import site_utils |
Dan Shi | 5e2efb7 | 2017-02-07 11:40:23 -0800 | [diff] [blame] | 17 | |
| 18 | try: |
| 19 | from chromite.lib import metrics |
| 20 | except ImportError: |
| 21 | metrics = site_utils.metrics_mock |
| 22 | |
Paul Hobbs | 29c37b3 | 2015-12-11 16:24:27 -0800 | [diff] [blame] | 23 | |
| 24 | def 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 Hobbs | ebc3460 | 2015-12-16 14:52:48 -0800 | [diff] [blame] | 29 | cutoff = datetime.now() - delta |
Paul Hobbs | 29c37b3 | 2015-12-11 16:24:27 -0800 | [diff] [blame] | 30 | return models.Job.objects.filter(created_on__gt=cutoff).count() |
| 31 | |
| 32 | |
| 33 | def main(): |
Aviv Keshet | e70fed8 | 2017-07-13 16:50:58 -0700 | [diff] [blame] | 34 | """Counts the number of AFE jobs in the last day and report to monarch.""" |
Paul Hobbs | 29c37b3 | 2015-12-11 16:24:27 -0800 | [diff] [blame] | 35 | 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 Hobbs | ebc3460 | 2015-12-16 14:52:48 -0800 | [diff] [blame] | 38 | count = number_of_jobs_since(timedelta(days=1)) |
Ningning Xia | c0c1061 | 2016-11-23 13:15:26 -0800 | [diff] [blame] | 39 | |
| 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 Hobbs | 29c37b3 | 2015-12-11 16:24:27 -0800 | [diff] [blame] | 44 | |
| 45 | |
| 46 | if __name__ == '__main__': |
| 47 | main() |