Skip to content

Poseidon/feature/state optimization and critical fixes#106

Open
Yasserelhaddar wants to merge 86 commits intofeature/103-paz-db-models-and-db-backend-refactorfrom
poseidon/feature/state-optimization-and-critical-fixes
Open

Poseidon/feature/state optimization and critical fixes#106
Yasserelhaddar wants to merge 86 commits intofeature/103-paz-db-models-and-db-backend-refactorfrom
poseidon/feature/state-optimization-and-critical-fixes

Conversation

@Yasserelhaddar
Copy link
Collaborator

@Yasserelhaddar Yasserelhaddar commented Jul 21, 2025

State Optimization, Camera Service Update and Critical Fixes

📋 Overview

This PR introduces comprehensive improvements to the Mindtrace application, including critical bug fixes, state management optimizations, database schema migration, and enhanced user experience.

🔧 Critical Fixes

🎥 Video Stream Error Prevention

  • Issue: Continuous error logging when cameras were deinitialized during video streaming
  • Fix: Enhanced video_stream function in hardware API to gracefully handle camera disconnection
  • Impact: Eliminates infinite error loops and improves service stability

🔐 Authentication & Authorization

  • Issue: JWT tokens not properly populating project assignments
  • Fix: Updated AuthService to correctly populate project_assignments from user projects
  • Impact: Proper role-based access control and project filtering

🗄️ Database Schema Consistency

  • Issue: Inconsistent password hashing (SHA-256 vs bcrypt)
  • Fix: Standardized on bcrypt hashing throughout the application
  • Impact: Secure and consistent authentication

🏗️ Architecture Improvements

📊 State Management Optimization

  • New Base Classes:

    • BaseManagementState: Common error/success handling
    • BasePaginationState: Consistent pagination patterns
    • BaseFilterState: Standardized filtering
    • BaseFormState: Common validation methods
    • BaseDialogState: Consistent dialog management
    • RoleBasedAccessMixin: Role-based access control
  • Optimized States:

    • OrganizationManagementState: Inherits from base classes
    • ProjectManagementState: Enhanced organization handling
    • UserManagementState: Improved pagination and filtering
    • CameraState: Better API integration and project filtering

🗃️ Database Schema Migration

  • Complete migration to Mindtrace ODM with Link relationships
  • Updated all models: Camera, Image, Model, ModelDeployment, Organization, Project, User
  • Enhanced repositories: Optimized queries and Link handling
  • Improved seeding: Comprehensive database initialization

🔌 Hardware API Refactoring

  • Service-based architecture: All endpoints in single app.py file
  • Removed unused route files: Simplified maintenance
  • UTC datetime handling: Fixed deprecation warnings
  • Enhanced error handling: Better exception management

🎨 UI/UX Enhancements

📱 Component Improvements

  • Project dialogs: Better organization selection
  • Admin key display: Fixed field name references
  • Camera configurator: Removed unnecessary toggle, added assignment summary
  • User assignment: Improved project loading and assignment flow

📊 Data Models

  • Flexible datetime handling: Support for both string and datetime objects
  • Enhanced validation: Better error messages and type checking
  • Consistent patterns: Standardized data structures

📁 Files Changed

🔧 Hardware API

  • mindtrace/hardware/mindtrace/hardware/api/app.py - Video stream fix and service architecture
  • Removed unused route files (8 files deleted)

🗄️ Database Layer

  • All model files updated for Mindtrace ODM
  • All repository files optimized
  • Enhanced seeding and initialization

🎯 Frontend States

  • poseidon/state/base.py - New base classes
  • All state files optimized to use base classes
  • Enhanced data models and validation

🎨 Frontend Components

  • Updated popups and dialogs
  • Improved camera configurator
  • Better component exports

🚀 Setup Instructions

Prerequisites

  • Docker installed and running
  • Python 3.12+ with uv package manager
  • MongoDB Docker container

1. Database Setup

# Start MongoDB container
docker run -d --name mindtrace-mongodb \
  -p 27017:27017 \
  -e MONGO_INITDB_ROOT_USERNAME=admin \
  -e MONGO_INITDB_ROOT_PASSWORD=password \
  mongo:latest

2. Hardware API Service

# Navigate to hardware directory
cd mindtrace/hardware
# Run the hardware API service
uv run uvicorn mindtrace.hardware.api.app:app --reload --host 0.0.0.0 --port 8001

3. Frontend Application

# Open new terminal and navigate to UI directory
cd mindtrace/ui/mindtrace/ui/poseidon

