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 25from atest import atest_utils 26 27# When combine 3 paths in a single try block, it's hard for the coverage 28# counting algorithm to judge the each real path clearly. So, separating them 29# into its own try block will increase the coverage. 30 31# Original code as follows, 32# try: 33# from asuite.metrics import metrics 34# from asuite.metrics import metrics_base 35# from asuite.metrics import metrics_utils 36# except ImportError: 37# logging.debug('Import metrics fail, can\'t send metrics.') 38# metrics = None 39# metrics_base = None 40# metrics_utils = None 41try: 42 from asuite.metrics import metrics 43except ImportError: 44 logging.debug('Import metrics fail, can\'t send metrics.') 45 metrics = None 46 47try: 48 from asuite.metrics import metrics_base 49except ImportError: 50 logging.debug('Import metrics fail, can\'t send metrics.') 51 metrics_base = None 52 53try: 54 from asuite.metrics import metrics_utils 55except ImportError: 56 logging.debug('Import metrics fail, can\'t send metrics.') 57 metrics_utils = None 58 59 60def starts_asuite_metrics(references): 61 """Starts to record metrics data. 62 63 Send a metrics data to log server at the same time. 64 65 Args: 66 references: a list of reference data, when importing whole Android 67 it contains 'is_android_tree'. 68 """ 69 if not metrics: 70 return 71 atest_utils.print_data_collection_notice() 72 metrics_base.MetricsBase.tool_name = constant.AIDEGEN_TOOL_NAME 73 metrics_utils.get_start_time() 74 command = ' '.join(sys.argv) 75 metrics.AtestStartEvent( 76 command_line=command, 77 test_references=references, 78 cwd=os.getcwd(), 79 os=platform.platform()) 80 81 82def ends_asuite_metrics(exit_code, stacktrace='', logs=''): 83 """Send the end event to log server. 84 85 Args: 86 exit_code: An integer of exit code. 87 stacktrace: A string of stacktrace. 88 logs: A string of logs. 89 90 Returns: 91 Boolean: False if metrics_utils does not exist. 92 True when successfully send metrics. 93 """ 94 if not metrics_utils: 95 return False 96 metrics_utils.send_exit_event( 97 exit_code, 98 stacktrace=stacktrace, 99 logs=logs) 100 return True 101 102 103def send_exception_metrics(exit_code, stack_trace, log, err_msg): 104 """Sends exception metrics. 105 106 For recording the exception metrics, this function is going to be called. 107 It is different to ends_asuite_metrics function, which will be called every 108 time AIDEGen process is finished. 109 The steps you need to do to call this function: 110 1. Create an exit code in constants.py. 111 2. Generate the stack trace info and the log for debugging. 112 3. Show the warning message to users. 113 114 Args: 115 exit_code: An integer of exit code. 116 stack_trace: A string of stacktrace. 117 log: A string of logs. 118 err_msg: A string to show warning. 119 """ 120 print('\n{} {}\n'.format(common_util.COLORED_INFO('Warning:'), err_msg)) 121 stack_trace = common_util.remove_user_home_path(stack_trace) 122 log = common_util.remove_user_home_path(log) 123 ends_asuite_metrics(exit_code, stack_trace, log) 124