API.purs 10.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
module Gargantext.Components.PhyloExplorer.API
  ( get
  , UpdateData(..)
  , TimeUnit(..), ReflexiveTimeUnit(..), TimeUnitCriteria(..)
  , Clique(..), ReflexiveClique(..), CliqueFilter(..)
  , toReflexiveTimeUnit, fromReflexiveTimeUnit, extractCriteria
  , toReflexiveClique
  , update, updateProgress
  ) where

import Gargantext.Prelude

import Data.Either (Either(..))
import Data.Generic.Rep (class Generic)
import Data.Maybe (Maybe(..))
import Data.Newtype (class Newtype)
import Data.Show.Generic (genericShow)
arturo's avatar
arturo committed
18 19
import Gargantext.Components.PhyloExplorer.JSON (PhyloJSON)
import Gargantext.Components.PhyloExplorer.Types (PhyloSet, parseToPhyloSet)
20 21 22 23 24 25 26 27 28 29
import Gargantext.Config.REST (AffRESTError)
import Gargantext.Routes (SessionRoute(..))
import Gargantext.Routes as GR
import Gargantext.Sessions (Session)
import Gargantext.Sessions as S
import Gargantext.Types (NodeID)
import Gargantext.Types as GT
import Record as Record
import Simple.JSON as JSON
import Simple.JSON.Generics as JSONG
30
import Type.Proxy (Proxy(..))
31 32


