AIDEGen: Remove metrics v1 version.
Bug: 137596880
Test: 1. m aidegen
2. aidegen-dev tradefed -s -v
3. aidegen-dev nothing -s -v
4. Check the message "LogRequest successfully sent to Clearcut"
exists in both steps 2 and 3.
Change-Id: I61363a92fb030ec438bb576219f780f76c362ab3
Merged-In: I645c6426c4361b74b39942c0532d7614410d8575
diff --git a/aidegen/aidegen_main.py b/aidegen/aidegen_main.py
index 186c1e0..0331c70 100644
--- a/aidegen/aidegen_main.py
+++ b/aidegen/aidegen_main.py
@@ -55,9 +55,8 @@
from aidegen.lib.errors import AIDEgenError
from aidegen.lib.errors import IDENotExistError
from aidegen.lib.ide_util import IdeUtil
-from aidegen.lib.metrics import log_usage
-from aidegen.lib.metrics import starts_asuite_metrics
-from aidegen.lib.metrics import ends_asuite_metrics
+from aidegen.lib.aidegen_metrics import starts_asuite_metrics
+from aidegen.lib.aidegen_metrics import ends_asuite_metrics
from aidegen.lib.module_info_util import generate_module_info_json
from aidegen.lib.project_file_gen import generate_eclipse_project_files
from aidegen.lib.project_file_gen import generate_ide_project_files
@@ -374,7 +373,6 @@
Args:
args: A list of system arguments.
"""
- log_usage()
# Pre-check for IDE relevant case, then handle dependency graph job.
ide_util_obj = _get_ide_util_instance(args)
_check_skip_build(args)
diff --git a/aidegen/aidegen_main_unittest.py b/aidegen/aidegen_main_unittest.py
index cc10d01..5c34591 100644
--- a/aidegen/aidegen_main_unittest.py
+++ b/aidegen/aidegen_main_unittest.py
@@ -24,7 +24,7 @@
import aidegen.unittest_constants as uc
from aidegen import aidegen_main
-from aidegen.lib import metrics
+from aidegen.lib import aidegen_metrics
from aidegen import constant
from aidegen.lib import common_util
from aidegen.lib.common_util import COLORED_INFO
@@ -124,8 +124,8 @@
self.assertTrue(mock_ide.called_with(projects))
@mock.patch.object(common_util, 'get_atest_module_info')
- @mock.patch.object(metrics, 'log_usage')
- def test_show_collect_data_notice(self, mock_log, mock_get):
+ @mock.patch.object(aidegen_metrics, 'starts_asuite_metrics')
+ def test_show_collect_data_notice(self, mock_metrics, mock_get):
"""Test main process always run through the target test function."""
target = 'nothing'
args = aidegen_main._parse_args([target, '-s', '-n'])
@@ -133,7 +133,7 @@
err = common_util.PATH_NOT_EXISTS_ERROR.format(target)
mock_get.side_effect = ProjectPathNotExistError(err)
aidegen_main.main_without_message(args)
- self.assertTrue(mock_log.called)
+ self.assertTrue(mock_metrics.called)
@mock.patch.object(common_util, 'get_related_paths')
def test_compile_targets_for_whole_android_tree(self, mock_get):
diff --git a/aidegen/lib/aidegen_metrics.py b/aidegen/lib/aidegen_metrics.py
new file mode 100644
index 0000000..a1b1115
--- /dev/null
+++ b/aidegen/lib/aidegen_metrics.py
@@ -0,0 +1,72 @@
+#!/usr/bin/env python3
+# Copyright 2018 - The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""AIDEgen metrics functions."""
+
+import logging
+import os
+import platform
+import sys
+
+from aidegen import constant
+from atest import atest_utils
+
+try:
+ from asuite.metrics import metrics
+ from asuite.metrics import metrics_base
+ from asuite.metrics import metrics_utils
+except ImportError:
+ logging.debug('Import metrics fail, can\'t send metrics')
+ metrics = None
+ metrics_base = None
+ metrics_utils = None
+
+
+def starts_asuite_metrics(references=None):
+ """Starts to record metrics data.
+
+ Send a metrics data to log server at the same time.
+
+ Args:
+ references: a list of reference data, when importing whole Android
+ it contains 'is_android_tree'.
+ """
+ if not metrics:
+ return
+ atest_utils.print_data_collection_notice()
+ metrics_base.MetricsBase.tool_name = constant.AIDEGEN_TOOL_NAME
+ metrics_utils.get_start_time()
+ command = ' '.join(sys.argv)
+ metrics.AtestStartEvent(
+ command_line=command,
+ test_references=references,
+ cwd=os.getcwd(),
+ os=platform.platform())
+
+
+def ends_asuite_metrics(exit_code, stacktrace='', logs=''):
+ """Send the end event to log server.
+
+ Args:
+ exit_code: An integer of exit code.
+ stacktrace: A string of stacktrace.
+ logs: A string of logs.
+ """
+ if not metrics_utils:
+ return
+ metrics_utils.send_exit_event(
+ exit_code,
+ stacktrace=stacktrace,
+ logs=logs)
diff --git a/aidegen/lib/metrics.py b/aidegen/lib/metrics.py
deleted file mode 100644
index a03db43..0000000
--- a/aidegen/lib/metrics.py
+++ /dev/null
@@ -1,147 +0,0 @@
-#!/usr/bin/env python3
-# Copyright 2018 - The Android Open Source Project
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-"""AIDEgen metrics functions."""
-
-import json
-import logging
-import os
-import platform
-import subprocess
-import sys
-import urllib.request
-import uuid
-
-from aidegen import constant
-from atest import atest_utils
-from metrics import metrics
-from metrics import metrics_base
-from metrics import metrics_utils
-
-_METRICS_URL = 'http://asuite-218222.appspot.com/aidegen/metrics'
-_VALID_DOMAINS = ['google.com', 'android.com']
-_COMMAND_GIT_CONFIG = ['git', 'config', '--get', 'user.email']
-_JSON_HEADERS = {'Content-Type': 'application/json'}
-_METRICS_RESPONSE = b'done'
-_DUMMY_UUID = '00000000-0000-4000-8000-000000000000'
-_METRICS_TIMEOUT = 2 #seconds
-_META_FILE = os.path.join(
- os.path.expanduser('~'), '.config', 'asuite', '.metadata')
-
-
-def log_usage():
- """Log aidegen run."""
- # Show privacy and license hint message before collect data.
- atest_utils.print_data_collection_notice()
- _log_event(_METRICS_URL, dummy_key_fallback=False, ldap=_get_ldap())
-
-
-def starts_asuite_metrics():
- """Starts to record metrics data.
-
- Send a metrics data to log server at the same time.
- """
- metrics_base.MetricsBase.tool_name = constant.AIDEGEN_TOOL_NAME
- metrics_utils.get_start_time()
- command = ' '.join(sys.argv)
- metrics.AtestStartEvent(
- command_line=command,
- test_references=[],
- cwd=os.getcwd(),
- os=platform.platform())
-
-
-def ends_asuite_metrics(exit_code, stacktrace='', logs=''):
- """Send the end event to log server.
-
- Args:
- exit_code: An integer of exit code.
- stacktrace: A string of stacktrace.
- logs: A string of logs.
- """
- metrics_utils.send_exit_event(
- exit_code,
- stacktrace=stacktrace,
- logs=logs)
-
-
-# pylint: disable=broad-except
-def _get_ldap():
- """Return string email username for valid domains only, None otherwise."""
- if not atest_utils.is_external_run():
- aidegen_project = os.path.join(constant.ANDROID_ROOT_PATH, 'tools',
- 'asuite', 'aidegen')
- email = subprocess.check_output(
- _COMMAND_GIT_CONFIG, cwd=aidegen_project).strip()
- ldap, domain = str(email, encoding="utf-8").split('@', 2)
- if domain in _VALID_DOMAINS:
- return ldap
- return None
-
-
-# pylint: disable=broad-except
-def _log_event(metrics_url, dummy_key_fallback=True, **kwargs):
- """Base log event function for asuite backend.
-
- Args:
- metrics_url: String, URL to report metrics to.
- dummy_key_fallback: Boolean, If True and unable to get grouping key,
- use a dummy key otherwise return out. Sometimes we
- don't want to return metrics for users we are
- unable to identify. Default True.
- kwargs: Dict, additional fields we want to return metrics for.
- """
- try:
- try:
- key = str(_get_grouping_key())
- except Exception:
- if not dummy_key_fallback:
- return
- key = _DUMMY_UUID
- data = {'grouping_key': key, 'run_id': str(uuid.uuid4())}
- if kwargs:
- data.update(kwargs)
- data = json.dumps(data).encode("utf-8")
- request = urllib.request.Request(
- metrics_url, data=data, headers=_JSON_HEADERS)
- response = urllib.request.urlopen(request, timeout=_METRICS_TIMEOUT)
- content = response.read()
- if content != _METRICS_RESPONSE:
- raise Exception('Unexpected metrics response: %s' % content)
- except Exception:
- logging.exception('Exception sending metrics')
-
-
-def _get_grouping_key():
- """Get grouping key. Returns UUID.uuid4."""
- if os.path.isfile(_META_FILE):
- with open(_META_FILE) as meta_file:
- try:
- return uuid.UUID(meta_file.read(), version=4)
- except ValueError:
- logging.exception('malformed group_key in file, rewriting')
-
- key = uuid.uuid4()
- dir_path = os.path.dirname(_META_FILE)
- if os.path.isfile(dir_path):
- os.remove(dir_path)
- try:
- os.makedirs(dir_path)
- except OSError as err:
- if not os.path.isdir(dir_path):
- raise err
- with open(_META_FILE, 'w+') as meta_file:
- meta_file.write(str(key))
- return key
diff --git a/aidegen/lib/metrics_unittest.py b/aidegen/lib/metrics_unittest.py
deleted file mode 100644
index 5ae4790..0000000
--- a/aidegen/lib/metrics_unittest.py
+++ /dev/null
@@ -1,41 +0,0 @@
-#!/usr/bin/env python3
-#
-# Copyright 2019, The Android Open Source Project
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-"""Unittests for metrics."""
-
-from __future__ import print_function
-
-import unittest
-from unittest import mock
-
-from aidegen.lib import metrics
-from atest import atest_utils
-
-
-class MetricsUnittests(unittest.TestCase):
-
- """Unit tests for metrics.py"""
- @mock.patch.object(atest_utils, 'is_external_run')
- @mock.patch.object(atest_utils, 'print_data_collection_notice')
- def test_log_usage(self, mock_notice, mock_external_check):
- """Test log_usage always run through the target test function."""
- metrics.log_usage()
- self.assertTrue(mock_notice.called)
- self.assertTrue(mock_external_check.called)
-
-
-if __name__ == '__main__':
- unittest.main()
diff --git a/aidegen/lib/module_info_util_unittest.py b/aidegen/lib/module_info_util_unittest.py
index 0cf7f6b..e900004 100644
--- a/aidegen/lib/module_info_util_unittest.py
+++ b/aidegen/lib/module_info_util_unittest.py
@@ -24,7 +24,6 @@
import aidegen.unittest_constants as uc
from aidegen.lib import errors
-from aidegen.lib import metrics
from aidegen.lib import module_info_util
from atest import module_info
@@ -173,10 +172,9 @@
module_info_util._is_new_json_file_generated(
jfile, original_file_mtime))
- @mock.patch.object(metrics, 'ends_asuite_metrics')
@mock.patch('builtins.input')
@mock.patch('glob.glob')
- def test_build_failed_handle(self, mock_glob, mock_input, _send_exit):
+ def test_build_failed_handle(self, mock_glob, mock_input):
"""Test _build_failed_handle with different situations."""
mock_glob.return_value = ['project/file.iml']
mock_input.return_value = 'N'