You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
2.6 KiB
74 lines
2.6 KiB
import logging |
|
logger = logging.getLogger(__name__) |
|
|
|
from django.contrib.auth.models import User |
|
from rest_framework import viewsets, views, mixins |
|
from rest_framework import permissions |
|
from rest_framework.decorators import action |
|
from rest_framework.response import Response |
|
from apiserver.api import serializers, models |
|
|
|
from datetime import datetime, timezone |
|
|
|
Base = viewsets.GenericViewSet |
|
List = mixins.ListModelMixin |
|
Retrieve = mixins.RetrieveModelMixin |
|
Create = mixins.CreateModelMixin |
|
Update = mixins.UpdateModelMixin |
|
Destroy = mixins.DestroyModelMixin |
|
|
|
|
|
class UserViewSet(viewsets.ModelViewSet): |
|
queryset = User.objects.all().order_by('-date_joined') |
|
serializer_class = serializers.UserSerializer |
|
permission_classes = [permissions.IsAuthenticated] |
|
|
|
class DataViewSet(Base, List, Retrieve): |
|
@action(detail=False, methods=['post']) |
|
def push(self, request): |
|
miner_data = request.data['miner_data'] |
|
cooler_data = request.data['cooler_data'] |
|
|
|
for miner_id, miner in miner_data.items(): |
|
time = miner['summary'][0]['STATUS'][0]['When'] |
|
|
|
models.MinerData.objects.update_or_create( |
|
time=datetime.fromtimestamp(time, tz=timezone.utc), |
|
miner_id=miner_id, |
|
defaults=dict( |
|
json_version=2, |
|
summary=miner['summary'][0]['SUMMARY'][0], |
|
fans=miner['fans'][0]['FANS'], |
|
devdetails=miner['devdetails'][0]['DEVDETAILS'], |
|
version=miner['version'][0]['VERSION'][0], |
|
devs=miner['devs'][0]['DEVS'], |
|
config=miner['config'][0]['CONFIG'][0], |
|
coin=miner['coin'][0]['COIN'][0], |
|
pools=miner['pools'][0]['POOLS'], |
|
tunerstatus=miner['tunerstatus'][0]['TUNERSTATUS'][0], |
|
temps=miner['temps'][0]['TEMPS'], |
|
), |
|
) |
|
|
|
time = cooler_data['time'] |
|
cooler_id = cooler_data['cooler_id'] |
|
|
|
models.CoolerData.objects.update_or_create( |
|
time=datetime.fromtimestamp(time, tz=timezone.utc), |
|
cooler_id=cooler_id, |
|
defaults=dict( |
|
tub_temp=cooler_data['tub_temp'], |
|
rad_temp=cooler_data['rad_temp'], |
|
max_temp=cooler_data['max_temp'], |
|
fan=cooler_data['fan'], |
|
pump=cooler_data['pump'], |
|
), |
|
) |
|
|
|
logging.info('Added {} miner data points from cooler {}.'.format( |
|
len(miner_data), |
|
cooler_id, |
|
)) |
|
|
|
return Response(200) |
|
|
|
|