Commit a4a45ff6 authored by Alexandre Delanoë's avatar Alexandre Delanoë

[GRAPH][OPTIM] store or compute if needed only.

parent 0df9416b
Pipeline #640 canceled with stage
...@@ -351,7 +351,7 @@ serverPrivateGargAPI' (AuthenticatedUser (NodeId uid)) ...@@ -351,7 +351,7 @@ serverPrivateGargAPI' (AuthenticatedUser (NodeId uid))
:<|> withAccess (Proxy :: Proxy TableNgramsApi) Proxy uid <$> PathNode <*> apiNgramsTableDoc :<|> withAccess (Proxy :: Proxy TableNgramsApi) Proxy uid <$> PathNode <*> apiNgramsTableDoc
:<|> count -- TODO: undefined :<|> count -- TODO: undefined
:<|> withAccess (Proxy :: Proxy SearchPairsAPI) Proxy uid <$> PathNode <*> searchPairs -- TODO: move elsewhere :<|> withAccess (Proxy :: Proxy SearchPairsAPI) Proxy uid <$> PathNode <*> searchPairs -- TODO: move elsewhere
:<|> withAccess (Proxy :: Proxy GraphAPI) Proxy uid <$> PathNode <*> graphAPI -- TODO: mock :<|> withAccess (Proxy :: Proxy GraphAPI) Proxy uid <$> PathNode <*> graphAPI uid -- TODO: mock
:<|> withAccess (Proxy :: Proxy TreeAPI) Proxy uid <$> PathNode <*> treeAPI :<|> withAccess (Proxy :: Proxy TreeAPI) Proxy uid <$> PathNode <*> treeAPI
:<|> addToCorpus :<|> addToCorpus
:<|> New.api -- TODO-SECURITY :<|> New.api -- TODO-SECURITY
......
...@@ -504,6 +504,12 @@ nodeGraphW maybeName maybeGraph pId = node NodeGraph name graph (Just pId) ...@@ -504,6 +504,12 @@ nodeGraphW maybeName maybeGraph pId = node NodeGraph name graph (Just pId)
name = maybe "Graph" identity maybeName name = maybe "Graph" identity maybeName
graph = maybe arbitraryGraph identity maybeGraph graph = maybe arbitraryGraph identity maybeGraph
mkGraph :: ParentId -> UserId -> Cmd err [GraphId]
mkGraph p u = insertNodesR [nodeGraphW Nothing Nothing p u]
insertGraph :: ParentId -> UserId -> HyperdataGraph -> Cmd err [GraphId]
insertGraph p u h = insertNodesR [nodeGraphW Nothing (Just h) p u]
------------------------------------------------------------------------ ------------------------------------------------------------------------
arbitraryPhylo :: HyperdataPhylo arbitraryPhylo :: HyperdataPhylo
arbitraryPhylo = HyperdataPhylo Nothing Nothing arbitraryPhylo = HyperdataPhylo Nothing Nothing
...@@ -691,11 +697,6 @@ defaultList cId = ...@@ -691,11 +697,6 @@ defaultList cId =
mkNode :: NodeType -> ParentId -> UserId -> Cmd err [NodeId] mkNode :: NodeType -> ParentId -> UserId -> Cmd err [NodeId]
mkNode nt p u = insertNodesR [nodeDefault nt p u] mkNode nt p u = insertNodesR [nodeDefault nt p u]
mkGraph :: ParentId -> UserId -> Cmd err [GraphId]
mkGraph p u = insertNodesR [nodeGraphW Nothing Nothing p u]
mkDashboard :: ParentId -> UserId -> Cmd err [NodeId] mkDashboard :: ParentId -> UserId -> Cmd err [NodeId]
mkDashboard p u = insertNodesR [nodeDashboardW Nothing Nothing p u] mkDashboard p u = insertNodesR [nodeDashboardW Nothing Nothing p u]
where where
......
...@@ -73,12 +73,13 @@ mkCmd k = do ...@@ -73,12 +73,13 @@ mkCmd k = do
conn <- view connection conn <- view connection
liftIO $ k conn liftIO $ k conn
runCmd :: (HasConnection env) => env runCmd :: (HasConnection env)
-> Cmd' env err a => env -> Cmd' env err a
-> IO (Either err a) -> IO (Either err a)
runCmd env m = runExceptT $ runReaderT m env runCmd env m = runExceptT $ runReaderT m env
runOpaQuery :: Default FromFields fields haskells => Select fields -> Cmd err [haskells] runOpaQuery :: Default FromFields fields haskells
=> Select fields -> Cmd err [haskells]
runOpaQuery q = mkCmd $ \c -> runQuery c q runOpaQuery q = mkCmd $ \c -> runQuery c q
formatPGSQuery :: PGS.ToRow a => PGS.Query -> a -> Cmd err DB.ByteString formatPGSQuery :: PGS.ToRow a => PGS.Query -> a -> Cmd err DB.ByteString
......
...@@ -26,7 +26,8 @@ module Gargantext.Viz.Graph.API ...@@ -26,7 +26,8 @@ module Gargantext.Viz.Graph.API
import Control.Lens -- (set, (^.), (_Just), (^?)) import Control.Lens -- (set, (^.), (_Just), (^?))
import Control.Monad.IO.Class (liftIO) import Control.Monad.IO.Class (liftIO)
import Gargantext.API.Ngrams (currentVersion, listNgramsChangedSince, Versioned(..)) import Data.Maybe (Maybe(..))
import Gargantext.API.Ngrams (currentVersion)
import Gargantext.API.Ngrams.Tools import Gargantext.API.Ngrams.Tools
import Gargantext.API.Types import Gargantext.API.Types
import Gargantext.Core.Types.Main import Gargantext.Core.Types.Main
...@@ -34,9 +35,9 @@ import Gargantext.Database.Config ...@@ -34,9 +35,9 @@ import Gargantext.Database.Config
import Gargantext.Database.Metrics.NgramsByNode (getNodesByNgramsOnlyUser) import Gargantext.Database.Metrics.NgramsByNode (getNodesByNgramsOnlyUser)
import Gargantext.Database.Schema.Ngrams import Gargantext.Database.Schema.Ngrams
import Gargantext.Database.Node.Select import Gargantext.Database.Node.Select
import Gargantext.Database.Schema.Node (getNode) import Gargantext.Database.Schema.Node (getNode, defaultList, insertGraph)
import Gargantext.Database.Schema.Node (defaultList)
import Gargantext.Database.Types.Node hiding (node_id) -- (GraphId, ListId, CorpusId, NodeId) import Gargantext.Database.Types.Node hiding (node_id) -- (GraphId, ListId, CorpusId, NodeId)
import Gargantext.Database.Node.UpdateOpaleye (updateHyperdata)
import Gargantext.Prelude import Gargantext.Prelude
import Gargantext.Viz.Graph import Gargantext.Viz.Graph
import Gargantext.Viz.Graph.Tools -- (cooc2graph) import Gargantext.Viz.Graph.Tools -- (cooc2graph)
...@@ -52,15 +53,15 @@ type GraphAPI = Get '[JSON] Graph ...@@ -52,15 +53,15 @@ type GraphAPI = Get '[JSON] Graph
:<|> Put '[JSON] Int :<|> Put '[JSON] Int
graphAPI :: NodeId -> GargServer GraphAPI graphAPI :: UserId -> NodeId -> GargServer GraphAPI
graphAPI n = getGraph n graphAPI u n = getGraph u n
:<|> postGraph n :<|> postGraph n
:<|> putGraph n :<|> putGraph n
------------------------------------------------------------------------ ------------------------------------------------------------------------
getGraph :: NodeId -> GargServer (Get '[JSON] Graph) getGraph :: UserId -> NodeId -> GargServer (Get '[JSON] Graph)
getGraph nId = do getGraph uId nId = do
nodeGraph <- getNode nId HyperdataGraph nodeGraph <- getNode nId HyperdataGraph
let graph = nodeGraph ^. node_hyperdata . hyperdataGraph let graph = nodeGraph ^. node_hyperdata . hyperdataGraph
let graphVersion = graph ^? _Just let graphVersion = graph ^? _Just
...@@ -73,14 +74,22 @@ getGraph nId = do ...@@ -73,14 +74,22 @@ getGraph nId = do
let cId = maybe (panic "[ERR:G.V.G.API] Node has no parent") let cId = maybe (panic "[ERR:G.V.G.API] Node has no parent")
identity identity
$ nodeGraph ^. node_parentId $ nodeGraph ^. node_parentId
case graph of case graph of
Nothing -> computeGraph 0 nId NgramsTerms v Nothing -> do
graph' <- computeGraph cId NgramsTerms v
_ <- insertGraph cId uId (HyperdataGraph $ Just graph')
pure graph'
Just graph' -> if graphVersion == Just v Just graph' -> if graphVersion == Just v
then pure graph' then pure graph'
else computeGraph 0 nId NgramsTerms v else do
graph'' <- computeGraph cId NgramsTerms v
computeGraph cId nId nt v = do _ <- updateHyperdata nId (HyperdataGraph $ Just graph'')
pure graph''
-- TODO use Database Monad only here ?
computeGraph :: CorpusId -> NgramsType -> Int -> GargServer (Get '[JSON] Graph)
computeGraph cId nt v = do
lId <- defaultList cId lId <- defaultList cId
let metadata = GraphMetadata "Title" [cId] let metadata = GraphMetadata "Title" [cId]
...@@ -99,7 +108,8 @@ computeGraph cId nId nt v = do ...@@ -99,7 +108,8 @@ computeGraph cId nId nt v = do
<$> getNodesByNgramsOnlyUser cId (lIds <> [lId]) nt (Map.keys ngs) <$> getNodesByNgramsOnlyUser cId (lIds <> [lId]) nt (Map.keys ngs)
graph <- liftIO $ cooc2graph 0 myCooc graph <- liftIO $ cooc2graph 0 myCooc
pure $ set graph_metadata (Just metadata) graph let graph' = set graph_metadata (Just metadata) graph
pure graph'
...@@ -109,8 +119,3 @@ postGraph = undefined ...@@ -109,8 +119,3 @@ postGraph = undefined
putGraph :: NodeId -> GargServer (Put '[JSON] Int) putGraph :: NodeId -> GargServer (Put '[JSON] Int)
putGraph = undefined putGraph = undefined
-- | Instances
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment