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:

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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:

  1. SourceSerializer: Serializes the Source model.

  2. MeasureSerializer: Serializes the Measure model, including the creation of new Measure instances.

  3. ChannelSerializer: Serializes the Channel model, including the creation of new Channel instances.

  4. ChunkSerializer: Serializes the Chunk model, including the creation of new Chunk instances.

  5. DictOrListField: Is a custom serializer field that can serialize both lists and dictionaries.

  6. TimestampField: Is a custom serializer field for timestamps. If the input is a float, it converts it to a datetime iso format string.

  7. 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:

  1. 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.

  2. SourceViewSet: Handles the CRUD operations for the Source model. It inherits from CustomCreateViewSet and viewsets.ModelViewSet.

  3. MeasureViewSet: Handles the CRUD operations for the Measure model. It inherits from CustomCreateViewSet and viewsets.ModelViewSet.

  4. ChannelViewSet: Handles the CRUD operations for the Channel model. It inherits from CustomCreateViewSet and viewsets.ModelViewSet.

  5. ChunkViewSet: Handles the CRUD operations for the Chunk model. It inherits from CustomCreateViewSet and viewsets.ModelViewSet.

  6. TimeSerieViewSet: Handles the CRUD operations for the TimeSerie model. It inherits from CustomCreateViewSet and viewsets.ModelViewSet. It has an additional list 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:

  1. /source: Handles requests related to the Source model, with the associated view being SourceViewSet.

  2. /measure: Handles requests related to the Measure model, with the associated view being MeasureViewSet.

  3. /channel: Handles requests related to the Channel model, with the associated view being ChannelViewSet.

  4. /timeserie: Handles requests related to the TimeSerie model, with the associated view being TimeSerieViewSet.

  5. /chunk: Handles requests related to the Chunk model, with the associated view being ChunkViewSet.

Additionally, the API has an authentication route configured at /api-auth/.