Generate random metrics values for datasets.
Source code in src/apps/core/management/commands/update_metrics.py
| def fake_metrics(self, identifiers: List[str]) -> int:
"""Generate random metrics values for datasets."""
self.stdout.write("Generating fake metrics")
factory.random.reseed_random(timezone.now())
existing_datasets = {
str(d["id"]) for d in Dataset.objects.filter(id__in=identifiers).values("id")
}
metrics = []
for identifier in identifiers:
if identifier not in existing_datasets:
self.stderr.write(f"Dataset not found: {identifier}")
continue
metrics.append(factories.DatasetMetricsFactory.build(dataset_id=identifier))
instances = DatasetMetrics.all_objects.bulk_create(
metrics,
update_conflicts=True,
update_fields=DatasetMetrics.metrics_fields,
unique_fields=["dataset"],
)
return len(instances)
|