API.purs 9.94 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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    request = S.get session $ PhyloAPI nodeId

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

newtype UpdateData = UpdateData
  { proximity     :: Number
  , 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
60 61
            (Proxy :: Proxy "proximity")
            (Proxy :: Proxy "_sc_phyloProximity")
62
        >>> Record.rename
63 64
            (Proxy :: Proxy "synchrony")
            (Proxy :: Proxy "_sc_phyloSynchrony")
65
        >>> Record.rename
66 67
            (Proxy :: Proxy "quality")
            (Proxy :: Proxy "_sc_phyloQuality")
68
        >>> Record.rename
69 70
            (Proxy :: Proxy "timeUnit")
            (Proxy :: Proxy "_sc_timeUnit")
71
        >>> Record.rename
72 73
            (Proxy :: Proxy "clique")
            (Proxy :: Proxy "_sc_clique")
74
        >>> Record.rename
75 76
            (Proxy :: Proxy "exportFilter")
            (Proxy :: Proxy "_sc_exportFilter")
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

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
99 100
            (Proxy :: Proxy "period")
            (Proxy :: Proxy "_epoch_period")
101
        >>> Record.rename
102 103
            (Proxy :: Proxy "step")
            (Proxy :: Proxy "_epoch_step")
104
        >>> Record.rename
105 106
            (Proxy :: Proxy "matchingFrame")
            (Proxy :: Proxy "_epoch_matchingFrame")
107
        >>> Record.insert
108
            (Proxy :: Proxy "tag")
109 110 111
            "Epoch"
      parseYear
          = Record.rename
112 113
            (Proxy :: Proxy "period")
            (Proxy :: Proxy "_year_period")
114
        >>> Record.rename
115 116
            (Proxy :: Proxy "step")
            (Proxy :: Proxy "_year_step")
117
        >>> Record.rename
118 119
            (Proxy :: Proxy "matchingFrame")
            (Proxy :: Proxy "_year_matchingFrame")
120
        >>> Record.insert
121
            (Proxy :: Proxy "tag")
122 123 124
            "Year"
      parseMonth
          = Record.rename
125 126
            (Proxy :: Proxy "period")
            (Proxy :: Proxy "_month_period")
127
        >>> Record.rename
128 129
            (Proxy :: Proxy "step")
            (Proxy :: Proxy "_month_step")
130
        >>> Record.rename
131 132
            (Proxy :: Proxy "matchingFrame")
            (Proxy :: Proxy "_month_matchingFrame")
133
        >>> Record.insert
134
            (Proxy :: Proxy "tag")
135 136 137
            "Month"
      parseWeek
          = Record.rename
138 139
            (Proxy :: Proxy "period")
            (Proxy :: Proxy "_week_period")
140
        >>> Record.rename
141 142
            (Proxy :: Proxy "step")
            (Proxy :: Proxy "_week_step")
143
        >>> Record.rename
144 145
            (Proxy :: Proxy "matchingFrame")
            (Proxy :: Proxy "_week_matchingFrame")
146
        >>> Record.insert
147
            (Proxy :: Proxy "tag")
148 149 150
            "Week"
      parseDay
          = Record.rename
151 152
            (Proxy :: Proxy "period")
            (Proxy :: Proxy "_day_period")
153
        >>> Record.rename
154 155
            (Proxy :: Proxy "step")
            (Proxy :: Proxy "_day_step")
156
        >>> Record.rename
157 158
            (Proxy :: Proxy "matchingFrame")
            (Proxy :: Proxy "_day_matchingFrame")
159
        >>> Record.insert
160
            (Proxy :: Proxy "tag")
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
            "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
220
            (Proxy :: Proxy "tag")
221 222
            "Fis"
        >>> Record.rename
223 224
            (Proxy :: Proxy "support")
            (Proxy :: Proxy "_fis_support")
225
        >>> Record.rename
226 227
            (Proxy :: Proxy "size")
            (Proxy :: Proxy "_fis_size")
228 229
      parseMaxClique
          = Record.insert
230
            (Proxy :: Proxy "tag")
231 232
            "MaxClique"
        >>> Record.rename
233 234
            (Proxy :: Proxy "size")
            (Proxy :: Proxy "_mcl_size")
235
        >>> Record.rename
236 237
            (Proxy :: Proxy "threshold")
            (Proxy :: Proxy "_mcl_threshold")
238
        >>> Record.rename
239 240
            (Proxy :: Proxy "filter")
            (Proxy :: Proxy "_mcl_filter")
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


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"