Skip to content

Prathvikmehra/CortexCrew_TimeCure

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

46 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ₯ Smart Appointment Scheduling System (HealthTech)


πŸš€ Project Overview

Traditional hospital appointment systems rely on fixed schedules and fail to handle:

  • Variable consultation times
  • Patient no-shows
  • Late arrivals
  • Real-time delays

πŸ‘‰ This leads to:

  • Long waiting times
  • Inefficient doctor utilization

🎯 Objective

To build an intelligent appointment scheduling system that:

  • Predicts consultation time
  • Predicts patient no-show probability
  • Dynamically updates queue in real-time
  • Reduces waiting time
  • Improves hospital efficiency

🧠 Core Idea

πŸ‘‰ Machine Learning = Prediction
πŸ‘‰ Backend = Decision making
πŸ‘‰ Real-time = Event-driven updates


πŸ“Š Dataset Used

We use the Kaggle dataset:

./KaggleV2-May-2016.csv


πŸ“ Dataset Summary

  • Total records: 110,527 medical appointments
  • Total features: 14 variables
  • Goal: Predict whether patient will show up or not and time

πŸ“– Data Dictionary

Feature Description
PatientId Unique patient ID
AppointmentID Appointment ID
Gender Male / Female
DataMarcacaoConsulta Appointment date
DataAgendamento Booking date
Age Patient age
Neighbourhood Location
Scholarship Govt. welfare program
Hipertension True/False
Diabetes True/False
Alcoholism True/False
Handcap True/False
SMS_received Reminder sent
No-show Target (Yes/No)

πŸ’‘ Inspiration

β€œWhat if it was possible to predict whether a patient will not show up?”


πŸ—οΈ System Architecture

Frontend (React) ↓ Backend (Node.js / Express) ↓ ML Service (Python - Flask) ↓ Models: β€’ No-show Prediction Model β€’ Time Prediction Model


πŸ”„ Complete Workflow


🧾 1. Appointment Booking

  1. Patient enters details:

    • Age
    • Health conditions
    • Other info
  2. Backend sends data to ML API

  3. ML returns: { "no_show_probability": 0.3, "estimated_time": 15 }

  4. Backend:

    • Assigns appointment slot
    • Stores in database

πŸ₯ 2. Patient Arrival

  • Patient checks in via:
    • App OR reception
      status = "arrived"

❌ 3. No-Show Handling

  • If patient does not arrive within threshold (e.g., 10 mins):

status = "no-show"

  • Remove patient from queue
  • Recalculate waiting times

⏰ 4. Late Arrival Handling

  • If patient arrives late:

status = "late"

  • Reinsert into queue: πŸ‘‰ Insert after current patient

  • Update queue dynamically


πŸ‘¨β€βš•οΈ 5. Consultation Process

Doctor dashboard actions:

  • Start Consultation
  • End Consultation

System calculates:

actual_time = end_time - start_time


πŸ”„ 6. Real-Time Update Logic (CORE)

After each consultation:

  1. Calculate delay:

delay = actual_time - predicted_time

  1. Update dynamic average:

new_avg = recent_patient_times

  1. Recalculate queue:

wait_time = sum(previous patients time) + delay

  1. Notify all patients

πŸ“± 7. Patient View

Users see:

  • Estimated wait time
  • Queue position
  • Real-time updates

πŸ€– Machine Learning Models


🧠 Model 1: No-Show Prediction

  • Type: Classification

  • Output:

    • 0 β†’ No-show
    • 1 β†’ Show
  • Features:

    • Age
    • SMS_received
    • Diabetes
    • Hypertension

⏱️ Model 2: Consultation Time Prediction

  • Type: Regression

  • Output:

    • Time in minutes
  • Features:

    • Age
    • Health condition
    • Visit type

⚠️ Important Notes

  • Models are trained using Kaggle dataset
  • Training happens once (offline)
  • Models are saved as .pkl files
  • No real-time retraining required

πŸ”— Backend + ML Integration

  • ML service built using Flask
  • Backend sends request to /predict API
  • ML returns both predictions in one response

βš™οΈ Key Design Principles

  • Event-driven updates (not continuous tracking)
  • Hybrid system (automation + manual confirmation)
  • Fair queue handling
  • Modular architecture

🚨 Edge Case Handling

Scenario Solution
No-show Remove from queue
Late arrival Reinsert after current patient
Delay Update wait time
Faster consultation Reduce waiting time

🎯 System Behavior Summary

βœ” Predicts appointment behavior
βœ” Tracks real-time events
βœ” Dynamically updates queue
βœ” Minimizes waiting time


πŸ§‘β€πŸ’» Team Task Distribution (4 Members)


πŸ‘¨β€πŸŽ¨ Member 1: Frontend (React)

  • Patient interface
  • Doctor dashboard
  • Queue display
  • Real-time UI updates

βš™οΈ Member 2: Backend (Node.js)

  • API development
  • Queue logic
  • Event handling (start/end/no-show)
  • Database management
  • ML API integration

🧠 Member 3: ML Engineer (No-Show Model)

  • Data preprocessing
  • Train classification model
  • Feature engineering
  • Export model (no_show.pkl)

⏱️ Member 4: ML Engineer (Time Prediction Model)

  • Train regression model
  • Predict consultation time
  • Optimize performance
  • Export model (time.pkl)

πŸ”₯ Final Pitch Line

β€œOur system combines machine learning-based predictions with event-driven real-time updates to dynamically optimize patient scheduling and minimize waiting time.”


πŸ† Conclusion

This system is:

  • Predictive (ML-based)
  • Real-time adaptive
  • Efficient and scalable
  • Practical for real-world healthcare systems

βš™οΈ Backend System – Smart Appointment Scheduling


πŸš€ Overview

The backend is the core brain of the system responsible for:

  • Managing patient queue
  • Integrating ML predictions
  • Handling real-time updates
  • Processing events (start, end, no-show, late, walk-in)
  • Serving data to frontend

🎯 Design Principle

πŸ‘‰ Store only what is:

  • Required for ML
  • Required for backend logic
  • Required for UI

πŸ—οΈ Tech Stack

  • Node.js
  • Express.js
  • Axios (for ML API calls)
  • MongoDB
  • Mongoose (ODM)

πŸ“ Folder Structure

backend/
β”œβ”€β”€ models/
β”‚ └── Patient.js
β”œβ”€β”€ config/
β”‚ └── db.js
β”œβ”€β”€ server.js

βš™οΈ Database Setup

1️⃣ Install

npm install mongoose

2️⃣ Connect Database

πŸ“„ config/db.js

const mongoose = require("mongoose");

const connectDB = async () => {
  try {
    await mongoose.connect("mongodb://127.0.0.1:27017/smart-appointment");
    console.log("MongoDB Connected βœ…");
  } catch (err) {
    console.error(err);
    process.exit(1);
  }
};

module.exports = connectDB;

3️⃣ Use in server

const connectDB = require("./config/db");
connectDB();

πŸ“¦ Final Patient Schema

πŸ“„ models/Patient.js

const mongoose = require("mongoose");

const patientSchema = new mongoose.Schema({

  // 🧠 ML INPUT FEATURES
  age: { type: Number, required: true },
  gender: { type: Number, default: 0 },        // 0 = female, 1 = male
  hypertension: { type: Number, default: 0 },
  diabetes: { type: Number, default: 0 },
  scholarship: { type: Number, default: 0 },
  sms: { type: Number, default: 0 },           // 0 = not sent, 1 = sent

  // πŸ€– ML OUTPUT
  predictedTime: { type: Number },
  noShowProb: { type: Number },

  // πŸ”„ QUEUE STATUS
  status: {
    type: String,
    enum: ["waiting", "arrived", "in-progress", "done", "no-show"],
    default: "waiting"
  },

  // πŸ‘€ TYPE
  type: {
    type: String,
    enum: ["booked", "walk-in"],
    default: "booked"
  },

  // ⏱️ TIME TRACKING
  startTime: { type: Date },
  endTime: { type: Date },
  actualTime: { type: Number },

  // πŸ“… META
  createdAt: {
    type: Date,
    default: Date.now
  }

});

