1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
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
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)
import Gargantext.Components.PhyloExplorer.JSON (PhyloJSON)
import Gargantext.Components.PhyloExplorer.Types (PhyloSet, parseToPhyloSet)
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
import Type.Proxy (Proxy(..))
get :: S.Session -> NodeID -> AffRESTError (PhyloSet)
get session nodeId = request >>= (_ <#> parseToPhyloSet) >>> pure
where
request :: AffRESTError (PhyloJSON)
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
(Proxy :: Proxy "proximity")
(Proxy :: Proxy "_sc_phyloProximity")
>>> Record.rename
(Proxy :: Proxy "synchrony")
(Proxy :: Proxy "_sc_phyloSynchrony")
>>> Record.rename
(Proxy :: Proxy "quality")
(Proxy :: Proxy "_sc_phyloQuality")
>>> Record.rename
(Proxy :: Proxy "timeUnit")
(Proxy :: Proxy "_sc_timeUnit")
>>> Record.rename
(Proxy :: Proxy "clique")
(Proxy :: Proxy "_sc_clique")
>>> Record.rename
(Proxy :: Proxy "exportFilter")
(Proxy :: Proxy "_sc_exportFilter")
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
(Proxy :: Proxy "period")
(Proxy :: Proxy "_epoch_period")
>>> Record.rename
(Proxy :: Proxy "step")
(Proxy :: Proxy "_epoch_step")
>>> Record.rename
(Proxy :: Proxy "matchingFrame")
(Proxy :: Proxy "_epoch_matchingFrame")
>>> Record.insert
(Proxy :: Proxy "tag")
"Epoch"
parseYear
= Record.rename
(Proxy :: Proxy "period")
(Proxy :: Proxy "_year_period")
>>> Record.rename
(Proxy :: Proxy "step")
(Proxy :: Proxy "_year_step")
>>> Record.rename
(Proxy :: Proxy "matchingFrame")
(Proxy :: Proxy "_year_matchingFrame")
>>> Record.insert
(Proxy :: Proxy "tag")
"Year"
parseMonth
= Record.rename
(Proxy :: Proxy "period")
(Proxy :: Proxy "_month_period")
>>> Record.rename
(Proxy :: Proxy "step")
(Proxy :: Proxy "_month_step")
>>> Record.rename
(Proxy :: Proxy "matchingFrame")
(Proxy :: Proxy "_month_matchingFrame")
>>> Record.insert
(Proxy :: Proxy "tag")
"Month"
parseWeek
= Record.rename
(Proxy :: Proxy "period")
(Proxy :: Proxy "_week_period")
>>> Record.rename
(Proxy :: Proxy "step")
(Proxy :: Proxy "_week_step")
>>> Record.rename
(Proxy :: Proxy "matchingFrame")
(Proxy :: Proxy "_week_matchingFrame")
>>> Record.insert
(Proxy :: Proxy "tag")
"Week"
parseDay
= Record.rename
(Proxy :: Proxy "period")
(Proxy :: Proxy "_day_period")
>>> Record.rename
(Proxy :: Proxy "step")
(Proxy :: Proxy "_day_step")
>>> Record.rename
(Proxy :: Proxy "matchingFrame")
(Proxy :: Proxy "_day_matchingFrame")
>>> Record.insert
(Proxy :: Proxy "tag")
"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
(Proxy :: Proxy "tag")
"Fis"
>>> Record.rename
(Proxy :: Proxy "support")
(Proxy :: Proxy "_fis_support")
>>> Record.rename
(Proxy :: Proxy "size")
(Proxy :: Proxy "_fis_size")
parseMaxClique
= Record.insert
(Proxy :: Proxy "tag")
"MaxClique"
>>> Record.rename
(Proxy :: Proxy "size")
(Proxy :: Proxy "_mcl_size")
>>> Record.rename
(Proxy :: Proxy "threshold")
(Proxy :: Proxy "_mcl_threshold")
>>> Record.rename
(Proxy :: Proxy "filter")
(Proxy :: Proxy "_mcl_filter")
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"