summaryrefslogtreecommitdiff
path: root/Omni/Jr/Web/Types.hs
blob: 025f3a6a5c96491ec443658cc8427b58476f6e19 (plain)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NoImplicitPrelude #-}

-- : dep servant-server
-- : dep lucid
-- : dep http-api-data
-- : dep aeson
module Omni.Jr.Web.Types
  ( TaskFilters (..),
    TimeRange (..),
    SortOrder (..),
    parseSortOrder,
    sortOrderToParam,
    sortOrderLabel,
    sortTasks,
    parseTimeRange,
    timeRangeToParam,
    getTimeRangeStart,
    startOfDay,
    startOfWeek,
    addDays,
    fromGregorian,
    daysSinceEpoch,
    startOfMonth,
    computeMetricsFromActivities,
    HomePage (..),
    ReadyQueuePage (..),
    BlockedPage (..),
    InterventionPage (..),
    TaskListPage (..),
    TaskDetailPage (..),
    GitCommit (..),
    TaskReviewPage (..),
    ReviewInfo (..),
    TaskDiffPage (..),
    StatsPage (..),
    KBPage (..),
    FactDetailPage (..),
    EpicsPage (..),
    RecentActivityNewPartial (..),
    RecentActivityMorePartial (..),
    ReadyCountPartial (..),
    StatusBadgePartial (..),
    PriorityBadgePartial (..),
    TaskListPartial (..),
    TaskMetricsPartial (..),
    AgentEventsPartial (..),
    DescriptionViewPartial (..),
    DescriptionEditPartial (..),
    FactEditForm (..),
    FactCreateForm (..),
    RejectForm (..),
    StatusForm (..),
    PriorityForm (..),
    DescriptionForm (..),
    NotesForm (..),
    CommentForm (..),
    Breadcrumb (..),
    Breadcrumbs,
    CSS,
    SSE,
  )
where

import Alpha
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as LBS
import qualified Data.List as List
import qualified Data.Text as Text
import qualified Data.Text.Lazy as LazyText
import Data.Time (Day, UTCTime (..), diffUTCTime, toGregorian)
import Data.Time.Calendar (DayOfWeek (..))
import Data.Time.Calendar.WeekDate (toWeekCalendar)
import qualified Lucid
import qualified Omni.Task.Core as TaskCore
import Servant (Accept (..), MimeRender (..))
import Web.FormUrlEncoded (FromForm (..), lookupUnique, parseUnique)

data TaskFilters = TaskFilters
  { filterStatus :: Maybe TaskCore.Status,
    filterPriority :: Maybe TaskCore.Priority,
    filterNamespace :: Maybe Text,
    filterType :: Maybe TaskCore.TaskType
  }
  deriving (Show, Eq)

data TimeRange = Today | Week | Month | AllTime
  deriving (Show, Eq)

data SortOrder
  = SortNewest
  | SortOldest
  | SortUpdated
  | SortPriorityHigh
  | SortPriorityLow
  deriving (Show, Eq)

parseSortOrder :: Maybe Text -> SortOrder
parseSortOrder (Just "oldest") = SortOldest
parseSortOrder (Just "updated") = SortUpdated
parseSortOrder (Just "priority-high") = SortPriorityHigh
parseSortOrder (Just "priority-low") = SortPriorityLow
parseSortOrder _ = SortNewest

sortOrderToParam :: SortOrder -> Text
sortOrderToParam SortNewest = "newest"
sortOrderToParam SortOldest = "oldest"
sortOrderToParam SortUpdated = "updated"
sortOrderToParam SortPriorityHigh = "priority-high"
sortOrderToParam SortPriorityLow = "priority-low"

sortOrderLabel :: SortOrder -> Text
sortOrderLabel SortNewest = "Newest First"
sortOrderLabel SortOldest = "Oldest First"
sortOrderLabel SortUpdated = "Recently Updated"
sortOrderLabel SortPriorityHigh = "Priority (High to Low)"
sortOrderLabel SortPriorityLow = "Priority (Low to High)"

sortTasks :: SortOrder -> [TaskCore.Task] -> [TaskCore.Task]
sortTasks SortNewest = List.sortBy (comparing (Down <. TaskCore.taskCreatedAt))
sortTasks SortOldest = List.sortBy (comparing TaskCore.taskCreatedAt)
sortTasks SortUpdated = List.sortBy (comparing (Down <. TaskCore.taskUpdatedAt))
sortTasks SortPriorityHigh = List.sortBy (comparing TaskCore.taskPriority)
sortTasks SortPriorityLow = List.sortBy (comparing (Down <. TaskCore.taskPriority))

