-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
136 lines (119 loc) · 4.42 KB
/
Copy pathdb.js
File metadata and controls
136 lines (119 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import Database from 'better-sqlite3';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
const __dirname = dirname(fileURLToPath(import.meta.url));
const dbPath = process.env.DB_PATH || join(__dirname, 'devlog.db');
const db = new Database(dbPath);
db.exec(`
CREATE TABLE IF NOT EXISTS sessions (
id TEXT PRIMARY KEY,
started_at TEXT NOT NULL,
ended_at TEXT NOT NULL,
breaks_minutes INTEGER NOT NULL DEFAULT 0,
notes TEXT
)
`);
db.exec(`
CREATE TABLE IF NOT EXISTS month_notes (
month_key TEXT PRIMARY KEY,
summary TEXT
)
`);
db.exec(`
CREATE TABLE IF NOT EXISTS week_notes (
week_key TEXT PRIMARY KEY,
summary TEXT,
goals TEXT
)
`);
export function getAllSessions() {
return db.prepare('SELECT * FROM sessions ORDER BY started_at DESC').all();
}
export function getSessionsByMonth(year, month) {
const start = `${year}-${String(month).padStart(2, '0')}-01`;
const end = `${year}-${String(month).padStart(2, '0')}-31`;
return db
.prepare(
`SELECT * FROM sessions
WHERE date(started_at) >= date(?) AND date(started_at) <= date(?)
ORDER BY started_at ASC`
)
.all(start, end);
}
export function getSessionsByDateRange(from, to) {
return db
.prepare(
`SELECT * FROM sessions
WHERE date(started_at) >= date(?) AND date(started_at) <= date(?)
ORDER BY started_at ASC`
)
.all(from, to);
}
export function createSession({ id, started_at, ended_at, breaks_minutes = 0, notes = '' }) {
db.prepare(
`INSERT INTO sessions (id, started_at, ended_at, breaks_minutes, notes)
VALUES (?, ?, ?, ?, ?)`
).run(id, started_at, ended_at, breaks_minutes, notes ?? '');
return { id, started_at, ended_at, breaks_minutes, notes };
}
export function updateSession(id, { started_at, ended_at, breaks_minutes, notes }) {
const stmt = db.prepare(
`UPDATE sessions SET started_at = ?, ended_at = ?, breaks_minutes = ?, notes = ?
WHERE id = ?`
);
stmt.run(started_at, ended_at, breaks_minutes ?? 0, notes ?? '', id);
}
export function deleteSession(id) {
db.prepare('DELETE FROM sessions WHERE id = ?').run(id);
}
export function getSessionsInfo() {
const count = db.prepare('SELECT COUNT(*) as cnt FROM sessions').get();
const first = db.prepare('SELECT started_at FROM sessions ORDER BY started_at ASC LIMIT 1').get();
const last = db.prepare('SELECT started_at FROM sessions ORDER BY started_at DESC LIMIT 1').get();
return {
count: count?.cnt || 0,
firstDate: first?.started_at?.slice(0, 10) || null,
lastDate: last?.started_at?.slice(0, 10) || null
};
}
export function getTotalStudiedMinutes() {
const rows = db.prepare('SELECT started_at, ended_at, breaks_minutes FROM sessions').all();
let total = 0;
for (const r of rows) {
const start = new Date(r.started_at).getTime();
const end = new Date(r.ended_at).getTime();
const duration = Math.round((end - start) / 60000);
total += Math.max(0, duration - (Number(r.breaks_minutes) || 0));
}
return total;
}
export function getMonthNote(monthKey) {
const row = db.prepare('SELECT summary FROM month_notes WHERE month_key = ?').get(monthKey);
return row ? row.summary : null;
}
export function setMonthNote(monthKey, summary) {
db.prepare(
'INSERT INTO month_notes (month_key, summary) VALUES (?, ?) ON CONFLICT(month_key) DO UPDATE SET summary = ?'
).run(monthKey, summary ?? '', summary ?? '');
}
export function getWeekNote(weekKey) {
const row = db.prepare('SELECT summary, goals FROM week_notes WHERE week_key = ?').get(weekKey);
if (!row) return { summary: null, goals: [] };
const goals = row.goals ? JSON.parse(row.goals) : [];
return { summary: row.summary, goals };
}
export function setWeekNote(weekKey, summary, goals) {
const current = db.prepare('SELECT summary, goals FROM week_notes WHERE week_key = ?').get(weekKey);
const prevSummary = current ? current.summary : '';
const prevGoals = current && current.goals ? current.goals : '[]';
const newSummary = summary !== undefined && summary !== null ? summary : prevSummary;
const newGoalsStr =
goals !== undefined && goals !== null && Array.isArray(goals)
? JSON.stringify(goals)
: prevGoals;
db.prepare(
`INSERT INTO week_notes (week_key, summary, goals) VALUES (?, ?, ?)
ON CONFLICT(week_key) DO UPDATE SET summary = excluded.summary, goals = excluded.goals`
).run(weekKey, newSummary ?? '', newGoalsStr);
}
export default db;