Django REST Framework implementation¶
This report provides an overview of the Time Series API implementation, which manages data related to sources, measures, channels, and time series. The API is built using the Django REST framework and follows a typical structure for a Django application.
Data Models¶
The TimeScaleDB App API interacts primarily with five models:
Source: This model represents a source of measures. Each source has a unique ID, name, location, device, protocol, version, description, and a timestamp indicating when it was created.
Measure: The Measure model represents a specific measure made by a source. Each measure has a label, name, description, and a reference to its source. A source can have multiple measures.
Channel: This model represents a specific channel of a measure. Each channel has a label, name, unit, sampling rate, description, count, and a reference to its measure. A measure can have multiple channels.
Chunk: The Chunk model represents a specific chunk of a measure. Each chunk has a label and a reference to its measure. A measure can have multiple chunks.
TimeSerie: This model represents a time series of data. Each record in the time series has a timestamp, a value, and references to a channel and a chunk.
Each of these models corresponds to a table in the database, and each attribute of a model corresponds to a field in the table. The relationships between the models (such as the ForeignKey fields) represent database relationships (such as foreign key constraints).
Serializers¶
Serializers are used to convert complex data types, such as Django models, into Python data types that can be rendered into JSON or other content types. The API has seven serializers:
SourceSerializer: Serializes the Source model.
MeasureSerializer: Serializes the Measure model, including the creation of new Measure instances.
ChannelSerializer: Serializes the Channel model, including the creation of new Channel instances.
ChunkSerializer: Serializes the Chunk model, including the creation of new Chunk instances.
DictOrListField: Is a custom serializer field that can serialize both lists and dictionaries.
TimestampField: Is a custom serializer field for timestamps. If the input is a float, it converts it to a datetime iso format string.
TimeserieSerializer: Serializes the TimeSerie model, including the creation of new TimeSerie instances.
Each of these classes and functions plays a critical role in handling API request and response data in the Timescaledbapp application.
Moreover, this module also includes a helper function, insert_batch, which allows to perform bulk create operation for a given model with a specified batch size. The default batch size is 1000.
Views¶
The API has five primary viewsets, one for each model:
CustomCreateViewSet: This is a base ViewSet to support custom create actions for models. The
create
method of this class uses the respective model’s serializer to create new instances of the model.SourceViewSet: Handles the CRUD operations for the Source model. It inherits from
CustomCreateViewSet
andviewsets.ModelViewSet
.MeasureViewSet: Handles the CRUD operations for the Measure model. It inherits from
CustomCreateViewSet
andviewsets.ModelViewSet
.ChannelViewSet: Handles the CRUD operations for the Channel model. It inherits from
CustomCreateViewSet
andviewsets.ModelViewSet
.ChunkViewSet: Handles the CRUD operations for the Chunk model. It inherits from
CustomCreateViewSet
andviewsets.ModelViewSet
.TimeSerieViewSet: Handles the CRUD operations for the TimeSerie model. It inherits from
CustomCreateViewSet
andviewsets.ModelViewSet
. It has an additionallist
method which returns all instances of the TimeSerie model or filters them according to the request.
Each of these classes plays a critical role in handling API requests and responses in the Timescaledbapp application.
URL Configuration¶
The API now has these endpoints configured:
/source
: Handles requests related to the Source model, with the associated view beingSourceViewSet
./measure
: Handles requests related to the Measure model, with the associated view beingMeasureViewSet
./channel
: Handles requests related to the Channel model, with the associated view beingChannelViewSet
./timeserie
: Handles requests related to the TimeSerie model, with the associated view beingTimeSerieViewSet
./chunk
: Handles requests related to the Chunk model, with the associated view beingChunkViewSet
.
Additionally, the API has an authentication route configured at /api-auth/
.