# Run the Reflex application
uv run reflex run

4. Database Seeding (Optional)

# Navigate to UI directory
cd mindtrace/ui/mindtrace/ui/poseidon

# Run database seeding
uv run python seed_db.py

🧪 Testing

Camera Operations

  1. Discover cameras: Hardware API should discover available cameras
  2. Initialize camera: Should work without errors
  3. Video streaming: Should start/stop cleanly without continuous errors
  4. Deinitialize camera: Should stop video stream gracefully

User Management

  1. Login: Should work with bcrypt passwords
  2. Project access: Should respect role-based permissions
  3. User assignment: Should work efficiently without slowness

Project Management

  1. Create projects: Should work for both admin and super admin
  2. Organization assignment: Should auto-assign for admins, allow selection for super admins
  3. Camera assignment: Should work properly with project filtering

🔍 Key Benefits

  • Performance: Optimized database queries and async operations
  • Maintainability: Standardized patterns and reduced code duplication
  • Reliability: Critical bug fixes prevent service instability
  • User Experience: Improved UI components and error handling
  • Security: Consistent authentication and authorization
  • Scalability: Better state management architecture

📊 Metrics

  • Files changed: 50+ files optimized
  • Code reduction: ~1000+ lines of redundant code removed
  • New features: 6 base classes for consistent patterns
  • Bug fixes: 3 critical issues resolved
  • Performance: Significant improvement in user assignment operations

🎯 Next Steps

After merging this PR:

  1. Monitor hardware API logs for any remaining video stream issues
  2. Test user management operations with different roles
  3. Verify camera assignment and project filtering
  4. Consider additional UI improvements based on user feedback

Branch: poseidon/feature/state-optimization-and-critical-fixes
Target: poseidon/dev
Type: Feature/Enhancement

- Removed individual route modules as we now use service-based architecture
- All endpoints are now defined directly in app.py using CameraAPIService
- Simplifies maintenance and reduces code duplication
- Routes were: __init__.py, backends.py, cameras.py, capture.py,
  config_async.py, config_persistence.py, config_sync.py, network.py
- Created BaseManagementState with common error/success handling
- Added BasePaginationState for consistent pagination patterns
- Implemented BaseFilterState for standardized filtering
- Added BaseFormState with common validation methods
- Created BaseDialogState for consistent dialog management
- Added RoleBasedAccessMixin for role-based access control
- Updated state/__init__.py with new exports
- Provides foundation for consistent state management across all modules
- OrganizationManagementState: Inherits from BaseDialogState & RoleBasedAccessMixin
- ProjectManagementState: Uses BaseDialogState with enhanced organization handling
- UserManagementState: Inherits from BasePaginationState & RoleBasedAccessMixin
- CameraState: Optimized to use BaseManagementState patterns
- Removed redundant methods and consolidated common functionality
- Standardized error/success message handling across all states
- Enhanced dialog management with consistent open/close patterns
- Improved async operation handling with unified patterns
- Updated BaseDataModel to accept Union[str, datetime] for created_at/updated_at
- Enhanced ProjectAssignmentData with flexible assigned_at field
- Fixed validation errors when database returns datetime objects vs strings
- Added minor optimizations to ModelDeploymentState
- Ensures compatibility between database datetime objects and frontend display
- Resolves 'str type expected' validation errors in organization/project data
Backend Services:
- AuthService: Fixed fetch_link syntax and improved error handling
- UserManagementService: Enhanced with better Link relationship handling

Database Repositories:
- CameraRepository: Added update_configuration method and optimizations
- ImageRepository: Enhanced Link handling and search functionality
- ModelRepository/ModelDeploymentRepository: Added update_timestamp calls
- OrganizationRepository: Optimized queries and Link handling
- UserRepository: Fixed project assignment logic and added debug logging

Database Seeding:
- Enhanced seed.py with comprehensive collection cleanup
- Improved data consistency and Link relationship creation
- Better error handling and logging
Components:
- Updated __init__.py with proper component exports
- Enhanced popups.py with improved state integration:
  - Fixed organization selection in project dialogs
  - Updated admin key display to use correct field name
  - Improved project assignment dialog with proper loading

Pages:
- Camera Configurator: Removed 'Show all cameras' toggle as requested
- Added assignment summary section for 'All Cameras' view
- Improved visual feedback and user experience

