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
20 import Data.Maybe (Maybe(..))
22 import GHC.Generics (Generic)
23 import Prelude (Enum, Bounded, minBound, maxBound)
25 import Servant.Job.Async (JobFunction(..), serveJobsAPI)
26 import Test.QuickCheck (elements)
27 import Test.QuickCheck.Arbitrary
29 import Gargantext.API.Admin.Orchestrator.Types (JobLog(..), AsyncJobs)
30 import Gargantext.API.Admin.Types (HasSettings)
31 import Gargantext.API.Prelude (GargServer, simuLogs)
32 import Gargantext.Core.Viz.Graph.API (recomputeGraph)
33 import Gargantext.Core.Methods.Distances (GraphMetric(..))
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)
39 ------------------------------------------------------------------------
40 type API = Summary " Update node according to NodeType params"
41 :> AsyncJobs JobLog '[JSON] UpdateNodeParams JobLog
43 ------------------------------------------------------------------------
44 data UpdateNodeParams = UpdateNodeParamsList { methodList :: !Method }
45 | UpdateNodeParamsGraph { methodGraph :: !GraphMetric }
46 | UpdateNodeParamsTexts { methodTexts :: !Granularity }
47 | UpdateNodeParamsBoard { methodBoard :: !Charts }
48 | LinkNodeReq { nodeType :: !NodeType, id :: !NodeId }
51 ----------------------------------------------------------------------
52 data Method = Basic | Advanced | WithModel
53 deriving (Generic, Eq, Ord, Enum, Bounded)
55 ----------------------------------------------------------------------
56 data Granularity = NewNgrams | NewTexts | Both
57 deriving (Generic, Eq, Ord, Enum, Bounded)
59 ----------------------------------------------------------------------
60 data Charts = Sources | Authors | Institutes | Ngrams | All
61 deriving (Generic, Eq, Ord, Enum, Bounded)
63 ------------------------------------------------------------------------
64 api :: UserId -> NodeId -> GargServer API
67 JobFunction (\p log'' ->
70 printDebug "updateNode" x
72 in updateNode uId nId p (liftBase . log')
75 updateNode :: (HasSettings env, FlowCmdM env err m)
81 updateNode uId nId (UpdateNodeParamsGraph metric) logStatus = do
83 logStatus JobLog { _scst_succeeded = Just 1
84 , _scst_failed = Just 0
85 , _scst_remaining = Just 1
86 , _scst_events = Just []
89 _ <- recomputeGraph uId nId (Just metric)
91 pure JobLog { _scst_succeeded = Just 2
92 , _scst_failed = Just 0
93 , _scst_remaining = Just 0
94 , _scst_events = Just []
97 updateNode _uId nid1 (LinkNodeReq nt nid2) logStatus = do
98 logStatus JobLog { _scst_succeeded = Just 1
99 , _scst_failed = Just 0
100 , _scst_remaining = Just 1
101 , _scst_events = Just []
104 NodeAnnuaire -> pairing nid2 nid1 Nothing -- defaultList
105 NodeCorpus -> pairing nid1 nid2 Nothing -- defaultList
106 _ -> panic $ "[G.API.N.Update.updateNode] NodeType not implemented"
109 pure JobLog { _scst_succeeded = Just 2
110 , _scst_failed = Just 0
111 , _scst_remaining = Just 0
112 , _scst_events = Just []
116 updateNode _uId _nId _p logStatus = do
117 simuLogs logStatus 10
119 ------------------------------------------------------------------------
120 -- TODO unPrefix "pn_" FromJSON, ToJSON, ToSchema, adapt frontend.
121 instance FromJSON UpdateNodeParams where
122 parseJSON = genericParseJSON (defaultOptions { sumEncoding = ObjectWithSingleField })
124 instance ToJSON UpdateNodeParams where
125 toJSON = genericToJSON (defaultOptions { sumEncoding = ObjectWithSingleField })
127 instance ToSchema UpdateNodeParams
128 instance Arbitrary UpdateNodeParams where
130 l <- UpdateNodeParamsList <$> arbitrary
131 g <- UpdateNodeParamsGraph <$> arbitrary
132 t <- UpdateNodeParamsTexts <$> arbitrary
133 b <- UpdateNodeParamsBoard <$> arbitrary
136 instance FromJSON Method
137 instance ToJSON Method
138 instance ToSchema Method
139 instance Arbitrary Method where
140 arbitrary = elements [ minBound .. maxBound ]
142 instance FromJSON Granularity
143 instance ToJSON Granularity
144 instance ToSchema Granularity
145 instance Arbitrary Granularity where
146 arbitrary = elements [ minBound .. maxBound ]
148 instance FromJSON Charts
149 instance ToJSON Charts
150 instance ToSchema Charts
151 instance Arbitrary Charts where
152 arbitrary = elements [ minBound .. maxBound ]
154 ------------------------------------------------------------------------