Skip to content

Commit

Permalink
chore: update v1 route to only push sensors data, add statistics view
Browse files Browse the repository at this point in the history
  • Loading branch information
thepsalmist committed Oct 7, 2024
1 parent 6d28784 commit 46abd43
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 6 deletions.
2 changes: 0 additions & 2 deletions .env.template
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
SENSORSAFRICA_CELERY_SLACK_WEBHOOK=""
SENSORSAFRICA_CELERY_SLACK_WEBHOOK_FAILURES_ONLY=""
SENSORSAFRICA_DATABASE_URL=""
SENSORSAFRICA_DEBUG=""
SENSORSAFRICA_FLOWER_ADMIN_PASSWORD=""
Expand Down
6 changes: 5 additions & 1 deletion sensorsafrica/api/v1/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from rest_framework import routers

router = routers.DefaultRouter()
router.register(r"push-sensor-data", PostSensorDataView)
router.register(r"node", NodeView)
router.register(r"sensor", SensorView)
router.register(r"data", VerboseSensorDataView)
Expand All @@ -31,3 +30,8 @@
router.register(r"filter", FilterView, basename="filter")

api_urls = router.urls

push_sensor_data_router = routers.DefaultRouter()
push_sensor_data_router.register(r"push-sensor-data", PostSensorDataView)

push_sensor_data_urls = push_sensor_data_router.urls
2 changes: 2 additions & 0 deletions sensorsafrica/api/v2/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
SensorLocationsView,
SensorTypesView,
SensorsView,
StatisticsView,
meta_data,
)

Expand All @@ -22,6 +23,7 @@
router.register(r"locations", SensorLocationsView, basename="sensor-locations")
router.register(r"sensors", SensorsView, basename="sensors")
router.register(r"sensor-types", SensorTypesView, basename="sensor-types")
router.register(r"statistics", StatisticsView, basename="statistics")

api_urls = [
url(r"^", include(router.urls)),
Expand Down
39 changes: 38 additions & 1 deletion sensorsafrica/api/v2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from django.conf import settings
from django.utils import timezone
from django.db import connection
from django.db.models import ExpressionWrapper, F, FloatField, Max, Min, Sum, Avg, Q
from django.db.models import ExpressionWrapper, F, FloatField, Max, Min, Sum, Avg, Q, Count
from django.db.models.functions import Cast, TruncHour, TruncDay, TruncMonth
from django.utils.decorators import method_decorator
from django.utils.text import slugify
Expand Down Expand Up @@ -486,3 +486,40 @@ def get_queryset(self):
return SensorData.objects.filter(
sensor__public=True, modified__range=[startdate, now]
)


class StatisticsView(viewsets.ViewSet):
authentication_classes = [SessionAuthentication, TokenAuthentication]
permission_classes = [IsAuthenticated]

def list(self, request):
user_count = User.objects.aggregate(count=Count('id'))['count']
sensor_count = Sensor.objects.aggregate(count=Count('id'))['count']
sensor_data_count = SensorData.objects.aggregate(count=Count('id'))['count']
sensor_data_value_count = SensorDataValue.objects.aggregate(count=Count('id'))['count']
sensor_type_count = SensorType.objects.aggregate(count=Count('id'))['count']
sensor_type_list = list(SensorType.objects.order_by('uid').values_list('name', flat=True))
location_count = SensorLocation.objects.aggregate(count=Count('id'))['count']

stats = {
'user': {
'count': user_count,
},
'sensor': {
'count': sensor_count,
},
'sensor_data': {
'count': sensor_data_count,
},
'sensor_data_value': {
'count': sensor_data_value_count,
},
'sensor_type': {
'count': sensor_type_count,
'list': sensor_type_list,
},
'location': {
'count': location_count,
}
}
return Response(stats)
4 changes: 2 additions & 2 deletions sensorsafrica/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
from rest_framework.authtoken.views import obtain_auth_token
from rest_framework.documentation import include_docs_urls

from .api.v1.router import api_urls as sensors_api_v1
from .api.v1.router import push_sensor_data_urls
from .api.v2.router import api_urls as sensors_api_v2

urlpatterns = [
url(r"^$", RedirectView.as_view(url="/docs/", permanent=False)),
url(r"^admin/", admin.site.urls),
url(r"^v1/", include(sensors_api_v1)),
url(r"^v1/push-sensor-data/", include(push_sensor_data_urls)),
url(r"^v2/", include(sensors_api_v2)),
url(r"^get-auth-token/", obtain_auth_token),
url(r"^auth/", include("rest_framework.urls", namespace="rest_framework")),
Expand Down

0 comments on commit 46abd43

Please sign in to comment.