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

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

18
newtype SessionId = SessionId String
19
type NodeID = Int
20 21 22 23 24 25 26 27 28

derive instance genericSessionId :: Generic SessionId _

instance eqSessionId :: Eq SessionId where
  eq = genericEq

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

29
data TermSize = MonoTerm | MultiTerm
30

31 32
data Term = Term String TermList

33
derive instance eqTermSize :: Eq TermSize
34

James Laver's avatar
James Laver committed
35 36 37 38
-- | Converts a data structure to a query string
class ToQuery a where
  toQuery :: a -> Query

39
instance showTermSize :: Show TermSize where
40 41 42
  show MonoTerm  = "MonoTerm"
  show MultiTerm = "MultiTerm"

43 44 45 46
readTermSize :: String -> Maybe TermSize
readTermSize "MonoTerm"  = Just MonoTerm
readTermSize "MultiTerm" = Just MultiTerm
readTermSize _           = Nothing
47

48 49
termSizes :: Array { desc :: String, mval :: Maybe TermSize }
termSizes = [ { desc: "All types",        mval: Nothing        }
50 51 52 53 54
            , { desc: "One-word terms",   mval: Just MonoTerm  }
            , { desc: "Multi-word terms", mval: Just MultiTerm }
            ]

data TermList = GraphTerm | StopTerm | CandidateTerm
55
-- TODO use generic JSON instance
56 57

derive instance eqTermList :: Eq TermList
58
derive instance ordTermList :: Ord TermList
59

60
instance encodeJsonTermList :: EncodeJson TermList where
61 62 63
  encodeJson GraphTerm     = encodeJson "GraphTerm"
  encodeJson StopTerm      = encodeJson "StopTerm"
  encodeJson CandidateTerm = encodeJson "CandidateTerm"
64

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

type ListTypeId = Int

listTypeId :: TermList -> ListTypeId
listTypeId GraphTerm     = 1
listTypeId StopTerm      = 2
listTypeId CandidateTerm = 3

instance showTermList :: Show TermList where
82 83 84
  show GraphTerm     = "GraphTerm"
  show StopTerm      = "StopTerm"
  show CandidateTerm = "CandidateTerm"
85

86 87
-- TODO: Can we replace the show instance above with this?
termListName :: TermList -> String
88
termListName GraphTerm = "Map List"
89 90 91
termListName StopTerm = "Stop List"
termListName CandidateTerm = "Candidate List"

92
readTermList :: String -> Maybe TermList
93 94 95 96
readTermList "GraphTerm"     = Just GraphTerm
readTermList "StopTerm"      = Just StopTerm
readTermList "CandidateTerm" = Just CandidateTerm
readTermList _               = Nothing
97 98 99

termLists :: Array { desc :: String, mval :: Maybe TermList }
termLists = [ { desc: "All terms",   mval: Nothing      }
100
            , { desc: "Map terms",   mval: Just GraphTerm   }
101 102 103 104
            , { desc: "Stop terms",  mval: Just StopTerm  }
            , { desc: "Candidate terms", mval: Just CandidateTerm }
            ]

105 106 107
-- | 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
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

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
132
              | Folder | FolderPrivate | FolderShared | FolderPublic
133 134 135
              | Annuaire
              | NodeContact
              | Corpus
136
              | Team
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
              | Url_Document
              | Dashboard
              | Error
              | Graph
              | Phylo
              | Individu
              | Node
              | Nodes
              | Tree
              | NodeList
              | Texts

derive instance eqNodeType :: Eq NodeType

instance showNodeType :: Show NodeType where
  show NodeUser      = "NodeUser"
153 154

  show Folder        = "NodeFolder"
155 156 157
  show FolderPrivate = "NodeFolderPrivate"  -- Node Private Worktop
  show FolderShared    = "NodeFolderShared" -- Node Share Worktop
  show FolderPublic  = "NodeFolderPublic"   -- Node Public Worktop
158

159
  show Annuaire      = "NodeAnnuaire"
160 161 162 163 164 165 166 167 168 169 170
  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"
171
  show Team          = "NodeTeam"
172 173 174 175
  show NodeList      = "NodeList"
  show Texts         = "NodeTexts"

readNodeType :: String -> NodeType
176 177 178 179
readNodeType "NodeUser"      = NodeUser

