|
6 | 6 |
|
7 | 7 | import insights_analytics_collector as base
|
8 | 8 |
|
9 |
| -# from django.core.serializers.json import DjangoJSONEncoder |
| 9 | +from django.core.serializers.json import DjangoJSONEncoder |
10 | 10 | # from awx.conf.license import get_license
|
11 | 11 | # from awx.main.models import Job
|
12 | 12 | # from awx.main.access import access_registry
|
13 | 13 | # from rest_framework.exceptions import PermissionDenied
|
14 | 14 | from metrics_utility.automation_controller_billing.package import Package
|
15 |
| -# from awx.main.utils import datetime_hook |
| 15 | +from awx.main.utils import datetime_hook |
16 | 16 | from awx.main.utils.pglock import advisory_lock
|
17 | 17 |
|
18 | 18 | logger = logging.getLogger('awx.main.analytics')
|
@@ -107,44 +107,33 @@ def _pg_advisory_lock(self, key, wait=False):
|
107 | 107 | yield lock
|
108 | 108 |
|
109 | 109 | def _last_gathering(self):
|
110 |
| - # TODO: fill in later, when integrated with consumption based billing in Controller |
111 |
| - |
112 |
| - # return settings.AUTOMATION_ANALYTICS_LAST_GATHER |
113 |
| - return {} |
| 110 | + # Not needed in this implementation, but we need to define an abstract method |
| 111 | + pass |
114 | 112 |
|
115 | 113 | def _load_last_gathered_entries(self):
|
116 |
| - # TODO: fill in later, when integrated with consumption based billing in Controller |
| 114 | + # We are reusing Settings used by Analytics, so we don't have to backport changes into analytics |
| 115 | + # We can safely do this, by making sure we use the same lock as Analytics, before we persist |
| 116 | + # these settings. |
| 117 | + from awx.conf.models import Setting |
117 | 118 |
|
118 |
| - # from awx.conf.models import Setting |
| 119 | + last_entries = Setting.objects.filter(key='AUTOMATION_ANALYTICS_LAST_ENTRIES').first() |
| 120 | + last_gathered_entries = json.loads((last_entries.value if last_entries is not None else '') or '{}', object_hook=datetime_hook) |
| 121 | + return last_gathered_entries |
119 | 122 |
|
120 |
| - # last_entries = Setting.objects.filter(key='AUTOMATION_ANALYTICS_LAST_ENTRIES').first() |
121 |
| - # last_gathered_entries = json.loads((last_entries.value if last_entries is not None else '') or '{}', object_hook=datetime_hook) |
122 |
| - # return last_gathered_entries |
123 |
| - |
124 |
| - return {} |
| 123 | + def _gather_finalize(self): |
| 124 | + """Persisting timestamps (manual/schedule mode only)""" |
| 125 | + if self.is_shipping_enabled(): |
| 126 | + # We need to wait on analytics lock, to update the last collected timestamp settings |
| 127 | + # so we don't clash with analytics job collection. |
| 128 | + with self._pg_advisory_lock("gather_analytics_lock", wait=True) as acquired: |
| 129 | + # We need to load fresh settings again as we're obtaning the lock, since |
| 130 | + # Analytics job could have changed this on the background and we'd be resetting |
| 131 | + # the Analytics values here. |
| 132 | + self._load_last_gathered_entries() |
| 133 | + self._update_last_gathered_entries() |
125 | 134 |
|
126 | 135 | def _save_last_gathered_entries(self, last_gathered_entries):
|
127 |
| - # TODO: fill in later, when integrated with consumption based billing in Controller |
128 |
| - |
129 |
| - # settings.AUTOMATION_ANALYTICS_LAST_ENTRIES = json.dumps(last_gathered_entries, cls=DjangoJSONEncoder) |
130 |
| - pass |
131 |
| - |
132 |
| - def _save_last_gather(self): |
133 |
| - # TODO: fill in later, when integrated with consumption based billing in Controller |
134 |
| - # from awx.main.signals import disable_activity_stream |
135 |
| - |
136 |
| - # with disable_activity_stream(): |
137 |
| - # if not settings.AUTOMATION_ANALYTICS_LAST_GATHER or self.gather_until > settings.AUTOMATION_ANALYTICS_LAST_GATHER: |
138 |
| - # # `AUTOMATION_ANALYTICS_LAST_GATHER` is set whether collection succeeds or fails; |
139 |
| - # # if collection fails because of a persistent, underlying issue and we do not set last_gather, |
140 |
| - # # we risk the collectors hitting an increasingly greater workload while the underlying issue |
141 |
| - # # remains unresolved. Put simply, if collection fails, we just move on. |
142 |
| - |
143 |
| - # # All that said, `AUTOMATION_ANALYTICS_LAST_GATHER` plays a much smaller role in determining |
144 |
| - # # what is actually collected than it used to; collectors now mostly rely on their respective entry |
145 |
| - # # under `last_entries` to determine what should be collected. |
146 |
| - # settings.AUTOMATION_ANALYTICS_LAST_GATHER = self.gather_until |
147 |
| - pass |
| 136 | + settings.AUTOMATION_ANALYTICS_LAST_ENTRIES = json.dumps(last_gathered_entries, cls=DjangoJSONEncoder) |
148 | 137 |
|
149 | 138 | @staticmethod
|
150 | 139 | def _package_class():
|
|
0 commit comments