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(..))
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)
34 import Gargantext.Database.Action.Flow.Pairing (pairing)
35 import Gargantext.Database.Action.Flow.Types (FlowCmdM)
36 import Gargantext.Database.Admin.Types.Node
37 import Gargantext.Database.Query.Table.Node (getNode)
38 import Gargantext.Database.Schema.Node (node_parent_id)
39 import Gargantext.Database.Schema.Ngrams (NgramsType(NgramsTerms))
40 import Gargantext.Prelude (Bool(..), Ord, Eq, (<$>), ($), liftBase, (.), printDebug, pure, show, cs, (<>), panic)
41 import qualified Gargantext.Utils.Aeson as GUA
42 import Prelude (Enum, Bounded, minBound, maxBound)
44 import Servant.Job.Async (JobFunction(..), serveJobsAPI)
45 import Test.QuickCheck (elements)
46 import Test.QuickCheck.Arbitrary
47 import qualified Data.Set as Set
49 ------------------------------------------------------------------------
50 type API = Summary " Update node according to NodeType params"
51 :> AsyncJobs JobLog '[JSON] UpdateNodeParams JobLog
53 ------------------------------------------------------------------------
54 data UpdateNodeParams = UpdateNodeParamsList { methodList :: !Method }
55 | UpdateNodeParamsGraph { methodGraph :: !GraphMetric }
56 | UpdateNodeParamsTexts { methodTexts :: !Granularity }
57 | UpdateNodeParamsBoard { methodBoard :: !Charts }
58 | LinkNodeReq { nodeType :: !NodeType, id :: !NodeId }
61 ----------------------------------------------------------------------
62 data Method = Basic | Advanced | WithModel
63 deriving (Generic, Eq, Ord, Enum, Bounded)
65 ----------------------------------------------------------------------
66 data Granularity = NewNgrams | NewTexts | Both
67 deriving (Generic, Eq, Ord, Enum, Bounded)
69 ----------------------------------------------------------------------
70 data Charts = Sources | Authors | Institutes | Ngrams | All
71 deriving (Generic, Eq, Ord, Enum, Bounded)
73 ------------------------------------------------------------------------
74 api :: UserId -> NodeId -> GargServer API
77 JobFunction (\p log'' ->
80 printDebug "updateNode" x
82 in updateNode uId nId p (liftBase . log')
85 updateNode :: (HasSettings env, FlowCmdM env err m)
91 updateNode uId nId (UpdateNodeParamsGraph metric) logStatus = do
93 logStatus JobLog { _scst_succeeded = Just 1
94 , _scst_failed = Just 0
95 , _scst_remaining = Just 1
96 , _scst_events = Just []
99 _ <- recomputeGraph uId nId (Just metric) True
101 pure JobLog { _scst_succeeded = Just 2
102 , _scst_failed = Just 0
103 , _scst_remaining = Just 0
104 , _scst_events = Just []
107 updateNode _uId nid1 (LinkNodeReq nt nid2) logStatus = do
108 logStatus JobLog { _scst_succeeded = Just 1
109 , _scst_failed = Just 0
110 , _scst_remaining = Just 1
111 , _scst_events = Just []
114 NodeAnnuaire -> pairing nid2 nid1 Nothing -- defaultList
115 NodeCorpus -> pairing nid1 nid2 Nothing -- defaultList
116 _ -> panic $ "[G.API.N.Update.updateNode] NodeType not implemented"
119 pure JobLog { _scst_succeeded = Just 2
120 , _scst_failed = Just 0
121 , _scst_remaining = Just 0
122 , _scst_events = Just []
125 -- | `Advanced` to update graphs
126 updateNode _uId lId (UpdateNodeParamsList Advanced) logStatus = do
127 logStatus JobLog { _scst_succeeded = Just 1
128 , _scst_failed = Just 0
129 , _scst_remaining = Just 2
130 , _scst_events = Just []
132 corpusId <- view node_parent_id <$> getNode lId
134 logStatus JobLog { _scst_succeeded = Just 2
135 , _scst_failed = Just 0
136 , _scst_remaining = Just 1
137 , _scst_events = Just []
140 _ <- case corpusId of
142 _ <- Metrics.updatePie' cId (Just lId) NgramsTypes.Authors Nothing
143 _ <- Metrics.updateTree' cId (Just lId) NgramsTypes.Institutes MapTerm
144 _ <- Metrics.updatePie' cId (Just lId) NgramsTypes.Sources Nothing
148 pure JobLog { _scst_succeeded = Just 3
149 , _scst_failed = Just 0
150 , _scst_remaining = Just 0
151 , _scst_events = Just []
154 updateNode _uId lId (UpdateNodeParamsList _mode) logStatus = do
155 logStatus JobLog { _scst_succeeded = Just 1
156 , _scst_failed = Just 0
157 , _scst_remaining = Just 2
158 , _scst_events = Just []
160 corpusId <- view node_parent_id <$> getNode lId
162 logStatus JobLog { _scst_succeeded = Just 2
163 , _scst_failed = Just 0
164 , _scst_remaining = Just 1
165 , _scst_events = Just []
168 _ <- case corpusId of
170 _ <- reIndexWith cId lId NgramsTerms (Set.singleton MapTerm)
171 _ <- updateNgramsOccurrences cId (Just lId)
175 pure JobLog { _scst_succeeded = Just 3
176 , _scst_failed = Just 0
177 , _scst_remaining = Just 0
178 , _scst_events = Just []
182 updateNode _uId _nId _p logStatus = do
183 simuLogs logStatus 10
185 ------------------------------------------------------------------------
186 -- TODO unPrefix "pn_" FromJSON, ToJSON, ToSchema, adapt frontend.
187 instance FromJSON UpdateNodeParams where
188 parseJSON = genericParseJSON (defaultOptions { sumEncoding = GUA.defaultTaggedObject })
190 instance ToJSON UpdateNodeParams where
191 toJSON = genericToJSON (defaultOptions { sumEncoding = GUA.defaultTaggedObject })
193 instance ToSchema UpdateNodeParams
194 instance Arbitrary UpdateNodeParams where
196 l <- UpdateNodeParamsList <$> arbitrary
197 g <- UpdateNodeParamsGraph <$> arbitrary
198 t <- UpdateNodeParamsTexts <$> arbitrary
199 b <- UpdateNodeParamsBoard <$> arbitrary
202 instance FromJSON Method
203 instance ToJSON Method
204 instance ToSchema Method
205 instance Arbitrary Method where
206 arbitrary = elements [ minBound .. maxBound ]
208 instance FromJSON Granularity
209 instance ToJSON Granularity
210 instance ToSchema Granularity
211 instance Arbitrary Granularity where
212 arbitrary = elements [ minBound .. maxBound ]
214 instance FromJSON Charts
215 instance ToJSON Charts
216 instance ToSchema Charts
217 instance Arbitrary Charts where
218 arbitrary = elements [ minBound .. maxBound ]
220 ------------------------------------------------------------------------