module.exports = mongoose.model("Patient", patientSchema);

πŸ“Š Why These Fields?

Field Reason
age, diabetes, etc. ML input
predictedTime Queue calculation
noShowProb SMS + logic
status Queue control
type Walk-in support
createdAt Queue ordering

πŸ”„ Database Workflow

🧾 1. Booking
Backend receives data, calls ML, stores record:

await Patient.create({
  age,
  gender,
  hypertension,
  diabetes,
  scholarship,
  sms: 0,
  predictedTime,
  noShowProb,
  status: "waiting",
  type: "booked"
});

πŸ₯ 2. Arrival

await Patient.updateOne(
  { _id: id },
  { status: "arrived" }
);

πŸ‘¨β€βš•οΈ 3. Start Consultation

await Patient.updateOne(
  { _id: id },
  {
    status: "in-progress",
    startTime: Date.now()
  }
);

⏹ 4. End Consultation

await Patient.updateOne(
  { _id: id },
  {
    status: "done",
    endTime: Date.now(),
    actualTime: calculatedTime
  }
);

❌ 5. No-Show

await Patient.updateOne(
  { _id: id },
  { status: "no-show" }
);

⏰ 6. Late Arrival

await Patient.updateOne(
  { _id: id },
  { status: "waiting" }
);

πŸ“Š Queue Generation (IMPORTANT)

πŸ‘‰ Queue is NOT stored
πŸ‘‰ It is dynamically generated

const queue = await Patient.find({
  status: { $in: ["waiting", "arrived", "in-progress"] }
}).sort({ createdAt: 1 });

⏱️ Wait Time Calculation

function calculateWaitTime(queue, index) {
  let time = 0;

  for (let i = 0; i < index; i++) {
    if (queue[i].status !== "no-show") {
      time += queue[i].predictedTime;
    }
  }

  return time;
}

πŸ”— Backend Integration Rules (VERY IMPORTANT)

πŸ‘‰ Backend and DB must agree on:

  • Field Names: predictedTime, noShowProb, sms, status
  • Status Values: waiting, arrived, in-progress, done, no-show

🚨 Edge Case Handling

Scenario DB Action
No-show Update status
Late arrival Update + reorder
Walk-in Insert new record
Delay Update actualTime
SMS sent Update sms

🧠 Important Rules

  • Do NOT store queue separately
  • Always fetch fresh data
  • Keep schema consistent
  • Do not change field names randomly

πŸ† Final Summary

This database system: βœ” Stores all patient data
βœ” Supports ML integration
βœ” Enables dynamic queue
βœ” Handles real-time updates
βœ” Ensures system consistency


πŸš€ What You Do Now

πŸ‘‰ Give this to your database teammate
πŸ‘‰ Start MongoDB setup
πŸ‘‰ Create model β†’ test insert


πŸ“± SMS Reminder System


πŸ’‘ What is SMS_received?

SMS_received is a feature in the dataset that tells us:

  • 0 = Patient has NOT received an SMS reminder yet
  • 1 = Patient HAS received an SMS reminder

Studies show patients who receive an SMS reminder are more likely to show up. So this feature directly improves our No-Show prediction accuracy.


🧠 How It Works in the ML Model

We use SMS_received only in the No-Show model (not in the Time model).

Model Uses SMS_received? Reason
No-Show Prediction βœ… YES SMS affects whether patient shows up
Time Prediction ❌ NO SMS has no effect on consultation duration

πŸ”„ SMS Flow β€” Step by Step

Step 1: Patient Books Appointment

  • Backend calls ML with SMS_received = 0 (SMS not sent yet)
  • ML returns no_show_probability and sms_strategy

