Spaces:
Sleeping
Sleeping
Download database.py from arpy8/drishti-api: direct link, hf CLI and curl.
- Browser
- Download file 3.11 kB
-
https://huggingface.co/spaces/arpy8/drishti-api/resolve/main/database.py
- Command line
-
hf download hf://spaces/arpy8/drishti-api/database.py
-
curl -L -o database.py https://huggingface.co/spaces/arpy8/drishti-api/resolve/main/database.py
3.11 kB
| import os | |
| import uuid | |
| import queue | |
| import threading | |
| import time | |
| from dotenv import load_dotenv | |
| from pymongo.server_api import ServerApi | |
| from pymongo.mongo_client import MongoClient | |
| load_dotenv(r"./.env") | |
| # Create a thread-safe queue | |
| update_queue = queue.Queue() | |
| # Flag to control the queue processor thread | |
| is_running = True | |
| def queue_processor(): | |
| """Process database updates from the queue""" | |
| while is_running: | |
| try: | |
| # Get an update task from the queue, wait up to 1 second | |
| task = update_queue.get(timeout=1) | |
| # Process the update | |
| uuid_value = task['uuid'] | |
| new_data = task['data'] | |
| result = user.update_one( | |
| {"uuid": str(uuid_value)}, | |
| {"$push": {"data": new_data}} | |
| ) | |
| # Mark the task as done | |
| update_queue.task_done() | |
| except queue.Empty: | |
| # No tasks in queue, continue waiting | |
| continue | |
| except Exception as e: | |
| print(f"Error processing queue item: {str(e)}") | |
| continue | |
| # Start the queue processor thread | |
| queue_processor_thread = threading.Thread(target=queue_processor, daemon=True) | |
| queue_processor_thread.start() | |
| uri = os.getenv("MONGO_STRING") | |
| client = MongoClient(uri, server_api=ServerApi("1")) | |
| db = client.get_database("drishti") | |
| user = db.user | |
| def register_user(name: str, mail: str, number: str, password): | |
| uuid_ = str(uuid.uuid4()) | |
| data = { | |
| "uuid": uuid_, | |
| "name": name, | |
| "email": mail, | |
| "phone_number": number, | |
| "password": password, | |
| "is_active": True, | |
| "data": [], | |
| } | |
| try: | |
| user.insert_one(data) | |
| return {"status": "success", "uuid": uuid_} | |
| except Exception as e: | |
| return {"status": "failed", "error": str(e)} | |
| def update_data_field(uuid_value, new_data): | |
| # Instead of updating directly, add to queue | |
| update_queue.put({ | |
| 'uuid': uuid_value, | |
| 'data': new_data | |
| }) | |
| return "Update queued successfully!" | |
| def fetch_data_field(uuid: str) -> dict: | |
| result = user.find_one({"uuid": str(uuid)}) | |
| if result: | |
| return result["data"] | |
| else: | |
| return {"status": "failed", "error": "No document matched the given UUID."} | |
| # Add cleanup function | |
| def cleanup(): | |
| global is_running | |
| is_running = False | |
| queue_processor_thread.join(timeout=5) | |
| if __name__ == "__main__": | |
| try: | |
| from pyperclip import copy | |
| rlt = fetch_data_field("95009d58-db41-4c40-8a5e-07f8821a525f") | |
| print(rlt) | |
| copy(rlt) | |
| finally: | |
| cleanup() | |
| # print(update_data_field("3cbb2a37-35b3-45f2-a4fd-499baa34880c", | |
| # { | |
| # "timestamp": "12:00", | |
| # "humidity": 50, | |
| # "temprature": 25, | |
| # "soil_moisture": 0.5 | |
| # } | |
| # ) | |
| # ) | |
| # print(register_user("Batu Khan", "batukhan99@gmail.com", "321654987", "abc123")) |