MCPcopy Create free account
hub / github.com/Flagsmith/flagsmith / get_organisation_metrics

Function get_organisation_metrics

api/platform_hub/services.py:165–363  ·  view source on GitHub ↗
(
    organisations: QuerySet[Organisation],
)

Source from the content-addressed store, hash-verified

163
164
165def get_organisation_metrics(
166 organisations: QuerySet[Organisation],
167) -> list[OrganisationMetricsData]:
168 cutoff_30d = timezone.now() - timedelta(days=30)
169 org_ids = set(organisations.values_list("id", flat=True))
170
171 orgs = organisations.prefetch_related(
172 "projects__environments",
173 "users",
174 ).order_by("id")
175
176 env_ids_by_org = _get_env_ids_for_orgs(organisations)
177 all_env_ids = [eid for eids in env_ids_by_org.values() for eid in eids]
178
179 # API usage data per org (30d/60d/90d)
180 org_usage_30d: dict[int, int] = {}
181 org_usage_60d: dict[int, int] = {}
182 org_usage_90d: dict[int, int] = {}
183 env_usage: dict[int, dict[str, int]] = {}
184 env_usage_30d: dict[int, dict[str, int]] = {}
185
186 if settings.USE_POSTGRES_FOR_ANALYTICS:
187 env_usage = _get_api_usage_for_envs_postgres(all_env_ids, days=90)
188 env_usage_30d = _get_api_usage_for_envs_postgres(all_env_ids, days=30)
189 env_usage_60d = _get_api_usage_for_envs_postgres(all_env_ids, days=60)
190
191 for oid, eids in env_ids_by_org.items():
192 org_usage_30d[oid] = sum(
193 sum(env_usage_30d.get(eid, {}).values()) for eid in eids
194 )
195 org_usage_60d[oid] = sum(
196 sum(env_usage_60d.get(eid, {}).values()) for eid in eids
197 )
198 org_usage_90d[oid] = sum(
199 sum(env_usage.get(eid, {}).values()) for eid in eids
200 )
201 elif settings.INFLUXDB_TOKEN:
202 org_usage_30d = _get_api_usage_for_orgs_influx(org_ids, days=30)
203 org_usage_60d = _get_api_usage_for_orgs_influx(org_ids, days=60)
204 org_usage_90d = _get_api_usage_for_orgs_influx(org_ids, days=90)
205 else:
206 logger.warning("no-analytics-database-configured")
207
208 # Allowed API calls per org
209 sub_caches = {
210 sc.organisation_id: sc
211 for sc in OrganisationSubscriptionInformationCache.objects.filter(
212 organisation__in=organisations,
213 )
214 }
215
216 # Flag counts per org
217 flag_counts = dict(
218 Feature.objects.filter(project__organisation__in=organisations)
219 .values("project__organisation_id")
220 .annotate(count=Count("id"))
221 .values_list("project__organisation_id", "count")
222 )

Callers

nothing calls this directly

Calls 12

ProjectMetricsDataClass · 0.90
_get_env_ids_for_orgsFunction · 0.85
setFunction · 0.50
getMethod · 0.45
allMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…