Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added metric/management/__init__.py
Empty file.
Empty file.
53 changes: 53 additions & 0 deletions metric/management/commands/create_dummy_metric_values.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import random
from datetime import timedelta

from django.core.management.base import BaseCommand
from django.utils import timezone

from app.models import App
from metric.models import Metric, MetricValue
from source.models import Source


class Command(BaseCommand):
help = "Crea MetricValues dummy para pruebas"

def handle(self, *args, **kwargs):
apps = App.objects.all()
metrics = Metric.objects.all()
sources = Source.objects.all()

if not apps.exists() or not metrics.exists():
self.stdout.write(self.style.ERROR("Debes tener apps y métricas creadas"))
return

created_count = 0

for app in apps:
for metric in metrics:
if metric.code == "average_rating":
for source in sources:
if (
source.code in ["itunes", "google_play"]
and metric in source.metrics.all()
):
for days_ago in range(30): # últimos 30 días
date = timezone.now() - timedelta(days=days_ago)
value = (
round(random.uniform(4.4, 4.5), 5)
if metric.value_type == "float"
else str(random.randint(1, 1000))
)

MetricValue.objects.create(
app=app,
metric=metric,
source=source,
value=str(value),
retrieved_at=date,
)
created_count += 1

self.stdout.write(
self.style.SUCCESS(f"✔ {created_count} MetricValues creados correctamente.")
)
19 changes: 19 additions & 0 deletions metric/migrations/0007_alter_metricvalue_retrieved_at.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 5.1.7 on 2025-05-20 14:28

import django.utils.timezone
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("metric", "0006_remove_metric_is_internal"),
]

operations = [
migrations.AlterField(
model_name="metricvalue",
name="retrieved_at",
field=models.DateTimeField(default=django.utils.timezone.now),
),
]
3 changes: 2 additions & 1 deletion metric/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.db import models
from django.utils import timezone

from app.models import App
from metric.constants.value_types import MetricValueType
Expand All @@ -19,7 +20,7 @@ class MetricValue(models.Model):
metric = models.ForeignKey(Metric, on_delete=models.CASCADE)
source = models.ForeignKey("source.Source", on_delete=models.CASCADE, null=True, blank=True)
value = models.CharField(max_length=255)
retrieved_at = models.DateTimeField(auto_now_add=True)
retrieved_at = models.DateTimeField(default=timezone.now)

def __str__(self):
return (
Expand Down
11 changes: 7 additions & 4 deletions metric/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

class MetricRepository:
def get_all(self):
return Metric.objects.all()
return Metric.objects.filter(id=2)
# return Metric.objects.all()

def get_by_id(self, pk):
return Metric.objects.get(id=pk)
Expand Down Expand Up @@ -33,7 +34,7 @@ def remove_sources(self, instance, sources_ids):

class MetricValueRepository:
def get_all(self):
return MetricValue.objects.all()
return MetricValue.objects.all().order_by("retrieved_at")

def get_by_id(self, pk):
return MetricValue.objects.get(id=pk)
Expand All @@ -51,6 +52,8 @@ def delete(self, instance):
instance.delete()

def get_by_app_and_metric(self, app_id, metric_id):
return MetricValue.objects.filter(app_id=app_id, metric_id=metric_id).select_related(
"source"
return (
MetricValue.objects.filter(app_id=app_id, metric_id=metric_id)
.select_related("source")
.order_by("retrieved_at")
)