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 Gargantext.API.Ngrams.List (reIndexWith)
27 import Gargantext.API.Prelude (GargServer, simuLogs)
28 import Gargantext.Core.Methods.Distances (GraphMetric(..))
29 import Gargantext.Core.Viz.Graph.API (recomputeGraph)
30 import Gargantext.Database.Query.Table.Node (getNode)
31 import Gargantext.Database.Schema.Node (node_parent_id)
32 import Gargantext.Core.Types.Main (ListType(..))
33 import Gargantext.Database.Schema.Ngrams (NgramsType(NgramsTerms))
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.Prelude (Ord, Eq, (<$>), ($), liftBase, (.), printDebug, pure, show, cs, (<>), panic)
38 import Prelude (Enum, Bounded, minBound, maxBound)
40 import Servant.Job.Async (JobFunction(..), serveJobsAPI)
41 import Test.QuickCheck (elements)
42 import Test.QuickCheck.Arbitrary
43 import qualified Data.Set as Set
45 ------------------------------------------------------------------------
46 type API = Summary " Update node according to NodeType params"
47 :> AsyncJobs JobLog '[JSON] UpdateNodeParams JobLog
49 ------------------------------------------------------------------------
50 data UpdateNodeParams = UpdateNodeParamsList { methodList :: !Method }
51 | UpdateNodeParamsGraph { methodGraph :: !GraphMetric }
52 | UpdateNodeParamsTexts { methodTexts :: !Granularity }
53 | UpdateNodeParamsBoard { methodBoard :: !Charts }
54 | LinkNodeReq { nodeType :: !NodeType, id :: !NodeId }
57 ----------------------------------------------------------------------
58 data Method = Basic | Advanced | WithModel
59 deriving (Generic, Eq, Ord, Enum, Bounded)
61 ----------------------------------------------------------------------
62 data Granularity = NewNgrams | NewTexts | Both
63 deriving (Generic, Eq, Ord, Enum, Bounded)
65 ----------------------------------------------------------------------
66 data Charts = Sources | Authors | Institutes | Ngrams | All
67 deriving (Generic, Eq, Ord, Enum, Bounded)
69 ------------------------------------------------------------------------
70 api :: UserId -> NodeId -> GargServer API
73 JobFunction (\p log'' ->
76 printDebug "updateNode" x
78 in updateNode uId nId p (liftBase . log')
81 updateNode :: (HasSettings env, FlowCmdM env err m)
87 updateNode uId nId (UpdateNodeParamsGraph metric) logStatus = do
89 logStatus JobLog { _scst_succeeded = Just 1
90 , _scst_failed = Just 0
91 , _scst_remaining = Just 1
92 , _scst_events = Just []
95 _ <- recomputeGraph uId nId (Just metric)
97 pure JobLog { _scst_succeeded = Just 2
98 , _scst_failed = Just 0
99 , _scst_remaining = Just 0
100 , _scst_events = Just []
103 updateNode _uId nid1 (LinkNodeReq nt nid2) logStatus = do
104 logStatus JobLog { _scst_succeeded = Just 1
105 , _scst_failed = Just 0
106 , _scst_remaining = Just 1
107 , _scst_events = Just []
110 NodeAnnuaire -> pairing nid2 nid1 Nothing -- defaultList
111 NodeCorpus -> pairing nid1 nid2 Nothing -- defaultList
112 _ -> panic $ "[G.API.N.Update.updateNode] NodeType not implemented"
115 pure JobLog { _scst_succeeded = Just 2
116 , _scst_failed = Just 0
117 , _scst_remaining = Just 0
118 , _scst_events = Just []
121 updateNode _uId lId (UpdateNodeParamsList _mode) logStatus = do
122 logStatus JobLog { _scst_succeeded = Just 1
123 , _scst_failed = Just 0
124 , _scst_remaining = Just 2
125 , _scst_events = Just []
127 corpusId <- view node_parent_id <$> getNode lId
129 logStatus JobLog { _scst_succeeded = Just 2
130 , _scst_failed = Just 0
131 , _scst_remaining = Just 1
132 , _scst_events = Just []
135 _ <- case corpusId of
136 Just cId -> reIndexWith cId lId NgramsTerms (Set.singleton MapTerm)
139 pure JobLog { _scst_succeeded = Just 3
140 , _scst_failed = Just 0
141 , _scst_remaining = Just 0
142 , _scst_events = Just []
146 updateNode _uId _nId _p logStatus = do
147 simuLogs logStatus 10
149 ------------------------------------------------------------------------
150 -- TODO unPrefix "pn_" FromJSON, ToJSON, ToSchema, adapt frontend.
151 instance FromJSON UpdateNodeParams where
152 parseJSON = genericParseJSON (defaultOptions { sumEncoding = ObjectWithSingleField })
154 instance ToJSON UpdateNodeParams where
155 toJSON = genericToJSON (defaultOptions { sumEncoding = ObjectWithSingleField })
157 instance ToSchema UpdateNodeParams
158 instance Arbitrary UpdateNodeParams where
160 l <- UpdateNodeParamsList <$> arbitrary
161 g <- UpdateNodeParamsGraph <$> arbitrary
162 t <- UpdateNodeParamsTexts <$> arbitrary
163 b <- UpdateNodeParamsBoard <$> arbitrary
166 instance FromJSON Method
167 instance ToJSON Method
168 instance ToSchema Method
169 instance Arbitrary Method where
170 arbitrary = elements [ minBound .. maxBound ]
172 instance FromJSON Granularity
173 instance ToJSON Granularity
174 instance ToSchema Granularity
175 instance Arbitrary Granularity where
176 arbitrary = elements [ minBound .. maxBound ]
178 instance FromJSON Charts
179 instance ToJSON Charts
180 instance ToSchema Charts
181 instance Arbitrary Charts where
182 arbitrary = elements [ minBound .. maxBound ]
184 ------------------------------------------------------------------------