-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CELE-119 feat: Add populate_db endpoint and connect it to ingestion s…
…cript
- Loading branch information
1 parent
15d329b
commit 5ca9fa6
Showing
9 changed files
with
193 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ cloud-harness/ | |
.vscode/ | ||
node_modules | ||
secret.json | ||
data/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
13 changes: 13 additions & 0 deletions
13
applications/visualizer/backend/api/authenticators/basic_auth_super_user.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from ninja.security import HttpBasicAuth | ||
from django.contrib.auth import authenticate as django_authenticate | ||
|
||
|
||
class BasicAuthSuperUser(HttpBasicAuth): | ||
def authenticate(self, request, username, password): | ||
# Authenticate user with Django's built-in authenticate function | ||
user = django_authenticate(request, username=username, password=password) | ||
if user and user.is_superuser: # Ensure the user is a superuser | ||
return user | ||
return None | ||
|
||
basic_auth_superuser = BasicAuthSuperUser() |
Empty file.
61 changes: 61 additions & 0 deletions
61
applications/visualizer/backend/api/decorators/streaming.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import asyncio | ||
import sys | ||
import threading | ||
from queue import Queue | ||
from functools import wraps | ||
from django.http import StreamingHttpResponse | ||
|
||
def with_stdout_streaming(func): | ||
""" | ||
A decorator that: | ||
- Runs the decorated function in a separate thread, | ||
- Captures anything it prints to stdout, | ||
- Streams that output asynchronously line-by-line as it's produced. | ||
""" | ||
@wraps(func) | ||
def wrapper(request, *args, **kwargs): | ||
q = Queue() | ||
|
||
def run_func(): | ||
# Redirect sys.stdout | ||
old_stdout = sys.stdout | ||
|
||
class QueueWriter: | ||
def write(self, data): | ||
if data: | ||
q.put(data) # Push data into the thread-safe queue | ||
|
||
def flush(self): | ||
pass # For compatibility with print | ||
|
||
sys.stdout = QueueWriter() | ||
|
||
try: | ||
func(request, *args, **kwargs) | ||
except Exception as e: | ||
q.put(f"Error: {e}\n") | ||
finally: | ||
# Signal completion | ||
q.put(None) | ||
sys.stdout = old_stdout | ||
|
||
# Run the function in a background thread | ||
t = threading.Thread(target=run_func) | ||
t.start() | ||
|
||
# Async generator to yield lines from the queue | ||
async def line_generator(): | ||
while True: | ||
line = await asyncio.to_thread(q.get) # Get item from thread-safe queue | ||
if line is None: # End signal | ||
break | ||
yield line | ||
await asyncio.sleep(0) # Yield control to event loop | ||
|
||
# Return a streaming response that sends data asynchronously | ||
response = StreamingHttpResponse(line_generator(), content_type="text/plain") | ||
response['Cache-Control'] = 'no-cache' | ||
response['X-Accel-Buffering'] = 'no' # Disable nginx buffering if using nginx | ||
return response | ||
|
||
return wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters