Skip to content

Commit

Permalink
Merge de29d21 into b353f90
Browse files Browse the repository at this point in the history
  • Loading branch information
dougthor42 committed Jan 11, 2019
2 parents b353f90 + de29d21 commit e809a05
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,8 @@


## Unreleased
+ [BREAKING] Updated REST API: "add" and "get" changed to "data". The HTTP
method will be used to determine adding or retrieving.
+ Implemented switching between sequential and time-series data. (#8).


Expand Down
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -130,7 +130,7 @@ To send in data using the HTTP POST method:
curl --data '{"metric": "foo.bar", "value": 52.88, "time": '${date +%s}'}' \
--header "Content-Type: application/json" \
--request POST \
http://$SERVER/api/v1/add`
http://$SERVER/api/v1/data`
```

Or via UDP:
Expand All @@ -154,5 +154,5 @@ metric shows you the graph of historical data.
You can also export the data as JSON by sending an HTTP GET request:

```
curl http://$SERVER/api/v1/get/$METRIC_NAME
curl http://$SERVER/api/v1/data/$METRIC_NAME
```
10 changes: 6 additions & 4 deletions add_data.sh
@@ -1,18 +1,20 @@
#! /bin/bash

PORT=5000
URL="http://localhost:$PORT/api/v1"
URL="http://localhost:$PORT"
API="$URL/api/v1/data"
C_TYPE="Content-Type: application/json"

for run in {1..10}
do
curl -d '{"metric":"foo.bar", "value":'`shuf -i 1-100 -n 1`'}' -H "Content-Type: application/json" -X POST "$URL"/add;
curl -d '{"metric":"foo.bar", "value":'`shuf -i 1-100 -n 1`'}' -H "$C_TYPE" -X POST "$API";
sleep 0.1;
done;
sleep 1;
curl -d '{"metric":"foo.bar", "value":'`shuf -i 1-100 -n 1`', "time":'`date +%s`'}' -H "Content-Type: application/json" -X POST "$URL"/add;
curl -d '{"metric":"foo.bar", "value":'`shuf -i 1-100 -n 1`', "time":'`date +%s`'}' -H "$C_TYPE" -X POST "$API";

for run in {1..5}
do
curl -d '{"metric":"baz", "value":'`shuf -i 1-100 -n 1`'}' -H "Content-Type: application/json" -X POST "$URL"/add;
curl -d '{"metric":"baz", "value":'`shuf -i 1-100 -n 1`'}' -H "$C_TYPE" -X POST "$API";
sleep 0.3;
done;
6 changes: 3 additions & 3 deletions src/trendlines/routes.py
Expand Up @@ -51,8 +51,8 @@ def plot(metric=None):
return render_template('trendlines/plot.html', name=metric, data=data)


@api.route("/api/v1/add", methods=['POST'])
def add():
@api.route("/api/v1/data", methods=['POST'])
def post_datapoint():
"""
Add a new value and possibly a metric if needed.
Expand Down Expand Up @@ -82,7 +82,7 @@ def add():


@api.route("/api/v1/<metric>", methods=["GET"])
@api.route("/api/v1/get/<metric>", methods=["GET"])
@api.route("/api/v1/data/<metric>", methods=["GET"])
def get_data_as_json(metric):
"""
Return data for a given metric as JSON.
Expand Down
10 changes: 5 additions & 5 deletions tests/test_routes.py
Expand Up @@ -36,20 +36,20 @@ def test_plot_with_data(client, populated_db):

def test_api_add(client):
data = {"metric": "test", "value": 10}
rv = client.post("/api/v1/add", json=data)
rv = client.post("/api/v1/data", json=data)
assert rv.status_code == 201
assert b"Added DataPoint to Metric" in rv.data


def test_api_add_with_missing_key(client):
data = {"value": 10}
rv = client.post("/api/v1/add", json=data)
rv = client.post("/api/v1/data", json=data)
assert rv.status_code == 400
assert b"Missing required key. Required keys are:" in rv.data


def test_api_get_data_as_json(client, populated_db):
rv = client.get("/api/v1/get/foo")
rv = client.get("/api/v1/data/foo")
assert rv.status_code == 200
assert rv.is_json
d = rv.get_json()['rows']
Expand All @@ -59,7 +59,7 @@ def test_api_get_data_as_json(client, populated_db):


def test_api_get_data_as_json_metric_not_found(client):
rv = client.get("/api/v1/get/missing")
rv = client.get("/api/v1/data/missing")
assert rv.status_code == 404
assert rv.is_json
d = rv.get_json()
Expand All @@ -69,7 +69,7 @@ def test_api_get_data_as_json_metric_not_found(client):


def test_api_get_data_as_json_no_data_for_metric(client, populated_db):
rv = client.get("/api/v1/get/empty_metric")
rv = client.get("/api/v1/data/empty_metric")
assert rv.status_code == 404
assert rv.is_json
d = rv.get_json()
Expand Down

0 comments on commit e809a05

Please sign in to comment.