- Better integration with optimized state management classes
- Consistent UI patterns across all management interfaces
- Enhanced models/__init__.py with comprehensive enum exports
- Added all missing enum imports (OrgRole, ProjectStatus, CameraStatus, etc.)
- Ensures all database enums are properly accessible across the application
- Completes the database model architecture improvements
- Final cleanup for consistent import patterns
@Yasserelhaddar Yasserelhaddar requested review from a user and canelbirlik July 21, 2025 14:55
@Yasserelhaddar Yasserelhaddar changed the base branch from poseidon/dev to feature/103-paz-db-models-and-db-backend-refactor July 21, 2025 15:06
Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Non-Urgent UI Issues:

  • The video stream does not work on my end for Daheng cameras.
  • Streams are not automatically closed when switching between cameras. As a result, a stream from the previously selected camera continues running even after clicking the "Configure" button for a different camera.

ghost

This comment was marked as resolved.

ghost

This comment was marked as resolved.

…dware

Camera Manager & Proxy:
- Enhanced CameraProxy with better error handling and async patterns
- Improved configure method with detailed error reporting
- Added comprehensive batch operations and HDR capture support
- Enhanced retry logic and network bandwidth management

API Enhancements:
- Added comprehensive REST API endpoints for camera operations
- Improved error handling with detailed exception mapping
- Enhanced documentation and type hints
- Added new endpoints for batch operations and HDR capture

Backend Improvements:
- Made ROI and image enhancement methods async in mock backends
- Enhanced error handling and logging throughout backends
- Improved configuration import/export functionality
- Added better validation and error reporting

Test Suite Fixes:
- Fixed test_cameras.py to use correct backend method names
- Added proper async/await patterns throughout tests
- Skipped tests for methods not implemented in backends
- Fixed discovery consistency test for mock cameras
- All 73 tests now pass (68 passed, 5 skipped, 0 failed)

This maintains backward compatibility while providing a robust, modern camera management system.
- Remove reset logic from seed files to make them production-safe
- Make project and organization fields required in Image model
- Remove debug logs from user repository and services
- Fix camera streaming issues (stop streams when switching cameras)
- Implement proper super admin organization management
- Fix Image model initialization in database init
- Clean up verbose database initialization messages
- Fix sample image viewer script to work with current schema
@YasserElHaddar16
Copy link
Contributor

@mazen-elabd the following changes has been pushed:

  • Merge the latest robust hardware API component.
  • Remove reset logic from seed files to make them production-safe
  • Make project and organization fields required in Image model
  • Remove debug logs from user repository and services
  • Fix camera streaming issues (stop streams when switching cameras)
  • Implement proper super admin organization management
  • Fix Image model initialization in database init
  • Clean up verbose database initialization messages
  • Fix sample image viewer script to work with current schema

Please try the application again.
It seems stable on my side.

Yasserelhaddar and others added 6 commits July 24, 2025 12:17
- Add mindtrace-storage dependency to hardware component
- Implement GCS upload for both single and HDR camera captures
- Add GCS configuration settings with auto-upload support
- Update API endpoints to handle GCS URIs in responses
- Add comprehensive GCS documentation and usage examples
- Support explicit GCS parameters and auto-upload via config
- Fix syntax errors and ensure consistent error handling
- Update README with complete GCS integration guide

Features:
- Three storage options: local, GCS, or both
- Auto-upload using configuration defaults
- HDR capture with GCS path patterns
- Metadata support for uploaded images
- Graceful error handling for GCS failures
- Add return_image parameter to CaptureRequest (single capture)
- Add return_images parameter to BatchCaptureRequest (batch capture)
- Update capture endpoints to conditionally return image data
- Update response messages to indicate when image data is excluded
- Update README with comprehensive documentation and examples
- Maintain backward compatibility (defaults to True)

This allows users to reduce response size and improve performance
when only GCS upload or local save is needed, without requiring
image data in the response.
- Fix Basler camera ErrorDescription property access
- Update camera manager GCS upload logic
- Add GCS configuration options
- Make media_type optional in responses
- Update UI camera card with capture button
- Fix camera state GCS path handling
- Update camera configurator UI
Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Working with the branch Yasser confirmed that we seem to get a camera model rebuild error when the code builds after edits in dev mode;

