[API] metrics refactoring of 'update' methods

parent 9449b840
...@@ -16,7 +16,9 @@ Metrics API ...@@ -16,7 +16,9 @@ Metrics API
module Gargantext.API.Metrics module Gargantext.API.Metrics
where where
import Control.Lens ((%~))
import Control.Lens.Getter (Getting) import Control.Lens.Getter (Getting)
import Control.Lens.Setter (ASetter)
import Data.HashMap.Strict qualified as HashMap import Data.HashMap.Strict qualified as HashMap
import Data.Vector (Vector) import Data.Vector (Vector)
import Gargantext.API.HashedResponse (HashedResponse, constructHashedResponse, hash) import Gargantext.API.HashedResponse (HashedResponse, constructHashedResponse, hash)
...@@ -42,6 +44,7 @@ import Gargantext.Database.Schema.Node (node_hyperdata) ...@@ -42,6 +44,7 @@ import Gargantext.Database.Schema.Node (node_hyperdata)
import Gargantext.Prelude hiding (hash) import Gargantext.Prelude hiding (hash)
import Servant.Server.Generic (AsServerT) import Servant.Server.Generic (AsServerT)
------------------------------------------------------------- -------------------------------------------------------------
scatterApi :: IsGargServer err env m => NodeId -> Named.ScatterAPI (AsServerT m) scatterApi :: IsGargServer err env m => NodeId -> Named.ScatterAPI (AsServerT m)
scatterApi id' = Named.ScatterAPI scatterApi id' = Named.ScatterAPI
...@@ -64,10 +67,8 @@ updateScatter :: HasNodeStory env err m ...@@ -64,10 +67,8 @@ updateScatter :: HasNodeStory env err m
-> TabType -> TabType
-> Maybe Limit -> Maybe Limit
-> m () -> m ()
updateScatter cId maybeListId tabType maybeLimit = do updateScatter cId mListId tabType maybeLimit = do
listId <- case maybeListId of listId <- getListOrDefault cId mListId
Just lid -> pure lid
Nothing -> defaultList cId
-- printDebug "[updateScatter] cId" cId -- printDebug "[updateScatter] cId" cId
-- printDebug "[updateScatter] maybeListId" maybeListId -- printDebug "[updateScatter] maybeListId" maybeListId
-- printDebug "[updateScatter] tabType" tabType -- printDebug "[updateScatter] tabType" tabType
...@@ -93,12 +94,7 @@ updateScatter' cId listId tabType maybeLimit = do ...@@ -93,12 +94,7 @@ updateScatter' cId listId tabType maybeLimit = do
listType t m = maybe (panicTrace errorMsg) fst $ HashMap.lookup t m listType t m = maybe (panicTrace errorMsg) fst $ HashMap.lookup t m
errorMsg = "API.Node.metrics: key absent" errorMsg = "API.Node.metrics: key absent"
node <- getNodeWith listId (Proxy :: Proxy HyperdataList) updateNodeMetrics listId tabType hl_scatter (Metrics metrics)
let hl = node ^. node_hyperdata
scatterMap = hl ^. hl_scatter
_ <- updateHyperdata listId $ hl { _hl_scatter = HashMap.insert tabType (Metrics metrics) scatterMap }
pure $ Metrics metrics
getScatterHash :: HasNodeStory env err m getScatterHash :: HasNodeStory env err m
=> CorpusId => CorpusId
...@@ -131,10 +127,8 @@ updateChart :: HasNodeError err ...@@ -131,10 +127,8 @@ updateChart :: HasNodeError err
-> Maybe ListId -> Maybe ListId
-> TabType -> TabType
-> DBCmd err () -> DBCmd err ()
updateChart cId maybeListId tabType = do updateChart cId mListId tabType = do
listId <- case maybeListId of listId <- getListOrDefault cId mListId
Just lid -> pure lid
Nothing -> defaultList cId
printDebug "[updateChart] cId" cId printDebug "[updateChart] cId" cId
printDebug "[updateChart] listId" listId printDebug "[updateChart] listId" listId
printDebug "[updateChart] tabType" tabType printDebug "[updateChart] tabType" tabType
...@@ -147,13 +141,8 @@ updateChart' :: HasNodeError err ...@@ -147,13 +141,8 @@ updateChart' :: HasNodeError err
-> TabType -> TabType
-> DBCmd err (ChartMetrics Histo) -> DBCmd err (ChartMetrics Histo)
updateChart' cId listId tabType = do updateChart' cId listId tabType = do
node <- getNodeWith listId (Proxy :: Proxy HyperdataList) metrics <- histoData cId
let hl = node ^. node_hyperdata updateNodeMetrics listId tabType hl_chart (ChartMetrics metrics)
chartMap = hl ^. hl_chart
h <- histoData cId
_ <- updateHyperdata listId $ hl { _hl_chart = HashMap.insert tabType (ChartMetrics h) chartMap }
pure $ ChartMetrics h
getChartHash :: HasNodeStory env err m getChartHash :: HasNodeStory env err m
...@@ -186,12 +175,10 @@ updatePie :: HasNodeStory env err m ...@@ -186,12 +175,10 @@ updatePie :: HasNodeStory env err m
-> Maybe ListId -> Maybe ListId
-> TabType -> TabType
-> m () -> m ()
updatePie cId maybeListId tabType = do updatePie cId mListId tabType = do
listId <- case maybeListId of listId <- getListOrDefault cId mListId
Just lid -> pure lid
Nothing -> defaultList cId
printDebug "[updatePie] cId" cId printDebug "[updatePie] cId" cId
printDebug "[updatePie] maybeListId" maybeListId printDebug "[updatePie] mListId" mListId
printDebug "[updatePie] tabType" tabType printDebug "[updatePie] tabType" tabType
_ <- updatePie' cId listId tabType _ <- updatePie' cId listId tabType
pure () pure ()
...@@ -202,14 +189,8 @@ updatePie' :: (HasNodeStory env err m) ...@@ -202,14 +189,8 @@ updatePie' :: (HasNodeStory env err m)
-> TabType -> TabType
-> m (ChartMetrics Histo) -> m (ChartMetrics Histo)
updatePie' cId listId tabType = do updatePie' cId listId tabType = do
node <- getNodeWith listId (Proxy :: Proxy HyperdataList) metrics <- chartData cId (ngramsTypeFromTabType tabType) MapTerm
let hl = node ^. node_hyperdata updateNodeMetrics listId tabType hl_pie (ChartMetrics metrics)
pieMap = hl ^. hl_pie
p <- chartData cId (ngramsTypeFromTabType tabType) MapTerm
_ <- updateHyperdata listId $ hl { _hl_pie = HashMap.insert tabType (ChartMetrics p) pieMap }
pure $ ChartMetrics p
getPieHash :: HasNodeStory env err m getPieHash :: HasNodeStory env err m
=> CorpusId => CorpusId
...@@ -243,12 +224,10 @@ updateTree :: HasNodeStory env err m ...@@ -243,12 +224,10 @@ updateTree :: HasNodeStory env err m
-> TabType -> TabType
-> ListType -> ListType
-> m () -> m ()
updateTree cId maybeListId tabType listType = do updateTree cId mListId tabType listType = do
listId <- case maybeListId of listId <- getListOrDefault cId mListId
Just lid -> pure lid
Nothing -> defaultList cId
printDebug "[updateTree] cId" cId printDebug "[updateTree] cId" cId
printDebug "[updateTree] maybeListId" maybeListId printDebug "[updateTree] mListId" mListId
printDebug "[updateTree] tabType" tabType printDebug "[updateTree] tabType" tabType
printDebug "[updateTree] listType" listType printDebug "[updateTree] listType" listType
_ <- updateTree' cId listId tabType listType _ <- updateTree' cId listId tabType listType
...@@ -261,13 +240,8 @@ updateTree' :: HasNodeStory env err m ...@@ -261,13 +240,8 @@ updateTree' :: HasNodeStory env err m
-> ListType -> ListType
-> m (ChartMetrics (Vector NgramsTree)) -> m (ChartMetrics (Vector NgramsTree))
updateTree' cId listId tabType listType = do updateTree' cId listId tabType listType = do
node <- getNodeWith listId (Proxy :: Proxy HyperdataList) metrics <- treeData cId (ngramsTypeFromTabType tabType) listType
let hl = node ^. node_hyperdata updateNodeMetrics listId tabType hl_tree (ChartMetrics metrics)
treeMap = hl ^. hl_tree
t <- treeData cId (ngramsTypeFromTabType tabType) listType
_ <- updateHyperdata listId $ hl { _hl_tree = HashMap.insert tabType (ChartMetrics t) treeMap }
pure $ ChartMetrics t
getTreeHash :: HasNodeStory env err m getTreeHash :: HasNodeStory env err m
=> CorpusId => CorpusId
...@@ -289,9 +263,7 @@ metricsGetter :: (HasNodeStory env err m, ToJSON a) ...@@ -289,9 +263,7 @@ metricsGetter :: (HasNodeStory env err m, ToJSON a)
-> (CorpusId -> ListId -> TabType -> m a) -> (CorpusId -> ListId -> TabType -> m a)
-> m (HashedResponse a) -> m (HashedResponse a)
metricsGetter cId mListId tabType l up = do metricsGetter cId mListId tabType l up = do
listId <- case mListId of listId <- getListOrDefault cId mListId
Just lid -> pure lid
Nothing -> defaultList cId
node <- getNodeWith listId (Proxy :: Proxy HyperdataList) node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
let metricsMap = node ^. node_hyperdata ^. l let metricsMap = node ^. node_hyperdata ^. l
mMetrics = HashMap.lookup tabType metricsMap mMetrics = HashMap.lookup tabType metricsMap
...@@ -302,3 +274,24 @@ metricsGetter cId mListId tabType l up = do ...@@ -302,3 +274,24 @@ metricsGetter cId mListId tabType l up = do
up cId listId tabType up cId listId tabType
pure $ constructHashedResponse metrics pure $ constructHashedResponse metrics
getListOrDefault :: HasNodeError err => CorpusId -> Maybe ListId -> DBCmd err ListId
getListOrDefault cId mListId = case mListId of
Just lid -> pure lid
Nothing -> defaultList cId
updateNodeMetrics :: (HasNodeError err)
=> ListId
-> TabType
-- -> _Lens
-> ASetter HyperdataList HyperdataList (HashMap.HashMap TabType a) (HashMap.HashMap TabType a)
-> a
-> DBCmd err a
updateNodeMetrics listId tabType setter metrics = do
node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
let hl = node ^. node_hyperdata
_ <- updateHyperdata listId (setter %~ (HashMap.insert tabType metrics) $ hl)
pure metrics
...@@ -446,11 +446,11 @@ getOrMkList pId uId = ...@@ -446,11 +446,11 @@ getOrMkList pId uId =
mkList' pId' uId' = insertDefaultNode NodeList pId' uId' mkList' pId' uId' = insertDefaultNode NodeList pId' uId'
-- | TODO remove defaultList -- | TODO remove defaultList
defaultList :: (HasNodeError err, HasDBid NodeType) => CorpusId -> DBCmd err ListId defaultList :: (HasNodeError err) => CorpusId -> DBCmd err ListId
defaultList cId = defaultList cId =
maybe (nodeError (NoListFound cId)) (pure . view node_id) . headMay =<< getListsWithParentId cId maybe (nodeError (NoListFound cId)) (pure . view node_id) . headMay =<< getListsWithParentId cId
getListsWithParentId :: HasDBid NodeType => NodeId -> DBCmd err [Node HyperdataList] getListsWithParentId :: NodeId -> DBCmd err [Node HyperdataList]
getListsWithParentId n = runOpaQuery $ selectNodesWith' n (Just NodeList) getListsWithParentId n = runOpaQuery $ selectNodesWith' n (Just NodeList)
-- | Returns the /root/ public node for the input user. By root we mean that -- | Returns the /root/ public node for the input user. By root we mean that
......
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