Types.purs 21.4 KB
Newer Older
1
module Gargantext.Types where
2

3
import Data.Argonaut (class DecodeJson, class EncodeJson, decodeJson, encodeJson, jsonEmptyObject, (.:), (:=), (~>))
4
import Data.Array as A
5
import Data.Either (Either(..))
6 7 8 9
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Eq (genericEq)
import Data.Generic.Rep.Ord (genericCompare)
import Data.Generic.Rep.Show (genericShow)
10
import Data.Int (toNumber)
11
import Data.Maybe (Maybe(..), maybe, fromMaybe)
12
import Effect.Aff (Aff)
Alexandre Delanoë's avatar
Alexandre Delanoë committed
13
import Gargantext.Prelude (class Read, read, class Show, show)
14
import Prelude
15
import Prim.Row (class Union)
James Laver's avatar
James Laver committed
16
import URI.Query (Query)
17

18 19 20
type ID     = Int
type Name   = String
type Reload = Int
21

22
newtype SessionId = SessionId String
23
type NodeID = Int
24 25 26 27 28 29 30 31 32

derive instance genericSessionId :: Generic SessionId _

instance eqSessionId :: Eq SessionId where
  eq = genericEq

instance showSessionId :: Show SessionId where
  show (SessionId s) = s

33
data TermSize = MonoTerm | MultiTerm
34

35 36
data Term = Term String TermList

37
derive instance eqTermSize :: Eq TermSize
38

James Laver's avatar
James Laver committed
39 40 41 42
-- | Converts a data structure to a query string
class ToQuery a where
  toQuery :: a -> Query

43
instance showTermSize :: Show TermSize where
44 45 46
  show MonoTerm  = "MonoTerm"
  show MultiTerm = "MultiTerm"

47 48 49 50 51
instance readTermSize :: Read TermSize where
  read :: String -> Maybe TermSize
  read "MonoTerm"  = Just MonoTerm
  read "MultiTerm" = Just MultiTerm
  read _           = Nothing
52

53 54
termSizes :: Array { desc :: String, mval :: Maybe TermSize }
termSizes = [ { desc: "All types",        mval: Nothing        }
55 56 57 58
            , { desc: "One-word terms",   mval: Just MonoTerm  }
            , { desc: "Multi-word terms", mval: Just MultiTerm }
            ]

59
data TermList = MapTerm | StopTerm | CandidateTerm
60
-- TODO use generic JSON instance
61 62

derive instance eqTermList :: Eq TermList
63
derive instance ordTermList :: Ord TermList
64

65
instance encodeJsonTermList :: EncodeJson TermList where
66
  encodeJson MapTerm       = encodeJson "MapTerm"
67 68
  encodeJson StopTerm      = encodeJson "StopTerm"
  encodeJson CandidateTerm = encodeJson "CandidateTerm"
69

70
instance decodeJsonTermList :: DecodeJson TermList where
71 72 73
  decodeJson json = do
    s <- decodeJson json
    case s of
74
      "MapTerm"       -> pure MapTerm
75 76
      "StopTerm"      -> pure StopTerm
      "CandidateTerm" -> pure CandidateTerm
77
      _               -> Left "Unexpected list name"
78 79 80 81

type ListTypeId = Int

listTypeId :: TermList -> ListTypeId
82
listTypeId MapTerm     = 1
83 84 85 86
listTypeId StopTerm      = 2
listTypeId CandidateTerm = 3

instance showTermList :: Show TermList where
87
  show MapTerm       = "MapTerm"
88 89
  show StopTerm      = "StopTerm"
  show CandidateTerm = "CandidateTerm"
90

91 92
-- TODO: Can we replace the show instance above with this?
termListName :: TermList -> String
93
termListName MapTerm = "Map List"
94 95 96
termListName StopTerm = "Stop List"
termListName CandidateTerm = "Candidate List"

97 98
instance readTermList :: Read TermList where
  read :: String -> Maybe TermList
99
  read "MapTerm"     = Just MapTerm
100 101 102
  read "StopTerm"      = Just StopTerm
  read "CandidateTerm" = Just CandidateTerm
  read _               = Nothing
