Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

BigQuery Analytics Cloud Function

Provides population-level symptom analytics for the clinician dashboard. Protected by Firebase Authentication.

Prerequisites

  1. BigQuery Dataset & Table must exist (see Step 1 below)
  2. Firebase project configured (you already have this)
  3. gcloud CLI authenticated (you already have this)

Step 1: Create BigQuery Dataset & Seed Data

Go to BigQuery Console and run this SQL:

-- Create the dataset (run this first, or create via UI)
-- Dataset ID: oncydra_analytics

-- Create and seed the table
CREATE OR REPLACE TABLE `oncydra.oncydra_analytics.symptom_trends`
(
  report_month DATE,
  symptom_name STRING,
  avg_severity FLOAT64,
  patient_count INT64
);

INSERT INTO `oncydra.oncydra_analytics.symptom_trends` (report_month, symptom_name, avg_severity, patient_count)
VALUES
  -- September 2024 - Baseline (high symptoms)
  ('2024-09-01', 'Nausea', 6.8, 312),
  ('2024-09-01', 'Fatigue', 7.5, 445),
  ('2024-09-01', 'Pain', 5.2, 289),
  ('2024-09-01', 'Appetite Loss', 6.1, 356),
  
  -- October 2024 - Starting to use Oncydra
  ('2024-10-01', 'Nausea', 5.9, 378),
  ('2024-10-01', 'Fatigue', 7.1, 462),
  ('2024-10-01', 'Pain', 4.8, 301),
  ('2024-10-01', 'Appetite Loss', 5.5, 389),
  
  -- November 2024 - Improvements visible
  ('2024-11-01', 'Nausea', 4.5, 421),
  ('2024-11-01', 'Fatigue', 6.2, 478),
  ('2024-11-01', 'Pain', 4.1, 334),
  ('2024-11-01', 'Appetite Loss', 4.8, 412),
  
  -- December 2024 - Strong results
  ('2024-12-01', 'Nausea', 3.2, 456),
  ('2024-12-01', 'Fatigue', 5.1, 489),
  ('2024-12-01', 'Pain', 3.5, 367),
  ('2024-12-01', 'Appetite Loss', 3.9, 434);

The Story: This data shows symptom severity DECREASING over 4 months as patients use Oncydra for daily voice check-ins. Nausea drops from 6.8 to 3.2 (53% improvement!).


Step 2: Deploy the Cloud Function

cd backend/analytics-function

gcloud functions deploy analytics-function \
  --gen2 \
  --runtime=python311 \
  --region=europe-west1 \
  --source=. \
  --entry-point=analytics \
  --trigger-http \
  --allow-unauthenticated \
  --project=oncydra

Note: --allow-unauthenticated lets the HTTP request through, but our code still verifies the Firebase token. This is the standard pattern.


Step 3: Get Your Function URL

After deploy, you'll see something like:

https://europe-west1-oncydra.cloudfunctions.net/analytics-function

Save this URL - you'll need it for the frontend.


Step 4: Test the Function

# This should return 401 Unauthorized (no token)
curl https://europe-west1-oncydra.cloudfunctions.net/analytics-function

# To test with a real token, use the frontend or get a token from Firebase console

API Endpoints

GET /

Returns symptom trends data (default)

Response:

{
  "success": true,
  "query_type": "trends",
  "data": [
    {
      "report_month": "2024-09-01",
      "symptom_name": "Nausea",
      "avg_severity": 6.8,
      "patient_count": 312
    },
    ...
  ]
}

GET /?type=summary

Returns summary statistics

Response:

{
  "success": true,
  "query_type": "summary",
  "data": {
    "symptom_types": 4,
    "total_patient_reports": 6323,
    "overall_avg_severity": 5.26,
    "max_patients_single_symptom": 489
  }
}

Frontend Usage

// In ClinicianDashboard.tsx or a separate hook
const fetchAnalytics = async () => {
  const user = auth.currentUser;
  if (!user) return;
  
  const token = await user.getIdToken();
  
  const response = await fetch(
    'https://europe-west1-oncydra.cloudfunctions.net/analytics-function',
    {
      headers: {
        'Authorization': `Bearer ${token}`
      }
    }
  );
  
  const data = await response.json();
  return data;
};

Troubleshooting

401 Unauthorized

  • Check that the user is logged in
  • Check that the token is being passed in the Authorization header

BigQuery errors

  • Make sure the dataset oncydra_analytics exists
  • Make sure the table symptom_trends has data
  • Check Cloud Function logs: gcloud functions logs read analytics-function --region=europe-west1

CORS errors

  • The function includes CORS headers, but if issues persist, check browser console for details