diff options
Diffstat (limited to 'Omni/Agent/Tools')
| -rw-r--r-- | Omni/Agent/Tools/Todos.hs | 67 |
1 files changed, 58 insertions, 9 deletions
diff --git a/Omni/Agent/Tools/Todos.hs b/Omni/Agent/Tools/Todos.hs index 81253c1..4c7d2be 100644 --- a/Omni/Agent/Tools/Todos.hs +++ b/Omni/Agent/Tools/Todos.hs @@ -27,6 +27,11 @@ module Omni.Agent.Tools.Todos completeTodo, deleteTodo, + -- * Reminders + listTodosDueForReminder, + markReminderSent, + reminderInterval, + -- * Database initTodosTable, @@ -40,7 +45,7 @@ import Alpha import Data.Aeson ((.!=), (.:), (.:?), (.=)) import qualified Data.Aeson as Aeson import qualified Data.Text as Text -import Data.Time (UTCTime, getCurrentTime) +import Data.Time (NominalDiffTime, UTCTime, addUTCTime, getCurrentTime) import Data.Time.Format (defaultTimeLocale, parseTimeM) import qualified Database.SQLite.Simple as SQL import qualified Omni.Agent.Engine as Engine @@ -75,7 +80,8 @@ test = todoTitle = "Buy milk", todoDueDate = Just now, todoCompleted = False, - todoCreatedAt = now + todoCreatedAt = now, + todoLastRemindedAt = Nothing } case Aeson.decode (Aeson.encode td) of Nothing -> Test.assertFailure "Failed to decode Todo" @@ -93,7 +99,8 @@ data Todo = Todo todoTitle :: Text, todoDueDate :: Maybe UTCTime, todoCompleted :: Bool, - todoCreatedAt :: UTCTime + todoCreatedAt :: UTCTime, + todoLastRemindedAt :: Maybe UTCTime } deriving (Show, Eq, Generic) @@ -105,7 +112,8 @@ instance Aeson.ToJSON Todo where "title" .= todoTitle td, "due_date" .= todoDueDate td, "completed" .= todoCompleted td, - "created_at" .= todoCreatedAt td + "created_at" .= todoCreatedAt td, + "last_reminded_at" .= todoLastRemindedAt td ] instance Aeson.FromJSON Todo where @@ -117,6 +125,7 @@ instance Aeson.FromJSON Todo where <*> (v .:? "due_date") <*> (v .: "completed") <*> (v .: "created_at") + <*> (v .:? "last_reminded_at") instance SQL.FromRow Todo where fromRow = @@ -126,6 +135,7 @@ instance SQL.FromRow Todo where <*> SQL.field <*> SQL.field <*> SQL.field + <*> SQL.field initTodosTable :: SQL.Connection -> IO () initTodosTable conn = do @@ -137,7 +147,8 @@ initTodosTable conn = do \ title TEXT NOT NULL,\ \ due_date TIMESTAMP,\ \ completed INTEGER NOT NULL DEFAULT 0,\ - \ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\ + \ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\ + \ last_reminded_at TIMESTAMP\ \)" SQL.execute_ conn @@ -145,6 +156,14 @@ initTodosTable conn = do SQL.execute_ conn "CREATE INDEX IF NOT EXISTS idx_todos_due ON todos(user_id, due_date)" + migrateTodosTable conn + +migrateTodosTable :: SQL.Connection -> IO () +migrateTodosTable conn = do + cols <- SQL.query_ conn "PRAGMA table_info(todos)" :: IO [(Int, Text, Text, Int, Maybe Text, Int)] + let colNames = map (\(_, name, _, _, _, _) -> name) cols + unless ("last_reminded_at" `elem` colNames) <| do + SQL.execute_ conn "ALTER TABLE todos ADD COLUMN last_reminded_at TIMESTAMP" parseDueDate :: Text -> Maybe UTCTime parseDueDate txt = @@ -172,7 +191,8 @@ createTodo uid title maybeDueDateStr = do todoTitle = title, todoDueDate = dueDate, todoCompleted = False, - todoCreatedAt = now + todoCreatedAt = now, + todoLastRemindedAt = Nothing } listTodos :: Text -> Int -> IO [Todo] @@ -181,7 +201,7 @@ listTodos uid limit = initTodosTable conn SQL.query conn - "SELECT id, user_id, title, due_date, completed, created_at \ + "SELECT id, user_id, title, due_date, completed, created_at, last_reminded_at \ \FROM todos WHERE user_id = ? \ \ORDER BY completed ASC, due_date ASC NULLS LAST, created_at DESC LIMIT ?" (uid, limit) @@ -192,7 +212,7 @@ listPendingTodos uid limit = initTodosTable conn SQL.query conn - "SELECT id, user_id, title, due_date, completed, created_at \ + "SELECT id, user_id, title, due_date, completed, created_at, last_reminded_at \ \FROM todos WHERE user_id = ? AND completed = 0 \ \ORDER BY due_date ASC NULLS LAST, created_at DESC LIMIT ?" (uid, limit) @@ -204,11 +224,40 @@ listOverdueTodos uid = do initTodosTable conn SQL.query conn - "SELECT id, user_id, title, due_date, completed, created_at \ + "SELECT id, user_id, title, due_date, completed, created_at, last_reminded_at \ \FROM todos WHERE user_id = ? AND completed = 0 AND due_date < ? \ \ORDER BY due_date ASC" (uid, now) +reminderInterval :: NominalDiffTime +reminderInterval = 24 * 60 * 60 + +listTodosDueForReminder :: IO [Todo] +listTodosDueForReminder = do + now <- getCurrentTime + let cutoff = addUTCTime (negate reminderInterval) now + Memory.withMemoryDb <| \conn -> do + initTodosTable conn + SQL.query + conn + "SELECT id, user_id, title, due_date, completed, created_at, last_reminded_at \ + \FROM todos \ + \WHERE completed = 0 \ + \ AND due_date IS NOT NULL \ + \ AND due_date < ? \ + \ AND (last_reminded_at IS NULL OR last_reminded_at < ?)" + (now, cutoff) + +markReminderSent :: Int -> IO () +markReminderSent tid = do + now <- getCurrentTime + Memory.withMemoryDb <| \conn -> do + initTodosTable conn + SQL.execute + conn + "UPDATE todos SET last_reminded_at = ? WHERE id = ?" + (now, tid) + completeTodo :: Text -> Int -> IO Bool completeTodo uid tid = Memory.withMemoryDb <| \conn -> do |
