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 qualified Gargantext.API.Metrics as Metrics
27 import Gargantext.API.Ngrams.List (reIndexWith)
28 import qualified Gargantext.API.Ngrams.Types as NgramsTypes
29 import Gargantext.API.Prelude (GargServer, simuLogs)
30 import Gargantext.Core.Methods.Distances (GraphMetric(..))
31 import Gargantext.Core.Types.Main (ListType(..))
32 import Gargantext.Core.Viz.Graph.API (recomputeGraph)
33 import Gargantext.Database.Action.Metrics (updateNgramsOccurrences, updateContextScore)
34 import Gargantext.Database.Action.Flow.Pairing (pairing)
35 import Gargantext.Database.Query.Table.Node (defaultList)
36 import Gargantext.Database.Action.Flow.Types (FlowCmdM)
37 import Gargantext.Database.Admin.Types.Node
38 import Gargantext.Database.Query.Table.Node (getNode)
39 import Gargantext.Database.Schema.Node (node_parent_id)
40 import Gargantext.Database.Schema.Ngrams (NgramsType(NgramsTerms))
41 import Gargantext.Prelude (Bool(..), Ord, Eq, (<$>), ($), liftBase, (.), printDebug, pure, show, cs, (<>), panic)
42 import qualified Gargantext.Utils.Aeson as GUA
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
50 ------------------------------------------------------------------------
51 type API = Summary " Update node according to NodeType params"
52 :> AsyncJobs JobLog '[JSON] UpdateNodeParams JobLog
54 ------------------------------------------------------------------------
55 data UpdateNodeParams = UpdateNodeParamsList { methodList :: !Method }
56 | UpdateNodeParamsGraph { methodGraph :: !GraphMetric }
57 | UpdateNodeParamsTexts { methodTexts :: !Granularity }
58 | UpdateNodeParamsBoard { methodBoard :: !Charts }
59 | LinkNodeReq { nodeType :: !NodeType, id :: !NodeId }
62 ----------------------------------------------------------------------
63 data Method = Basic | Advanced | WithModel
64 deriving (Generic, Eq, Ord, Enum, Bounded)
66 ----------------------------------------------------------------------
67 data Granularity = NewNgrams | NewTexts | Both
68 deriving (Generic, Eq, Ord, Enum, Bounded)
70 ----------------------------------------------------------------------
71 data Charts = Sources | Authors | Institutes | Ngrams | All
72 deriving (Generic, Eq, Ord, Enum, Bounded)
74 ------------------------------------------------------------------------
75 api :: UserId -> NodeId -> GargServer API
78 JobFunction (\p log'' ->
81 printDebug "updateNode" x
83 in updateNode uId nId p (liftBase . log')
86 updateNode :: (HasSettings env, FlowCmdM env err m)
92 updateNode uId nId (UpdateNodeParamsGraph metric) logStatus = do
94 logStatus JobLog { _scst_succeeded = Just 1
95 , _scst_failed = Just 0
96 , _scst_remaining = Just 1
97 , _scst_events = Just []
100 _ <- recomputeGraph uId nId (Just metric) True
102 pure JobLog { _scst_succeeded = Just 2
103 , _scst_failed = Just 0
104 , _scst_remaining = Just 0
105 , _scst_events = Just []
108 updateNode _uId nid1 (LinkNodeReq nt nid2) logStatus = do
109 logStatus JobLog { _scst_succeeded = Just 1
110 , _scst_failed = Just 0
111 , _scst_remaining = Just 1
112 , _scst_events = Just []
115 NodeAnnuaire -> pairing nid2 nid1 Nothing -- defaultList
116 NodeCorpus -> pairing nid1 nid2 Nothing -- defaultList
117 _ -> panic $ "[G.API.N.Update.updateNode] NodeType not implemented"
120 pure JobLog { _scst_succeeded = Just 2
121 , _scst_failed = Just 0
122 , _scst_remaining = Just 0
123 , _scst_events = Just []
126 -- | `Advanced` to update graphs
127 updateNode _uId lId (UpdateNodeParamsList Advanced) logStatus = do
128 logStatus JobLog { _scst_succeeded = Just 1
129 , _scst_failed = Just 0
130 , _scst_remaining = Just 2
131 , _scst_events = Just []
133 corpusId <- view node_parent_id <$> getNode lId
135 logStatus JobLog { _scst_succeeded = Just 2
136 , _scst_failed = Just 0
137 , _scst_remaining = Just 1
138 , _scst_events = Just []
141 _ <- case corpusId of
143 _ <- Metrics.updatePie' cId (Just lId) NgramsTypes.Authors Nothing
144 _ <- Metrics.updateTree' cId (Just lId) NgramsTypes.Institutes MapTerm
145 _ <- Metrics.updatePie' cId (Just lId) NgramsTypes.Sources Nothing
149 pure JobLog { _scst_succeeded = Just 3
150 , _scst_failed = Just 0
151 , _scst_remaining = Just 0
152 , _scst_events = Just []
155 updateNode _uId lId (UpdateNodeParamsList _mode) logStatus = do
156 logStatus JobLog { _scst_succeeded = Just 1
157 , _scst_failed = Just 0
158 , _scst_remaining = Just 2
159 , _scst_events = Just []
161 corpusId <- view node_parent_id <$> getNode lId
163 logStatus JobLog { _scst_succeeded = Just 2
164 , _scst_failed = Just 0
165 , _scst_remaining = Just 1
166 , _scst_events = Just []
169 _ <- case corpusId of
171 _ <- reIndexWith cId lId NgramsTerms (Set.singleton MapTerm)
172 _ <- updateNgramsOccurrences cId (Just lId)
176 pure JobLog { _scst_succeeded = Just 3
177 , _scst_failed = Just 0
178 , _scst_remaining = Just 0
179 , _scst_events = Just []
182 updateNode _uId tId (UpdateNodeParamsTexts _mode) logStatus = do
183 logStatus JobLog { _scst_succeeded = Just 1
184 , _scst_failed = Just 0
185 , _scst_remaining = Just 2
186 , _scst_events = Just []
188 corpusId <- view node_parent_id <$> getNode tId
189 lId <- defaultList $ fromMaybe (panic "[G.A.N.Update] updateNode/UpdateNodeParamsTexts: no defaultList") corpusId
191 logStatus JobLog { _scst_succeeded = Just 2
192 , _scst_failed = Just 0
193 , _scst_remaining = Just 1
194 , _scst_events = Just []
197 _ <- case corpusId of
199 _ <- reIndexWith cId lId NgramsTerms (Set.singleton MapTerm)
200 _ <- updateNgramsOccurrences cId (Just lId)
201 _ <- updateContextScore cId (Just lId)
202 -- printDebug "updateContextsScore" (cId, lId, u)
206 pure JobLog { _scst_succeeded = Just 3
207 , _scst_failed = Just 0
208 , _scst_remaining = Just 0
209 , _scst_events = Just []
216 updateNode _uId _nId _p logStatus = do
217 simuLogs logStatus 10
219 ------------------------------------------------------------------------
220 -- TODO unPrefix "pn_" FromJSON, ToJSON, ToSchema, adapt frontend.
221 instance FromJSON UpdateNodeParams where
222 parseJSON = genericParseJSON (defaultOptions { sumEncoding = GUA.defaultTaggedObject })
224 instance ToJSON UpdateNodeParams where
225 toJSON = genericToJSON (defaultOptions { sumEncoding = GUA.defaultTaggedObject })
227 instance ToSchema UpdateNodeParams
228 instance Arbitrary UpdateNodeParams where
230 l <- UpdateNodeParamsList <$> arbitrary
231 g <- UpdateNodeParamsGraph <$> arbitrary
232 t <- UpdateNodeParamsTexts <$> arbitrary
233 b <- UpdateNodeParamsBoard <$> arbitrary
236 instance FromJSON Method
237 instance ToJSON Method
238 instance ToSchema Method
239 instance Arbitrary Method where
240 arbitrary = elements [ minBound .. maxBound ]
242 instance FromJSON Granularity
243 instance ToJSON Granularity
244 instance ToSchema Granularity
245 instance Arbitrary Granularity where
246 arbitrary = elements [ minBound .. maxBound ]
248 instance FromJSON Charts
249 instance ToJSON Charts
250 instance ToSchema Charts
251 instance Arbitrary Charts where
252 arbitrary = elements [ minBound .. maxBound ]
254 ------------------------------------------------------------------------