readNodeType "NodeFolder"    = Folder
readNodeType "NodeFolderPrivate" = FolderPrivate
180
readNodeType "NodeFolderShared"  = FolderShared
181 182
readNodeType "NodeFolderPublic"  = FolderPublic

183 184 185 186 187 188 189 190 191 192 193
readNodeType "NodeAnnuaire"  = Annuaire
readNodeType "NodeDashboard" = Dashboard
readNodeType "Document"      = Url_Document
readNodeType "NodeGraph"     = Graph
readNodeType "NodePhylo"     = Phylo
readNodeType "Individu"      = Individu
readNodeType "Node"          = Node
readNodeType "Nodes"         = Nodes
readNodeType "NodeCorpus"    = Corpus
readNodeType "NodeContact"   = NodeContact
readNodeType "Tree"          = Tree
194
readNodeType "NodeTeam"      = Team
195 196
readNodeType "NodeList"      = NodeList
readNodeType "NodeTexts"     = Texts
197
readNodeType "Annuaire"      = Annuaire
198
readNodeType _               = Error
199 200 201 202


fldr :: NodeType -> Boolean -> String
fldr NodeUser false = "fa fa-user-circle"
203 204
fldr NodeUser true  = "fa fa-user"
------------------------------------------------------
205 206
fldr Folder  false  = "fa fa-folder"
fldr Folder  true   = "fa fa-folder-open-o"
207
------------------------------------------------------
208
fldr FolderPrivate true  = "fa fa-lock"
209
fldr FolderPrivate false = "fa fa-lock-circle"
210

211
fldr FolderShared  true  = "fa fa-share-alt"
Alexandre Delanoë's avatar
Alexandre Delanoë committed
212
fldr FolderShared  false = "fa fa-share-circle"
213
fldr Team  true   = "fa fa-users"
214
fldr Team  false  = "fa fa-users-closed"
215

216 217
fldr FolderPublic true  = "fa fa-globe-circle"
fldr FolderPublic false = "fa fa-globe"
218
------------------------------------------------------
219

220
fldr Corpus true  = "fa fa-book"
Alexandre Delanoë's avatar
Alexandre Delanoë committed
221
fldr Corpus false = "fa fa-book-circle"
222 223 224 225 226 227 228 229

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"

230 231
fldr Annuaire true  = "fa fa-address-card-o"
fldr Annuaire false = "fa fa-address-card"
232 233 234 235

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

236 237
fldr _        false  = "fa fa-folder-o"
fldr _        true   = "fa fa-folder-open"
238 239 240



241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
{-
------------------------------------------------------------
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
    pure $ readNodeType obj

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

nodeTypePath :: NodeType -> String
259 260
nodeTypePath Folder    = "folder"
nodeTypePath FolderPrivate = "folderPrivate"
261
nodeTypePath FolderShared  = "folderShared"
262
nodeTypePath FolderPublic  = "folderPublic"
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
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"
278
nodeTypePath Team      = "team"
279 280 281 282
------------------------------------------------------------

type ListId = Int

283 284 285 286 287 288 289
data ScoreType = Occurrences

derive instance genericScoreType :: Generic ScoreType _

instance showScoreType :: Show ScoreType where
  show = genericShow

290 291 292 293 294 295 296 297
type NgramsGetOpts =
  { tabType        :: TabType
  , offset         :: Offset
  , limit          :: Limit
  , orderBy        :: Maybe OrderBy
  , listIds        :: Array ListId
  , termListFilter :: Maybe TermList
  , termSizeFilter :: Maybe TermSize
298
  , scoreType      :: ScoreType
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
  , searchQuery    :: String
  }

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
  , tabType   :: TabType
  -- , listId  :: ListId
  -- , limit   :: Maybe Limit
  }

James Laver's avatar
James Laver committed
325
data NodePath = NodePath SessionId NodeType (Maybe Id)
326 327

nodePath :: NodePath -> String
James Laver's avatar
James Laver committed
328
nodePath (NodePath s t i) = nodeTypePath t <> "/" <> show s <> id
James Laver's avatar
James Laver committed
329
  where id = maybe "" (\j -> "/" <> show j) i
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354

data ChartType = Histo | Scatter | ChartPie | ChartTree

instance showChartType :: Show ChartType
  where
    show Histo    = "chart"
    show Scatter  = "scatter"
    show ChartPie = "pie"
    show ChartTree = "tree"

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

------------------------------------------------------------
355 356 357
-- V0 is the dummy case (impossible)
data ApiVersion = V0 | V10 | V11

358
instance showApiVersion :: Show ApiVersion where
359
  show V0  = "v0"
360 361 362 363 364 365 366 367
  show V10 = "v1.0"
  show V11 = "v1.1"

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

368 369 370 371 372 373 374 375 376 377 378 379
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
------------------------------------------------------------

380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
data CTabNgramType = CTabTerms | CTabSources | CTabAuthors | CTabInstitutes

derive instance eqCTabNgramType :: Eq CTabNgramType

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

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)

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)    

derive instance eqTabType :: Eq TabType

derive instance genericTabType :: Generic TabType _

instance showTabType :: Show TabType where
  show = genericShow
421 422 423

type TableResult a = {count :: Int, docs :: Array a}
type AffTableResult a = Aff (TableResult a)
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447

data Mode = Authors | Sources | Institutes | Terms

derive instance genericMode :: Generic Mode _

instance showMode :: Show Mode where
  show = genericShow

derive instance eqMode :: Eq Mode
instance ordMode :: Ord Mode where
  compare = genericCompare

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
448

449 450 451 452 453 454 455 456 457 458
-- Async tasks

-- corresponds to /add/form/async or /add/query/async
data AsyncTaskType = Form | Query
derive instance genericAsyncTaskType :: Generic AsyncTaskType _

asyncTaskTypePath :: AsyncTaskType -> String
asyncTaskTypePath Form = "add/form/async/"
asyncTaskTypePath Query = "add/query/async/"

459 460
type AsyncTaskID = String

461
data AsyncTaskStatus = Running | Failed | Finished | Killed
462
derive instance genericAsyncTaskStatus :: Generic AsyncTaskStatus _
463
derive instance eqAsyncTaskStatus :: Eq AsyncTaskStatus
464 465 466 467 468 469
instance decodeJsonAsyncTaskStatus :: DecodeJson AsyncTaskStatus where
  decodeJson json = do
    obj <- decodeJson json
    pure $ readAsyncTaskStatus obj

readAsyncTaskStatus :: String -> AsyncTaskStatus
470 471 472 473 474
readAsyncTaskStatus "failed"   = Failed
readAsyncTaskStatus "finished" = Finished
readAsyncTaskStatus "killed"   = Killed
readAsyncTaskStatus "running"  = Running
readAsyncTaskStatus _ = Running
475

476
newtype AsyncTask = AsyncTask {
477
    id     :: AsyncTaskID
478
  , status :: AsyncTaskStatus
479 480 481 482 483 484 485 486 487
  }
derive instance genericAsyncTask :: Generic AsyncTask _

instance decodeJsonAsyncTask :: DecodeJson AsyncTask where
  decodeJson json = do
    obj <- decodeJson json
    id <- obj .: "id"
    status <- obj .: "status"
    pure $ AsyncTask {id, status}
488

489 490 491 492 493
newtype AsyncTaskWithType = AsyncTaskWithType {
    task :: AsyncTask
  , typ  :: AsyncTaskType
  }

494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
newtype AsyncProgress = AsyncProgress {
    id :: AsyncTaskID
  , log :: Array AsyncTaskLog
  , status :: AsyncTaskStatus
  }
derive instance genericAsyncProgress :: Generic AsyncProgress _
instance decodeJsonAsyncProgress :: DecodeJson AsyncProgress where
  decodeJson json = do
    obj <- decodeJson json
    id <- obj .: "id"
    log <- obj .: "log"
    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
    obj <- decodeJson json
    events <- obj .: "events"
    failed <- obj .: "failed"
    remaining <- obj .: "remaining"
    succeeded <- obj .: "succeeded"
    pure $ AsyncTaskLog {events, failed, remaining, succeeded}

progressPercent :: AsyncProgress -> Number
525
progressPercent (AsyncProgress {log}) = perc
526
  where
527 528 529
    perc = case A.head log of
      Nothing -> 0.0
      Just (AsyncTaskLog {failed, remaining, succeeded}) -> 100.0*nom/denom
530
        where
531 532
          nom = toNumber $ failed + succeeded
          denom = toNumber $ failed + succeeded + remaining