Step 2: Backend Reads the SMS Strategy

ML says Risk Level What Backend Does
sms_strategy = "high_risk" πŸ”΄ prob > 0.4 Send SMS 24 hours before + 2 hours before
sms_strategy = "medium_risk" 🟑 prob 0.2–0.4 Send SMS 24 hours before only
sms_strategy = "low_risk" 🟒 prob ≀ 0.2 No SMS needed

Step 3: After SMS is Sent

  • Backend marks sms_received = 1 in patient record
  • Backend calls ML again with SMS_received = 1
  • ML returns updated (lower) no_show_probability
  • Queue wait times are recalculated

πŸ“¦ API β€” What the ML Returns

Request (on booking):

{
  "Age": 35,
  "Gender": 0,
  "Hipertension": 1,
  "Diabetes": 0,
  "Alcoholism": 0,
  "Handcap": 0,
  "Scholarship": 0,
  "SMS_received": 0
}

Response:

{
  "no_show_probability": 0.35,
  "estimated_time": 18.0,
  "sms_strategy": "medium_risk",
  "status": "success"
}

Request (after SMS is sent):

{
  "SMS_received": 1,
  ...same fields...
}

Response (updated):

{
  "no_show_probability": 0.18,
  "estimated_time": 18.0,
  "sms_strategy": "low_risk",
  "status": "success"
}

πŸ—‚οΈ Simple Summary

Patient books
    ↓
ML predicts risk (SMS_received = 0)
    ↓
High/Medium risk? β†’ Schedule SMS reminder
    ↓
SMS sent β†’ Update patient record (SMS_received = 1)
    ↓
ML re-predicts updated risk (lower now)
    ↓
Queue recalculated with new probability

πŸ” Authentication System (Independent)


🎯 Design Principle: Separation of Concerns

The Patient Schema we designed earlier is strictly for Queue Management and ML Predictions (it essentially represents an Appointment in the queue).

Do NOT mix login credentials into the Patient queue schema. A patient might visit multiple times (multiple queue records over time), but they should only have one permanent login account.

Therefore, authentication must be completely independent from the queue logic!


πŸ—οΈ Recommended Auth Structure

Create a separate User model to handle registration and login for both Receptionists and Patients.

πŸ“ New Model File: models/User.js

const mongoose = require("mongoose");

const userSchema = new mongoose.Schema({
  name: { type: String, required: true },
  email: { type: String, required: true, unique: true },
  password: { type: String, required: true }, // ⚠️ Store HASHED (use bcrypt)
  
  // Role string determines permissions across the app
  role: {
    type: String,
    enum: ["patient", "receptionist", "doctor"],
    default: "patient"
  },
  
  phone: { type: String } // Useful to link to the ML SMS feature
});

module.exports = mongoose.model("User", userSchema);

πŸ”„ Workflow for Auth + Queues

  1. Registration:

    • Patient registers on frontend β†’ creates a User document in database.
    • Receptionists/Doctors are pre-created manually by admins.
  2. Login / JWT:

    • User logs in via /api/auth/login.
    • Backend verifies password (bcrypt) and returns a JWT token containing their userId and role.
  3. Booking an Appointment (Connecting them):

    • An authenticated patient hits the /book queue endpoint.
    • The backend securely reads their userId from the JWT token and attaches it to the newly generated Patient (queue) document.

🚨 Essential Rules for Authentication

  • Never store plain-text passwords: Always use bcrypt to hash the password before saving to MongoDB.
  • Secure endpoints via Routes Guarding: Use JWT to secure your Backend APIs. Receptionists should have access to /start, /end, and /late, while Patients can only access /book and view the /queue.
  • Database Separation:
    • Collection 1: Users (For login / auth / profiles)
    • Collection 2: Patients (For daily appointments / queues / ML)

About

TimeCure is an intelligent healthcare scheduling system that combines predictive insights with real-time queue optimization to minimize patient wait times and maximize doctor efficiency.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • JavaScript 69.8%
  • Python 30.2%