Return all counts tracked by this counter.
(self)
| 77 | return self.get_counts() |
| 78 | |
| 79 | def get_counts(self) -> Dict[str, Number]: |
| 80 | """Return all counts tracked by this counter.""" |
| 81 | now = time.time() |
| 82 | # TODO(b/144421838): use futures instead of blocking. |
| 83 | if self._parent and (now - self._last_sync_time) > self._time_delta: |
| 84 | with self._lock: |
| 85 | counts = _prefix_keys(self._counts, self._prefix) |
| 86 | # Reset the local counts, as they will be merged into the parent and the |
| 87 | # cache. |
| 88 | self._counts = {} |
| 89 | self._cache = self._parent.increment(**counts) |
| 90 | self._last_sync_time = now |
| 91 | |
| 92 | # Potentially prefix the keys in the counts dictionary. |
| 93 | counts = _prefix_keys(self._counts, self._prefix) |
| 94 | |
| 95 | # If there's no prefix make a copy of the dictionary so we don't modify the |
| 96 | # internal self._counts. |
| 97 | if not self._prefix: |
| 98 | counts = dict(counts) |
| 99 | |
| 100 | # Combine local counts with any parent counts. |
| 101 | for key, value in self._cache.items(): |
| 102 | counts[key] = counts.get(key, 0) + value |
| 103 | |
| 104 | if self._prefix and self._return_only_prefixed: |
| 105 | counts = dict([(key[len(self._prefix) + 1:], value) |
| 106 | for key, value in counts.items() |
| 107 | if key.startswith(f'{self._prefix}_')]) |
| 108 | return counts |
| 109 | |
| 110 | def save(self) -> Mapping[str, Mapping[str, Number]]: |
| 111 | return {'counts': self._counts, 'cache': self._cache} |