diff --git a/src/azure-cli-core/setup.py b/src/azure-cli-core/setup.py index 6531643f021..0abcd410597 100644 --- a/src/azure-cli-core/setup.py +++ b/src/azure-cli-core/setup.py @@ -44,7 +44,7 @@ DEPENDENCIES = [ 'argcomplete~=1.8', - 'azure-cli-telemetry==1.0.6.*', + 'azure-cli-telemetry==1.0.7.*', 'azure-mgmt-core>=1.2.0,<2', 'cryptography', 'humanfriendly~=10.0', diff --git a/src/azure-cli-telemetry/HISTORY.rst b/src/azure-cli-telemetry/HISTORY.rst index 6d1b84deec8..bb320ffd38f 100644 --- a/src/azure-cli-telemetry/HISTORY.rst +++ b/src/azure-cli-telemetry/HISTORY.rst @@ -2,6 +2,10 @@ Release History =============== +1.0.7 ++++++ +* Support specifying `telemetry.push_interval_in_hours` to force push telemetry cache file + 1.0.6 +++++ * Add `__version__` in `__init__.py` diff --git a/src/azure-cli-telemetry/azure/cli/telemetry/components/records_collection.py b/src/azure-cli-telemetry/azure/cli/telemetry/components/records_collection.py index d44795fa9ea..a1055aeb41a 100644 --- a/src/azure-cli-telemetry/azure/cli/telemetry/components/records_collection.py +++ b/src/azure-cli-telemetry/azure/cli/telemetry/components/records_collection.py @@ -8,6 +8,7 @@ import shutil import stat import tempfile +from knack.config import CLIConfig class RecordsCollection: @@ -35,8 +36,13 @@ def snapshot_and_read(self): if not os.path.isdir(folder): return + # Collect all cache.x files. If it has been a long time since last sent, also collect cache file itself. + push_interval = datetime.timedelta(hours=self._get_push_interval_config()) + include_cache = datetime.datetime.now() - self._last_sent > push_interval + candidates = [(fn, os.stat(os.path.join(folder, fn))) for fn in os.listdir(folder) + if include_cache or fn != 'cache'] + # sort the cache files base on their last modification time. - candidates = [(fn, os.stat(os.path.join(folder, fn))) for fn in os.listdir(folder) if fn != 'cache'] candidates = [(fn, file_stat) for fn, file_stat in candidates if stat.S_ISREG(file_stat.st_mode)] candidates.sort(key=lambda pair: pair[1].st_mtime, reverse=True) # move the newer cache file first @@ -66,6 +72,12 @@ def snapshot_and_read(self): onerror=lambda _, p, tr: self._logger.error('Fail to remove file %s', p)) self._logger.info('Remove directory %s', tmp) + def _get_push_interval_config(self): + config = CLIConfig(config_dir=self._config_dir) + threshold = config.getint('telemetry', 'push_interval_in_hours', fallback=24) + # the threshold for push telemetry can't be less than 1 hour, default value is 24 hours + return threshold if threshold >= 1 else 24 + def _read_file(self, path): """ Read content of a telemetry cache file and parse them into records. """ try: diff --git a/src/azure-cli-telemetry/azure/cli/telemetry/tests/test_records_collection.py b/src/azure-cli-telemetry/azure/cli/telemetry/tests/test_records_collection.py index a1966459160..b1415672f67 100644 --- a/src/azure-cli-telemetry/azure/cli/telemetry/tests/test_records_collection.py +++ b/src/azure-cli-telemetry/azure/cli/telemetry/tests/test_records_collection.py @@ -35,19 +35,21 @@ def test_create_records_collection(self): # take snapshot and move the files collection.snapshot_and_read() - # all but one file, the 'cache' file, is moved. - self.assert_cache_files_count(1) + # all files are moved, including the 'cache' file. + self.assert_cache_files_count(0) # total records - self.assertEqual(1750, len([r for r in collection])) + self.assertEqual(1758, len([r for r in collection])) def test_create_records_collection_with_last_send(self): - last_send = datetime.datetime(year=2018, month=6, day=5, hour=16, minute=36, second=7) + last_send = datetime.datetime.now() - datetime.timedelta(hours=6) collection = RecordsCollection(last_send, self.work_dir) collection.snapshot_and_read() + # the threshold for pushing 'cache' file is 24, so 'cache' file should not be moved self.assert_cache_files_count(1) - self.assertEqual(453, len([r for r in collection])) + # no new records since last_send + self.assertEqual(0, len([r for r in collection])) def test_create_records_collection_against_missing_config_folder(self): collection = RecordsCollection(datetime.datetime.min, tempfile.mktemp()) diff --git a/src/azure-cli-telemetry/setup.py b/src/azure-cli-telemetry/setup.py index c2fb68bfc7e..e1fee7bb07b 100755 --- a/src/azure-cli-telemetry/setup.py +++ b/src/azure-cli-telemetry/setup.py @@ -8,7 +8,7 @@ from codecs import open from setuptools import setup -VERSION = "1.0.6" +VERSION = "1.0.7" CLASSIFIERS = [ 'Development Status :: 5 - Production/Stable', diff --git a/src/azure-cli/requirements.py3.Darwin.txt b/src/azure-cli/requirements.py3.Darwin.txt index 8099c0fc93e..9be281faad5 100644 --- a/src/azure-cli/requirements.py3.Darwin.txt +++ b/src/azure-cli/requirements.py3.Darwin.txt @@ -5,7 +5,7 @@ asn1crypto==0.24.0 azure-appconfiguration==1.1.1 azure-batch==12.0.0 azure-cli-core==2.38.0 -azure-cli-telemetry==1.0.6 +azure-cli-telemetry==1.0.7 azure-cli==2.38.0 azure-common==1.1.22 azure-core==1.24.0 diff --git a/src/azure-cli/requirements.py3.Linux.txt b/src/azure-cli/requirements.py3.Linux.txt index fbae8a05910..5bc464331a7 100644 --- a/src/azure-cli/requirements.py3.Linux.txt +++ b/src/azure-cli/requirements.py3.Linux.txt @@ -5,7 +5,7 @@ asn1crypto==0.24.0 azure-appconfiguration==1.1.1 azure-batch==12.0.0 azure-cli-core==2.38.0 -azure-cli-telemetry==1.0.6 +azure-cli-telemetry==1.0.7 azure-cli==2.38.0 azure-common==1.1.22 azure-core==1.24.0 diff --git a/src/azure-cli/requirements.py3.windows.txt b/src/azure-cli/requirements.py3.windows.txt index 336e24659c9..297685d471b 100644 --- a/src/azure-cli/requirements.py3.windows.txt +++ b/src/azure-cli/requirements.py3.windows.txt @@ -5,7 +5,7 @@ asn1crypto==0.24.0 azure-appconfiguration==1.1.1 azure-batch==12.0.0 azure-cli-core==2.38.0 -azure-cli-telemetry==1.0.6 +azure-cli-telemetry==1.0.7 azure-cli==2.38.0 azure-common==1.1.22 azure-core==1.24.0