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 Gargantext.Core.Types.Individu (User(..))
20 import Control.Lens (view)
22 import Data.Maybe (Maybe(..), fromMaybe)
24 import GHC.Generics (Generic)
25 import Gargantext.API.Admin.EnvTypes (GargJob(..), Env)
26 import Gargantext.API.Admin.Orchestrator.Types (JobLog(..), AsyncJobs)
27 import Gargantext.API.Admin.Types (HasSettings)
28 import Gargantext.API.Ngrams.List (reIndexWith)
29 --import Gargantext.API.Ngrams.Types (TabType(..))
30 import Gargantext.API.Prelude (GargM, GargError, simuLogs)
31 import Gargantext.Core.Methods.Similarities (GraphMetric(..))
32 import Gargantext.Core.Types.Main (ListType(..))
33 import Gargantext.Core.Viz.Graph.API (recomputeGraph)
34 import Gargantext.Core.Viz.Graph.Tools (PartitionMethod(..), BridgenessMethod(..))
35 import Gargantext.Core.Viz.Graph.Types (Strength)
36 import Gargantext.Core.Viz.Phylo (PhyloSubConfigAPI(..), subConfigAPI2config)
37 import Gargantext.Core.Viz.Phylo.API.Tools (flowPhyloAPI)
38 import Gargantext.Database.Action.Mail (sendMail)
39 import Gargantext.Database.Action.Flow.Pairing (pairing)
40 import Gargantext.Database.Action.Flow.Types (FlowCmdM)
41 import Gargantext.Database.Action.Metrics (updateNgramsOccurrences, updateContextScore)
42 import Gargantext.Database.Admin.Types.Hyperdata
43 import Gargantext.Database.Admin.Types.Node
44 import Gargantext.Database.Query.Table.Node (defaultList, getNode)
45 import Gargantext.Database.Query.Table.Node.UpdateOpaleye (updateHyperdata)
46 import Gargantext.Database.Schema.Ngrams (NgramsType(NgramsTerms))
47 import Gargantext.Database.Schema.Node (node_parent_id)
48 import Gargantext.Prelude (Bool(..), Ord, Eq, (<$>), ($), {-printDebug,-} pure, show, cs, (<>), panic, (<*>))
49 import Gargantext.Utils.Jobs (serveJobsAPI, MonadJobStatus(..))
50 import Prelude (Enum, Bounded, minBound, maxBound)
52 import Test.QuickCheck (elements)
53 import Test.QuickCheck.Arbitrary
54 import qualified Data.Set as Set
55 import qualified Gargantext.API.Metrics as Metrics
56 import qualified Gargantext.API.Ngrams.Types as NgramsTypes
57 import qualified Gargantext.Utils.Aeson as GUA
59 ------------------------------------------------------------------------
60 type API = Summary " Update node according to NodeType params"
61 :> AsyncJobs JobLog '[JSON] UpdateNodeParams JobLog
63 ------------------------------------------------------------------------
64 data UpdateNodeParams = UpdateNodeParamsList { methodList :: !Method }
66 | UpdateNodeParamsGraph { methodGraphMetric :: !GraphMetric
67 , methodGraphClustering :: !PartitionMethod
68 , methodGraphBridgeness :: !BridgenessMethod
69 , methodGraphEdgesStrength :: !Strength
70 , methodGraphNodeType1 :: !NgramsType
71 , methodGraphNodeType2 :: !NgramsType
74 | UpdateNodeParamsTexts { methodTexts :: !Granularity }
76 | UpdateNodeParamsBoard { methodBoard :: !Charts }
78 | LinkNodeReq { nodeType :: !NodeType
81 | UpdateNodePhylo { config :: !PhyloSubConfigAPI }
84 ----------------------------------------------------------------------
85 data Method = Basic | Advanced | WithModel
86 deriving (Generic, Eq, Ord, Enum, Bounded)
88 ----------------------------------------------------------------------
89 data Granularity = NewNgrams | NewTexts | Both
90 deriving (Generic, Eq, Ord, Enum, Bounded)
92 ----------------------------------------------------------------------
93 data Charts = Sources | Authors | Institutes | Ngrams | All
94 deriving (Generic, Eq, Ord, Enum, Bounded)
96 ------------------------------------------------------------------------
97 api :: UserId -> NodeId -> ServerT API (GargM Env GargError)
99 serveJobsAPI UpdateNodeJob $ \jHandle p ->
100 updateNode uId nId p jHandle
102 updateNode :: (HasSettings env, FlowCmdM env err m, MonadJobStatus m)
108 updateNode uId nId (UpdateNodeParamsGraph metric partitionMethod bridgeMethod strength nt1 nt2) jobHandle = do
110 markStarted 2 jobHandle
111 -- printDebug "Computing graph: " method
112 _ <- recomputeGraph uId nId partitionMethod bridgeMethod (Just metric) (Just strength) nt1 nt2 True
113 -- printDebug "Graph computed: " method
114 markComplete jobHandle
116 updateNode _uId nid1 (LinkNodeReq nt nid2) jobHandle = do
117 markStarted 2 jobHandle
119 NodeAnnuaire -> pairing nid2 nid1 Nothing -- defaultList
120 NodeCorpus -> pairing nid1 nid2 Nothing -- defaultList
121 _ -> panic $ "[G.API.N.Update.updateNode] NodeType not implemented"
122 <> cs (show nt <> " nid1: " <> show nid1 <> " nid2: " <> show nid2)
124 markComplete jobHandle
126 -- | `Advanced` to update graphs
127 updateNode _uId lId (UpdateNodeParamsList Advanced) jobHandle = do
128 markStarted 3 jobHandle
129 corpusId <- view node_parent_id <$> getNode lId
131 markProgress 1 jobHandle
133 _ <- case corpusId of
135 _ <- Metrics.updatePie cId (Just lId) NgramsTypes.Authors Nothing
136 _ <- Metrics.updateTree cId (Just lId) NgramsTypes.Institutes MapTerm
137 _ <- Metrics.updatePie cId (Just lId) NgramsTypes.Sources Nothing
141 markComplete jobHandle
143 updateNode _uId lId (UpdateNodeParamsList _mode) jobHandle = do
144 markStarted 3 jobHandle
145 corpusId <- view node_parent_id <$> getNode lId
147 markProgress 1 jobHandle
149 _ <- case corpusId of
151 _ <- reIndexWith cId lId NgramsTerms (Set.singleton MapTerm)
152 _ <- updateNgramsOccurrences cId (Just lId)
156 markComplete jobHandle
158 updateNode userId phyloId (UpdateNodePhylo config) jobHandle = do
159 markStarted 3 jobHandle
160 corpusId' <- view node_parent_id <$> getNode phyloId
161 markProgress 1 jobHandle
163 let corpusId = fromMaybe (panic "") corpusId'
165 phy <- flowPhyloAPI (subConfigAPI2config config) corpusId
166 markProgress 2 jobHandle
169 logStatus JobLog { _scst_succeeded = Just 2
170 , _scst_failed = Just 0
171 , _scst_remaining = Just 1
172 , _scst_events = Just []
175 _ <- updateHyperdata phyloId (HyperdataPhylo Nothing (Just phy))
176 sendMail (UserDBId userId)
177 markComplete jobHandle
179 updateNode _uId tId (UpdateNodeParamsTexts _mode) jobHandle = do
180 markStarted 3 jobHandle
181 corpusId <- view node_parent_id <$> getNode tId
182 lId <- defaultList $ fromMaybe (panic "[G.A.N.Update] updateNode/UpdateNodeParamsTexts: no defaultList") corpusId
183 markProgress 1 jobHandle
185 _ <- case corpusId of
187 _ <- reIndexWith cId lId NgramsTerms (Set.singleton MapTerm)
188 _ <- updateNgramsOccurrences cId (Just lId)
189 _ <- updateContextScore cId (Just lId)
190 _ <- Metrics.updateChart cId (Just lId) NgramsTypes.Docs Nothing
191 -- printDebug "updateContextsScore" (cId, lId, u)
195 markComplete jobHandle
198 updateNode _uId _nId _p jobHandle = do
199 simuLogs jobHandle 10
201 ------------------------------------------------------------------------
202 -- TODO unPrefix "pn_" FromJSON, ToJSON, ToSchema, adapt frontend.
203 instance FromJSON UpdateNodeParams where
204 parseJSON = genericParseJSON (defaultOptions { sumEncoding = GUA.defaultTaggedObject })
206 instance ToJSON UpdateNodeParams where
207 toJSON = genericToJSON (defaultOptions { sumEncoding = GUA.defaultTaggedObject })
209 instance ToSchema UpdateNodeParams
210 instance Arbitrary UpdateNodeParams where
212 l <- UpdateNodeParamsList <$> arbitrary
213 g <- UpdateNodeParamsGraph <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary
214 t <- UpdateNodeParamsTexts <$> arbitrary
215 b <- UpdateNodeParamsBoard <$> arbitrary
218 instance FromJSON Method
219 instance ToJSON Method
220 instance ToSchema Method
221 instance Arbitrary Method where
222 arbitrary = elements [ minBound .. maxBound ]
224 instance FromJSON Granularity
225 instance ToJSON Granularity
226 instance ToSchema Granularity
227 instance Arbitrary Granularity where
228 arbitrary = elements [ minBound .. maxBound ]
230 instance FromJSON Charts
231 instance ToJSON Charts
232 instance ToSchema Charts
233 instance Arbitrary Charts where
234 arbitrary = elements [ minBound .. maxBound ]
236 ------------------------------------------------------------------------