Skip to content

Commit e0a6ace

Browse files
author
Simon Prickett
committed
Finished the server README.
1 parent 31b1579 commit e0a6ace

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

server/README.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,46 @@ With a really big dataset, we'd want to do something better than this. At minim
8989

9090
#### Getting the Metadata for a Specific Image
9191

92-
TODO
92+
This route returns all of the metadata about a specific image from Redis. That means every field in the image's Redis Hash except for the one holding the actual image data.
93+
94+
One approach to getting all the fields except one would be to just get all of them with the `HGETALL` command, then discard what we don't want in the Flask/Python layer. As the field we don't want contains an entire image and could be several megabytes in size... this is wasteful and causes a lot of unnecessary work on the Redis server and data transfer bandwidth between the Redis server and Flask/Python application.
95+
96+
There isn't an "all except" command or variant of `HGETALL`, so instead I'm using the `HMGET command`, passing it a list of every field in the hash except the image data one. This isn't ideal, as if the capture script added more meta data in future I have to adjust the code here to read it and pass it to the front end, but it's what works!
97+
98+
```python
99+
IMAGE_DATA_FIELD_NAME = "image_data"
100+
IMAGE_MIME_TYPE_FIELD_NAME = "mime_type"
101+
IMAGE_TIMESTAMP_FIELD_NAME = "timestamp"
102+
IMAGE_META_DATA_FIELDS = [
103+
IMAGE_TIMESTAMP_FIELD_NAME,
104+
IMAGE_MIME_TYPE_FIELD_NAME
105+
# Anything else that is captured on the Pi can go here.
106+
]
107+
...
108+
109+
@app.route(f"/{API_ROUTE_PREFIX}/data/<image_id>")
110+
def get_image_data(image_id):
111+
# Look for the image meta data in Redis.
112+
image_meta_data = redis_client.hmget(f"{IMAGE_KEY_PREFIX}:{image_id}", IMAGE_META_DATA_FIELDS)
113+
```
114+
115+
If the image isn't found, we'll get nothing back and should return a 404 to the front end:
116+
117+
```python
118+
if image_meta_data[0] is None:
119+
return f"Image {image_id} not found.", 404
120+
```
121+
122+
If we did get data, we convert it to a Python dictionary, decoding the String values to UTF-8 and returning the completed dictionary. Flask will return this as a JSON object response to the front end:
123+
124+
```python
125+
data_dict = dict()
126+
data_dict[IMAGE_TIMESTAMP_FIELD_NAME] = image_meta_data[0].decode(STRING_ENCODING)
127+
data_dict[IMAGE_MIME_TYPE_FIELD_NAME] = image_meta_data[1].decode(STRING_ENCODING)
128+
return data_dict
129+
```
130+
131+
This could also probably be done more dynamically with less hard coding of field names, but it works for this demo!
93132

94133
#### Getting the Binary Data for a Specific Image
95134

0 commit comments

Comments
 (0)