103 104 105

termLists :: Array { desc :: String, mval :: Maybe TermList }
termLists = [ { desc: "All terms",   mval: Nothing      }
106
            , { desc: "Map terms",   mval: Just MapTerm   }
107 108 109 110
            , { desc: "Stop terms",  mval: Just StopTerm  }
            , { desc: "Candidate terms", mval: Just CandidateTerm }
            ]

111 112 113
-- | Proof that row `r` is a subset of row `s`
class Optional (r :: # Type) (s :: # Type)
instance optionalInstance :: Union r t s => Optional r s
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

showTabType' :: TabType -> String
showTabType' (TabCorpus   t) = show t
showTabType' (TabDocument t) = show t
showTabType' (TabPairing t) = show t

data TabPostQuery = TabPostQuery {
    offset :: Int
  , limit :: Int
  , orderBy :: OrderBy
  , tabType :: TabType
  , query :: String
  }

instance encodeJsonTabPostQuery :: EncodeJson TabPostQuery where
  encodeJson (TabPostQuery post) =
        "view"       := showTabType' post.tabType
     ~> "offset"     := post.offset
     ~> "limit"      := post.limit
     ~> "orderBy"    := show post.orderBy
     ~> "query"      := post.query
     ~> jsonEmptyObject

data NodeType = NodeUser
138
              | Folder | FolderPrivate | FolderShared | FolderPublic
139 140 141
              | Annuaire
              | NodeContact
              | Corpus
142
              | Team
143 144 145 146 147 148 149 150 151 152 153
              | Url_Document
              | Dashboard
              | Error
              | Graph
              | Phylo
              | Individu
              | Node
              | Nodes
              | Tree
              | NodeList
              | Texts
154 155 156
              -- TODO Optional Nodes
              | NodeFrameWrite
              | NodeFrameCalc
157 158
              | NodePublic NodeType

159 160 161 162 163

derive instance eqNodeType :: Eq NodeType

instance showNodeType :: Show NodeType where
  show NodeUser      = "NodeUser"
164 165

  show Folder        = "NodeFolder"
166
  show FolderPrivate = "NodeFolderPrivate"  -- Node Private Worktop
Alexandre Delanoë's avatar
Alexandre Delanoë committed
167
  show FolderShared  = "NodeFolderShared"   -- Node Share Worktop
168
  show FolderPublic  = "NodeFolderPublic"   -- Node Public Worktop
169

170
  show Annuaire      = "NodeAnnuaire"
171 172 173 174 175 176 177 178 179 180 181
  show NodeContact   = "NodeContact"
  show Corpus        = "NodeCorpus"
  show Dashboard     = "NodeDashboard"
  show Url_Document  = "NodeDocument"
  show Error         = "NodeError"
  show Graph         = "NodeGraph"
  show Phylo         = "NodePhylo"
  show Individu      = "NodeIndividu"
  show Node          = "Node"
  show Nodes         = "Nodes"
  show Tree          = "NodeTree"
182
  show Team          = "NodeTeam"
183 184
  show NodeList      = "NodeList"
  show Texts         = "NodeTexts"
185 186
  show NodeFrameWrite = "NodeFrameWrite"
  show NodeFrameCalc  = "NodeFrameCalc"
187
  show (NodePublic nt) = "NodePublic" <> show nt
188

189

190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
instance readNodeType :: Read NodeType where
  read "NodeUser"          = Just NodeUser
  read "NodeFolder"        = Just Folder
  read "NodeFolderPrivate" = Just FolderPrivate
  read "NodeFolderShared"  = Just FolderShared
  read "NodeFolderPublic"  = Just FolderPublic
  read "NodeAnnuaire"  = Just Annuaire
  read "NodeDashboard" = Just Dashboard
  read "Document"      = Just Url_Document
  read "NodeGraph"     = Just Graph
  read "NodePhylo"     = Just Phylo
  read "Individu"      = Just Individu
  read "Node"          = Just Node
  read "Nodes"         = Just Nodes
  read "NodeCorpus"    = Just Corpus
  read "NodeContact"   = Just NodeContact
  read "Tree"          = Just Tree
  read "NodeTeam"      = Just Team
  read "NodeList"      = Just NodeList
  read "NodeTexts"     = Just Texts
  read "Annuaire"      = Just Annuaire
211 212
  read "NodeFrameWrite" = Just NodeFrameWrite
  read "NodeFrameCalc"  = Just NodeFrameCalc
213
  -- TODO NodePublic read ?
214
  read _               = Nothing
215 216 217 218


fldr :: NodeType -> Boolean -> String
fldr NodeUser false = "fa fa-user-circle"
219 220
fldr NodeUser true  = "fa fa-user"
------------------------------------------------------
221 222
fldr Folder  false  = "fa fa-folder"
fldr Folder  true   = "fa fa-folder-open-o"
223
------------------------------------------------------
224
fldr FolderPrivate true  = "fa fa-lock"
225
fldr FolderPrivate false = "fa fa-lock-circle"
226

227
fldr FolderShared  true  = "fa fa-share-alt"
Alexandre Delanoë's avatar
Alexandre Delanoë committed
228
fldr FolderShared  false = "fa fa-share-circle"
229
fldr Team  true   = "fa fa-users"
230
fldr Team  false  = "fa fa-users-closed"
231

232 233
fldr FolderPublic true  = "fa fa-globe-circle"
fldr FolderPublic false = "fa fa-globe"
234
------------------------------------------------------
235

236
fldr Corpus true  = "fa fa-book"
Alexandre Delanoë's avatar
Alexandre Delanoë committed
237
fldr Corpus false = "fa fa-book-circle"
238 239 240 241 242 243 244 245

fldr Phylo _ = "fa fa-code-fork"

fldr Graph _ = "fa fa-hubzilla"
fldr Texts _ = "fa fa-newspaper-o"
fldr Dashboard _ = "fa fa-signal"
fldr NodeList _ = "fa fa-list"

246 247
fldr Annuaire true  = "fa fa-address-card-o"
fldr Annuaire false = "fa fa-address-card"
248 249 250 251

fldr NodeContact true  = "fa fa-address-card-o"
fldr NodeContact false = "fa fa-address-card"

252
fldr NodeFrameWrite true  = "fa fa-file-text-o"
253
fldr NodeFrameWrite false = "fa fa-file-text"
254

255 256
fldr NodeFrameCalc true  = "fa fa-calculator"
fldr NodeFrameCalc false = "fa fa-calculator"
257

258 259
fldr (NodePublic nt) b   = fldr nt b

260
fldr _        true   = "fa fa-folder-open"
261
fldr _        false  = "fa fa-folder-o"
262 263


264 265 266 267 268 269 270 271 272
publicize :: NodeType -> NodeType
publicize (NodePublic nt) = NodePublic nt
publicize nt              = NodePublic nt

isPublic :: NodeType -> Boolean
isPublic (NodePublic _) = true
isPublic FolderPublic   = true
isPublic _              = false

273 274 275 276 277 278 279 280 281 282 283 284
{-
------------------------------------------------------------
instance ordNodeType :: Ord NodeType where
  compare n1 n2 = compare (show n1) (show n2)

instance eqNodeType :: Eq NodeType where
  eq n1 n2  = eq (show n1) (show n2)
-}
------------------------------------------------------------
instance decodeJsonNodeType :: DecodeJson NodeType where
  decodeJson json = do
    obj <- decodeJson json
285
    pure $ fromMaybe Error $ read obj
286 287 288 289 290

instance encodeJsonNodeType :: EncodeJson NodeType where
  encodeJson nodeType = encodeJson $ show nodeType

nodeTypePath :: NodeType -> String
291 292
nodeTypePath Folder    = "folder"
nodeTypePath FolderPrivate = "folderPrivate"
293
nodeTypePath FolderShared  = "folderShared"
294
nodeTypePath FolderPublic  = "folderPublic"
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
nodeTypePath Annuaire  = "annuaire"
nodeTypePath Corpus    = "corpus"
nodeTypePath Dashboard = "dashboard"
nodeTypePath Url_Document  = "document"
nodeTypePath Error     = "ErrorNodeType"
nodeTypePath Graph     = "graph"
nodeTypePath Phylo     = "phylo"
nodeTypePath Individu  = "individu"
nodeTypePath Node      = "node"
nodeTypePath Nodes     = "nodes"
nodeTypePath NodeUser  = "user"
nodeTypePath NodeContact = "contact"
nodeTypePath Tree      = "tree"
nodeTypePath NodeList  = "lists"
nodeTypePath Texts     = "texts"
310
nodeTypePath Team      = "team"
311 312
nodeTypePath NodeFrameWrite = "write"
nodeTypePath NodeFrameCalc  = "calc"
313
nodeTypePath (NodePublic nt) = nodeTypePath nt
314

315 316 317 318
------------------------------------------------------------

type ListId = Int

319 320 321
data ScoreType = Occurrences

derive instance genericScoreType :: Generic ScoreType _
322 323
instance eqScoreType :: Eq ScoreType where
  eq = genericEq
324 325 326
instance showScoreType :: Show ScoreType where
  show = genericShow

327 328
type SearchQuery = String

329 330 331
type NgramsGetOpts =
  { tabType        :: TabType
  , limit          :: Limit
332
  , offset         :: Maybe Offset
333 334 335 336
  , orderBy        :: Maybe OrderBy
  , listIds        :: Array ListId
  , termListFilter :: Maybe TermList
  , termSizeFilter :: Maybe TermSize
337
  , searchQuery    :: SearchQuery
338 339
  }

340 341 342 343 344 345
type NgramsGetTableAllOpts =
  { tabType        :: TabType
  , listIds        :: Array ListId
  , scoreType      :: ScoreType
  }

346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
type SearchOpts =
  { {-id :: Int
    , query    :: Array String
    ,-}
    listId   :: Int
  , limit    :: Limit
  , offset   :: Offset
  , orderBy  :: Maybe OrderBy
  }

type CorpusMetricOpts =
  { tabType :: TabType
  , listId  :: ListId
  , limit   :: Maybe Limit
  }

type ChartOpts =
  { chartType :: ChartType
364 365
  , listId    :: ListId
  , limit     :: Maybe Limit
366 367 368
  , tabType   :: TabType
  }

James Laver's avatar
James Laver committed
369
data NodePath = NodePath SessionId NodeType (Maybe Id)
370 371

nodePath :: NodePath -> String
James Laver's avatar
James Laver committed
372
nodePath (NodePath s t i) = nodeTypePath t <> "/" <> show s <> id
James Laver's avatar
James Laver committed
373
  where id = maybe "" (\j -> "/" <> show j) i
374

375
data ChartType = Histo | Scatter | ChartPie | ChartBar | ChartTree
376 377 378 379 380

instance showChartType :: Show ChartType
  where
    show Histo    = "chart"
    show Scatter  = "scatter"
381
    show ChartBar = "bar"
382 383 384
    show ChartPie = "pie"
    show ChartTree = "tree"

385 386 387 388 389 390 391 392
chartTypeFromString :: String -> Maybe ChartType
chartTypeFromString "bar"     = Just ChartBar
chartTypeFromString "chart"   = Just Histo
chartTypeFromString "pie"     = Just ChartPie
chartTypeFromString "scatter" = Just Scatter
chartTypeFromString "tree"    = Just ChartTree
chartTypeFromString _         = Nothing

393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
type Id  = Int
type Limit  = Int
type Offset = Int
data OrderBy = DateAsc  | DateDesc
             | TitleAsc | TitleDesc
             | ScoreAsc | ScoreDesc
             | TermAsc  | TermDesc
             | SourceAsc | SourceDesc

derive instance genericOrderBy :: Generic OrderBy _

instance showOrderBy :: Show OrderBy where
  show = genericShow

------------------------------------------------------------
408 409 410
-- V0 is the dummy case (impossible)
data ApiVersion = V0 | V10 | V11

411
instance showApiVersion :: Show ApiVersion where
412
  show V0  = "v0"
413 414 415 416 417 418 419 420
  show V10 = "v1.0"
  show V11 = "v1.1"

instance eqApiVersion :: Eq ApiVersion where
  eq V10 V10 = true
  eq V11 V11 = true
  eq _ _ = false

421 422 423 424 425 426 427 428 429 430 431 432
instance encodeJsonApiVersion :: EncodeJson ApiVersion where
  encodeJson v = encodeJson (show v)

instance decodeJsonApiVersion :: DecodeJson ApiVersion where
  decodeJson json = do
    v <- decodeJson json
    case v of
         "v1.0" -> pure V10
         "v1.1" -> pure V11
         _      -> pure V0
------------------------------------------------------------

433 434
-- Types of ngrams. Used to display user-selectable tabs and is sent via API,
-- wrapped in `TabNgramType a :: TabSubType`
435 436 437
data CTabNgramType = CTabTerms | CTabSources | CTabAuthors | CTabInstitutes

derive instance eqCTabNgramType :: Eq CTabNgramType
438
derive instance ordCTabNgramType :: Ord CTabNgramType
439 440 441 442 443 444 445 446 447 448

instance showCTabNgramType :: Show CTabNgramType where
  show CTabTerms      = "Terms"
  show CTabSources    = "Sources"
  show CTabAuthors    = "Authors"
  show CTabInstitutes = "Institutes"

data PTabNgramType = PTabPatents | PTabBooks | PTabCommunication

derive instance eqPTabNgramType :: Eq PTabNgramType
449
derive instance ordPTabNgramType :: Ord PTabNgramType
450 451 452 453 454 455 456 457 458

instance showPTabNgramType :: Show PTabNgramType where
  show PTabPatents       = "Patents"
  show PTabBooks         = "Books"
  show PTabCommunication = "Communication"

data TabSubType a = TabDocs | TabNgramType a | TabTrash | TabMoreLikeFav | TabMoreLikeTrash

derive instance eqTabSubType :: Eq a => Eq (TabSubType a)
459
derive instance ordTabSubType :: Ord a => Ord (TabSubType a)
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
{- instance encodeTabSubType a :: EncodeJson a => EncodeJson (TabSubType a) where
  encodeJson TabDocs =
       "type" := "TabDocs"
    ~> "data" := Nothing
    ~> jsonEmptyObject
  encodeJson (TabNgramType a) =
       "type" := "TabNgramType"
    ~> "data" := encodeJson a
    ~> jsonEmptyObject
  encodeJson TabTrash =
       "type" := "TabTrash"
    ~> "data" := Nothing
    ~> jsonEmptyObject
  encodeJson TabMoreLikeFav =
       "type" := "TabMoreLikeFav"
    ~> "data" := Nothing
    ~> jsonEmptyObject
  encodeJson TabMoreLikeTrash =
       "type" := "TabMoreLikeTrash"
    ~> "data" := Nothing
    ~> jsonEmptyObject
instance decodeTabSubType a :: DecodeJson a => DecodeJson (TabSubType a) where
  decodeJson j = do
    obj <- decodeJson j
    typ <- obj .: "type"
    dat <- obj .: "data"
    case typ of
      "TabDocs" -> TabDocs
      "TabNgramType" -> TabNgramType dat
      "TabTrash" -> TabTrash
      "TabMoreLikeFav" -> TabMoreLikeFav
      "TabMoreLikeTrash" -> TabMoreLikeTrash
      _ -> Left ("Unknown type '" <> typ <> "'") -}
493 494 495 496 497 498 499 500 501 502 503 504 505

instance showTabSubType :: Show a => Show (TabSubType a) where
  show TabDocs          = "Docs"
  show (TabNgramType a) = show a
  show TabTrash         = "Trash"
  show TabMoreLikeFav   = "MoreFav"
  show TabMoreLikeTrash = "MoreTrash"

data TabType
  = TabCorpus   (TabSubType CTabNgramType)
  | TabPairing  (TabSubType PTabNgramType)
  | TabDocument (TabSubType CTabNgramType)    

506
derive instance genericTabType :: Generic TabType _
507
derive instance eqTabType :: Eq TabType
508
derive instance ordTabType :: Ord TabType
509 510
instance showTabType :: Show TabType where
  show = genericShow
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
{- instance encodeTabType :: EncodeJson TabType where
  encodeJson (TabCorpus d) = 
       "type"  := "TabCorpus"
    ~> "data"  := encodeJson d
    ~> jsonEmptyObject
  encodeJson (TabDocument d) = 
       "type"  := "TabDocument"
    ~> "data"  := encodeJson d
    ~> jsonEmptyObject
  encodeJson (TabPairing d) = 
       "type"  := "TabPairing"
    ~> "data"  := encodeJson d
    ~> jsonEmptyObject
instance decodeTabType :: DecodeJson TabType where
  decodeJson j = do
    obj <- decodeJson j
    typ <- obj .: "type"
    dat <- obj .: "data"
    case typ of
      "TabCorpus" -> TabCorpus dat
      "TabDocument" -> TabDocument dat
      "TabPairing" -> TabPairing dat
      _ -> Left ("Unknown type '" <> typ <> "'") -}
534 535 536

type TableResult a = {count :: Int, docs :: Array a}
type AffTableResult a = Aff (TableResult a)
537

538 539 540 541
data Mode = Authors
          | Sources
          | Institutes
          | Terms
542 543 544 545 546 547 548

derive instance genericMode :: Generic Mode _
instance showMode :: Show Mode where
  show = genericShow
derive instance eqMode :: Eq Mode
instance ordMode :: Ord Mode where
  compare = genericCompare
549 550 551
instance encodeMode :: EncodeJson Mode where
  encodeJson x = encodeJson $ show x

552 553 554 555 556 557 558 559 560 561 562 563
modeTabType :: Mode -> CTabNgramType
modeTabType Authors    = CTabAuthors
modeTabType Sources    = CTabSources
modeTabType Institutes = CTabInstitutes
modeTabType Terms      = CTabTerms

modeFromString :: String -> Maybe Mode
modeFromString "Authors" = Just Authors
modeFromString "Sources" = Just Sources
modeFromString "Institutes" = Just Institutes
modeFromString "Terms" = Just Terms
modeFromString _ = Nothing
564

565 566 567
-- Async tasks

-- corresponds to /add/form/async or /add/query/async
568 569 570
data AsyncTaskType = Form
                   | GraphT
                   | Query
571
                   | AddNode
572
                   | UpdateNode
573

574
derive instance genericAsyncTaskType :: Generic AsyncTaskType _
575 576 577 578 579 580
instance eqAsyncTaskType :: Eq AsyncTaskType where
  eq = genericEq
instance showAsyncTaskType :: Show AsyncTaskType where
  show = genericShow
instance encodeJsonAsyncTaskType :: EncodeJson AsyncTaskType where
  encodeJson t     = encodeJson $ show t
581 582 583 584 585 586 587
instance decodeJsonAsyncTaskType :: DecodeJson AsyncTaskType where
  decodeJson json = do
    obj <- decodeJson json
    case obj of
      "Form"       -> pure Form
      "GraphT"     -> pure GraphT
      "Query"      -> pure Query
Alexandre Delanoë's avatar
Alexandre Delanoë committed
588
      "AddNode"    -> pure AddNode
589
      s            -> Left ("Unknown string " <> s)
590 591

asyncTaskTypePath :: AsyncTaskType -> String
592
asyncTaskTypePath Form   = "add/form/async/"
593 594
asyncTaskTypePath Query  = "query/"
asyncTaskTypePath GraphT = "async/"
595
asyncTaskTypePath AddNode = "async/nobody/"
596
asyncTaskTypePath UpdateNode = "update/"
597

598

599 600
type AsyncTaskID = String

601 602 603 604 605 606 607
data AsyncTaskStatus = Running
                     | Pending
                     | Received
                     | Started
                     | Failed
                     | Finished
                     | Killed
608
derive instance genericAsyncTaskStatus :: Generic AsyncTaskStatus _
609

610 611
instance showAsyncTaskStatus :: Show AsyncTaskStatus where
  show = genericShow
612
derive instance eqAsyncTaskStatus :: Eq AsyncTaskStatus
613

614 615
instance encodeJsonAsyncTaskStatus :: EncodeJson AsyncTaskStatus where
  encodeJson s = encodeJson $ show s
616

617 618 619
instance decodeJsonAsyncTaskStatus :: DecodeJson AsyncTaskStatus where
  decodeJson json = do
    obj <- decodeJson json
620 621 622 623 624 625 626 627 628 629 630
    pure $ fromMaybe Running $ read obj

instance readAsyncTaskStatus :: Read AsyncTaskStatus where
  read "IsFailure"  = Just Failed
  read "IsFinished" = Just Finished
  read "IsKilled"   = Just Killed
  read "IsPending"  = Just Pending
  read "IsReceived" = Just Received
  read "IsRunning"  = Just Running
  read "IsStarted"  = Just Started
  read _            = Nothing
631

632 633 634 635 636
newtype AsyncTask =
  AsyncTask { id     :: AsyncTaskID
            , status :: AsyncTaskStatus
            }

637
derive instance genericAsyncTask :: Generic AsyncTask _
638 639 640 641 642 643 644
instance eqAsyncTask :: Eq AsyncTask where
  eq = genericEq
instance encodeJsonAsyncTask :: EncodeJson AsyncTask where
  encodeJson (AsyncTask { id, status }) =
        "id"       := id
     ~> "status"   := status
     ~> jsonEmptyObject
645 646
instance decodeJsonAsyncTask :: DecodeJson AsyncTask where
  decodeJson json = do
647 648
    obj    <- decodeJson json
    id     <- obj .: "id"
649
    status <- obj .: "status"
650
    pure $ AsyncTask { id, status }
651 652 653 654 655

newtype AsyncTaskWithType = AsyncTaskWithType {
    task :: AsyncTask
  , typ  :: AsyncTaskType
  }
656 657 658 659 660 661 662 663
derive instance genericAsyncTaskWithType :: Generic AsyncTaskWithType _
instance eqAsyncTaskWithType :: Eq AsyncTaskWithType where
  eq = genericEq
instance encodeJsonAsyncTaskWithType :: EncodeJson AsyncTaskWithType where
  encodeJson (AsyncTaskWithType { task, typ }) =
        "task"       := task
     ~> "typ"        := typ
     ~> jsonEmptyObject
664 665
instance decodeJsonAsyncTaskWithType :: DecodeJson AsyncTaskWithType where
  decodeJson json = do
666
    obj  <- decodeJson json
667
    task <- obj .: "task"
668
    typ  <- obj .: "typ"
669 670
    pure $ AsyncTaskWithType { task, typ }

671 672 673 674 675 676 677 678
newtype AsyncProgress = AsyncProgress {
    id :: AsyncTaskID
  , log :: Array AsyncTaskLog
  , status :: AsyncTaskStatus
  }
derive instance genericAsyncProgress :: Generic AsyncProgress _
instance decodeJsonAsyncProgress :: DecodeJson AsyncProgress where
  decodeJson json = do
679 680 681
    obj    <- decodeJson json
    id     <- obj .: "id"
    log    <- obj .: "log"
682 683 684 685 686 687 688 689 690 691 692 693
    status <- obj .: "status"
    pure $ AsyncProgress {id, log, status}

newtype AsyncTaskLog = AsyncTaskLog {
    events :: Array String
  , failed :: Int
  , remaining :: Int
  , succeeded :: Int
  }
derive instance genericAsyncTaskLog :: Generic AsyncTaskLog _
instance decodeJsonAsyncTaskLog :: DecodeJson AsyncTaskLog where
  decodeJson json = do
694 695 696
    obj       <- decodeJson json
    events    <- obj .: "events"
    failed    <- obj .: "failed"
697 698 699 700 701
    remaining <- obj .: "remaining"
    succeeded <- obj .: "succeeded"
    pure $ AsyncTaskLog {events, failed, remaining, succeeded}

progressPercent :: AsyncProgress -> Number
702
progressPercent (AsyncProgress {log}) = perc
703
  where
704 705 706
    perc = case A.head log of
      Nothing -> 0.0
      Just (AsyncTaskLog {failed, remaining, succeeded}) -> 100.0*nom/denom
707
        where
708 709
          nom = toNumber $ failed + succeeded
          denom = toNumber $ failed + succeeded + remaining