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
21 import GHC.Generics (Generic)
22 import Data.Maybe (Maybe(..))
23 import Gargantext.API.Admin.Settings (HasSettings)
24 import Gargantext.API.Admin.Orchestrator.Types (JobLog(..))
25 import Gargantext.API.Node.Corpus.New (AsyncJobs)
26 import Gargantext.API.Prelude (GargServer, simuLogs)
27 import Gargantext.Viz.Graph.Distances (GraphMetric(..), Distance(..))
28 import Gargantext.Viz.Graph.API (recomputeGraph)
29 import Gargantext.Database.Action.Flow.Types (FlowCmdM)
30 import Gargantext.Database.Admin.Types.Node
31 import Gargantext.Prelude (Ord, Eq, (<$>), ($), liftBase, (.), printDebug, pure)
32 import Prelude (Enum, Bounded, minBound, maxBound)
34 import Servant.Job.Async (JobFunction(..), serveJobsAPI)
35 import Test.QuickCheck (elements)
36 import Test.QuickCheck.Arbitrary
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 }
50 ----------------------------------------------------------------------
51 data Method = Basic | Advanced | WithModel
52 deriving (Generic, Eq, Ord, Enum, Bounded)
54 ----------------------------------------------------------------------
55 data Granularity = NewNgrams | NewTexts | Both
56 deriving (Generic, Eq, Ord, Enum, Bounded)
58 ----------------------------------------------------------------------
59 data Charts = Sources | Authors | Institutes | Ngrams | All
60 deriving (Generic, Eq, Ord, Enum, Bounded)
62 ------------------------------------------------------------------------
63 api :: UserId -> NodeId -> GargServer API
66 JobFunction (\p log ->
69 printDebug "updateNode" x
71 in updateNode uId nId p (liftBase . log')
74 updateNode :: (HasSettings env, FlowCmdM env err m)
80 updateNode uId nId (UpdateNodeParamsGraph metric) logStatus = do
82 logStatus JobLog { _scst_succeeded = Just 1
83 , _scst_failed = Just 0
84 , _scst_remaining = Just 1
85 , _scst_events = Just []
89 Order1 -> recomputeGraph uId nId Conditional
90 Order2 -> recomputeGraph uId nId Distributional
92 pure JobLog { _scst_succeeded = Just 2
93 , _scst_failed = Just 0
94 , _scst_remaining = Just 0
95 , _scst_events = Just []
99 updateNode _uId _nId _p logStatus = do
100 simuLogs logStatus 10
102 ------------------------------------------------------------------------
103 -- TODO unPrefix "pn_" FromJSON, ToJSON, ToSchema, adapt frontend.
104 instance FromJSON UpdateNodeParams where
105 parseJSON = genericParseJSON (defaultOptions { sumEncoding = ObjectWithSingleField })
107 instance ToJSON UpdateNodeParams where
108 toJSON = genericToJSON (defaultOptions { sumEncoding = ObjectWithSingleField })
110 instance ToSchema UpdateNodeParams
111 instance Arbitrary UpdateNodeParams where
113 l <- UpdateNodeParamsList <$> arbitrary
114 g <- UpdateNodeParamsGraph <$> arbitrary
115 t <- UpdateNodeParamsTexts <$> arbitrary
116 b <- UpdateNodeParamsBoard <$> arbitrary
119 instance FromJSON Method
120 instance ToJSON Method
121 instance ToSchema Method
122 instance Arbitrary Method where
123 arbitrary = elements [ minBound .. maxBound ]
125 instance FromJSON Granularity
126 instance ToJSON Granularity
127 instance ToSchema Granularity
128 instance Arbitrary Granularity where
129 arbitrary = elements [ minBound .. maxBound ]
131 instance FromJSON Charts
132 instance ToJSON Charts
133 instance ToSchema Charts
134 instance Arbitrary Charts where
135 arbitrary = elements [ minBound .. maxBound ]
137 ------------------------------------------------------------------------