blob: b85d60bd86ea83b9a38fd38f72f2cde3f4acf6f2 [file] [log] [blame]
Fang Deng042c1472014-10-23 13:56:41 -07001# 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 import host_states
13from autotest_lib.client.common_lib.cros.graphite import es_utils
14
15
16# Metadata db type string for job time stats
17DEFAULT_KEY = 'job_time_breakdown'
18
19# Job breakdown statuses
20_hs = host_states.Status
21_qs = host_queue_entry_states.Status
22_status_list = [
23 _qs.QUEUED, _qs.RESETTING, _qs.VERIFYING,
24 _qs.PROVISIONING, _hs.REPAIRING, _qs.CLEANING,
25 _qs.RUNNING, _qs.GATHERING, _qs.PARSING]
26STATUS = enum.Enum(*_status_list, string_values=True)
27
28
29def record_state_duration(
30 job_or_task_id, hostname, status, duration_secs,
31 type_str=DEFAULT_KEY, is_special_task=False):
32 """Record state duration for a job or a task.
33
34 @param job_or_task_id: Integer, representing a job id or a special task id.
35 @param hostname: String, representing a hostname.
36 @param status: One of the enum values of job_overhead.STATUS.
37 @param duration_secs: Duration of the job/task in secs.
38 @param is_special_task: True/Fals, whether this is a special task.
39 @param type_str: The elastic search type string to be used when sending data
40 to metadata db.
41 """
42 if not job_or_task_id or not hostname or not status:
43 logging.error(
44 'record_state_duration failed: job_or_task_id=%s, '
45 'hostname=%s, status=%s', job_or_task_id, hostname, status)
46 return
47 id_str = 'task_id' if is_special_task else 'job_id'
48 metadata = {
49 id_str: int(job_or_task_id),
50 'hostname': hostname,
51 'status': status,
52 'duration': duration_secs}
53 es_utils.ESMetadata().post(
54 type_str=type_str, metadata=metadata)