Skip to content

Commit c887383

Browse files
authored
CLI: use library collectors (#339)
* CLI: use library collectors use metrics_utility.library.collectors, via cli_* wrappers cli collectors get since, until, output params, passed to library, and used to specify collector output exposed all exisiting collectors in the cli limit_slicer becomes until_slicer, using until over now() full_sync removed, unused register description removed, unused Issue: AAP-66077 * collector output - DictOutput, DataframeOutput, CollectorOutput library collectors now get an output= param, defaulting to either `DataframeOutput`, where `output.sql(db, query)` returns a pandas dataframe, or to `DictOutput`, where `output.dict({...})` returns a dict. for the CLI calls, `CollectorOutput` gets passed instead, initialized with a temporary path for files, and outputs a filelist from output.sql, and a dict from output.dict. this allows preserving the csv streaming for the cli, while keeping the collector output a dataframe for anonymized * review fixups * claude fix tests * more fixups * unify on bool_from_env; fix 0 check * pytest approx - ensure no rel=1e-6 for timestamps (was a 28 minutes tolerance) * simplify the vcpu not started error flow * test fixups * make sure until puts this into the right day, but also always within the since/until interval * claude up test coverage * test_gather_ranges - update for until_slicer review changes
1 parent aa754e2 commit c887383

59 files changed

Lines changed: 2030 additions & 3083 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

metrics_utility/automation_controller_billing/collector.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from metrics_utility.automation_controller_billing.helpers import get_last_entries_from_db
1111
from metrics_utility.automation_controller_billing.package.factory import Factory as PackageFactory
12+
from metrics_utility.base.utils import bool_from_env
1213
from metrics_utility.library.lock import lock
1314
from metrics_utility.logger import logger
1415

@@ -28,6 +29,7 @@ def __init__(self, collection_type=base.Collector.SCHEDULED_COLLECTION, collecto
2829
# TODO: extract advisory lock name in the superclass and log message, so we can change it here and then use
2930
# this method from superclass
3031
# TODO: extract to superclass ability to push extra params into config.json
32+
# FIXME: subset is only used for tests, mock registered collectors instead?
3133
def gather(self, dest=None, subset=None, since=None, until=None, billing_provider_params=None):
3234
"""Entry point for gathering
3335
@@ -95,21 +97,20 @@ def _load_last_gathered_entries(self):
9597

9698
def _gather_finalize(self):
9799
"""Persisting timestamps (manual/schedule mode only)"""
98-
99-
disabled_str = os.getenv('METRICS_UTILITY_DISABLE_SAVE_LAST_GATHERED_ENTRIES', 'false')
100-
disabled = False
101-
if disabled_str and (disabled_str.lower() == 'true'):
102-
disabled = True
103-
104-
if self.ship and not disabled:
105-
# We need to wait on analytics lock, to update the last collected timestamp settings
106-
# so we don't clash with analytics job collection.
107-
with lock('gather_analytics_lock', wait=True, db=connection):
108-
# We need to load fresh settings again as we're obtaning the lock, since
109-
# Analytics job could have changed this on the background and we'd be resetting
110-
# the Analytics values here.
111-
self._load_last_gathered_entries()
112-
self._update_last_gathered_entries()
100+
if not self.ship:
101+
return
102+
103+
if bool_from_env('METRICS_UTILITY_DISABLE_SAVE_LAST_GATHERED_ENTRIES'):
104+
return
105+
106+
# We need to wait on analytics lock, to update the last collected timestamp settings
107+
# so we don't clash with analytics job collection.
108+
with lock('gather_analytics_lock', wait=True, db=connection):
109+
# We need to load fresh settings again as we're obtaning the lock, since
110+
# Analytics job could have changed this on the background and we'd be resetting
111+
# the Analytics values here.
112+
self._load_last_gathered_entries()
113+
self._update_last_gathered_entries()
113114

114115
def _save_last_gathered_entries(self, last_gathered_entries):
115116
settings.AUTOMATION_ANALYTICS_LAST_ENTRIES = json.dumps(last_gathered_entries, cls=DjangoJSONEncoder)

0 commit comments

Comments
 (0)