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.Orchestrator.Types (JobLog(..), AsyncJobs)
25 import Gargantext.API.Admin.Types (HasSettings)
26 import Gargantext.API.Ngrams.List (reIndexWith)
27 import Gargantext.API.Prelude (GargServer, simuLogs)
28 import Gargantext.Core.Methods.Distances (GraphMetric(..))
29 import Gargantext.Core.Types.Main (ListType(..))
30 import Gargantext.Core.Viz.Graph.API (recomputeGraph)
31 import Gargantext.Core.Viz.Graph.Tools (PartitionMethod(..))
32 import Gargantext.Core.Viz.Phylo (PhyloSubConfig(..), subConfig2config)
33 import Gargantext.Core.Viz.Phylo.API.Tools (flowPhyloAPI)
34 import Gargantext.Database.Action.Flow.Pairing (pairing)
35 import Gargantext.Database.Action.Flow.Types (FlowCmdM)
36 import Gargantext.Database.Action.Metrics (updateNgramsOccurrences, updateContextScore)
37 import Gargantext.Database.Admin.Types.Hyperdata
38 import Gargantext.Database.Admin.Types.Node
39 import Gargantext.Database.Query.Table.Node (defaultList, getNode, insertNodes, node)
40 import Gargantext.Database.Schema.Ngrams (NgramsType(NgramsTerms))
41 import Gargantext.Database.Schema.Node (node_parent_id)
42 import Gargantext.Prelude (Bool(..), Ord, Eq, (<$>), ($), liftBase, (.), printDebug, pure, show, cs, (<>), panic, (<*>))
43 import Prelude (Enum, Bounded, minBound, maxBound)
45 import Servant.Job.Async (JobFunction(..), serveJobsAPI)
46 import Test.QuickCheck (elements)
47 import Test.QuickCheck.Arbitrary
48 import qualified Data.Set as Set
49 import qualified Gargantext.API.Metrics as Metrics
50 import qualified Gargantext.API.Ngrams.Types as NgramsTypes
51 import qualified Gargantext.Utils.Aeson as GUA
53 ------------------------------------------------------------------------
54 type API = Summary " Update node according to NodeType params"
55 :> AsyncJobs JobLog '[JSON] UpdateNodeParams JobLog
57 ------------------------------------------------------------------------
58 data UpdateNodeParams = UpdateNodeParamsList { methodList :: !Method }
60 | UpdateNodeParamsGraph { methodGraphMetric :: !GraphMetric
61 , methodGraphClustering :: !PartitionMethod
64 | UpdateNodeParamsTexts { methodTexts :: !Granularity }
66 | UpdateNodeParamsBoard { methodBoard :: !Charts }
68 | LinkNodeReq { nodeType :: !NodeType
71 | UpdateNodePhylo { config :: !PhyloSubConfig }
74 ----------------------------------------------------------------------
75 data Method = Basic | Advanced | WithModel
76 deriving (Generic, Eq, Ord, Enum, Bounded)
78 ----------------------------------------------------------------------
79 data Granularity = NewNgrams | NewTexts | Both
80 deriving (Generic, Eq, Ord, Enum, Bounded)
82 ----------------------------------------------------------------------
83 data Charts = Sources | Authors | Institutes | Ngrams | All
84 deriving (Generic, Eq, Ord, Enum, Bounded)
86 ------------------------------------------------------------------------
87 api :: UserId -> NodeId -> GargServer API
90 JobFunction (\p log'' ->
93 printDebug "updateNode" x
95 in updateNode uId nId p (liftBase . log')
98 updateNode :: (HasSettings env, FlowCmdM env err m)
104 updateNode uId nId (UpdateNodeParamsGraph metric method) logStatus = do
106 logStatus JobLog { _scst_succeeded = Just 1
107 , _scst_failed = Just 0
108 , _scst_remaining = Just 1
109 , _scst_events = Just []
112 _ <- recomputeGraph uId nId method (Just metric) True
114 pure JobLog { _scst_succeeded = Just 2
115 , _scst_failed = Just 0
116 , _scst_remaining = Just 0
117 , _scst_events = Just []
120 updateNode _uId nid1 (LinkNodeReq nt nid2) logStatus = do
121 logStatus JobLog { _scst_succeeded = Just 1
122 , _scst_failed = Just 0
123 , _scst_remaining = Just 1
124 , _scst_events = Just []
127 NodeAnnuaire -> pairing nid2 nid1 Nothing -- defaultList
128 NodeCorpus -> pairing nid1 nid2 Nothing -- defaultList
129 _ -> panic $ "[G.API.N.Update.updateNode] NodeType not implemented"
132 pure JobLog { _scst_succeeded = Just 2
133 , _scst_failed = Just 0
134 , _scst_remaining = Just 0
135 , _scst_events = Just []
138 -- | `Advanced` to update graphs
139 updateNode _uId lId (UpdateNodeParamsList Advanced) logStatus = do
140 logStatus JobLog { _scst_succeeded = Just 1
141 , _scst_failed = Just 0
142 , _scst_remaining = Just 2
143 , _scst_events = Just []
145 corpusId <- view node_parent_id <$> getNode lId
147 logStatus JobLog { _scst_succeeded = Just 2
148 , _scst_failed = Just 0
149 , _scst_remaining = Just 1
150 , _scst_events = Just []
153 _ <- case corpusId of
155 _ <- Metrics.updatePie' cId (Just lId) NgramsTypes.Authors Nothing
156 _ <- Metrics.updateTree' cId (Just lId) NgramsTypes.Institutes MapTerm
157 _ <- Metrics.updatePie' cId (Just lId) NgramsTypes.Sources Nothing
161 pure JobLog { _scst_succeeded = Just 3
162 , _scst_failed = Just 0
163 , _scst_remaining = Just 0
164 , _scst_events = Just []
167 updateNode _uId lId (UpdateNodeParamsList _mode) logStatus = do
168 logStatus JobLog { _scst_succeeded = Just 1
169 , _scst_failed = Just 0
170 , _scst_remaining = Just 2
171 , _scst_events = Just []
173 corpusId <- view node_parent_id <$> getNode lId
175 logStatus JobLog { _scst_succeeded = Just 2
176 , _scst_failed = Just 0
177 , _scst_remaining = Just 1
178 , _scst_events = Just []
181 _ <- case corpusId of
183 _ <- reIndexWith cId lId NgramsTerms (Set.singleton MapTerm)
184 _ <- updateNgramsOccurrences cId (Just lId)
188 pure JobLog { _scst_succeeded = Just 3
189 , _scst_failed = Just 0
190 , _scst_remaining = Just 0
191 , _scst_events = Just []
194 updateNode userId phyloId (UpdateNodePhylo config) logStatus = do
195 logStatus JobLog { _scst_succeeded = Just 1
196 , _scst_failed = Just 0
197 , _scst_remaining = Just 2
198 , _scst_events = Just []
201 corpusId' <- view node_parent_id <$> getNode phyloId
203 let corpusId = fromMaybe (panic "") corpusId'
205 phy <- flowPhyloAPI (subConfig2config config) corpusId
207 logStatus JobLog { _scst_succeeded = Just 2
208 , _scst_failed = Just 0
209 , _scst_remaining = Just 1
210 , _scst_events = Just []
213 _phyloId <- insertNodes [node NodePhylo "Phylo" (HyperdataPhylo Nothing (Just phy)) (Just corpusId) userId]
215 pure JobLog { _scst_succeeded = Just 3
216 , _scst_failed = Just 0
217 , _scst_remaining = Just 0
218 , _scst_events = Just []
222 updateNode _uId tId (UpdateNodeParamsTexts _mode) logStatus = do
223 logStatus JobLog { _scst_succeeded = Just 1
224 , _scst_failed = Just 0
225 , _scst_remaining = Just 2
226 , _scst_events = Just []
228 corpusId <- view node_parent_id <$> getNode tId
229 lId <- defaultList $ fromMaybe (panic "[G.A.N.Update] updateNode/UpdateNodeParamsTexts: no defaultList") corpusId
231 logStatus JobLog { _scst_succeeded = Just 2
232 , _scst_failed = Just 0
233 , _scst_remaining = Just 1
234 , _scst_events = Just []
237 _ <- case corpusId of
239 _ <- reIndexWith cId lId NgramsTerms (Set.singleton MapTerm)
240 _ <- updateNgramsOccurrences cId (Just lId)
241 _ <- updateContextScore cId (Just lId)
242 -- printDebug "updateContextsScore" (cId, lId, u)
246 pure JobLog { _scst_succeeded = Just 3
247 , _scst_failed = Just 0
248 , _scst_remaining = Just 0
249 , _scst_events = Just []
256 updateNode _uId _nId _p logStatus = do
257 simuLogs logStatus 10
259 ------------------------------------------------------------------------
260 -- TODO unPrefix "pn_" FromJSON, ToJSON, ToSchema, adapt frontend.
261 instance FromJSON UpdateNodeParams where
262 parseJSON = genericParseJSON (defaultOptions { sumEncoding = GUA.defaultTaggedObject })
264 instance ToJSON UpdateNodeParams where
265 toJSON = genericToJSON (defaultOptions { sumEncoding = GUA.defaultTaggedObject })
267 instance ToSchema UpdateNodeParams
268 instance Arbitrary UpdateNodeParams where
270 l <- UpdateNodeParamsList <$> arbitrary
271 g <- UpdateNodeParamsGraph <$> arbitrary <*> arbitrary
272 t <- UpdateNodeParamsTexts <$> arbitrary
273 b <- UpdateNodeParamsBoard <$> arbitrary
276 instance FromJSON Method
277 instance ToJSON Method
278 instance ToSchema Method
279 instance Arbitrary Method where
280 arbitrary = elements [ minBound .. maxBound ]
282 instance FromJSON Granularity
283 instance ToJSON Granularity
284 instance ToSchema Granularity
285 instance Arbitrary Granularity where
286 arbitrary = elements [ minBound .. maxBound ]
288 instance FromJSON Charts
289 instance ToJSON Charts
290 instance ToSchema Charts
291 instance Arbitrary Charts where
292 arbitrary = elements [ minBound .. maxBound ]
294 ------------------------------------------------------------------------