Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming. Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
131 lines
5.3 KiB
Go
131 lines
5.3 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"time"
|
|
)
|
|
|
|
// Spans are an indexed projection of running transitions. They are committed
|
|
// with the session/audit, never with a per-second timer write.
|
|
func (s *Store) ensureTaskFocusSpans() error {
|
|
_, err := s.DB.Exec(`CREATE TABLE IF NOT EXISTS organizer_focus_spans(session_uuid TEXT NOT NULL,version INTEGER NOT NULL,domain_uuid TEXT NOT NULL,owner_uuid TEXT NOT NULL,task_uuid TEXT NOT NULL,phase TEXT NOT NULL,start_ms INTEGER NOT NULL,end_ms INTEGER NOT NULL,closed INTEGER NOT NULL,PRIMARY KEY(session_uuid,version));
|
|
CREATE INDEX IF NOT EXISTS organizer_focus_spans_day ON organizer_focus_spans(domain_uuid,owner_uuid,start_ms);
|
|
CREATE INDEX IF NOT EXISTS organizer_time_blocks_calendar ON organizer_time_blocks(domain_uuid,owner_uuid,calendar_uuid,event_uid) WHERE state='active';
|
|
INSERT OR IGNORE INTO organizer_focus_spans SELECT uuid,version,domain_uuid,owner_uuid,task_uuid,phase,max(created_at_ms,deadline_ms-remaining_ms),deadline_ms,0 FROM organizer_focus WHERE state='running'`)
|
|
return err
|
|
}
|
|
|
|
func recordFocusSpanTx(ctx context.Context, tx *sql.Tx, f TaskFocus) error {
|
|
if f.State == "running" {
|
|
_, err := tx.ExecContext(ctx, `INSERT INTO organizer_focus_spans SELECT uuid,version,domain_uuid,owner_uuid,task_uuid,phase,updated_at_ms,deadline_ms,0 FROM organizer_focus WHERE uuid=?`, f.UUID)
|
|
return err
|
|
}
|
|
_, err := tx.ExecContext(ctx, `UPDATE organizer_focus_spans SET end_ms=min(end_ms,max(start_ms,?)),closed=1 WHERE session_uuid=? AND closed=0`, f.UpdatedMS, f.UUID)
|
|
return err
|
|
}
|
|
|
|
type TaskDaySummary struct {
|
|
TaskCount int `json:"task_count"`
|
|
EstimateMinutes int `json:"estimate_minutes"`
|
|
FocusMS int64 `json:"focus_ms"`
|
|
TasksPartial bool `json:"tasks_partial"`
|
|
FocusPartial bool `json:"focus_partial"`
|
|
}
|
|
|
|
func (s *Store) TaskDaySummary(ctx context.Context, domain, actor string, start, end, now time.Time) (TaskDaySummary, error) {
|
|
var out TaskDaySummary
|
|
if !end.After(start) || end.Sub(start) > 26*time.Hour {
|
|
return out, ErrTaskInvalid
|
|
}
|
|
items, next, err := s.ListTasks(ctx, domain, actor, TaskQuery{Kind: "task", View: "today", Today: start.Format("2006-01-02"), Limit: 100})
|
|
if err != nil {
|
|
return out, err
|
|
}
|
|
out.TaskCount = len(items)
|
|
out.TasksPartial = next != ""
|
|
for _, item := range items {
|
|
out.EstimateMinutes += item.Estimate
|
|
}
|
|
// Each running interval is at most 180 minutes, including resumed ones.
|
|
rows, err := s.DB.QueryContext(ctx, `SELECT start_ms,end_ms FROM organizer_focus_spans WHERE domain_uuid=? AND owner_uuid=? AND start_ms>=? AND start_ms<? AND end_ms>? AND phase='focus' ORDER BY start_ms,session_uuid,version LIMIT 1001`, domain, actor, start.Add(-180*time.Minute).UnixMilli(), end.UnixMilli(), start.UnixMilli())
|
|
if err != nil {
|
|
return out, err
|
|
}
|
|
defer rows.Close()
|
|
n := 0
|
|
for rows.Next() {
|
|
var a, b int64
|
|
if err = rows.Scan(&a, &b); err != nil {
|
|
return out, err
|
|
}
|
|
n++
|
|
if n > 1000 {
|
|
out.FocusPartial = true
|
|
break
|
|
}
|
|
out.FocusMS += max(int64(0), min(b, end.UnixMilli(), now.UnixMilli())-max(a, start.UnixMilli()))
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
func (s *Store) TaskBlockCalendars(ctx context.Context, domain, actor string) ([]string, bool, error) {
|
|
if err := taskActor(ctx, s.DB, domain, actor, false); err != nil {
|
|
return nil, false, err
|
|
}
|
|
rows, err := s.DB.QueryContext(ctx, `SELECT calendar_uuid FROM organizer_time_blocks WHERE domain_uuid=? AND owner_uuid=? AND state='active' GROUP BY calendar_uuid ORDER BY calendar_uuid LIMIT 21`, domain, actor)
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
defer rows.Close()
|
|
out := []string{}
|
|
for rows.Next() {
|
|
var id string
|
|
if err = rows.Scan(&id); err != nil {
|
|
return nil, false, err
|
|
}
|
|
out = append(out, id)
|
|
}
|
|
more := len(out) > 20
|
|
if more {
|
|
out = out[:20]
|
|
}
|
|
return out, more, rows.Err()
|
|
}
|
|
|
|
type TaskDayBlock struct {
|
|
UUID string `json:"uuid"`
|
|
TaskUUID string `json:"task_uuid"`
|
|
Title string `json:"title"`
|
|
Start int64 `json:"start_ms"`
|
|
End int64 `json:"end_ms"`
|
|
EventUID string `json:"-"`
|
|
Href string `json:"href"`
|
|
}
|
|
|
|
// Call ONLY inside Repository.ReadPersonalLocalIndex: that lock enforces the
|
|
// current calendar policy and keeps this cache at the canonical manifest head.
|
|
func (s *Store) TaskDayBlocks(ctx context.Context, domain, actor, calendar string, start, end time.Time) ([]TaskDayBlock, bool, error) {
|
|
if err := taskActor(ctx, s.DB, domain, actor, false); err != nil {
|
|
return nil, false, err
|
|
}
|
|
rows, err := s.DB.QueryContext(ctx, `SELECT b.uuid,b.task_uuid,i.title,e.start*1000,e.end*1000,b.event_uid FROM calendar_event_cache e JOIN organizer_time_blocks b ON b.domain_uuid=e.domain AND b.calendar_uuid=e.calendar AND b.event_uid=e.uid JOIN organizer_items i ON i.domain_uuid=b.domain_uuid AND i.uuid=b.task_uuid AND i.owner_uuid=b.owner_uuid WHERE b.domain_uuid=? AND b.owner_uuid=? AND b.calendar_uuid=? AND b.state='active' AND e.start<? AND e.end>? ORDER BY e.start,b.uuid LIMIT 101`, domain, actor, calendar, end.Unix(), start.Unix())
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
defer rows.Close()
|
|
out := []TaskDayBlock{}
|
|
for rows.Next() {
|
|
var b TaskDayBlock
|
|
if err = rows.Scan(&b.UUID, &b.TaskUUID, &b.Title, &b.Start, &b.End, &b.EventUID); err != nil {
|
|
return nil, false, err
|
|
}
|
|
out = append(out, b)
|
|
}
|
|
more := len(out) > 100
|
|
if more {
|
|
out = out[:100]
|
|
}
|
|
return out, more, rows.Err()
|
|
}
|