1# Copyright 2017 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"""Shared constants and methods for result utilities."""
6
7import collections
8
9
10# Following are key names for directory summaries. The keys are started with /
11# so it can be differentiated with a valid file name. The short keys are
12# designed for smaller file size of the directory summary.
13
14# Original size of the directory or file
15ORIGINAL_SIZE_BYTES = '/S'
16# Size of the directory or file after trimming
17TRIMMED_SIZE_BYTES = '/T'
18# Size of the directory or file being collected from client side
19COLLECTED_SIZE_BYTES = '/C'
20# A dictionary of sub-directories' summary: name: {directory_summary}
21DIRS = '/D'
22# Default root directory name. To allow summaries to be merged effectively, all
23# summaries are collected with root directory of ''
24ROOT_DIR = ''
25
26# Information of test result sizes to be stored in tko_job_keyvals.
27# The total size (in kB) of test results that generated during the test,
28# including:
29#  * server side test logs and result files.
30#  * client side test logs, sysinfo, system logs and crash dumps.
31# Note that a test can collect the same test result files from DUT multiple
32# times during the test, before and after each iteration/test. So the value of
33# client_result_collected_KB could be larger than the value of
34# result_uploaded_KB, which is the size of result directory on the server side,
35# even if the test result throttling is not applied.
36#
37# Attributes of the named tuple includes:
38# client_result_collected_KB: The total size (in KB) of test results collected
39#         from test device.
40# original_result_total_KB: The original size (in KB) of test results before
41#         being trimmed.
42# result_uploaded_KB: The total size (in KB) of test results to be uploaded by
43#         gs_offloader.
44# result_throttled: Flag to indicate if test results collection is throttled.
45ResultSizeInfo = collections.namedtuple(
46        'ResultSizeInfo',
47        ['client_result_collected_KB',
48         'original_result_total_KB',
49         'result_uploaded_KB',
50         'result_throttled'])
51
52
53def get_result_size_info(client_collected_bytes, summary):
54    """Get the result size information.
55
56    @param client_collected_bytes: Size in bytes of results collected from the
57            test device.
58    @param summary: A dictionary of directory summary.
59    @return: A namedtuple of result size informations, including:
60            client_result_collected_KB: The total size (in KB) of test results
61                    collected from test device.
62            original_result_total_KB: The original size (in KB) of test results
63                    before being trimmed.
64            result_uploaded_KB: The total size (in KB) of test results to be
65                    uploaded.
66            result_throttled: True if test results collection is throttled.
67    """
68    root_entry = summary[ROOT_DIR]
69    client_result_collected_KB= client_collected_bytes / 1024
70    original_result_total_KB = root_entry[ORIGINAL_SIZE_BYTES] / 1024
71    result_uploaded_KB = root_entry[TRIMMED_SIZE_BYTES] / 1024
72    # Test results are considered to be throttled if the total size of
73    # results collected is different from the total size of trimmed results
74    # from the client side.
75    result_throttled = (
76            root_entry[ORIGINAL_SIZE_BYTES] != root_entry[TRIMMED_SIZE_BYTES])
77
78    return ResultSizeInfo(client_result_collected_KB=client_result_collected_KB,
79                          original_result_total_KB=original_result_total_KB,
80                          result_uploaded_KB=result_uploaded_KB,
81                          result_throttled=result_throttled)