Skip to content

Commit

Permalink
fix(gluwave): 🗃️ fix carbs on board performance
Browse files Browse the repository at this point in the history
also fixes the hydration error
  • Loading branch information
Kalhama committed Oct 2, 2024
1 parent 5cb972a commit 7f1b4f1
Show file tree
Hide file tree
Showing 9 changed files with 399 additions and 77 deletions.
3 changes: 3 additions & 0 deletions gluwave/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ ENV GITHUB_SECRET=_
ENV DATABASE_URL=_
ENV DATABASE_URL_UNPOOLED=_

# Must be UTC
ENV TZ=UTC

WORKDIR /app
COPY package.json pnpm-lock.yaml ./
RUN pnpm install --frozen-lockfile
Expand Down
70 changes: 70 additions & 0 deletions gluwave/drizzle/0008_optimized_metrics_function.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
-- Custom SQL migration file, put you code below! --
DROP VIEW metrics;

CREATE OR REPLACE FUNCTION metrics(
start_time TIMESTAMP,
end_time TIMESTAMP,
input_user_id TEXT
)
RETURNS TABLE (
glucose_id INTEGER,
user_id TEXT,
"timestamp" TIMESTAMP,
glucose DOUBLE PRECISION,
step INTERVAL,
glucose_change DOUBLE PRECISION,
timestamp_next TIMESTAMP,
insulin_change DOUBLE PRECISION,
observed_carbs DOUBLE PRECISION
) AS $$
BEGIN
RETURN QUERY
WITH glucose_change AS (
SELECT
glucose.id as glucose_id,
glucose.user_id,
glucose.timestamp,
glucose.amount AS glucose,
LEAD(glucose.timestamp) OVER (PARTITION BY glucose.user_id ORDER BY glucose.timestamp) - glucose.timestamp AS step,
LEAD(glucose.amount) OVER (PARTITION BY glucose.user_id ORDER BY glucose.timestamp) - glucose.amount AS glucose_change,
LEAD(glucose.timestamp) OVER (PARTITION BY glucose.user_id ORDER BY glucose.timestamp) AS timestamp_next
FROM glucose
WHERE start_time <= glucose.timestamp AND glucose.timestamp <= end_time
AND glucose.user_id = input_user_id
), insulin_change AS (
SELECT
glucose_change.glucose_id,
SUM(COALESCE(total_insulin_absorbed(
t => glucose_change.timestamp_next,
start => insulin.timestamp,
amount => insulin.amount
), 0) - COALESCE(total_insulin_absorbed(
t => glucose_change.timestamp,
start => insulin.timestamp,
amount => insulin.amount
), 0))::DOUBLE PRECISION as insulin_change
FROM glucose_change
LEFT JOIN insulin
ON glucose_change.user_id = insulin.user_id
AND insulin.timestamp <= glucose_change.timestamp_next AND glucose_change.timestamp <= insulin.timestamp + interval '6 hours'
GROUP BY glucose_change.glucose_id
ORDER BY glucose_change.glucose_id
)
SELECT
glucose_change.glucose_id,
glucose_change.user_id,
glucose_change.timestamp,
glucose_change.glucose,
glucose_change.step,
glucose_change.glucose_change,
glucose_change.timestamp_next,
insulin_change.insulin_change,
glucose_change.glucose_change / "user"."correctionRatio" * "user"."carbohydrateRatio" + insulin_change.insulin_change * "user"."carbohydrateRatio" AS observed_carbs
FROM glucose_change
INNER JOIN insulin_change
ON glucose_change.glucose_id = insulin_change.glucose_id
INNER JOIN "user"
ON "user".id = glucose_change.user_id
ORDER BY glucose_change.glucose_id;
END;
$$ LANGUAGE plpgsql;
278 changes: 278 additions & 0 deletions gluwave/drizzle/meta/0008_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
{
"id": "f0563d16-ea76-453a-94da-411189c443b3",
"prevId": "4b5b4bd2-7282-4dc0-8634-1f9e2f5bb747",
"version": "7",
"dialect": "postgresql",
"tables": {
"public.carbs": {
"name": "carbs",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"primaryKey": true,
"notNull": true
},
"timestamp": {
"name": "timestamp",
"type": "timestamp",
"primaryKey": false,
"notNull": true
},
"amount": {
"name": "amount",
"type": "double precision",
"primaryKey": false,
"notNull": true
},
"decay": {
"name": "decay",
"type": "integer",
"primaryKey": false,
"notNull": true
},
"user_id": {
"name": "user_id",
"type": "text",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {
"carbs_user_id_user_id_fk": {
"name": "carbs_user_id_user_id_fk",
"tableFrom": "carbs",
"columnsFrom": [
"user_id"
],
"tableTo": "user",
"columnsTo": [
"id"
],
"onUpdate": "no action",
"onDelete": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"public.glucose": {
"name": "glucose",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"primaryKey": true,
"notNull": true
},
"timestamp": {
"name": "timestamp",
"type": "timestamp",
"primaryKey": false,
"notNull": true
},
"amount": {
"name": "amount",
"type": "double precision",
"primaryKey": false,
"notNull": true
},
"user_id": {
"name": "user_id",
"type": "text",
"primaryKey": false,
"notNull": true
},
"device": {
"name": "device",
"type": "text",
"primaryKey": false,
"notNull": false
}
},
"indexes": {},
"foreignKeys": {
"glucose_user_id_user_id_fk": {
"name": "glucose_user_id_user_id_fk",
"tableFrom": "glucose",
"columnsFrom": [
"user_id"
],
"tableTo": "user",
"columnsTo": [
"id"
],
"onUpdate": "no action",
"onDelete": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"public.insulin": {
"name": "insulin",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"primaryKey": true,
"notNull": true
},
"timestamp": {
"name": "timestamp",
"type": "timestamp",
"primaryKey": false,
"notNull": true
},
"amount": {
"name": "amount",
"type": "double precision",
"primaryKey": false,
"notNull": true
},
"user_id": {
"name": "user_id",
"type": "text",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {
"insulin_user_id_user_id_fk": {
"name": "insulin_user_id_user_id_fk",
"tableFrom": "insulin",
"columnsFrom": [
"user_id"
],
"tableTo": "user",
"columnsTo": [
"id"
],
"onUpdate": "no action",
"onDelete": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"public.session": {
"name": "session",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true
},
"user_id": {
"name": "user_id",
"type": "text",
"primaryKey": false,
"notNull": true
},
"expires_at": {
"name": "expires_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {
"session_user_id_user_id_fk": {
"name": "session_user_id_user_id_fk",
"tableFrom": "session",
"columnsFrom": [
"user_id"
],
"tableTo": "user",
"columnsTo": [
"id"
],
"onUpdate": "no action",
"onDelete": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"public.user": {
"name": "user",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true
},
"github_id": {
"name": "github_id",
"type": "text",
"primaryKey": false,
"notNull": true
},
"carbohydrateRatio": {
"name": "carbohydrateRatio",
"type": "double precision",
"primaryKey": false,
"notNull": true,
"default": 10
},
"correctionRatio": {
"name": "correctionRatio",
"type": "double precision",
"primaryKey": false,
"notNull": true,
"default": 1
},
"target": {
"name": "target",
"type": "double precision",
"primaryKey": false,
"notNull": true,
"default": 6
},
"insulinOnBoardOffset": {
"name": "insulinOnBoardOffset",
"type": "double precision",
"primaryKey": false,
"notNull": true,
"default": 0
},
"apikey": {
"name": "apikey",
"type": "text",
"primaryKey": false,
"notNull": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"user_github_id_unique": {
"name": "user_github_id_unique",
"columns": [
"github_id"
],
"nullsNotDistinct": false
}
}
}
},
"enums": {},
"schemas": {},
"sequences": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}
7 changes: 7 additions & 0 deletions gluwave/drizzle/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@
"when": 1727714440744,
"tag": "0007_metrics",
"breakpoints": true
},
{
"idx": 8,
"version": "7",
"when": 1727835817312,
"tag": "0008_optimized_metrics_function",
"breakpoints": true
}
]
}
Loading

0 comments on commit 7f1b4f1

Please sign in to comment.