Sensor log storage and queries
English translation
Use the device log structure to check whether storing the same key again creates a new record or changes the existing record.
Before practice
01 How should you store a measurement that arrives again?
Description
Use the device and measurement time as the key, and define a duplicate handling rule such as updating or ignoring.
02 Is having no data the same as an invalid request?
Description
Distinguish intervals with no data, which return empty results, from uninterpretable intervals, which are input errors.
Execution conditions
Uses Python's sqlite3; no DB server or account is required.
1. Store duplicate measurements only once
Starting conditions
The measurements for device m1 at s are 20,21,22,23. Update entries with the same device and time to the latest value.
Procedure
- Check SQLite's composite primary key and store the four provided measurements.
- Check the number of stored rows and the value of , and explain whether the duplicate handling rule is correct.
Results to retain
- Storage results and 1 duplicate handling example.
Items to record and points to check
- Input count
-
- Stored row count
-
- Duplicate key
-
- Final value
-
Points to check
- Did you distinguish measurement time from reception time?
- Did you use parameters instead of appending values directly to SQL strings?
2. Query by time interval and device
Starting conditions
Add with values 30,31,32 for device m2. Query only m1 for 1≤t≤2.
Procedure
- Write a query function that takes a device, start time, and end time as arguments.
- Test the specified interval and an interval with no values once each.
- Plot query results as time versus measurement.
Results to retain
- A query function, two query results, and a time series plot.
Items to record and points to check
- Device
-
- Query interval
-
- Number of rows
-
- Value range
-
Points to check
- Did you specify the sort order in SQL?
- Did you avoid replacing empty results with errors or a value of 0?
3. Connect sensors to an API
Starting conditions
Assume the HTTP API in the next unit calls the query function. The request is device=m1,start=0,end=2.
Procedure
- Define the field structure for passing query results as JSON.
- Define response rules that distinguish a nonexistent device from start>end.
- Identify the data and query rules to retain when switching to an external storage system such as DynamoDB.
Results to retain
- Two response examples for valid and invalid requests, and rules to retain when replacing storage.
Items to record and points to check
- Request
-
- Validation results
-
- Response fields
-
- Expected row count
-
Points to check
- Did you verify storage and query principles without an external service account?
- Did you preserve the meanings of device ID and measurement time in the API?
Official examples and data