arturo's avatar
arturo committed
33 34
get :: S.Session -> NodeID -> AffRESTError (PhyloSet)
get session nodeId = request >>= (_ <#> parseToPhyloSet) >>> pure
35
  where
arturo's avatar
arturo committed
36
    request :: AffRESTError (PhyloJSON)
37 38 39 40 41
    request = S.get session $ PhyloAPI nodeId

----------------------------------------------------------

newtype UpdateData = UpdateData
42 43
  { defaultMode   :: Boolean
  , proximity     :: Number
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
  , synchrony     :: Number
  , quality       :: Number
  , timeUnit      :: TimeUnit
  , clique        :: Clique
  , exportFilter  :: Number
  }

derive instance Generic UpdateData _
derive instance Eq UpdateData
derive instance Newtype UpdateData _
instance Show UpdateData where show = genericShow
derive newtype instance JSON.ReadForeign UpdateData
instance JSON.WriteForeign UpdateData where
  writeImpl (UpdateData o) = (JSON.writeImpl <<< rename) o
    where
      rename
          = Record.rename
61 62 63
            (Proxy :: Proxy "defaultMode")
            (Proxy :: Proxy "_sc_defaultMode")
        >>> Record.rename
64 65
            (Proxy :: Proxy "proximity")
            (Proxy :: Proxy "_sc_phyloProximity")
66
        >>> Record.rename
67 68
            (Proxy :: Proxy "synchrony")
            (Proxy :: Proxy "_sc_phyloSynchrony")
69
        >>> Record.rename
70 71
            (Proxy :: Proxy "quality")
            (Proxy :: Proxy "_sc_phyloQuality")
72
        >>> Record.rename
73 74
            (Proxy :: Proxy "timeUnit")
            (Proxy :: Proxy "_sc_timeUnit")
75
        >>> Record.rename
76 77
            (Proxy :: Proxy "clique")
            (Proxy :: Proxy "_sc_clique")
78
        >>> Record.rename
79 80
            (Proxy :: Proxy "exportFilter")
            (Proxy :: Proxy "_sc_exportFilter")
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

data TimeUnit
  = Epoch TimeUnitCriteria
  | Year  TimeUnitCriteria
  | Month TimeUnitCriteria
  | Week  TimeUnitCriteria
  | Day   TimeUnitCriteria

derive instance Generic TimeUnit _
derive instance Eq TimeUnit
instance Show TimeUnit where show = genericShow
instance JSON.ReadForeign TimeUnit where readImpl = JSONG.untaggedSumRep
instance JSON.WriteForeign TimeUnit where
  writeImpl = case _ of
    Epoch (TimeUnitCriteria o) -> (JSON.writeImpl <<< parseEpoch) o
    Year  (TimeUnitCriteria o) -> (JSON.writeImpl <<< parseYear) o
    Month (TimeUnitCriteria o) -> (JSON.writeImpl <<< parseMonth) o
    Week  (TimeUnitCriteria o) -> (JSON.writeImpl <<< parseWeek) o
    Day   (TimeUnitCriteria o) -> (JSON.writeImpl <<< parseDay) o
    where
      parseEpoch
          = Record.rename
103 104
            (Proxy :: Proxy "period")
            (Proxy :: Proxy "_epoch_period")
105
        >>> Record.rename
106 107
            (Proxy :: Proxy "step")
            (Proxy :: Proxy "_epoch_step")
108
        >>> Record.rename
109 110
            (Proxy :: Proxy "matchingFrame")
            (Proxy :: Proxy "_epoch_matchingFrame")
111
        >>> Record.insert
112
            (Proxy :: Proxy "tag")
113 114 115
            "Epoch"
      parseYear
          = Record.rename
116 117
            (Proxy :: Proxy "period")
            (Proxy :: Proxy "_year_period")
118
        >>> Record.rename
119 120
            (Proxy :: Proxy "step")
            (Proxy :: Proxy "_year_step")
121
        >>> Record.rename
122 123
            (Proxy :: Proxy "matchingFrame")
            (Proxy :: Proxy "_year_matchingFrame")
124
        >>> Record.insert
125
            (Proxy :: Proxy "tag")
126 127 128
            "Year"
      parseMonth
          = Record.rename
129 130
            (Proxy :: Proxy "period")
            (Proxy :: Proxy "_month_period")
131
        >>> Record.rename
132 133
            (Proxy :: Proxy "step")
            (Proxy :: Proxy "_month_step")
134
        >>> Record.rename
135 136
            (Proxy :: Proxy "matchingFrame")
            (Proxy :: Proxy "_month_matchingFrame")
137
        >>> Record.insert
138
            (Proxy :: Proxy "tag")
139 140 141
            "Month"
      parseWeek
          = Record.rename
142 143
            (Proxy :: Proxy "period")
            (Proxy :: Proxy "_week_period")
144
        >>> Record.rename
145 146
            (Proxy :: Proxy "step")
            (Proxy :: Proxy "_week_step")
147
        >>> Record.rename
148 149
            (Proxy :: Proxy "matchingFrame")
            (Proxy :: Proxy "_week_matchingFrame")
150
        >>> Record.insert
151
            (Proxy :: Proxy "tag")
152 153 154
            "Week"
      parseDay
          = Record.rename
155 156
            (Proxy :: Proxy "period")
            (Proxy :: Proxy "_day_period")
157
        >>> Record.rename
158 159
            (Proxy :: Proxy "step")
            (Proxy :: Proxy "_day_step")
160
        >>> Record.rename
161 162
            (Proxy :: Proxy "matchingFrame")
            (Proxy :: Proxy "_day_matchingFrame")
163
        >>> Record.insert
164
            (Proxy :: Proxy "tag")
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
            "Day"


data ReflexiveTimeUnit
  = Epoch_
  | Year_
  | Month_
  | Week_
  | Day_

derive instance Generic ReflexiveTimeUnit _
derive instance Eq ReflexiveTimeUnit
instance Show ReflexiveTimeUnit where show = genericShow
instance Read ReflexiveTimeUnit where
  read :: String -> Maybe ReflexiveTimeUnit
  read = case _ of
    "Epoch_" -> Just Epoch_
    "Year_"  -> Just Year_
    "Month_" -> Just Month_
    "Week_"  -> Just Week_
    "Day_"   -> Just Day_
    _        -> Nothing


newtype TimeUnitCriteria = TimeUnitCriteria
  { period        :: Int
  , step          :: Int
  , matchingFrame :: Int
  }

derive instance Generic TimeUnitCriteria _
derive instance Eq TimeUnitCriteria
derive instance Newtype TimeUnitCriteria _
instance Show TimeUnitCriteria where show = genericShow
derive newtype instance JSON.ReadForeign TimeUnitCriteria


data Clique
  = FIS
    { support   :: Int
    , size      :: Int
    }
  | MaxClique
    { size      :: Int
    , threshold :: Number
    , filter    :: CliqueFilter
    }

derive instance Eq Clique
derive instance Generic Clique _
instance Show Clique where show = genericShow
instance JSON.ReadForeign Clique where readImpl = JSONG.untaggedSumRep
instance JSON.WriteForeign Clique where
  writeImpl = case _ of
    FIS o       -> (JSON.writeImpl <<< parseFIS) o
    MaxClique o -> (JSON.writeImpl <<< parseMaxClique) o
    where
      parseFIS
          = Record.insert
224
            (Proxy :: Proxy "tag")
225 226
            "Fis"
        >>> Record.rename
227 228
            (Proxy :: Proxy "support")
            (Proxy :: Proxy "_fis_support")
229
        >>> Record.rename
230 231
            (Proxy :: Proxy "size")
            (Proxy :: Proxy "_fis_size")
232 233
      parseMaxClique
          = Record.insert
234
            (Proxy :: Proxy "tag")
235 236
            "MaxClique"
        >>> Record.rename
237 238
            (Proxy :: Proxy "size")
            (Proxy :: Proxy "_mcl_size")
239
        >>> Record.rename
240 241
            (Proxy :: Proxy "threshold")
            (Proxy :: Proxy "_mcl_threshold")
242
        >>> Record.rename
243 244
            (Proxy :: Proxy "filter")
            (Proxy :: Proxy "_mcl_filter")
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


data ReflexiveClique
  = FIS_
  | MaxClique_

derive instance Generic ReflexiveClique _
derive instance Eq ReflexiveClique
instance Show ReflexiveClique where show = genericShow
instance Read ReflexiveClique where
  read :: String -> Maybe ReflexiveClique
  read = case _ of
    "FIS_"       -> Just FIS_
    "MaxClique_" -> Just MaxClique_
    _            -> Nothing


data CliqueFilter = ByThreshold | ByNeighbours

derive instance Eq CliqueFilter
derive instance Generic CliqueFilter _
instance Show CliqueFilter where show = genericShow
instance JSON.ReadForeign CliqueFilter where readImpl = JSONG.enumSumRep
instance JSON.WriteForeign CliqueFilter where writeImpl = JSON.writeImpl <<< show
instance Read CliqueFilter where
  read :: String -> Maybe CliqueFilter
  read = case _ of
    "ByThreshold"  -> Just ByThreshold
    "ByNeighbours" -> Just ByNeighbours
    _              -> Nothing


toReflexiveTimeUnit :: TimeUnit -> ReflexiveTimeUnit
toReflexiveTimeUnit (Epoch _) = Epoch_
toReflexiveTimeUnit (Year _)  = Year_
toReflexiveTimeUnit (Month _) = Month_
toReflexiveTimeUnit (Week _)  = Week_
toReflexiveTimeUnit (Day _)   = Day_

fromReflexiveTimeUnit :: ReflexiveTimeUnit -> TimeUnitCriteria -> TimeUnit
fromReflexiveTimeUnit Epoch_ c = Epoch c
fromReflexiveTimeUnit Year_  c = Year c
fromReflexiveTimeUnit Month_ c = Month c
fromReflexiveTimeUnit Week_  c = Week c
fromReflexiveTimeUnit Day_   c = Day c

extractCriteria :: TimeUnit -> TimeUnitCriteria
extractCriteria (Epoch (o :: TimeUnitCriteria)) = o
extractCriteria (Year  (o :: TimeUnitCriteria)) = o
extractCriteria (Month (o :: TimeUnitCriteria)) = o
extractCriteria (Week  (o :: TimeUnitCriteria)) = o
extractCriteria (Day   (o :: TimeUnitCriteria)) = o


toReflexiveClique :: Clique -> ReflexiveClique
toReflexiveClique (FIS _)       = FIS_
toReflexiveClique (MaxClique _) = MaxClique_



update ::
     Session
  -> NodeID
  -> Unit
  -> AffRESTError GT.AsyncTaskWithType
update session nodeId _
    = S.post session request {}
  >>= case _ of
    Left err   -> pure $ Left err
    Right task -> pure $ Right $ GT.AsyncTaskWithType
      { task
      , typ: GT.UpdateNode
      }

  where

    request = GR.NodeAPI GT.Node (Just nodeId)
      (GT.asyncTaskTypePath GT.UpdateNode)


updateProgress ::
     Session
  -> NodeID
  -> GT.AsyncTaskWithType
  -> AffRESTError GT.AsyncProgress
updateProgress
  session
  nodeId
  (GT.AsyncTaskWithType { task: GT.AsyncTask { id } })
  =
    S.get session request

  where

    request = GR.NodeAPI GT.Node (Just nodeId)
      (GT.asyncTaskTypePath GT.UpdateNode <> pollParams)

    pollParams = "/" <> id <> "/poll?limit1"