parseTimeRange :: Maybe Text -> TimeRange
parseTimeRange (Just "today") = Today
parseTimeRange (Just "week") = Week
parseTimeRange (Just "month") = Month
parseTimeRange _ = AllTime

timeRangeToParam :: TimeRange -> Text
timeRangeToParam Today = "today"
timeRangeToParam Week = "week"
timeRangeToParam Month = "month"
timeRangeToParam AllTime = "all"

getTimeRangeStart :: TimeRange -> UTCTime -> Maybe UTCTime
getTimeRangeStart AllTime _ = Nothing
getTimeRangeStart Today now = Just (startOfDay now)
getTimeRangeStart Week now = Just (startOfWeek now)
getTimeRangeStart Month now = Just (startOfMonth now)

startOfDay :: UTCTime -> UTCTime
startOfDay t = UTCTime (utctDay t) 0

startOfWeek :: UTCTime -> UTCTime
startOfWeek t =
  let day = utctDay t
      (_, _, dow) = toWeekCalendar day
      daysBack = dow - 1
   in UTCTime (addDays (negate (toInteger daysBack)) day) 0

addDays :: Integer -> Day -> Day
addDays n d =
  let (y, m, dayNum) = toGregorian d
   in fromGregorian y m (dayNum + fromInteger n)

fromGregorian :: Integer -> Int -> Int -> Day
fromGregorian y m d = toEnum (fromInteger (daysSinceEpoch y m d))

