1#!/usr/bin/env python3 2# Copyright 2018 - The Android Open Source Project 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16"""AIDEgen metrics functions.""" 17 18import logging 19import os 20import platform 21import sys 22 23from aidegen import constant 24from aidegen.lib import common_util 25 26# When combining 3 paths in a single try block, it's hard for the coverage 27# counting algorithm to judge each real path clearly. So, separating them 28# into its own try block will increase the coverage. 29 30# Original code as follows, 31# try: 32# from atest.metrics import metrics 33# from atest.metrics import metrics_base 34# from atest.metrics import metrics_utils 35# except ImportError: 36# logging.debug('Import metrics fail, can\'t send metrics.') 37# metrics = None 38# metrics_base = None 39# metrics_utils = None 40try: 41 from atest.metrics import metrics 42except ImportError: 43 logging.debug('Import metrics fail, can\'t send metrics.') 44 metrics = None 45 46try: 47 from atest.metrics import metrics_base 48except ImportError: 49 logging.debug('Import metrics fail, can\'t send metrics.') 50 metrics_base = None 51 52try: 53 from atest.metrics import metrics_utils 54except ImportError: 55 logging.debug('Import metrics fail, can\'t send metrics.') 56 metrics_utils = None 57 58 59def starts_asuite_metrics(references): 60 """Starts to record metrics data. 61 62 Send a metrics data to log server at the same time. 63 64 Args: 65 references: a list of reference data, when importing whole Android 66 it contains 'is_android_tree'. 67 """ 68 if not metrics: 69 return 70 metrics_utils.print_data_collection_notice() 71 metrics_base.MetricsBase.tool_name = constant.AIDEGEN_TOOL_NAME 72 metrics_utils.get_start_time() 73 command = ' '.join(sys.argv) 74 metrics.AtestStartEvent( 75 command_line=command, 76 test_references=references, 77 cwd=os.getcwd(), 78 os=platform.platform()) 79 80 81def ends_asuite_metrics(exit_code, stacktrace='', logs=''): 82 """Send the end event to log server. 83 84 Args: 85 exit_code: An integer of exit code. 86 stacktrace: A string of stacktrace. 87 logs: A string of logs. 88 89 Returns: 90 Boolean: False if metrics_utils does not exist. 91 True when successfully send metrics. 92 """ 93 if not metrics_utils: 94 return False 95 metrics_utils.send_exit_event( 96 exit_code, 97 stacktrace=stacktrace, 98 logs=logs) 99 return True 100 101 102def send_exception_metrics(exit_code, stack_trace, log, err_msg): 103 """Sends exception metrics. 104 105 For recording the exception metrics, this function is going to be called. 106 It is different to ends_asuite_metrics function, which will be called every 107 time AIDEGen process is finished. 108 The steps you need to do to call this function: 109 1. Create an exit code in constants.py. 110 2. Generate the stack trace info and the log for debugging. 111 3. Show the warning message to users. 112 113 Args: 114 exit_code: An integer of exit code. 115 stack_trace: A string of stacktrace. 116 log: A string of logs. 117 err_msg: A string to show warning. 118 """ 119 print('\n{} {}\n'.format(common_util.COLORED_INFO('Warning:'), err_msg)) 120 stack_trace = common_util.remove_user_home_path(stack_trace) 121 log = common_util.remove_user_home_path(log) 122 ends_asuite_metrics(exit_code, stack_trace, log) 123 124 125def performance_metrics(process_type, duration): 126 """ Records each process runtime and send it to clearcut. 127 128 Args: 129 process_type: An integer of process type. 130 duration: Runtime for a specific process. 131 132 Returns: 133 Boolean: False if metrics does not exist. 134 True when successfully send metrics. 135 """ 136 if not metrics: 137 return False 138 139 metrics.LocalDetectEvent( 140 detect_type=process_type, 141 result=int(duration) 142 ) 143 return True 144