• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Utils for recording job overhead in metadata db."""
6
7import logging
8
9from autotest_lib.client.common_lib import enum
10from autotest_lib.client.common_lib import host_queue_entry_states
11from autotest_lib.client.common_lib import host_states
12from autotest_lib.site_utils import metadata_reporter
13
14
15# Metadata db type string for job time stats
16DEFAULT_KEY = 'job_time_breakdown'
17
18# Metadata db type string for suite time stats
19SUITE_RUNTIME_KEY = 'suite_runtime'
20
21# Job breakdown statuses
22_hs = host_states.Status
23_qs = host_queue_entry_states.Status
24_status_list = [
25        _qs.QUEUED, _qs.RESETTING, _qs.VERIFYING,
26        _qs.PROVISIONING, _hs.REPAIRING, _qs.CLEANING,
27        _qs.RUNNING, _qs.GATHERING, _qs.PARSING]
28STATUS = enum.Enum(*_status_list, string_values=True)
29
30
31def record_state_duration(
32        job_or_task_id, hostname, status, duration_secs,
33        type_str=DEFAULT_KEY, is_special_task=False):
34    """Record state duration for a job or a task.
35
36    @param job_or_task_id: Integer, representing a job id or a special task id.
37    @param hostname: String, representing a hostname.
38    @param status: One of the enum values of job_overhead.STATUS.
39    @param duration_secs: Duration of the job/task in secs.
40    @param is_special_task: True/Fals, whether this is a special task.
41    @param type_str: The elastic search type string to be used when sending data
42                     to metadata db.
43    """
44    if not job_or_task_id or not hostname or not status:
45        logging.error(
46                'record_state_duration failed: job_or_task_id=%s, '
47                'hostname=%s, status=%s', job_or_task_id, hostname, status)
48        return
49    id_str = 'task_id' if is_special_task else 'job_id'
50    metadata = {
51            id_str: int(job_or_task_id),
52            'hostname': hostname,
53            'status': status,
54            'duration': duration_secs,
55            '_type': type_str}
56    metadata_reporter.queue(metadata)
57
58
59def record_suite_runtime(suite_job_id, suite_name, board, build, num_child_jobs,
60                         runtime_in_secs):
61    """Record suite runtime.
62
63    @param suite_job_id: The job id of the suite for which we are going to
64                         collect stats.
65    @param suite_name: The suite name, e.g. 'bvt', 'dummy'.
66    @param board: The target board for which the suite is run,
67                  e.g., 'lumpy', 'link'.
68    @param build: The build for which the suite is run,
69                  e.g. 'lumpy-release/R35-5712.0.0'.
70    @param num_child_jobs: Total number of child jobs of the suite.
71    @param runtime_in_secs: Duration of the suite from the start to the end.
72    """
73    metadata = {
74            'suite_job_id': suite_job_id,
75            'suite_name': suite_name,
76            'board': board,
77            'build': build,
78            'num_child_jobs': num_child_jobs,
79            'duration': runtime_in_secs,
80            '_type': SUITE_RUNTIME_KEY}
81    metadata_reporter.queue(metadata)
82