Skip to content
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

Grafana #340

Merged
merged 14 commits into from Apr 9, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 2 additions & 11 deletions README.md
Expand Up @@ -10,12 +10,9 @@

# Team Project repo

Heroku Production (master) URI: [https://nyu-mercury-prod.herokuapp.com/](https://nyu-mercury-prod.herokuapp.com/)

Heroku Staging (develop) URI: [https://nyu-mercury.herokuapp.com](https://nyu-mercury.herokuapp.com)

Heroku Dashboard: [https://dashboard.heroku.com/pipelines/35c0558f-127e-482b-8cdf-3f4d24464872](https://dashboard.heroku.com/pipelines/35c0558f-127e-482b-8cdf-3f4d24464872)
Heroku (master): [https://spring2020-cs-gy-9223-prod.herokuapp.com/](https://spring2020-cs-gy-9223-prod.herokuapp.com/)

*It's not automaticaly deployed for the time being. Please reach out to Dan Gopstein (dg2514@nyu.edu) or Yonguk Jeong (yj1679@nyu.edu) for access to the heroku instance.

# First time repo setup
1. From the root of the repo, run `scripts/setup.sh`. Activate your virtualenv first.
Expand Down Expand Up @@ -47,9 +44,3 @@ Run `python manage.py runserver` from the root of this Git repo

# HOWTO Run tests locally
Run `python manage.py test`

# HOWTO Install SymmetricDS
```
cd symmetricds
make install
```
12 changes: 12 additions & 0 deletions mercury/tests/test_measurement.py
Expand Up @@ -37,9 +37,21 @@ def fake_valid(res, raise_exception=True):
class TestMeasurement(TestCase):
def setUp(self) -> None:
self.post_url = "mercury:measurement"
self.post_url2 = "mercury:measurementWO"
self.uuid = "d81cac8d-26e1-4983-a942-1922e54a943d"
self.uuid2 = "d81cac8d-26e1-4983-a942-1922e54a943a"

def post_radio_data_wo_event(self):
response = self.client.post(
reverse(self.post_url2),
data={
"sensor_id": 1,
"values": {"power": "2", "speed": 1},
"date": datetime.datetime(2020, 2, 2, 20, 21, 22),
},
)
return response

def post_radio_data(self):
# POST sensor data to the measurement url
response = self.client.post(
Expand Down
5 changes: 5 additions & 0 deletions mercury/urls.py
Expand Up @@ -76,4 +76,9 @@
measurement.MeasurementView.as_view(),
name="measurement",
),
path(
"measurement/",
measurement.MeasurementWithoutEvent.as_view(),
name="measurementWO",
),
]
65 changes: 44 additions & 21 deletions mercury/views/measurement.py
Expand Up @@ -12,12 +12,42 @@ def build_error(str):
return json.dumps({"error": str})


def add_measurement(request, event):
json_data = request.data
if isinstance(json_data, str):
json_data = json.loads(json_data)

res = {"event_uuid": event.uuid}
key_map = {
"timestamp": "date",
"sensor_id": "sensor_id",
"value": "values",
}

for key, json_key in key_map.items():
if json_key not in json_data:
return Response(
build_error("Missing required params " + json_key),
status=status.HTTP_400_BAD_REQUEST,
)
res[key] = json_data[json_key]

serializer = AGMeasurementSerializer(data=res)
try:
serializer.is_valid(raise_exception=True)
serializer.save()
except serializers.ValidationError:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
else:
return Response(serializer.data, status=status.HTTP_201_CREATED)


class MeasurementView(APIView):
def post(self, request, event_uuid=None):
"""
The post receives sensor data through internet
Url example:
http://localhost:8000/radioreceiver/d81cac8d-26e1-4983-a942-1922e54a943d
http://localhost:8000/measurement/d81cac8d-26e1-4983-a942-1922e54a943d
Post Json Data Example
{
"sensor_id": 1,
Expand All @@ -38,26 +68,19 @@ def post(self, request, event_uuid=None):
build_error("Event uuid not found"), status=status.HTTP_404_NOT_FOUND
)

json_data = request.data
if isinstance(json_data, str):
json_data = json.loads(json_data)
return add_measurement(request, event)

res = {"event_uuid": event_uuid}
dic = {"timestamp": "date", "sensor_id": "sensor_id", "value": "values"}

for d in dic:
if json_data.get(dic[d]) is None:
return Response(
build_error("Missing required params " + dic[d]),
status=status.HTTP_400_BAD_REQUEST,
)
res[d] = json_data[dic[d]]

serializer = AGMeasurementSerializer(data=res)
class MeasurementWithoutEvent(APIView):
def post(self, request):
"""
TODO: fetch the active event
Now we use the first event in the db
"""
try:
serializer.is_valid(raise_exception=True)
serializer.save()
except serializers.ValidationError:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
else:
return Response(serializer.data, status=status.HTTP_201_CREATED)
events = AGEvent.objects.all()
event = events.first()
except AGEvent.DoesNotExist:
event = False

return add_measurement(request, event)