Commit 66f2d55d authored by Alexandre Delanoë's avatar Alexandre Delanoë

[Crypto] Hash API (hash fun)

parent f25271ad
...@@ -362,14 +362,14 @@ loadPage session { corpusId, listId, nodeId, query, tabType } = do ...@@ -362,14 +362,14 @@ loadPage session { corpusId, listId, nodeId, query, tabType } = do
--liftEffect $ log3 "loading documents page: loadPage with Offset and limit" offset limit --liftEffect $ log3 "loading documents page: loadPage with Offset and limit" offset limit
-- res <- get $ toUrl endConfigStateful Back (Tab tabType offset limit (convOrderBy <$> orderBy)) (Just nodeId) -- res <- get $ toUrl endConfigStateful Back (Tab tabType offset limit (convOrderBy <$> orderBy)) (Just nodeId)
let p = NodeAPI Node (Just nodeId) $ "table" <> "?tabType=" <> (showTabType' tabType) let p = NodeAPI Node (Just nodeId) $ "table" <> "?tabType=" <> (showTabType' tabType)
HashedResponse { md5, value: res } <- (get session p) :: Aff (HashedResponse (TableResult Response)) HashedResponse { hash, value: res } <- (get session p) :: Aff (HashedResponse (TableResult Response))
let docs = res2corpus <$> res.docs let docs = res2corpus <$> res.docs
let ret = if mock then let ret = if mock then
--Tuple 0 (take limit $ drop offset sampleData) --Tuple 0 (take limit $ drop offset sampleData)
Tuple 0 sampleData Tuple 0 sampleData
else else
Tuple res.count docs Tuple res.count docs
pure $ HashedResponse { md5, value: ret } pure $ HashedResponse { hash, value: ret }
where where
res2corpus :: Response -> DocumentsView res2corpus :: Response -> DocumentsView
res2corpus (Response r) = res2corpus (Response r) =
...@@ -382,9 +382,9 @@ loadPage session { corpusId, listId, nodeId, query, tabType } = do ...@@ -382,9 +382,9 @@ loadPage session { corpusId, listId, nodeId, query, tabType } = do
, ngramCount : r.ngramCount , ngramCount : r.ngramCount
} }
getPageMD5 :: Session -> PageParams -> Aff String getPageHash :: Session -> PageParams -> Aff String
getPageMD5 session { corpusId, listId, nodeId, query, tabType } = do getPageHash session { corpusId, listId, nodeId, query, tabType } = do
let p = NodeAPI Node (Just nodeId) $ "table/md5" <> "?tabType=" <> (showTabType' tabType) let p = NodeAPI Node (Just nodeId) $ "table/hash" <> "?tabType=" <> (showTabType' tabType)
(get session p) :: Aff String (get session p) :: Aff String
...@@ -403,7 +403,7 @@ pageLayoutCpt :: R.Component PageLayoutProps ...@@ -403,7 +403,7 @@ pageLayoutCpt :: R.Component PageLayoutProps
pageLayoutCpt = R.hooksComponent "G.C.DocsTable.pageLayout" cpt where pageLayoutCpt = R.hooksComponent "G.C.DocsTable.pageLayout" cpt where
cpt props@{frontends, session, nodeId, listId, corpusId, tabType, query, params} _ = cpt props@{frontends, session, nodeId, listId, corpusId, tabType, query, params} _ =
-- useLoader path (loadPage session) paint -- useLoader path (loadPage session) paint
useLoaderWithCache path keyFunc (getPageMD5 session) (loadPage session) paint useLoaderWithCache path keyFunc (getPageHash session) (loadPage session) paint
where where
path = { nodeId, listId, corpusId, tabType, query, params } path = { nodeId, listId, corpusId, tabType, query, params }
paint (Tuple count docs) = page params (newProps count) docs paint (Tuple count docs) = page params (newProps count) docs
......
...@@ -9,7 +9,7 @@ import Reactix as R ...@@ -9,7 +9,7 @@ import Reactix as R
import Gargantext.Prelude import Gargantext.Prelude
import Gargantext.Components.Nodes.Corpus.Chart.Types (Reload, Path, Props, MetricsProps, ReloadPath) import Gargantext.Components.Nodes.Corpus.Chart.Types (Reload, Path, Props, MetricsProps, ReloadPath)
import Gargantext.Hooks.Loader (MD5, HashedResponse, useLoader, useLoaderWithCache, useLoaderWithCacheAPI) import Gargantext.Hooks.Loader (Hash, HashedResponse, useLoader, useLoaderWithCache, useLoaderWithCacheAPI)
import Gargantext.Sessions (Session) import Gargantext.Sessions (Session)
import Gargantext.Utils.CacheAPI as GUC import Gargantext.Utils.CacheAPI as GUC
...@@ -33,7 +33,7 @@ metricsLoadViewCpt = R.hooksComponent "G.C.N.C.C.metricsLoadView" cpt ...@@ -33,7 +33,7 @@ metricsLoadViewCpt = R.hooksComponent "G.C.N.C.C.metricsLoadView" cpt
loaded { path, reload, session } l loaded { path, reload, session } l
type MetricsWithCacheLoadViewProps res ret = ( type MetricsWithCacheLoadViewProps res ret = (
getMetricsMD5 :: Session -> ReloadPath -> Aff MD5 getMetricsHash :: Session -> ReloadPath -> Aff Hash
, handleResponse :: HashedResponse res -> ret , handleResponse :: HashedResponse res -> ret
, loaded :: Record MetricsProps -> ret -> R.Element , loaded :: Record MetricsProps -> ret -> R.Element
, mkRequest :: ReloadPath -> GUC.Request , mkRequest :: ReloadPath -> GUC.Request
...@@ -48,13 +48,13 @@ metricsWithCacheLoadViewCpt :: forall res ret. DecodeJson res => ...@@ -48,13 +48,13 @@ metricsWithCacheLoadViewCpt :: forall res ret. DecodeJson res =>
R.Component (MetricsWithCacheLoadViewProps res ret) R.Component (MetricsWithCacheLoadViewProps res ret)
metricsWithCacheLoadViewCpt = R.hooksComponent "G.C.N.C.C.metricsWithCacheLoadView" cpt metricsWithCacheLoadViewCpt = R.hooksComponent "G.C.N.C.C.metricsWithCacheLoadView" cpt
where where
cpt { getMetricsMD5, handleResponse, loaded, mkRequest, path, reload, session } _ = do cpt { getMetricsHash, handleResponse, loaded, mkRequest, path, reload, session } _ = do
-- useLoaderWithCache (fst reload /\ path) (metricsKeyFunc keyFunc) (getMetricsMD5 session) (getMetrics session) $ \l -> -- useLoaderWithCache (fst reload /\ path) (metricsKeyFunc keyFunc) (getMetricsHash session) (getMetrics session) $ \l ->
-- loaded session path reload l -- loaded session path reload l
-- metricsKeyFunc keyFunc st@(_ /\ { corpusId, listId, tabType }) = -- metricsKeyFunc keyFunc st@(_ /\ { corpusId, listId, tabType }) =
-- "metrics-" <> (show tabType) <> "-" <> (show corpusId) <> "-" <> (show listId) <> "--" <> (keyFunc st) -- "metrics-" <> (show tabType) <> "-" <> (show corpusId) <> "-" <> (show listId) <> "--" <> (keyFunc st)
useLoaderWithCacheAPI { cacheEndpoint: (getMetricsMD5 session) useLoaderWithCacheAPI { cacheEndpoint: (getMetricsHash session)
, handleResponse , handleResponse
, mkRequest , mkRequest
, path: (fst reload /\ path) , path: (fst reload /\ path)
......
...@@ -6,24 +6,22 @@ import Data.Maybe (Maybe(..)) ...@@ -6,24 +6,22 @@ import Data.Maybe (Maybe(..))
import Data.Tuple (Tuple(..)) import Data.Tuple (Tuple(..))
import Data.Tuple.Nested ((/\)) import Data.Tuple.Nested ((/\))
import Effect.Aff (Aff) import Effect.Aff (Aff)
import Reactix as R
import Reactix.DOM.HTML as H
import Gargantext.Prelude
import Gargantext.Components.Charts.Options.ECharts (Options(..), chart, xAxis', yAxis')
import Gargantext.Components.Charts.Options.Series (seriesBarD1)
import Gargantext.Components.Charts.Options.Color (grey) import Gargantext.Components.Charts.Options.Color (grey)
import Gargantext.Components.Charts.Options.Font (itemStyle, mkTooltip, templateFormatter)
import Gargantext.Components.Charts.Options.Data (dataSerie) import Gargantext.Components.Charts.Options.Data (dataSerie)
import Gargantext.Components.Charts.Options.ECharts (Options(..), chart, xAxis', yAxis')
import Gargantext.Components.Charts.Options.Font (itemStyle, mkTooltip, templateFormatter)
import Gargantext.Components.Charts.Options.Series (seriesBarD1)
import Gargantext.Components.Nodes.Corpus.Chart.Common (metricsLoadView, metricsWithCacheLoadView) import Gargantext.Components.Nodes.Corpus.Chart.Common (metricsLoadView, metricsWithCacheLoadView)
import Gargantext.Components.Nodes.Corpus.Chart.Types import Gargantext.Components.Nodes.Corpus.Chart.Types
import Gargantext.Components.Nodes.Corpus.Chart.Utils as U import Gargantext.Components.Nodes.Corpus.Chart.Utils as U
import Gargantext.Hooks.Loader (HashedResponse(..)) import Gargantext.Hooks.Loader (HashedResponse(..))
import Gargantext.Prelude
import Gargantext.Routes (SessionRoute(..)) import Gargantext.Routes (SessionRoute(..))
import Gargantext.Sessions (Session, get) import Gargantext.Sessions (Session, get)
import Gargantext.Types (ChartType(..), TabType(..)) import Gargantext.Types (ChartType(..), TabType(..))
import Gargantext.Utils.CacheAPI as GUC import Gargantext.Utils.CacheAPI as GUC
import Reactix as R
import Reactix.DOM.HTML as H
newtype ChartMetrics = ChartMetrics { newtype ChartMetrics = ChartMetrics {
"data" :: HistoMetrics "data" :: HistoMetrics
...@@ -64,14 +62,14 @@ chartOptions (HistoMetrics { dates: dates', count: count'}) = Options ...@@ -64,14 +62,14 @@ chartOptions (HistoMetrics { dates: dates', count: count'}) = Options
-- getMetrics :: Session -> Tuple Reload (Record Path) -> Aff (HashedResponse HistoMetrics) -- getMetrics :: Session -> Tuple Reload (Record Path) -> Aff (HashedResponse HistoMetrics)
-- getMetrics session (_ /\ { corpusId, limit, listId, tabType }) = do -- getMetrics session (_ /\ { corpusId, limit, listId, tabType }) = do
-- HashedResponse { md5, value: ChartMetrics ms } <- get session chart -- HashedResponse { hash, value: ChartMetrics ms } <- get session chart
-- pure $ HashedResponse { md5, value: ms."data" } -- pure $ HashedResponse { hash, value: ms."data" }
-- where -- where
-- chart = Chart {chartType: Histo, listId, tabType, limit} (Just corpusId) -- chart = Chart {chartType: Histo, listId, tabType, limit} (Just corpusId)
getMetricsMD5 :: Session -> Tuple Reload (Record Path) -> Aff String getMetricsHash :: Session -> Tuple Reload (Record Path) -> Aff String
getMetricsMD5 session (_ /\ { corpusId, limit, listId, tabType }) = do getMetricsHash session (_ /\ { corpusId, limit, listId, tabType }) = do
get session $ ChartMD5 { chartType: Histo, listId, tabType } (Just corpusId) get session $ ChartHash { chartType: Histo, listId, tabType } (Just corpusId)
chartUrl :: Record Path -> SessionRoute chartUrl :: Record Path -> SessionRoute
chartUrl { corpusId, limit, listId, tabType } = Chart {chartType: Histo, limit, listId, tabType} (Just corpusId) chartUrl { corpusId, limit, listId, tabType } = Chart {chartType: Histo, limit, listId, tabType} (Just corpusId)
...@@ -91,7 +89,7 @@ histoCpt = R.hooksComponent "G.C.N.C.C.H.histo" cpt ...@@ -91,7 +89,7 @@ histoCpt = R.hooksComponent "G.C.N.C.C.H.histo" cpt
cpt { path, session } _ = do cpt { path, session } _ = do
reload <- R.useState' 0 reload <- R.useState' 0
pure $ metricsWithCacheLoadView { pure $ metricsWithCacheLoadView {
getMetricsMD5 getMetricsHash
, handleResponse , handleResponse
, loaded , loaded
, mkRequest: mkRequest session , mkRequest: mkRequest session
......
...@@ -100,14 +100,14 @@ scatterOptions metrics' = Options ...@@ -100,14 +100,14 @@ scatterOptions metrics' = Options
-- getMetrics :: Session -> Tuple Reload (Record Path) -> Aff (HashedResponse Loaded) -- getMetrics :: Session -> Tuple Reload (Record Path) -> Aff (HashedResponse Loaded)
-- getMetrics session (_ /\ { corpusId, limit, listId, tabType }) = do -- getMetrics session (_ /\ { corpusId, limit, listId, tabType }) = do
-- HashedResponse { md5, value: Metrics ms } <- get session metrics' -- HashedResponse { hash, value: Metrics ms } <- get session metrics'
-- pure $ HashedResponse { md5, value: ms."data" } -- pure $ HashedResponse { hash, value: ms."data" }
-- where -- where
-- metrics' = CorpusMetrics {limit, listId, tabType} (Just corpusId) -- metrics' = CorpusMetrics {limit, listId, tabType} (Just corpusId)
getMetricsMD5 :: Session -> Tuple Reload (Record Path) -> Aff String getMetricsHash :: Session -> Tuple Reload (Record Path) -> Aff String
getMetricsMD5 session (_ /\ { corpusId, listId, tabType }) = getMetricsHash session (_ /\ { corpusId, listId, tabType }) =
get session $ CorpusMetricsMD5 { listId, tabType } (Just corpusId) get session $ CorpusMetricsHash { listId, tabType } (Just corpusId)
chartUrl :: Record Path -> SessionRoute chartUrl :: Record Path -> SessionRoute
chartUrl { corpusId, limit, listId, tabType } = CorpusMetrics { limit, listId, tabType } (Just corpusId) chartUrl { corpusId, limit, listId, tabType } = CorpusMetrics { limit, listId, tabType } (Just corpusId)
...@@ -127,7 +127,7 @@ metricsCpt = R.hooksComponent "G.C.N.C.C.M.metrics" cpt ...@@ -127,7 +127,7 @@ metricsCpt = R.hooksComponent "G.C.N.C.C.M.metrics" cpt
cpt {path, session} _ = do cpt {path, session} _ = do
reload <- R.useState' 0 reload <- R.useState' 0
pure $ metricsWithCacheLoadView { pure $ metricsWithCacheLoadView {
getMetricsMD5 getMetricsHash
, handleResponse , handleResponse
, loaded , loaded
, mkRequest: mkRequest session , mkRequest: mkRequest session
......
...@@ -9,24 +9,22 @@ import Data.String (take, joinWith, Pattern(..), split, length) ...@@ -9,24 +9,22 @@ import Data.String (take, joinWith, Pattern(..), split, length)
import Data.Tuple (Tuple(..)) import Data.Tuple (Tuple(..))
import Data.Tuple.Nested ((/\)) import Data.Tuple.Nested ((/\))
import Effect.Aff (Aff) import Effect.Aff (Aff)
import Reactix as R
import Reactix.DOM.HTML as H
import Gargantext.Prelude
import Gargantext.Components.Charts.Options.ECharts (Options(..), chart, xAxis', yAxis')
import Gargantext.Components.Charts.Options.Series (seriesBarD1, seriesPieD1)
import Gargantext.Components.Charts.Options.Color (blue) import Gargantext.Components.Charts.Options.Color (blue)
import Gargantext.Components.Charts.Options.Font (itemStyle, mkTooltip, templateFormatter)
import Gargantext.Components.Charts.Options.Data (dataSerie) import Gargantext.Components.Charts.Options.Data (dataSerie)
import Gargantext.Components.Charts.Options.ECharts (Options(..), chart, xAxis', yAxis')
import Gargantext.Components.Charts.Options.Font (itemStyle, mkTooltip, templateFormatter)
import Gargantext.Components.Charts.Options.Series (seriesBarD1, seriesPieD1)
import Gargantext.Components.Nodes.Corpus.Chart.Common (metricsLoadView, metricsWithCacheLoadView) import Gargantext.Components.Nodes.Corpus.Chart.Common (metricsLoadView, metricsWithCacheLoadView)
import Gargantext.Components.Nodes.Corpus.Chart.Types import Gargantext.Components.Nodes.Corpus.Chart.Types
import Gargantext.Components.Nodes.Corpus.Chart.Utils as U import Gargantext.Components.Nodes.Corpus.Chart.Utils as U
import Gargantext.Hooks.Loader (HashedResponse(..)) import Gargantext.Hooks.Loader (HashedResponse(..))
import Gargantext.Prelude
import Gargantext.Routes (SessionRoute(..)) import Gargantext.Routes (SessionRoute(..))
import Gargantext.Sessions (Session, get) import Gargantext.Sessions (Session, get)
import Gargantext.Types (ChartType(..), TabType) import Gargantext.Types (ChartType(..), TabType)
import Gargantext.Utils.CacheAPI as GUC import Gargantext.Utils.CacheAPI as GUC
import Reactix as R
import Reactix.DOM.HTML as H
newtype ChartMetrics = ChartMetrics { newtype ChartMetrics = ChartMetrics {
"data" :: HistoMetrics "data" :: HistoMetrics
...@@ -84,13 +82,13 @@ chartOptionsPie (HistoMetrics { dates: dates', count: count'}) = Options ...@@ -84,13 +82,13 @@ chartOptionsPie (HistoMetrics { dates: dates', count: count'}) = Options
-- getMetrics :: Session -> Tuple Reload (Record Path) -> Aff (HashedResponse HistoMetrics) -- getMetrics :: Session -> Tuple Reload (Record Path) -> Aff (HashedResponse HistoMetrics)
-- getMetrics session (_ /\ { corpusId, limit, listId, tabType }) = do -- getMetrics session (_ /\ { corpusId, limit, listId, tabType }) = do
-- HashedResponse { md5, value: ChartMetrics ms } <- GUC.get session chart --get session chart -- HashedResponse { hash, value: ChartMetrics ms } <- GUC.get session chart --get session chart
-- pure $ HashedResponse { md5, value: ms."data" } -- pure $ HashedResponse { hash, value: ms."data" }
-- where chart = Chart {chartType: ChartPie, limit, listId, tabType} (Just corpusId) -- where chart = Chart {chartType: ChartPie, limit, listId, tabType} (Just corpusId)
getMetricsMD5 :: Session -> Tuple Reload (Record Path) -> Aff String getMetricsHash :: Session -> Tuple Reload (Record Path) -> Aff String
getMetricsMD5 session (_ /\ { corpusId, limit, listId, tabType }) = do getMetricsHash session (_ /\ { corpusId, limit, listId, tabType }) = do
get session $ ChartMD5 { chartType: ChartPie, listId, tabType } (Just corpusId) get session $ ChartHash { chartType: ChartPie, listId, tabType } (Just corpusId)
chartUrl :: Record Path -> SessionRoute chartUrl :: Record Path -> SessionRoute
chartUrl { corpusId, limit, listId, tabType } = Chart {chartType: ChartPie, limit, listId, tabType} (Just corpusId) chartUrl { corpusId, limit, listId, tabType } = Chart {chartType: ChartPie, limit, listId, tabType} (Just corpusId)
...@@ -110,7 +108,7 @@ pieCpt = R.hooksComponent "G.C.N.C.C.P.pie" cpt ...@@ -110,7 +108,7 @@ pieCpt = R.hooksComponent "G.C.N.C.C.P.pie" cpt
cpt { path, session } _ = do cpt { path, session } _ = do
reload <- R.useState' 0 reload <- R.useState' 0
pure $ metricsWithCacheLoadView { pure $ metricsWithCacheLoadView {
getMetricsMD5 getMetricsHash
, handleResponse , handleResponse
, loaded: loadedPie , loaded: loadedPie
, mkRequest: mkRequest session , mkRequest: mkRequest session
...@@ -138,7 +136,7 @@ barCpt = R.hooksComponent "LoadedMetricsBar" cpt ...@@ -138,7 +136,7 @@ barCpt = R.hooksComponent "LoadedMetricsBar" cpt
reload <- R.useState' 0 reload <- R.useState' 0
--pure $ metricsLoadView {getMetrics, loaded: loadedBar, path, reload, session} --pure $ metricsLoadView {getMetrics, loaded: loadedBar, path, reload, session}
pure $ metricsWithCacheLoadView { pure $ metricsWithCacheLoadView {
getMetricsMD5 getMetricsHash
, handleResponse , handleResponse
, loaded: loadedPie , loaded: loadedPie
, mkRequest: mkRequest session , mkRequest: mkRequest session
......
...@@ -55,14 +55,14 @@ scatterOptions nodes = Options ...@@ -55,14 +55,14 @@ scatterOptions nodes = Options
-- getMetrics :: Session -> Tuple Reload (Record Path) -> Aff (HashedResponse Loaded) -- getMetrics :: Session -> Tuple Reload (Record Path) -> Aff (HashedResponse Loaded)
-- getMetrics session (_ /\ { corpusId, limit, listId, tabType }) = do -- getMetrics session (_ /\ { corpusId, limit, listId, tabType }) = do
-- HashedResponse { md5, value: Metrics ms } <- GUC.get session chart -- HashedResponse { hash, value: Metrics ms } <- GUC.get session chart
-- pure $ HashedResponse { md5, value: ms."data" } -- pure $ HashedResponse { hash, value: ms."data" }
-- where -- where
-- chart = Chart {chartType : ChartTree, limit, listId, tabType} (Just corpusId) -- chart = Chart {chartType : ChartTree, limit, listId, tabType} (Just corpusId)
getMetricsMD5 :: Session -> Tuple Reload (Record Path) -> Aff String getMetricsHash :: Session -> Tuple Reload (Record Path) -> Aff String
getMetricsMD5 session (_ /\ { corpusId, limit, listId, tabType }) = do getMetricsHash session (_ /\ { corpusId, limit, listId, tabType }) = do
get session $ ChartMD5 { chartType: ChartTree, listId, tabType } (Just corpusId) get session $ ChartHash { chartType: ChartTree, listId, tabType } (Just corpusId)
chartUrl :: Record Path -> SessionRoute chartUrl :: Record Path -> SessionRoute
chartUrl { corpusId, limit, listId, tabType } = Chart {chartType: ChartTree, limit, listId, tabType} (Just corpusId) chartUrl { corpusId, limit, listId, tabType } = Chart {chartType: ChartTree, limit, listId, tabType} (Just corpusId)
...@@ -82,7 +82,7 @@ treeCpt = R.hooksComponent "G.C.N.C.C.T.tree" cpt ...@@ -82,7 +82,7 @@ treeCpt = R.hooksComponent "G.C.N.C.C.T.tree" cpt
cpt {path, session} _ = do cpt {path, session} _ = do
reload <- R.useState' 0 reload <- R.useState' 0
pure $ metricsWithCacheLoadView { pure $ metricsWithCacheLoadView {
getMetricsMD5 getMetricsHash
, handleResponse , handleResponse
, loaded , loaded
, mkRequest: mkRequest session , mkRequest: mkRequest session
......
...@@ -16,7 +16,6 @@ import Gargantext.Components.Nodes.Corpus.Chart.Histo (histo) ...@@ -16,7 +16,6 @@ import Gargantext.Components.Nodes.Corpus.Chart.Histo (histo)
import Gargantext.Components.Nodes.Corpus.Types (CorpusData, Hyperdata(..), getCorpusInfo, CorpusInfo(..)) import Gargantext.Components.Nodes.Corpus.Types (CorpusData, Hyperdata(..), getCorpusInfo, CorpusInfo(..))
import Gargantext.Components.Tab as Tab import Gargantext.Components.Tab as Tab
import Gargantext.Components.Table as Table import Gargantext.Components.Table as Table
import Gargantext.Hooks.Loader (useLoaderWithCache)
import Gargantext.Ends (Frontends) import Gargantext.Ends (Frontends)
import Gargantext.Sessions (Session) import Gargantext.Sessions (Session)
import Gargantext.Types (CTabNgramType(..), TabSubType(..), TabType(..)) import Gargantext.Types (CTabNgramType(..), TabSubType(..), TabType(..))
......
...@@ -192,9 +192,9 @@ sessionPath (R.CorpusMetrics { listId, limit, tabType} i) = ...@@ -192,9 +192,9 @@ sessionPath (R.CorpusMetrics { listId, limit, tabType} i) =
<> "?ngrams=" <> show listId <> "?ngrams=" <> show listId
<> "&ngramsType=" <> showTabType' tabType <> "&ngramsType=" <> showTabType' tabType
<> maybe "" limitUrl limit <> maybe "" limitUrl limit
sessionPath (R.CorpusMetricsMD5 { listId, tabType} i) = sessionPath (R.CorpusMetricsHash { listId, tabType} i) =
sessionPath $ R.NodeAPI Corpus i sessionPath $ R.NodeAPI Corpus i
$ "metrics/md5" $ "metrics/hash"
<> "?ngrams=" <> show listId <> "?ngrams=" <> show listId
<> "&ngramsType=" <> showTabType' tabType <> "&ngramsType=" <> showTabType' tabType
-- TODO fix this url path -- TODO fix this url path
...@@ -209,10 +209,10 @@ sessionPath (R.Chart {chartType, listId, limit, tabType} i) = ...@@ -209,10 +209,10 @@ sessionPath (R.Chart {chartType, listId, limit, tabType} i) =
Just li -> "&limit=" <> show li Just li -> "&limit=" <> show li
Nothing -> "" Nothing -> ""
-- <> maybe "" limitUrl limit -- <> maybe "" limitUrl limit
sessionPath (R.ChartMD5 { chartType, listId, tabType } i) = sessionPath (R.ChartHash { chartType, listId, tabType } i) =
sessionPath $ R.NodeAPI Corpus i sessionPath $ R.NodeAPI Corpus i
$ show chartType $ show chartType
<> "/md5?ngramsType=" <> showTabType' tabType <> "/hash?ngramsType=" <> showTabType' tabType
<> "&listType=" <> show MapTerm-- <> show listId <> "&listType=" <> show MapTerm-- <> show listId
<> defaultListAdd listId <> defaultListAdd listId
-- sessionPath (R.NodeAPI (NodeContact s a i) i) = sessionPath $ "annuaire/" <> show a <> "/contact/" <> show i -- sessionPath (R.NodeAPI (NodeContact s a i) i) = sessionPath $ "annuaire/" <> show a <> "/contact/" <> show i
......
module Gargantext.Hooks.Loader where module Gargantext.Hooks.Loader where
import DOM.Simple.Console (log2)
import Data.Argonaut (class DecodeJson, class EncodeJson, decodeJson, encodeJson, (.:), (:=), (~>), jsonEmptyObject) import Data.Argonaut (class DecodeJson, class EncodeJson, decodeJson, encodeJson, (.:), (:=), (~>), jsonEmptyObject)
import Data.Argonaut.Core (stringify) import Data.Argonaut.Core (stringify)
import Data.Argonaut.Parser (jsonParser) import Data.Argonaut.Parser (jsonParser)
...@@ -7,21 +8,18 @@ import Data.Either (Either(..)) ...@@ -7,21 +8,18 @@ import Data.Either (Either(..))
import Data.Maybe (Maybe(..), isJust, maybe) import Data.Maybe (Maybe(..), isJust, maybe)
import Data.Tuple (fst) import Data.Tuple (fst)
import Data.Tuple.Nested ((/\)) import Data.Tuple.Nested ((/\))
import DOM.Simple.Console (log2)
import Effect.Aff (Aff, launchAff_, throwError) import Effect.Aff (Aff, launchAff_, throwError)
import Effect.Class (liftEffect) import Effect.Class (liftEffect)
import Effect.Exception (error) import Effect.Exception (error)
import Milkis as M
import Reactix as R
import Web.Storage.Storage as WSS
import Gargantext.Prelude
import Gargantext.Components.LoadingSpinner (loadingSpinner) import Gargantext.Components.LoadingSpinner (loadingSpinner)
import Gargantext.Ends (class ToUrl, toUrl) import Gargantext.Ends (class ToUrl, toUrl)
import Gargantext.Prelude
import Gargantext.Utils as GU import Gargantext.Utils as GU
import Gargantext.Utils.CacheAPI as GUC import Gargantext.Utils.CacheAPI as GUC
import Gargantext.Utils.Reactix as R2 import Gargantext.Utils.Reactix as R2
import Milkis as M
import Reactix as R
import Web.Storage.Storage as WSS
useLoader :: forall path st. Eq path useLoader :: forall path st. Eq path
...@@ -53,24 +51,24 @@ useLoaderEffect path state@(state' /\ setState) loader = do ...@@ -53,24 +51,24 @@ useLoaderEffect path state@(state' /\ setState) loader = do
liftEffect $ setState $ const $ Just l liftEffect $ setState $ const $ Just l
type MD5 = String type Hash = String
newtype HashedResponse a = HashedResponse { newtype HashedResponse a = HashedResponse {
md5 :: MD5 hash :: Hash
, value :: a , value :: a
} }
instance decodeHashedResponse :: DecodeJson a => DecodeJson (HashedResponse a) where instance decodeHashedResponse :: DecodeJson a => DecodeJson (HashedResponse a) where
decodeJson json = do decodeJson json = do
obj <- decodeJson json obj <- decodeJson json
md5 <- obj .: "md5" hash <- obj .: "hash"
value <- obj .: "value" value <- obj .: "value"
pure $ HashedResponse { md5, value } pure $ HashedResponse { hash, value }
instance encodeHashedResponse :: EncodeJson a => EncodeJson (HashedResponse a) where instance encodeHashedResponse :: EncodeJson a => EncodeJson (HashedResponse a) where
encodeJson (HashedResponse { md5, value }) = do encodeJson (HashedResponse { hash, value }) = do
"md5" := encodeJson md5 "hash" := encodeJson hash
~> "value" := encodeJson value ~> "value" := encodeJson value
~> jsonEmptyObject ~> jsonEmptyObject
...@@ -81,9 +79,9 @@ useLoaderWithCache :: forall path st. Eq path => DecodeJson st => EncodeJson st ...@@ -81,9 +79,9 @@ useLoaderWithCache :: forall path st. Eq path => DecodeJson st => EncodeJson st
-> (path -> Aff (HashedResponse st)) -> (path -> Aff (HashedResponse st))
-> (st -> R.Element) -> (st -> R.Element)
-> R.Hooks R.Element -> R.Hooks R.Element
useLoaderWithCache path keyFunc md5Endpoint loader render = do useLoaderWithCache path keyFunc hashEndpoint loader render = do
state <- R.useState' Nothing state <- R.useState' Nothing
useCachedLoaderEffect { cacheEndpoint: md5Endpoint useCachedLoaderEffect { cacheEndpoint: hashEndpoint
, keyFunc , keyFunc
, loadRealData: loadRealData state , loadRealData: loadRealData state
, path , path
...@@ -93,11 +91,11 @@ useLoaderWithCache path keyFunc md5Endpoint loader render = do ...@@ -93,11 +91,11 @@ useLoaderWithCache path keyFunc md5Endpoint loader render = do
loadRealData :: R.State (Maybe st) -> String -> String -> WSS.Storage -> Aff Unit loadRealData :: R.State (Maybe st) -> String -> String -> WSS.Storage -> Aff Unit
loadRealData (_ /\ setState) key keyCache localStorage = do loadRealData (_ /\ setState) key keyCache localStorage = do
--R2.affEffect "G.H.Loader.useCachedLoaderEffect" $ do --R2.affEffect "G.H.Loader.useCachedLoaderEffect" $ do
HashedResponse { md5, value: l } <- loader path HashedResponse { hash, value: l } <- loader path
liftEffect $ do liftEffect $ do
let value = stringify $ encodeJson l let value = stringify $ encodeJson l
WSS.setItem key value localStorage WSS.setItem key value localStorage
WSS.setItem keyCache md5 localStorage WSS.setItem keyCache hash localStorage
setState $ const $ Just l setState $ const $ Just l
pure unit pure unit
...@@ -159,7 +157,7 @@ useCachedLoaderEffect { cacheEndpoint, keyFunc, loadRealData, path, state: state ...@@ -159,7 +157,7 @@ useCachedLoaderEffect { cacheEndpoint, keyFunc, loadRealData, path, state: state
type LoaderWithCacheAPIProps path res ret = ( type LoaderWithCacheAPIProps path res ret = (
cacheEndpoint :: path -> Aff MD5 cacheEndpoint :: path -> Aff Hash
, handleResponse :: HashedResponse res -> ret , handleResponse :: HashedResponse res -> ret
, mkRequest :: path -> GUC.Request , mkRequest :: path -> GUC.Request
, path :: path , path :: path
...@@ -180,7 +178,7 @@ useLoaderWithCacheAPI { cacheEndpoint, handleResponse, mkRequest, path, renderer ...@@ -180,7 +178,7 @@ useLoaderWithCacheAPI { cacheEndpoint, handleResponse, mkRequest, path, renderer
pure $ maybe (loadingSpinner {}) renderer (fst state) pure $ maybe (loadingSpinner {}) renderer (fst state)
type LoaderWithCacheAPIEffectProps path res ret = ( type LoaderWithCacheAPIEffectProps path res ret = (
cacheEndpoint :: path -> Aff MD5 cacheEndpoint :: path -> Aff Hash
, handleResponse :: HashedResponse res -> ret , handleResponse :: HashedResponse res -> ret
, mkRequest :: path -> GUC.Request , mkRequest :: path -> GUC.Request
, path :: path , path :: path
...@@ -205,19 +203,19 @@ useCachedAPILoaderEffect { cacheEndpoint ...@@ -205,19 +203,19 @@ useCachedAPILoaderEffect { cacheEndpoint
let cacheName = "cache-api-loader" let cacheName = "cache-api-loader"
let req = mkRequest path let req = mkRequest path
let keyCache = "cached-api-md5-" <> (show path) let keyCache = "cached-api-hash-" <> (show path)
-- log2 "[useCachedLoader] mState" mState -- log2 "[useCachedLoader] mState" mState
launchAff_ $ do launchAff_ $ do
cache <- GUC.openCache $ GUC.CacheName cacheName cache <- GUC.openCache $ GUC.CacheName cacheName
-- TODO Parallelize? -- TODO Parallelize?
hr@(HashedResponse { md5, value }) <- GUC.cachedJson cache req hr@(HashedResponse { hash, value }) <- GUC.cachedJson cache req
cacheReal <- cacheEndpoint path cacheReal <- cacheEndpoint path
val <- if md5 == cacheReal then val <- if hash == cacheReal then
pure hr pure hr
else do else do
_ <- GUC.delete cache req _ <- GUC.delete cache req
hr@(HashedResponse { md5, value }) <- GUC.cachedJson cache req hr@(HashedResponse { hash, value }) <- GUC.cachedJson cache req
if md5 == cacheReal then if hash == cacheReal then
pure hr pure hr
else else
throwError $ error $ "Fetched clean cache but hashes don't match" throwError $ error $ "Fetched clean cache but hashes don't match"
......
...@@ -48,9 +48,9 @@ data SessionRoute ...@@ -48,9 +48,9 @@ data SessionRoute
| ListDocument (Maybe ListId) (Maybe Id) | ListDocument (Maybe ListId) (Maybe Id)
| Search SearchOpts (Maybe Id) | Search SearchOpts (Maybe Id)
| CorpusMetrics CorpusMetricOpts (Maybe Id) | CorpusMetrics CorpusMetricOpts (Maybe Id)
| CorpusMetricsMD5 { listId :: ListId, tabType :: TabType } (Maybe Id) | CorpusMetricsHash { listId :: ListId, tabType :: TabType } (Maybe Id)
| Chart ChartOpts (Maybe Id) | Chart ChartOpts (Maybe Id)
| ChartMD5 { chartType :: ChartType, listId :: ListId, tabType :: TabType } (Maybe Id) | ChartHash { chartType :: ChartType, listId :: ListId, tabType :: TabType } (Maybe Id)
instance showAppRoute :: Show AppRoute where instance showAppRoute :: Show AppRoute where
show Home = "Home" show Home = "Home"
......
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