daysSinceEpoch :: Integer -> Int -> Int -> Integer
daysSinceEpoch y m d =
  let a = (14 - m) `div` 12
      y' = y + 4800 - toInteger a
      m' = m + 12 * a - 3
      jdn = d + (153 * m' + 2) `div` 5 + 365 * fromInteger y' + fromInteger y' `div` 4 - fromInteger y' `div` 100 + fromInteger y' `div` 400 - 32045
   in toInteger jdn - 2440588

startOfMonth :: UTCTime -> UTCTime
startOfMonth t =
  let day = utctDay t
      (y, m, _) = toGregorian day
   in UTCTime (fromGregorian y m 1) 0

computeMetricsFromActivities :: [TaskCore.Task] -> [TaskCore.TaskActivity] -> TaskCore.AggregatedMetrics
computeMetricsFromActivities tasks activities =
  let completedCount = length [t | t <- tasks, TaskCore.taskStatus t == TaskCore.Done]
      totalCost = sum [c | act <- activities, Just c <- [TaskCore.activityCostCents act]]
      totalTokens = sum [t | act <- activities, Just t <- [TaskCore.activityTokensUsed act]]
      totalDuration = sum [calcDuration act | act <- activities]
   in TaskCore.AggregatedMetrics
        { TaskCore.aggTotalCostCents = totalCost,
          TaskCore.aggTotalDurationSeconds = totalDuration,
          TaskCore.aggCompletedTasks = completedCount,
          TaskCore.aggTotalTokens = totalTokens
        }
  where
    calcDuration act = case (TaskCore.activityStartedAt act, TaskCore.activityCompletedAt act) of
      (Just start, Just end) -> floor (diffUTCTime end start)
      _ -> 0

data CSS

instance Accept CSS where
  contentType _ = "text/css"

instance MimeRender CSS LazyText.Text where
  mimeRender _ = LazyText.encodeUtf8

data SSE

instance Accept SSE where
  contentType _ = "text/event-stream"

instance MimeRender SSE BS.ByteString where
  mimeRender _ = LBS.fromStrict

data HomePage = HomePage TaskCore.TaskStats [TaskCore.Task] [TaskCore.Task] Bool TaskCore.AggregatedMetrics TimeRange UTCTime

data ReadyQueuePage = ReadyQueuePage [TaskCore.Task] SortOrder UTCTime

data BlockedPage = BlockedPage [(TaskCore.Task, Int)] SortOrder UTCTime

data InterventionPage = InterventionPage TaskCore.HumanActionItems SortOrder UTCTime

data TaskListPage = TaskListPage [TaskCore.Task] TaskFilters SortOrder UTCTime

data TaskDetailPage
  = TaskDetailFound TaskCore.Task [TaskCore.Task] [TaskCore.TaskActivity] (Maybe TaskCore.RetryContext) [GitCommit] (Maybe TaskCore.AggregatedMetrics) [TaskCore.StoredEvent] UTCTime
  | TaskDetailNotFound Text

data GitCommit = GitCommit
  { commitHash :: Text,
    commitShortHash :: Text,
    commitSummary :: Text,
    commitAuthor :: Text,
    commitRelativeDate :: Text,
    commitFilesChanged :: Int
  }
  deriving (Show, Eq)

data TaskReviewPage
  = ReviewPageFound TaskCore.Task ReviewInfo
  | ReviewPageNotFound Text

data ReviewInfo
  = ReviewNoCommit
  | ReviewMergeConflict Text [Text]
  | ReviewReady Text Text

data TaskDiffPage
  = DiffPageFound Text Text Text
  | DiffPageNotFound Text Text

data StatsPage = StatsPage TaskCore.TaskStats (Maybe Text)

newtype KBPage = KBPage [TaskCore.Fact]

data FactDetailPage
  = FactDetailFound TaskCore.Fact UTCTime
  | FactDetailNotFound Int

data FactEditForm = FactEditForm Text Text Text

instance FromForm FactEditForm where
  fromForm form = do
    content <- parseUnique "content" form
    let files = fromRight "" (lookupUnique "files" form)
    let confidence = fromRight "0.8" (lookupUnique "confidence" form)
    Right (FactEditForm content files confidence)

data FactCreateForm = FactCreateForm Text Text Text Text

instance FromForm FactCreateForm where
  fromForm form = do
    project <- parseUnique "project" form
    content <- parseUnique "content" form
    let files = fromRight "" (lookupUnique "files" form)
    let confidence = fromRight "0.8" (lookupUnique "confidence" form)
    Right (FactCreateForm project content files confidence)

data EpicsPage = EpicsPage [TaskCore.Task] [TaskCore.Task] SortOrder

data RecentActivityNewPartial = RecentActivityNewPartial [TaskCore.Task] (Maybe Int)

data RecentActivityMorePartial = RecentActivityMorePartial [TaskCore.Task] Int Bool

newtype ReadyCountPartial = ReadyCountPartial Int

data StatusBadgePartial = StatusBadgePartial TaskCore.Status Text

data PriorityBadgePartial = PriorityBadgePartial TaskCore.Priority Text

newtype TaskListPartial = TaskListPartial [TaskCore.Task]

data TaskMetricsPartial = TaskMetricsPartial Text [TaskCore.TaskActivity] (Maybe TaskCore.RetryContext) UTCTime

data AgentEventsPartial = AgentEventsPartial Text [TaskCore.StoredEvent] Bool UTCTime

data DescriptionViewPartial = DescriptionViewPartial Text Text Bool

data DescriptionEditPartial = DescriptionEditPartial Text Text Bool

newtype RejectForm = RejectForm (Maybe Text)

instance FromForm RejectForm where
  fromForm form = Right (RejectForm (either (const Nothing) Just (lookupUnique "notes" form)))

newtype StatusForm = StatusForm TaskCore.Status

instance FromForm StatusForm where
  fromForm form = do
    statusText <- parseUnique "status" form
    case readMaybe (Text.unpack statusText) of
      Just s -> Right (StatusForm s)
      Nothing -> Left "Invalid status"

newtype PriorityForm = PriorityForm TaskCore.Priority

instance FromForm PriorityForm where
  fromForm form = do
    priorityText <- parseUnique "priority" form
    case readMaybe (Text.unpack priorityText) of
      Just p -> Right (PriorityForm p)
      Nothing -> Left "Invalid priority"

newtype DescriptionForm = DescriptionForm Text

instance FromForm DescriptionForm where
  fromForm form = do
    desc <- parseUnique "description" form
    Right (DescriptionForm desc)

newtype NotesForm = NotesForm Text

instance FromForm NotesForm where
  fromForm form = do
    notes <- parseUnique "notes" form
    Right (NotesForm notes)

newtype CommentForm = CommentForm Text

instance FromForm CommentForm where
  fromForm form = do
    commentText <- parseUnique "comment" form
    Right (CommentForm commentText)

data Breadcrumb = Breadcrumb
  { breadcrumbLabel :: Text,
    breadcrumbUrl :: Maybe Text
  }

type Breadcrumbs = [Breadcrumb]