Author SHA1 Message Date
Brian Bjarke Jensen 9e5e5b58c7 added env vars 2025-11-12 18:23:35 +01:00
12 changed files with 66 additions and 334 deletions
@@ -6,7 +6,7 @@ import logging
from baby_monitor.repositories.interfaces import (
TokenRepositoryInterface,
)
from baby_monitor.repositories.token import (
from baby_monitor.repositories.token.memory_token import (
InMemoryTokenRepository,
)
@@ -43,7 +43,7 @@ def _initialize_token_repository() -> TokenRepositoryInterface:
) from e
try:
from baby_monitor.repositories.token import (
from baby_monitor.repositories.token.redis_token import (
RedisTokenRepository,
)
@@ -1,11 +0,0 @@
"""Token repository implementations and configurations."""
from .default_ttl import DEFAULT_TTL
from .memory_token import InMemoryTokenRepository
from .redis_token import RedisTokenRepository
__all__ = [
"DEFAULT_TTL",
"InMemoryTokenRepository",
"RedisTokenRepository",
]
@@ -1,3 +0,0 @@
"""Definition of Token default TTL."""
DEFAULT_TTL = 28800 # seconds -> 8 hours
@@ -3,7 +3,6 @@
from datetime import datetime, timedelta, UTC
from baby_monitor.repositories.interfaces import TokenRepositoryInterface
from baby_monitor.repositories.token import DEFAULT_TTL
class InMemoryTokenRepository(TokenRepositoryInterface):
@@ -17,8 +16,9 @@ class InMemoryTokenRepository(TokenRepositoryInterface):
def __init__(self) -> None:
# token -> (user_id, expiry_time)
self._tokens: dict[str, tuple[int, datetime]] = {}
self._default_ttl = 3600 # Store default TTL for sliding expiration
def store(self, token: str, user_id: int, ttl: int = DEFAULT_TTL) -> None:
def store(self, token: str, user_id: int, ttl: int = 3600) -> None:
"""Store a token with TTL (time to live in seconds)."""
expiry = datetime.now(UTC) + timedelta(seconds=ttl)
self._tokens[token] = (user_id, expiry)
@@ -39,7 +39,7 @@ class InMemoryTokenRepository(TokenRepositoryInterface):
return None
# Sliding expiration: extend the token lifetime
new_expiry = datetime.now(UTC) + timedelta(seconds=DEFAULT_TTL)
new_expiry = datetime.now(UTC) + timedelta(seconds=self._default_ttl)
self._tokens[token] = (user_id, new_expiry)
return user_id
@@ -3,14 +3,14 @@
from typing import Any
from baby_monitor.repositories.interfaces import TokenRepositoryInterface
from baby_monitor.repositories.token import DEFAULT_TTL
class RedisTokenRepository(TokenRepositoryInterface):
"""
Redis-based token storage.
This implementation uses Redis to store tokens with TTL. Implements sliding expiration.
Requires redis package: pip install redis
Set REDIS_URI environment variable (e.g., redis://localhost:6379/0)
"""
def __init__(self, redis_client: Any) -> None:
@@ -21,8 +21,9 @@ class RedisTokenRepository(TokenRepositoryInterface):
redis_client: Redis client instance from redis.from_url()
"""
self.redis = redis_client
self.default_ttl = 3600 # Store default TTL for sliding expiration
def store(self, token: str, user_id: int, ttl: int = DEFAULT_TTL) -> None:
def store(self, token: str, user_id: int, ttl: int = 3600) -> None:
"""Store a token with TTL (time to live in seconds)."""
key = f"token:{token}"
self.redis.setex(key, ttl, str(user_id))
@@ -36,7 +37,7 @@ class RedisTokenRepository(TokenRepositoryInterface):
user_id_str = self.redis.get(key)
if user_id_str:
# Sliding expiration: extend the token lifetime
self.redis.expire(key, DEFAULT_TTL)
self.redis.expire(key, self.default_ttl)
return int(user_id_str)
return None
+2 -2
View File
@@ -506,8 +506,8 @@
ID: ${user.id} • Created: ${new Date(user.created_at).toLocaleDateString()}
</div>
</div>
<button
class="delete-user-btn"
<button
class="delete-user-btn"
onclick="showDeleteModal(${user.id}, '${user.username}', ${user.is_admin})"
${user.is_admin ? 'disabled title="Cannot delete admin users"' : ""}
>
+3 -21
View File
@@ -281,9 +281,6 @@
let currentDaysRange =
parseInt(localStorage.getItem("lastDiaperTimeRange")) || 7;
let selectedChildId = null; // null means show first/only child
let barChartInstance = null; // Track chart instance for proper cleanup
let poopPieChartInstance = null;
let peePieChartInstance = null;
// Restore last selected child from localStorage
const lastChildId = localStorage.getItem("lastDiaperChildFilter");
@@ -785,12 +782,7 @@
function renderBarChart(chartData) {
const ctx = document.getElementById("diaperChart").getContext("2d");
// Destroy existing chart instance if it exists
if (barChartInstance) {
barChartInstance.destroy();
}
barChartInstance = new Chart(ctx, {
new Chart(ctx, {
type: "bar",
data: {
labels: chartData.labels,
@@ -883,11 +875,6 @@
function renderPoopPieChart(chartData) {
const ctx = document.getElementById("poopPieChart").getContext("2d");
// Destroy existing chart instance if it exists
if (poopPieChartInstance) {
poopPieChartInstance.destroy();
}
const colors = {
Light: {
bg: "rgba(139, 69, 19, 0.5)",
@@ -914,7 +901,7 @@
(label) => colors[label]?.border || "rgba(200, 200, 200, 1)",
);
poopPieChartInstance = new Chart(ctx, {
new Chart(ctx, {
type: "pie",
data: {
labels: chartData.labels,
@@ -970,11 +957,6 @@
function renderPeePieChart(chartData) {
const ctx = document.getElementById("peePieChart").getContext("2d");
// Destroy existing chart instance if it exists
if (peePieChartInstance) {
peePieChartInstance.destroy();
}
const colors = {
Light: {
bg: "rgba(255, 215, 0, 0.5)",
@@ -1001,7 +983,7 @@
(label) => colors[label]?.border || "rgba(200, 200, 200, 1)",
);
peePieChartInstance = new Chart(ctx, {
new Chart(ctx, {
type: "pie",
data: {
labels: chartData.labels,
+2 -14
View File
@@ -319,8 +319,6 @@
parseInt(localStorage.getItem("lastFeedingTimeRange")) || 7;
let selectedChildId = null; // null means show first/only child
let barChartInstance = null;
let pieChartInstance = null;
let durationChartInstance = null;
// Restore last selected child from localStorage
const lastChildId = localStorage.getItem("lastFeedingChildFilter");
@@ -981,11 +979,6 @@
function renderPieChart(chartData) {
const ctx = document.getElementById("pieChart").getContext("2d");
// Destroy existing chart instance if it exists
if (pieChartInstance) {
pieChartInstance.destroy();
}
// Color mapping for each type
const colorMap = {
"Left Breast": {
@@ -1010,7 +1003,7 @@
(label) => colorMap[label]?.border || "rgba(200, 200, 200, 1)",
);
pieChartInstance = new Chart(ctx, {
new Chart(ctx, {
type: "pie",
data: {
labels: chartData.labels,
@@ -1066,12 +1059,7 @@
function renderDurationChart(chartData) {
const ctx = document.getElementById("durationChart").getContext("2d");
// Destroy existing chart instance if it exists
if (durationChartInstance) {
durationChartInstance.destroy();
}
durationChartInstance = new Chart(ctx, {
new Chart(ctx, {
type: "bar",
data: {
labels: chartData.labels,
+41 -218
View File
@@ -167,52 +167,6 @@
margin-top: 0.5rem;
opacity: 0.9;
}
.recent-activity {
margin-top: 2rem;
padding: 1.5rem;
background: #f8f9fa;
border-radius: 8px;
}
.recent-activity h3 {
margin-top: 0;
margin-bottom: 1rem;
color: #333;
font-size: 1.1rem;
}
.activity-grid {
display: flex;
flex-direction: column;
gap: 1rem;
}
.activity-card {
background: white;
padding: 1rem;
border-radius: 6px;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}
.activity-card .icon {
font-size: 1.5rem;
margin-bottom: 0.5rem;
}
.activity-card .type {
font-weight: 600;
color: #333;
margin-bottom: 0.25rem;
}
.activity-card .time {
font-size: 0.85rem;
color: #666;
}
.activity-card .details {
font-size: 0.85rem;
color: #888;
margin-top: 0.5rem;
}
.activity-card.empty {
opacity: 0.5;
font-style: italic;
color: #999;
}
</style>
</head>
<body>
@@ -367,7 +321,6 @@
let activeSleep = null;
let lastSleep = null;
let sleepUpdateTimerInterval = null;
let lastDiaperChange = null;
async function loadFeedingStatus(children) {
userChildren = children;
@@ -384,8 +337,8 @@
activeFeeding = await activeResponse.json();
}
// Fetch the 10 most recent feedings to calculate the most recent session
const feedingsResponse = await fetch("/api/feedings?limit=10", {
// Fetch all feedings to get the last one
const feedingsResponse = await fetch("/api/feedings", {
headers: {
Authorization: `Bearer ${token}`,
},
@@ -394,68 +347,8 @@
if (feedingsResponse.ok) {
const feedings = await feedingsResponse.json();
if (feedings.length > 0) {
// Calculate the most recent feeding session
let sessionStart = feedings[0].start_time;
let sessionEnd = feedings[0].end_time || feedings[0].start_time;
let sessionChildId = feedings[0].child_id;
let sessionEntries = [feedings[0]];
for (let i = 1; i < feedings.length; i++) {
const entry = feedings[i];
// Only group entries for the same child (ignore feeding type)
if (entry.child_id !== sessionChildId) {
break;
}
// If the gap between this entry's end_time and current sessionStart is more than 30 min, stop
const prevEnd = entry.end_time || entry.start_time;
const sessionStartDate = new Date(sessionStart);
const prevEndDate = new Date(prevEnd);
const diffMins = Math.abs(
(sessionStartDate - prevEndDate) / 60000,
);
if (diffMins > 30) {
break;
}
// Extend session to include this entry
sessionStart = entry.start_time;
sessionEntries.push(entry);
}
// Calculate accumulated feeding time for all events in the session
let accumulatedMinutes = 0;
sessionEntries.forEach((entry) => {
if (entry.end_time && entry.start_time) {
const start = new Date(entry.start_time);
const end = new Date(entry.end_time);
const diffMs = end - start;
if (diffMs > 0) {
accumulatedMinutes += Math.floor(diffMs / 60000);
}
}
});
// Build a string showing all feeding types used during the session
const feedingTypesSet = new Set();
sessionEntries.forEach((entry) => {
if (entry.feeding_type) {
feedingTypesSet.add(entry.feeding_type);
}
});
const feedingTypesString = Array.from(feedingTypesSet)
.map(formatFeedingType)
.join(", ");
// Compose a session object
lastFeeding = {
child_id: sessionChildId,
feeding_type: feedings[0].feeding_type,
start_time: sessionStart,
end_time: sessionEnd,
entries: sessionEntries,
child_name: feedings[0].child_name,
accumulated_minutes: accumulatedMinutes,
feeding_types_string: feedingTypesString,
};
// Feedings are ordered by start_time desc, so first is most recent
lastFeeding = feedings[0];
}
}
@@ -483,20 +376,6 @@
lastSleep = sleeps[0];
}
}
// Fetch all diaper changes to get the last one
const diaperResponse = await fetch("/api/diaper-changes", {
headers: {
Authorization: `Bearer ${token}`,
},
});
if (diaperResponse.ok) {
const diapers = await diaperResponse.json();
if (diapers.length > 0) {
lastDiaperChange = diapers[0];
}
}
} catch (error) {
console.error("Error loading feeding status:", error);
}
@@ -570,7 +449,7 @@
</button>
`
: `
<button class="feeding-button" onclick="window.location.href='/log-feeding.html'">
<button class="feeding-button" onclick="showStartFeedingModal()">
🍼 Started Feeding
</button>
`;
@@ -599,100 +478,9 @@
</button>
${feedingButtonHtml}
${sleepButtonHtml}
${buildRecentActivitySection()}
`;
}
function buildRecentActivitySection() {
// Build diaper card
const diaperCard = lastDiaperChange
? `
<a href="/diapers.html" class="activity-card" style="text-decoration:none; color:inherit;">
<div class="icon">🧷</div>
<div class="type">Last Diaper</div>
<div class="time">${formatTime(lastDiaperChange.change_time)}</div>
<div class="details">
${lastDiaperChange.poop_amount ? `💩 ${lastDiaperChange.poop_amount}` : ""}
${lastDiaperChange.poop_amount && lastDiaperChange.pee_amount ? " • " : ""}
${lastDiaperChange.pee_amount ? `💧 ${lastDiaperChange.pee_amount}` : ""}
</div>
</a>
`
: `
<a href="/diapers.html" class="activity-card empty" style="text-decoration:none; color:inherit;">
<div class="icon">🧷</div>
<div class="type">No diaper changes yet</div>
</a>
`;
// Build feeding card
const feedingCard = lastFeeding
? `
<a href="/feedings.html" class="activity-card" style="text-decoration:none; color:inherit;">
<div class="icon">🍼</div>
<div class="type">Last Feeding Session</div>
<div class="time">${formatTime(lastFeeding.start_time)}</div>
<div class="details">
${lastFeeding.feeding_types_string ? `${lastFeeding.feeding_types_string}` : ""}
${lastFeeding.feeding_types_string && lastFeeding.accumulated_minutes ? " • " : ""}
${lastFeeding.accumulated_minutes} minutes total
${lastFeeding.end_time ? "" : " (ongoing)"}
</div>
</a>
`
: `
<a href="/feedings.html" class="activity-card empty" style="text-decoration:none; color:inherit;">
<div class="icon">🍼</div>
<div class="type">No feedings yet</div>
</a>
`;
// Build sleep card
const sleepCard = lastSleep
? `
<a href="/sleep.html" class="activity-card" style="text-decoration:none; color:inherit;">
<div class="icon">😴</div>
<div class="type">Last Sleep</div>
<div class="time">${formatTime(lastSleep.start_time)}</div>
<div class="details">
Duration: ${lastSleep.end_time ? calculateDuration(lastSleep.start_time, lastSleep.end_time) : "ongoing"}
</div>
</a>
`
: `
<a href="/sleep.html" class="activity-card empty" style="text-decoration:none; color:inherit;">
<div class="icon">😴</div>
<div class="type">No sleep sessions yet</div>
</a>
`;
return `
<div class="recent-activity">
<h3>📊 Recent Activity</h3>
<div class="activity-grid">
${diaperCard}
${feedingCard}
${sleepCard}
</div>
</div>
`;
}
function calculateDuration(startTime, endTime) {
const start = new Date(startTime);
const end = new Date(endTime);
const diffMs = end - start;
const diffMins = Math.floor(diffMs / 60000);
if (diffMins < 60) {
return `${diffMins} min`;
}
const hours = Math.floor(diffMins / 60);
const mins = diffMins % 60;
return `${hours}h ${mins}m`;
}
function formatFeedingType(type) {
const types = {
left_breast: "Left Breast",
@@ -864,7 +652,42 @@
}
function showStartSleepModal() {
window.location.href = "/log-sleep.html";
const modal = document.getElementById("startSleepModal");
const childSelect = document.getElementById("modalSleepChildId");
// Clear and populate child select or show single child
if (userChildren.length === 1) {
// Single child: replace dropdown with text display
const child = userChildren[0];
const formGroup = childSelect.parentElement;
formGroup.innerHTML = `
<label for="modalSleepChildName">Child</label>
<input
type="text"
id="modalSleepChildName"
value="${child.name}"
readonly
style="background-color: #f5f5f5; cursor: default;"
/>
<input type="hidden" id="modalSleepChildId" value="${child.id}" />
`;
} else {
// Multiple children: show dropdown
childSelect.innerHTML = '<option value="">Select a child</option>';
userChildren.forEach((child) => {
const option = document.createElement("option");
option.value = child.id;
option.textContent = child.name;
childSelect.appendChild(option);
});
// Set default based on last sleep
if (lastSleep) {
childSelect.value = lastSleep.child_id;
}
}
modal.classList.add("show");
}
function closeStartSleepModal() {
-23
View File
@@ -336,29 +336,6 @@
lastDiaperChange.child_id;
}
}
// Prepopulate poop and pee fields from most recent entry
try {
const response = await fetch("/api/diaper-changes", {
headers: { Authorization: `Bearer ${token}` },
});
if (response.ok) {
const changes = await response.json();
if (changes.length > 0) {
const last = changes[0];
document.getElementById("poopAmount").value =
last.poop_amount || "";
document.getElementById("poopColor").value =
last.poop_color || "";
document.getElementById("peeAmount").value =
last.pee_amount || "";
document.getElementById("peeColor").value =
last.pee_color || "";
}
}
} catch (error) {
// Ignore error, just use default
}
}
} else {
showMessage("Failed to load children", "error");
-16
View File
@@ -303,22 +303,6 @@
// If editing, load the feeding data
if (isEditMode) {
loadFeedingData();
} else {
// Set default feeding type to most recent log entry
try {
const response = await fetch("/api/feedings", {
headers: { Authorization: `Bearer ${token}` },
});
if (response.ok) {
const feedings = await response.json();
if (feedings.length > 0) {
document.getElementById("feedingType").value =
feedings[0].feeding_type;
}
}
} catch (error) {
// Ignore error, just use default
}
}
} else {
showMessage("Failed to load children", "error");
+8 -17
View File
@@ -345,16 +345,13 @@
}
}
function attachTimeRangeListener() {
const timeRangeFilter = document.getElementById("timeRangeFilter");
if (timeRangeFilter) {
timeRangeFilter.addEventListener("change", function () {
// Save selection to localStorage
localStorage.setItem("lastSleepTimeRange", this.value);
renderCharts();
});
}
}
document
.getElementById("timeRangeFilter")
.addEventListener("change", function () {
// Save selection to localStorage
localStorage.setItem("lastSleepTimeRange", this.value);
loadData();
});
async function loadData() {
try {
@@ -390,7 +387,7 @@
readonly
style="background-color: #f5f5f5; cursor: default; padding: 8px; border: 1px solid #ddd; border-radius: 4px; font-size: 14px;"
/>
<label for="timeRangeFilter">Time Range:</label>
<select id="timeRangeFilter">
<option value="1">Last 24 hours</option>
@@ -446,9 +443,6 @@
attachChildFilterListener();
}
// Always attach time range listener after recreating the controls
attachTimeRangeListener();
renderCharts();
} catch (error) {
console.error("Error loading data:", error);
@@ -1104,9 +1098,6 @@
</div>
<div class="sleep-duration">${durationText}</div>
</div>
<div class="sleep-actions">
<button class="edit-button" onclick="window.location.href='/log-sleep.html?id=${sleep.id}'">Edit</button>
</div>
</div>
`;
}