future: <Task finished name='Task-13' coro=<AsyncServer._handle_event_internal() done, defined at /home/mazen/dev/mindtrace/.venv/lib/python3.13/site-packages/socketio/async_server.py:610> exception=PydanticUserError('Camera is not fully defined; you should define Organization, then call Camera.model_rebuild().')>
Traceback (most recent call last):
File "/home/mazen/dev/mindtrace/.venv/lib/python3.13/site-packages/socketio/async_server.py", line 612, in _handle_event_internal
r = await server._trigger_event(data[0], namespace, sid, *data[1:])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/mazen/dev/mindtrace/.venv/lib/python3.13/site-packages/socketio/async_server.py", line 662, in _trigger_event
return await handler.trigger_event(event, *args)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/mazen/dev/mindtrace/.venv/lib/python3.13/site-packages/socketio/async_namespace.py", line 38, in trigger_event
ret = await handler(*args)
^^^^^^^^^^^^^^^^^^^^
File "/home/mazen/dev/mindtrace/.venv/lib/python3.13/site-packages/reflex/app.py", line 1961, in on_event
await self.emit_update(update=update, sid=sid)
File "/home/mazen/dev/mindtrace/.venv/lib/python3.13/site-packages/reflex/app.py", line 1892, in emit_update
await asyncio.create_task(
self.emit(str(constants.SocketEvent.EVENT), update, to=sid)
)
File "/home/mazen/dev/mindtrace/.venv/lib/python3.13/site-packages/socketio/async_namespace.py", line 70, in emit
return await self.server.emit(event, data=data, to=to, room=room,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...<3 lines>...
ignore_queue=ignore_queue)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/mazen/dev/mindtrace/.venv/lib/python3.13/site-packages/socketio/async_server.py", line 178, in emit
await self.manager.emit(event, data, namespace, room=room,
skip_sid=skip_sid, callback=callback,
ignore_queue=ignore_queue)
File "/home/mazen/dev/mindtrace/.venv/lib/python3.13/site-packages/socketio/async_manager.py", line 39, in emit
encoded_packet = pkt.encode()
File "/home/mazen/dev/mindtrace/.venv/lib/python3.13/site-packages/socketio/packet.py", line 64, in encode
encoded_packet += self.json.dumps(data, separators=(',', ':'))
~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/mazen/dev/mindtrace/.venv/lib/python3.13/site-packages/reflex/utils/format.py", line 679, in json_dumps
return json.dumps(obj, **kwargs)
~~~~~~~~~~^^^^^^^^^^^^^^^
File "/home/mazen/.local/share/uv/python/cpython-3.13.3-linux-x86_64-gnu/lib/python3.13/json/init.py", line 238, in dumps
**kw).encode(obj)
~~~~~~^^^^^
File "/home/mazen/.local/share/uv/python/cpython-3.13.3-linux-x86_64-gnu/lib/python3.13/json/encoder.py", line 200, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/home/mazen/.local/share/uv/python/cpython-3.13.3-linux-x86_64-gnu/lib/python3.13/json/encoder.py", line 261, in iterencode
return _iterencode(o, 0)
File "/home/mazen/dev/mindtrace/.venv/lib/python3.13/site-packages/reflex/utils/serializers.py", line 175, in serialize
serialized = serializer(value)
File "/home/mazen/dev/mindtrace/.venv/lib/python3.13/site-packages/reflex/utils/serializers.py", line 320, in serialize_base_model_v2
return model.model_dump()
~~~~~~~~~~~~~~~~^^
File "/home/mazen/dev/mindtrace/.venv/lib/python3.13/site-packages/pydantic/main.py", line 453, in model_dump
return self.pydantic_serializer.to_python(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/mazen/dev/mindtrace/.venv/lib/python3.13/site-packages/pydantic/_internal/_mock_val_ser.py", line 100, in getattr
raise PydanticUserError(self._error_message, code=self._code)
pydantic.errors.PydanticUserError: Camera is not fully defined; you should define Organization, then call Camera.model_rebuild().

For further information visit https://errors.pydantic.dev/2.11/u/class-not-fully-defined

- Fix Camera model rebuild error by ensuring models are rebuilt in CameraState initialization
- Add inference page route to poseidon.py for /inference endpoint
- Update GCS bucket configuration to use settings.GCP_BUCKET_NAME
- Optimize database initialization to prevent redundant model rebuilds
- Add proper error handling for model rebuild operations
- Add safe_convert_link() helper function to properly handle Link objects
- Fix 'str' object has no attribute 'get_full_name' error
- Ensure project, organization, and uploaded_by fields are converted correctly
- Add fallback handling for different object types (None, string, object with get_full_name, etc.)
@canelbirlik canelbirlik added the paz Raised from paz team in the mindtrace package label Jul 30, 2025
@ghost ghost mentioned this pull request Jul 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

paz Raised from paz team in the mindtrace package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants