Types.purs 14.7 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
import Data.Int (toNumber)
8
import Data.Maybe (Maybe(..), maybe)
9
import Effect.Aff (Aff)
10
import Prim.Row (class Union)
James Laver's avatar
James Laver committed
11
import URI.Query (Query)
12
import Data.Generic.Rep (class Generic)
13
import Data.Generic.Rep.Eq (genericEq)
14
import Data.Generic.Rep.Ord (genericCompare)
15
import Data.Generic.Rep.Show (genericShow)
16

17 18 19 20 21 22 23 24 25 26
newtype SessionId = SessionId String

derive instance genericSessionId :: Generic SessionId _

instance eqSessionId :: Eq SessionId where
  eq = genericEq

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

27
data TermSize = MonoTerm | MultiTerm
28

29 30
data Term = Term String TermList

31
derive instance eqTermSize :: Eq TermSize
32

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

37
instance showTermSize :: Show TermSize where
38 39 40
  show MonoTerm  = "MonoTerm"
  show MultiTerm = "MultiTerm"

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

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

data TermList = GraphTerm | StopTerm | CandidateTerm
53
-- TODO use generic JSON instance
54 55

derive instance eqTermList :: Eq TermList
56
derive instance ordTermList :: Ord TermList
57

58
instance encodeJsonTermList :: EncodeJson TermList where
59 60 61
  encodeJson GraphTerm     = encodeJson "GraphTerm"
  encodeJson StopTerm      = encodeJson "StopTerm"
  encodeJson CandidateTerm = encodeJson "CandidateTerm"
62

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

type ListTypeId = Int

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

instance showTermList :: Show TermList where
80 81 82
  show GraphTerm     = "GraphTerm"
  show StopTerm      = "StopTerm"
  show CandidateTerm = "CandidateTerm"
83

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

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

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

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

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
130
              | Folder | FolderPrivate | FolderShared | FolderPublic
131 132 133
              | Annuaire
              | NodeContact
              | Corpus
134
              | Team
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
              | 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"
151 152 153

  show Folder        = "NodeFolder"
  show FolderPrivate = "NodeFolderPrivate"
154
  show FolderShared    = "NodeFolderShared"
155 156
  show FolderPublic  = "NodeFolderPublic"

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

readNodeType :: String -> NodeType
174 175 176 177
readNodeType "NodeUser"      = NodeUser

readNodeType "NodeFolder"    = Folder
readNodeType "NodeFolderPrivate" = FolderPrivate
178
readNodeType "NodeFolderShared"  = FolderShared
179 180
readNodeType "NodeFolderPublic"  = FolderPublic

181 182 183 184 185 186 187 188 189 190 191
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
192
readNodeType "NodeTeam"      = Team
193 194 195
readNodeType "NodeList"      = NodeList
readNodeType "NodeTexts"     = Texts
readNodeType _               = Error
196 197 198 199 200 201 202 203 204


fldr :: NodeType -> Boolean -> String
fldr NodeUser false = "fa fa-user-circle"
fldr NodeUser true  = "fa fa-user-circle-o"

fldr FolderPrivate true  = "fa fa-lock"
fldr FolderPrivate false = "fa fa-expeditedssl"

205
fldr FolderShared  true  = "fa fa-share-alt"
206
fldr FolderShared  false = "fa fa-share-alt-square"
207 208
fldr Team  true   = "fa fa-users"
fldr Team  false  = "fa fa-users"
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231

fldr FolderPublic _  = "fa fa-globe"

fldr Corpus _ = "fa fa-book" -- "fa fa-snowflake-o"

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"

fldr Annuaire true  = "fa fa-address-book-o"
fldr Annuaire false = "fa fa-address-book"

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

fldr _        false  = "fa fa-folder"
fldr _        true   = "fa fa-folder-o"



232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
{-
------------------------------------------------------------
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
250 251
nodeTypePath Folder    = "folder"
nodeTypePath FolderPrivate = "folderPrivate"
252
nodeTypePath FolderShared  = "folderShared"
253
nodeTypePath FolderPublic  = "folderPublic"
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
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"
269
nodeTypePath Team      = "team"
270 271 272 273
------------------------------------------------------------

type ListId = Int

274 275 276 277 278 279 280
data ScoreType = Occurrences

derive instance genericScoreType :: Generic ScoreType _

instance showScoreType :: Show ScoreType where
  show = genericShow

281 282 283 284 285 286 287 288
type NgramsGetOpts =
  { tabType        :: TabType
  , offset         :: Offset
  , limit          :: Limit
  , orderBy        :: Maybe OrderBy
  , listIds        :: Array ListId
  , termListFilter :: Maybe TermList
  , termSizeFilter :: Maybe TermSize
289
  , scoreType      :: ScoreType
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
  , 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
316
data NodePath = NodePath SessionId NodeType (Maybe Id)
317 318

nodePath :: NodePath -> String
James Laver's avatar
James Laver committed
319
nodePath (NodePath s t i) = nodeTypePath t <> "/" <> show s <> id
James Laver's avatar
James Laver committed
320
  where id = maybe "" (\j -> "/" <> show j) i
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

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

------------------------------------------------------------
346 347 348
-- V0 is the dummy case (impossible)
data ApiVersion = V0 | V10 | V11

349
instance showApiVersion :: Show ApiVersion where
350
  show V0  = "v0"
351 352 353 354 355 356 357 358
  show V10 = "v1.0"
  show V11 = "v1.1"

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

359 360 361 362 363 364 365 366 367 368 369 370
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
------------------------------------------------------------

371 372 373 374 375 376 377 378 379 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
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
412 413 414

type TableResult a = {count :: Int, docs :: Array a}
type AffTableResult a = Aff (TableResult a)
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438

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
439

440 441
type AsyncTaskID = String

442
data AsyncTaskStatus = Running | Failed | Finished | Killed
443
derive instance genericAsyncTaskStatus :: Generic AsyncTaskStatus _
444
derive instance eqAsyncTaskStatus :: Eq AsyncTaskStatus
445 446 447 448 449 450
instance decodeJsonAsyncTaskStatus :: DecodeJson AsyncTaskStatus where
  decodeJson json = do
    obj <- decodeJson json
    pure $ readAsyncTaskStatus obj

readAsyncTaskStatus :: String -> AsyncTaskStatus
451 452 453 454 455
readAsyncTaskStatus "failed"   = Failed
readAsyncTaskStatus "finished" = Finished
readAsyncTaskStatus "killed"   = Killed
readAsyncTaskStatus "running"  = Running
readAsyncTaskStatus _ = Running
456

457
newtype AsyncTask = AsyncTask {
458 459
    id :: AsyncTaskID
  , status :: AsyncTaskStatus
460 461 462 463 464 465 466 467 468
  }
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}
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500

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
501
progressPercent (AsyncProgress {log}) = perc
502
  where
503 504 505
    perc = case A.head log of
      Nothing -> 0.0
      Just (AsyncTaskLog {failed, remaining, succeeded}) -> 100.0*nom/denom
506
        where
507 508
          nom = toNumber $ failed + succeeded
          denom = toNumber $ failed + succeeded + remaining