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(..), Distance(..))
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 []
90 Order1 -> recomputeGraph uId nId Conditional
91 Order2 -> recomputeGraph uId nId Distributional
93 pure JobLog { _scst_succeeded = Just 2
94 , _scst_failed = Just 0
95 , _scst_remaining = Just 0
96 , _scst_events = Just []
99 updateNode _uId nid1 (LinkNodeReq nt nid2) logStatus = do
100 logStatus JobLog { _scst_succeeded = Just 1
101 , _scst_failed = Just 0
102 , _scst_remaining = Just 1
103 , _scst_events = Just []
106 NodeAnnuaire -> pairing nid2 nid1 Nothing -- defaultList
107 NodeCorpus -> pairing nid1 nid2 Nothing -- defaultList
108 _ -> panic $ "[G.API.N.Update.updateNode] NodeType not implemented"
111 pure JobLog { _scst_succeeded = Just 2
112 , _scst_failed = Just 0
113 , _scst_remaining = Just 0
114 , _scst_events = Just []
118 updateNode _uId _nId _p logStatus = do
119 simuLogs logStatus 10
121 ------------------------------------------------------------------------
122 -- TODO unPrefix "pn_" FromJSON, ToJSON, ToSchema, adapt frontend.
123 instance FromJSON UpdateNodeParams where
124 parseJSON = genericParseJSON (defaultOptions { sumEncoding = ObjectWithSingleField })
126 instance ToJSON UpdateNodeParams where
127 toJSON = genericToJSON (defaultOptions { sumEncoding = ObjectWithSingleField })
129 instance ToSchema UpdateNodeParams
130 instance Arbitrary UpdateNodeParams where
132 l <- UpdateNodeParamsList <$> arbitrary
133 g <- UpdateNodeParamsGraph <$> arbitrary
134 t <- UpdateNodeParamsTexts <$> arbitrary
135 b <- UpdateNodeParamsBoard <$> arbitrary
138 instance FromJSON Method
139 instance ToJSON Method
140 instance ToSchema Method
141 instance Arbitrary Method where
142 arbitrary = elements [ minBound .. maxBound ]
144 instance FromJSON Granularity
145 instance ToJSON Granularity
146 instance ToSchema Granularity
147 instance Arbitrary Granularity where
148 arbitrary = elements [ minBound .. maxBound ]
150 instance FromJSON Charts
151 instance ToJSON Charts
152 instance ToSchema Charts
153 instance Arbitrary Charts where
154 arbitrary = elements [ minBound .. maxBound ]
156 ------------------------------------------------------------------------