Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

add stream API #42

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

add stream API #42

wants to merge 7 commits into from

Conversation

preyunk
Copy link
Contributor

@preyunk preyunk commented Jan 29, 2024

Add stream API to stream data for a large number of results, this can be controlled by chunk_size query parameter, the server will sleep for 1 second when it has yieled chunk_size data. The server needs to sleep for 1 second since it needs time to gather the records (chunk_size).

Add stream API to stream data for a large number of results,
this can be controlled by chunk_size query parameter,
the server will sleep for 1 second when it has yieled `chunk_size` data.
The server needs to sleep for 1 second since it needs time to gather the records (chunk_size).

Signed-off-by: Priyank Singh <[email protected]>
@preyunk preyunk requested a review from piyusql January 29, 2024 15:33
@@ -3,14 +3,19 @@
# SPDX-License-Identifier: BSD-2-Clause

import json
import time
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// TODO remove this import

Copy link
Contributor

@piyusql piyusql left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix as per the reviews.

@@ -33,6 +38,29 @@ def create_django_model(request, db_name, app_label, model_name):
return JSONResponse(res, status=e.status_code)


@method_decorator(require_http_methods(['GET']), name='dispatch')
class StreamView(View):
STREAM_SEPERATOR = '$$'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A simple comma will also work to make every dict a row.

@@ -292,35 +294,55 @@ def query_has_properties(self):
return bool(set(self.params.fields).intersection(
self.model_config.get_properties()))

def _get_model_fields(self, row):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change the method name to _get_model_row_dict to make it more readable.

@@ -269,7 +269,9 @@ def _apply_opts(self):
self.qset = self.qset[:self.params.limit]
elif isinstance(value, list):
# handle values case where property is passed in fields
if qset_opt == 'values' and self.query_has_properties():
if qset_opt == 'values' and stream:
return self.yield_fields()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's reform this to make a unified approach b/w return and non-return for each method, in this case this is being a special case here to return generator.
One more point regrading count query which is being evaluated after values operator, if count option is passed, stream will beof no use and should be set to False.


def queryset(self):
def queryset(self, stream=False):
# construct Q object from dictionary
query = Query(self.params.filter)
if self.params.db_name:
self.qset = self.model_config.model.objects.using(
self.params.db_name).filter(query.Q)
else:
self.qset = self.model_config.model.objects.filter(query.Q)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handle the stream part using self.yqset

use stream as a query parameter in read, this gives the flexibility to
ignore stream if count or some other paramter is present

Signed-off-by: Priyank Singh <[email protected]>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants