-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(gluwave): 🗃️ fix carbs on board performance
also fixes the hydration error
- Loading branch information
Showing
9 changed files
with
399 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.