1#! /usr/bin/python
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
9import argparse
10from datetime import datetime, timedelta
11import sys
12
13import common
14from autotest_lib.frontend import setup_django_environment
15from autotest_lib.frontend.afe import models
16from autotest_lib.client.common_lib.cros.graphite import autotest_stats
17
18
19def number_of_jobs_since(delta):
20    """Returns the number of jobs kicked off in the last |duration| minutes.
21
22    @param delta: A timedelta which indicates the maximum age of the jobs to count
23    """
24    cutoff = datetime.now() - delta
25    return models.Job.objects.filter(created_on__gt=cutoff).count()
26
27
28def main():
29    """Counts the number of AFE jobs in the last day, then pushes the count to statsd"""
30    parser = argparse.ArgumentParser(
31        description=('A script which records the number of afe jobs run in a time interval.'))
32    parser.parse_args(sys.argv[1:])
33    count = number_of_jobs_since(timedelta(days=1))
34    autotest_stats.Gauge('jobs_rate').send('afe_daily_count', count)
35
36
37if __name__ == '__main__':
38    main()
39