• 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.client.common_lib.cros.graphite import autotest_es
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    autotest_es.post(type_str=type_str, metadata=metadata)
56
57
58def record_suite_runtime(suite_job_id, suite_name, board, build, num_child_jobs,
59                         runtime_in_secs):
60    """Record suite runtime.
61
62    @param suite_job_id: The job id of the suite for which we are going to
63                         collect stats.
64    @param suite_name: The suite name, e.g. 'bvt', 'dummy'.
65    @param board: The target board for which the suite is run,
66                  e.g., 'lumpy', 'link'.
67    @param build: The build for which the suite is run,
68                  e.g. 'lumpy-release/R35-5712.0.0'.
69    @param num_child_jobs: Total number of child jobs of the suite.
70    @param runtime_in_secs: Duration of the suite from the start to the end.
71    """
72    metadata = {
73            'suite_job_id': suite_job_id,
74            'suite_name': suite_name,
75            'board': board,
76            'build': build,
77            'num_child_jobs': num_child_jobs,
78            'duration': runtime_in_secs}
79    autotest_es.post(type_str=SUITE_RUNTIME_KEY, metadata=metadata)
80