Technical Guide
The Lavender app front-end is made with React and uses the Firestore JS SDK to connect to a Google Firebase Firestore cloud project. Account management happens through Firebase Authentication.
User-facing error validation happens client-side through the JavaScript util files, but Firebase Security Rules perform additional role-based enforcements server-side. Account creation and deletion happen through Cloud Functions for Firebase.
Algorithm
The algorithm is built with Python and uses JSON as the input and output format. The sleep optimization algorithm analyzes a user’s schedule to provide personalized sleep recommendations based on their work shifts and available free time.
Core Components
The algorithm consists of several key functions:
-
Schedule Analysis
build_matrix()
: Processes the input JSON data containing schedule informationget_start_end_times()
: Identifies work shift boundariesnight_or_day_shift()
: Classifies shifts as day, evening, or night shifts based on timing
-
Sleep Pattern Detection
king_of_the_hill_algo()
: Finds the longest continuous period of available sleep timeget_sleep_episode()
: Identifies potential “booster” sleep periods after shifts
-
Schedule Optimization
modify_sleep_schedule()
: Generates sleep recommendations based on:- Current total sleep hours
- Shift patterns (day/night/evening)
- Previous and upcoming shift schedules
- Global metadata about sleep patterns
How It Works
-
The algorithm takes a JSON input containing:
- Time intervals (in minutes) for the schedule
- Work shift information
- Available free time slots
- Global metadata about sleep patterns
-
It analyzes the schedule to:
- Identify work shift boundaries
- Classify shift types (day/night/evening)
- Find optimal sleep windows
- Calculate total available sleep time
-
Based on the analysis, it provides recommendations for:
- Primary sleep periods
- Additional “booster” sleep episodes when needed
- Schedule modifications to achieve optimal sleep duration
Currently the algorithm is not yet integrated with the app front-end, but eventually this will happen through Cloud Functions as well.
Development Challenges and Solutions
Throughout the development process, we encountered several significant challenges:
Codebase Rewrite
The decision to rewrite the codebase was driven by the need for a more maintainable and user-friendly application. While this was a substantial undertaking, it allowed us to:
- Implement a more intuitive UI/UX design
- Establish better code organization and documentation
- Create a more scalable architecture for future development
Sleep Optimization Algorithm Development
Developing the sleep optimization algorithm presented unique challenges:
- Learning and implementing sleep psychology research principles
- Creating an algorithm that could handle various shift work patterns
- Ensuring the algorithm’s recommendations were practical and implementable
- Balancing scientific accuracy with computational efficiency
Firebase Integration
Working with Firebase, especially Cloud Functions and the Admin SDK required significant learning and adaptation:
- Understanding the security implications of different Firebase services
- Implementing proper authentication and authorization flows
- Setting up secure server-side operations through Cloud Functions
- Managing the interaction between the frontend and backend services
These challenges were addressed through extensive research and iterative development processes. The solutions implemented have resulted in a more robust and maintainable application.