2 Module : Gargantext.API.Node.Update
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
12 {-# LANGUAGE TemplateHaskell #-}
13 {-# LANGUAGE TypeOperators #-}
14 {-# OPTIONS_GHC -fno-warn-orphans #-}
16 module Gargantext.API.Node.Update
19 import Control.Lens (view)
21 import Data.Maybe (Maybe(..), fromMaybe)
23 import GHC.Generics (Generic)
24 import Gargantext.API.Admin.EnvTypes (GargJob(..), Env)
25 import Gargantext.API.Admin.Orchestrator.Types (JobLog(..), AsyncJobs)
26 import Gargantext.API.Admin.Types (HasSettings)
27 import Gargantext.API.Ngrams.List (reIndexWith)
28 --import Gargantext.API.Ngrams.Types (TabType(..))
29 import Gargantext.API.Prelude (GargM, GargError, simuLogs)
30 import Gargantext.Core.Methods.Distances (GraphMetric(..))
31 import Gargantext.Core.Types.Main (ListType(..))
32 import Gargantext.Core.Viz.Graph (Strength)
33 import Gargantext.Core.Viz.Graph.API (recomputeGraph)
34 import Gargantext.Core.Viz.Graph.Tools (PartitionMethod(..))
35 import Gargantext.Core.Viz.Phylo (PhyloSubConfig(..), subConfig2config)
36 import Gargantext.Core.Viz.Phylo.API.Tools (flowPhyloAPI)
37 import Gargantext.Database.Action.Flow.Pairing (pairing)
38 import Gargantext.Database.Action.Flow.Types (FlowCmdM)
39 import Gargantext.Database.Action.Metrics (updateNgramsOccurrences, updateContextScore)
40 import Gargantext.Database.Admin.Types.Hyperdata
41 import Gargantext.Database.Admin.Types.Node
42 import Gargantext.Database.Query.Table.Node (defaultList, getNode)
43 import Gargantext.Database.Query.Table.Node.UpdateOpaleye (updateHyperdata)
44 import Gargantext.Database.Schema.Ngrams (NgramsType(NgramsTerms))
45 import Gargantext.Database.Schema.Node (node_parent_id)
46 import Gargantext.Prelude (Bool(..), Ord, Eq, (<$>), ($), liftBase, (.), printDebug, pure, show, cs, (<>), panic, (<*>))
47 import Gargantext.Utils.Jobs (serveJobsAPI)
48 import Prelude (Enum, Bounded, minBound, maxBound)
50 import Test.QuickCheck (elements)
51 import Test.QuickCheck.Arbitrary
52 import qualified Data.Set as Set
53 import qualified Gargantext.API.Metrics as Metrics
54 import qualified Gargantext.API.Ngrams.Types as NgramsTypes
55 import qualified Gargantext.Utils.Aeson as GUA
57 ------------------------------------------------------------------------
58 type API = Summary " Update node according to NodeType params"
59 :> AsyncJobs JobLog '[JSON] UpdateNodeParams JobLog
61 ------------------------------------------------------------------------
62 data UpdateNodeParams = UpdateNodeParamsList { methodList :: !Method }
64 | UpdateNodeParamsGraph { methodGraphMetric :: !GraphMetric
65 , methodGraphClustering :: !PartitionMethod
66 , methodGraphEdgesStrength :: !Strength
69 | UpdateNodeParamsTexts { methodTexts :: !Granularity }
71 | UpdateNodeParamsBoard { methodBoard :: !Charts }
73 | LinkNodeReq { nodeType :: !NodeType
76 | UpdateNodePhylo { config :: !PhyloSubConfig }
79 ----------------------------------------------------------------------
80 data Method = Basic | Advanced | WithModel
81 deriving (Generic, Eq, Ord, Enum, Bounded)
83 ----------------------------------------------------------------------
84 data Granularity = NewNgrams | NewTexts | Both
85 deriving (Generic, Eq, Ord, Enum, Bounded)
87 ----------------------------------------------------------------------
88 data Charts = Sources | Authors | Institutes | Ngrams | All
89 deriving (Generic, Eq, Ord, Enum, Bounded)
91 ------------------------------------------------------------------------
92 api :: UserId -> NodeId -> ServerT API (GargM Env GargError)
94 serveJobsAPI UpdateNodeJob $ \p log'' ->
97 printDebug "updateNode" x
99 in updateNode uId nId p (liftBase . log')
101 updateNode :: (HasSettings env, FlowCmdM env err m)
107 updateNode uId nId (UpdateNodeParamsGraph metric method strength) logStatus = do
109 logStatus JobLog { _scst_succeeded = Just 1
110 , _scst_failed = Just 0
111 , _scst_remaining = Just 1
112 , _scst_events = Just []
114 printDebug "Computing graph: " method
115 _ <- recomputeGraph uId nId method (Just metric) (Just strength) True
116 printDebug "Graph computed: " method
118 pure JobLog { _scst_succeeded = Just 2
119 , _scst_failed = Just 0
120 , _scst_remaining = Just 0
121 , _scst_events = Just []
124 updateNode _uId nid1 (LinkNodeReq nt nid2) logStatus = do
125 logStatus JobLog { _scst_succeeded = Just 1
126 , _scst_failed = Just 0
127 , _scst_remaining = Just 1
128 , _scst_events = Just []
131 NodeAnnuaire -> pairing nid2 nid1 Nothing -- defaultList
132 NodeCorpus -> pairing nid1 nid2 Nothing -- defaultList
133 _ -> panic $ "[G.API.N.Update.updateNode] NodeType not implemented"
134 <> cs (show nt <> " nid1: " <> show nid1 <> " nid2: " <> show nid2)
136 pure JobLog { _scst_succeeded = Just 2
137 , _scst_failed = Just 0
138 , _scst_remaining = Just 0
139 , _scst_events = Just []
142 -- | `Advanced` to update graphs
143 updateNode _uId lId (UpdateNodeParamsList Advanced) logStatus = do
144 logStatus JobLog { _scst_succeeded = Just 1
145 , _scst_failed = Just 0
146 , _scst_remaining = Just 2
147 , _scst_events = Just []
149 corpusId <- view node_parent_id <$> getNode lId
151 logStatus JobLog { _scst_succeeded = Just 2
152 , _scst_failed = Just 0
153 , _scst_remaining = Just 1
154 , _scst_events = Just []
157 _ <- case corpusId of
159 _ <- Metrics.updatePie cId (Just lId) NgramsTypes.Authors Nothing
160 _ <- Metrics.updateTree cId (Just lId) NgramsTypes.Institutes MapTerm
161 _ <- Metrics.updatePie cId (Just lId) NgramsTypes.Sources Nothing
165 pure JobLog { _scst_succeeded = Just 3
166 , _scst_failed = Just 0
167 , _scst_remaining = Just 0
168 , _scst_events = Just []
171 updateNode _uId lId (UpdateNodeParamsList _mode) logStatus = do
172 logStatus JobLog { _scst_succeeded = Just 1
173 , _scst_failed = Just 0
174 , _scst_remaining = Just 2
175 , _scst_events = Just []
177 corpusId <- view node_parent_id <$> getNode lId
179 logStatus JobLog { _scst_succeeded = Just 2
180 , _scst_failed = Just 0
181 , _scst_remaining = Just 1
182 , _scst_events = Just []
185 _ <- case corpusId of
187 _ <- reIndexWith cId lId NgramsTerms (Set.singleton MapTerm)
188 _ <- updateNgramsOccurrences cId (Just lId)
192 pure JobLog { _scst_succeeded = Just 3
193 , _scst_failed = Just 0
194 , _scst_remaining = Just 0
195 , _scst_events = Just []
198 updateNode _userId phyloId (UpdateNodePhylo config) logStatus = do
199 logStatus JobLog { _scst_succeeded = Just 1
200 , _scst_failed = Just 0
201 , _scst_remaining = Just 2
202 , _scst_events = Just []
205 corpusId' <- view node_parent_id <$> getNode phyloId
207 let corpusId = fromMaybe (panic "") corpusId'
209 phy <- flowPhyloAPI (subConfig2config config) corpusId
211 logStatus JobLog { _scst_succeeded = Just 2
212 , _scst_failed = Just 0
213 , _scst_remaining = Just 1
214 , _scst_events = Just []
217 _ <- updateHyperdata phyloId (HyperdataPhylo Nothing (Just phy))
219 pure JobLog { _scst_succeeded = Just 3
220 , _scst_failed = Just 0
221 , _scst_remaining = Just 0
222 , _scst_events = Just []
226 updateNode _uId tId (UpdateNodeParamsTexts _mode) logStatus = do
227 logStatus JobLog { _scst_succeeded = Just 1
228 , _scst_failed = Just 0
229 , _scst_remaining = Just 2
230 , _scst_events = Just []
232 corpusId <- view node_parent_id <$> getNode tId
233 lId <- defaultList $ fromMaybe (panic "[G.A.N.Update] updateNode/UpdateNodeParamsTexts: no defaultList") corpusId
235 logStatus JobLog { _scst_succeeded = Just 2
236 , _scst_failed = Just 0
237 , _scst_remaining = Just 1
238 , _scst_events = Just []
241 _ <- case corpusId of
243 _ <- reIndexWith cId lId NgramsTerms (Set.singleton MapTerm)
244 _ <- updateNgramsOccurrences cId (Just lId)
245 _ <- updateContextScore cId (Just lId)
246 _ <- Metrics.updateChart cId (Just lId) NgramsTypes.Docs Nothing
247 -- printDebug "updateContextsScore" (cId, lId, u)
251 pure JobLog { _scst_succeeded = Just 3
252 , _scst_failed = Just 0
253 , _scst_remaining = Just 0
254 , _scst_events = Just []
261 updateNode _uId _nId _p logStatus = do
262 simuLogs logStatus 10
264 ------------------------------------------------------------------------
265 -- TODO unPrefix "pn_" FromJSON, ToJSON, ToSchema, adapt frontend.
266 instance FromJSON UpdateNodeParams where
267 parseJSON = genericParseJSON (defaultOptions { sumEncoding = GUA.defaultTaggedObject })
269 instance ToJSON UpdateNodeParams where
270 toJSON = genericToJSON (defaultOptions { sumEncoding = GUA.defaultTaggedObject })
272 instance ToSchema UpdateNodeParams
273 instance Arbitrary UpdateNodeParams where
275 l <- UpdateNodeParamsList <$> arbitrary
276 g <- UpdateNodeParamsGraph <$> arbitrary <*> arbitrary <*> arbitrary
277 t <- UpdateNodeParamsTexts <$> arbitrary
278 b <- UpdateNodeParamsBoard <$> arbitrary
281 instance FromJSON Method
282 instance ToJSON Method
283 instance ToSchema Method
284 instance Arbitrary Method where
285 arbitrary = elements [ minBound .. maxBound ]
287 instance FromJSON Granularity
288 instance ToJSON Granularity
289 instance ToSchema Granularity
290 instance Arbitrary Granularity where
291 arbitrary = elements [ minBound .. maxBound ]
293 instance FromJSON Charts
294 instance ToJSON Charts
295 instance ToSchema Charts
296 instance Arbitrary Charts where
297 arbitrary = elements [ minBound .. maxBound ]
299 ------------------------------------------------------------------------