Skip to content

[ON HOLD] Fetch Data Statistics #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
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
7 changes: 7 additions & 0 deletions contrib/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@
python manage.py migrate --noinput # Apply database migrations
python manage.py collectstatic --clear --noinput # Collect static files

# recreate the cached static json files
python manage.py cache_static_json_data --interval 5m
python manage.py cache_static_json_data --interval 1h
python manage.py cache_static_json_data --interval 24h

# Prepare log files and start outputting logs to stdout
touch /src/logs/celery.log
touch /src/logs/gunicorn.log
touch /src/logs/access.log
tail -n 0 -f /src/logs/*.log &

#purge all configured task queues
celery -A sensorsafrica purge
celery -A sensorsafrica beat -l info &> /src/logs/celery.log &
celery -A sensorsafrica worker --hostname=$DOKKU_APP_NAME -l info &> /src/logs/celery.log &
celery -A sensorsafrica flower --basic_auth=$SENSORSAFRICA_FLOWER_ADMIN_USERNAME:$SENSORSAFRICA_FLOWER_ADMIN_PASSWORD &> /src/logs/celery.log &
Expand Down
52 changes: 1 addition & 51 deletions sensorsafrica/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.utils.html import format_html
from django.conf.urls import include, url
from django.template.response import TemplateResponse
from .api.models import LastActiveNodes, SensorDataStat, City
from .api.models import LastActiveNodes, City
from django.db.models import Q

from feinstaub.sensors.admin import (
Expand Down Expand Up @@ -78,56 +78,6 @@ def save_related(self, request, form, formsets, change):
pass


@admin.register(SensorDataStat)
class SensorDataStatAdmin(admin.ModelAdmin):
readonly_fields = [
"node",
"sensor",
"location",
"city_slug",
"value_type",
"average",
"maximum",
"minimum",
"timestamp",
]
search_fields = ["city_slug", "value_type"]
list_display = [
"node",
"sensor",
"location",
"city_slug",
"value_type",
"average",
"maximum",
"minimum",
"timestamp",
"created",
"modified",
]
list_filter = ["timestamp", "node", "sensor", "location"]

def get_actions(self, request):
actions = super(SensorDataStatAdmin, self).get_actions(request)
del actions["delete_selected"]
return actions

def has_add_permission(self, request):
return False

def has_delete_permission(self, request, obj=None):
return False

def save_model(self, request, obj, form, change):
pass

def delete_model(self, request, obj):
pass

def save_related(self, request, form, formsets, change):
pass


@admin.register(City)
class CityAdmin(admin.ModelAdmin):
search_fields = ["slug", "name", "country"]
Expand Down
48 changes: 12 additions & 36 deletions sensorsafrica/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@


class City(TimeStampedModel):
slug = models.CharField(max_length=255, db_index=True, null=False, blank=False)
name = models.CharField(max_length=255, db_index=True, null=False, blank=False)
country = models.CharField(max_length=255, db_index=True, null=False, blank=False)
location = models.CharField(max_length=255, db_index=True, null=False, blank=False)
latitude = models.DecimalField(max_digits=14, decimal_places=11, null=True, blank=True)
longitude = models.DecimalField(max_digits=14, decimal_places=11, null=True, blank=True)
slug = models.CharField(
max_length=255, db_index=True, null=False, blank=False)
name = models.CharField(
max_length=255, db_index=True, null=False, blank=False)
country = models.CharField(
max_length=255, db_index=True, null=False, blank=False)
location = models.CharField(
max_length=255, db_index=True, null=False, blank=False)
latitude = models.DecimalField(
max_digits=14, decimal_places=11, null=True, blank=True)
longitude = models.DecimalField(
max_digits=14, decimal_places=11, null=True, blank=True)

class Meta:
verbose_name_plural = "Cities"
Expand All @@ -20,36 +26,6 @@ def save(self, *args, **kwargs):
return super(City, self).save(*args, **kwargs)


class SensorDataStat(TimeStampedModel):
node = models.ForeignKey(Node)
sensor = models.ForeignKey(Sensor)
location = models.ForeignKey(SensorLocation)

city_slug = models.CharField(max_length=255, db_index=True, null=False, blank=False)
value_type = models.CharField(max_length=255, db_index=True, null=False, blank=False)

average = models.FloatField(null=False, blank=False)
maximum = models.FloatField(null=False, blank=False)
minimum = models.FloatField(null=False, blank=False)

# Number of data points averaged
sample_size = models.IntegerField(null=False, blank=False)
# Last datetime of calculated stats
last_datetime = models.DateTimeField()

timestamp = models.DateTimeField()

def __str__(self):
return "%s %s %s avg=%s min=%s max=%s" % (
self.timestamp,
self.city_slug,
self.value_type,
self.average,
self.minimum,
self.maximum,
)


class LastActiveNodes(TimeStampedModel):
node = models.ForeignKey(Node)
location = models.ForeignKey(SensorLocation)
Expand Down
2 changes: 1 addition & 1 deletion sensorsafrica/api/v2/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

data_router = routers.DefaultRouter()

data_router.register(r"", SensorDataStatView)
data_router.register(r"", SensorDataStatView, basename="sensor_data_stat_view")

city_router = routers.DefaultRouter()

Expand Down
37 changes: 29 additions & 8 deletions sensorsafrica/api/v2/serializers.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
from rest_framework import serializers


class SensorDataStatSerializer(serializers.Serializer):
average = serializers.FloatField()
minimum = serializers.FloatField()
maximum = serializers.FloatField()
value_type = serializers.CharField(max_length=200)
start_datetime = serializers.DateTimeField()
end_datetime = serializers.DateTimeField()
city_slug = serializers.CharField(max_length=200)
class RawSensorDataStatSerializer(serializers.Serializer):
average = serializers.SerializerMethodField()
minimum = serializers.SerializerMethodField()
maximum = serializers.SerializerMethodField()
value_type = serializers.SerializerMethodField()
start_datetime = serializers.SerializerMethodField()
end_datetime = serializers.SerializerMethodField()
city_name = serializers.SerializerMethodField()

def get_city_name(self, obj):
return obj[0]

def get_start_datetime(self, obj):
return obj[1]

def get_end_datetime(self, obj):
return obj[2]

def get_average(self, obj):
return obj[3]

def get_minimum(self, obj):
return obj[4]

def get_maximum(self, obj):
return obj[5]

def get_value_type(self, obj):
return obj[6]


class CitySerializer(serializers.Serializer):
Expand Down
Loading