Commit ab44b6b4 authored by Justin Woo's avatar Justin Woo

use ngrams endpoint multiple times instead of list endpoint to fetch ngrams table

parent 0770e5ba
...@@ -22,15 +22,14 @@ import Data.Set as Set ...@@ -22,15 +22,14 @@ import Data.Set as Set
import Data.Symbol (SProxy(..)) import Data.Symbol (SProxy(..))
import Data.Tuple (Tuple(..), snd) import Data.Tuple (Tuple(..), snd)
import Data.Tuple.Nested ((/\)) import Data.Tuple.Nested ((/\))
import Debug.Trace (spy)
import Effect (Effect) import Effect (Effect)
import Gargantext.Components.AutoUpdate (autoUpdateElt) import Gargantext.Components.AutoUpdate (autoUpdateElt)
import Gargantext.Components.Loader (loader) import Gargantext.Components.Loader (loader)
import Gargantext.Components.LoadingSpinner (loadingSpinner) import Gargantext.Components.LoadingSpinner (loadingSpinner)
import Gargantext.Components.NgramsTable.Core (CoreState, NgramsElement(..), NgramsPatch(..), NgramsTable, NgramsTablePatch, NgramsTerm, PageParams, PatchMap(..), Replace, Versioned(..), VersionedNgramsTable, _NgramsElement, _NgramsTable, _PatchMap, _children, _list, _ngrams, _occurrences, _root, addNewNgram, applyNgramsPatches, applyPatchSet, commitPatch, convOrderBy, fromNgramsPatches, initialPageParams, loadNgramsTable, loadNgramsTableAll, ngramsTermText, normNgram, patchSetFromMap, replace, rootsOf, singletonNgramsTablePatch, syncPatches) import Gargantext.Components.NgramsTable.Core (CoreState, NgramsElement(..), NgramsPatch(..), NgramsTable, NgramsTablePatch, NgramsTerm, PageParams, PatchMap(..), Replace, Versioned(..), VersionedNgramsTable, _NgramsElement, _NgramsTable, _PatchMap, _children, _list, _ngrams, _occurrences, _root, addNewNgram, applyNgramsPatches, applyPatchSet, commitPatch, convOrderBy, fromNgramsPatches, initialPageParams, loadNgramsTableAll, ngramsTermText, normNgram, patchSetFromMap, replace, rootsOf, singletonNgramsTablePatch, syncPatches)
import Gargantext.Components.Table as T import Gargantext.Components.Table as T
import Gargantext.Sessions (Session) import Gargantext.Sessions (Session)
import Gargantext.Types (CTabNgramType, Mode(..), OrderBy(..), TabType, TermList(..), readTermList, readTermSize, termLists, termSizes) import Gargantext.Types (CTabNgramType, OrderBy(..), TabType, TermList(..), readTermList, readTermSize, termLists, termSizes)
import Gargantext.Utils (queryMatchesLabel) import Gargantext.Utils (queryMatchesLabel)
import Gargantext.Utils.Reactix as R2 import Gargantext.Utils.Reactix as R2
import Prelude (class Show, Unit, bind, const, discard, identity, map, mempty, not, pure, show, unit, (#), ($), (&&), (+), (/=), (<$>), (<<<), (<>), (=<<), (==), (||), otherwise, when) import Prelude (class Show, Unit, bind, const, discard, identity, map, mempty, not, pure, show, unit, (#), ($), (&&), (+), (/=), (<$>), (<<<), (<>), (=<<), (==), (||), otherwise, when)
...@@ -416,11 +415,9 @@ mainNgramsTableCpt = R.hooksComponent "MainNgramsTable" cpt ...@@ -416,11 +415,9 @@ mainNgramsTableCpt = R.hooksComponent "MainNgramsTable" cpt
path /\ setPath <- R.useState' $ initialPageParams session nodeId [defaultListId] tabType path /\ setPath <- R.useState' $ initialPageParams session nodeId [defaultListId] tabType
let paint versioned = loadedNgramsTable' {tabNgramType, path: path /\ setPath, versioned} let paint versioned = loadedNgramsTable' {tabNgramType, path: path /\ setPath, versioned}
-- TODO: get rid of ngramsTble loading
-- pure $ loader path loadNgramsTable paint
pure $ loader path loadNgramsTableAll \loaded -> do pure $ loader path loadNgramsTableAll \loaded -> do
case Map.lookup Sources loaded of case Map.lookup tabType loaded of
Just versioned -> paint versioned Just (versioned :: VersionedNgramsTable) -> paint versioned
Nothing -> loadingSpinner {} Nothing -> loadingSpinner {}
type NgramsDepth = {ngrams :: NgramsTerm, depth :: Int} type NgramsDepth = {ngrams :: NgramsTerm, depth :: Int}
......
...@@ -54,10 +54,9 @@ import Prelude ...@@ -54,10 +54,9 @@ import Prelude
import Control.Monad.Cont.Trans (lift) import Control.Monad.Cont.Trans (lift)
import Control.Monad.State (class MonadState, execState) import Control.Monad.State (class MonadState, execState)
import Data.Argonaut (class DecodeJson, class EncodeJson, Json, decodeJson, encodeJson, jsonEmptyObject, (.:), (.:!), (:=), (~>)) import Data.Argonaut (class DecodeJson, class EncodeJson, decodeJson, encodeJson, jsonEmptyObject, (.:), (.:!), (:=), (~>))
import Data.Array (head) import Data.Array (head)
import Data.Array as A import Data.Array as A
import Data.Array as Array
import Data.Bifunctor (lmap) import Data.Bifunctor (lmap)
import Data.Either (Either(..)) import Data.Either (Either(..))
import Data.Foldable (class Foldable, foldMap, foldl, foldr) import Data.Foldable (class Foldable, foldMap, foldl, foldr)
...@@ -73,7 +72,7 @@ import Data.Lens.Record (prop) ...@@ -73,7 +72,7 @@ import Data.Lens.Record (prop)
import Data.List ((:), List(Nil)) import Data.List ((:), List(Nil))
import Data.Map (Map) import Data.Map (Map)
import Data.Map as Map import Data.Map as Map
import Data.Maybe (Maybe(..), fromMaybe, isNothing, maybe) import Data.Maybe (Maybe(..), isNothing, maybe)
import Data.Newtype (class Newtype) import Data.Newtype (class Newtype)
import Data.Set (Set) import Data.Set (Set)
import Data.Set as Set import Data.Set as Set
...@@ -81,15 +80,15 @@ import Data.String as S ...@@ -81,15 +80,15 @@ import Data.String as S
import Data.String.Regex (Regex, regex, replace) as R import Data.String.Regex (Regex, regex, replace) as R
import Data.String.Regex.Flags (global, multiline) as R import Data.String.Regex.Flags (global, multiline) as R
import Data.Symbol (SProxy(..)) import Data.Symbol (SProxy(..))
import Data.Traversable (class Traversable, traverse, traverse_, sequence) import Data.Traversable (class Traversable, for, sequence, traverse, traverse_)
import Data.TraversableWithIndex (class TraversableWithIndex, traverseWithIndex) import Data.TraversableWithIndex (class TraversableWithIndex, traverseWithIndex)
import Data.Tuple (Tuple(..)) import Data.Tuple (Tuple(..))
import Effect.Aff (Aff, error, throwError) import Effect.Aff (Aff)
import Foreign.Object as FO import Foreign.Object as FO
import Gargantext.Components.Table as T import Gargantext.Components.Table as T
import Gargantext.Routes (SessionRoute(..)) import Gargantext.Routes (SessionRoute(..))
import Gargantext.Sessions (Session, get, put, post) import Gargantext.Sessions (Session, get, put, post)
import Gargantext.Types (CTabNgramType(..), Mode, OrderBy(..), ScoreType(..), TabType, TermList(..), TermSize, decodeMode) import Gargantext.Types (CTabNgramType(..), OrderBy(..), ScoreType(..), TabSubType(..), TabType(..), TermList(..), TermSize)
import Gargantext.Utils.KarpRabin (indicesOfAny) import Gargantext.Utils.KarpRabin (indicesOfAny)
import Partial (crashWith) import Partial (crashWith)
import Partial.Unsafe (unsafePartial) import Partial.Unsafe (unsafePartial)
...@@ -667,51 +666,23 @@ loadNgramsTable ...@@ -667,51 +666,23 @@ loadNgramsTable
, termListFilter, termSizeFilter , termListFilter, termSizeFilter
, searchQuery, scoreType } (Just nodeId) , searchQuery, scoreType } (Just nodeId)
decodeLists :: FO.Object (Versioned Json) -> Either String NgramsList type NgramsListByTabType = Map TabType VersionedNgramsTable
decodeLists obj = do
tuples :: Array (Tuple Mode VersionedNgramsTable) <- traverse decodeModeMember $ FO.toUnfoldable obj loadNgramsTableAll :: PageParams -> Aff NgramsListByTabType
pure $ Map.fromFoldable tuples loadNgramsTableAll { nodeId, listIds, session, scoreType } = do
where let
decodeModeMember :: Tuple String (Versioned Json) -> Either String (Tuple Mode VersionedNgramsTable) cTagNgramTypes =
decodeModeMember (Tuple key versioned) = do [ CTabTerms
mode <- decodeMode key , CTabSources
versionedNgramsTable <- decodeVersionedNgramsTable versioned , CTabAuthors
pure $ Tuple mode versionedNgramsTable , CTabInstitutes
]
decodeVersionedNgramsTable :: Versioned Json -> Either String VersionedNgramsTable query tabType = GetNgramsTableAll { tabType, listIds, scoreType } (Just nodeId)
decodeVersionedNgramsTable (Versioned r) = do
dataObj :: FO.Object { size :: Int, list :: TermList, children :: Set NgramsTerm } <- decodeJson r."data" Map.fromFoldable <$> for cTagNgramTypes \cTagNgramType -> do
let let tabType = TabCorpus $ TabNgramType cTagNgramType
(tuples :: Array _) = result :: VersionedNgramsTable <- get session $ query tabType
FO.toUnfoldable dataObj <#> \(Tuple key values) -> pure $ Tuple tabType result
let ngrams = NormNgramsTerm key
in Tuple ngrams $ mkNgramsElement ngrams values
pure $ Versioned r { "data" = NgramsTable $ Map.fromFoldable tuples }
mkNgramsElement :: NgramsTerm -> { size :: Int, list :: TermList, children :: Set NgramsTerm } -> NgramsElement
mkNgramsElement ngrams r = NgramsElement
{ ngrams
, list: r.list
, children: r.children
, parent: Nothing
, root: Nothing
, occurrences: 1 -- TODO: to be fixed by using different route?
}
type NgramsList = (Map Mode VersionedNgramsTable)
loadNgramsTableAll :: PageParams -> Aff NgramsList
loadNgramsTableAll
{ nodeId, listIds, termListFilter, termSizeFilter, session, scoreType
, searchQuery, tabType, params: {offset, limit, orderBy}}
= do
obj <- get session query
case decodeLists obj of
Right x -> pure x
Left e -> throwError $ error $ "Failed to load ngrams list: " <> e
where query = ListsRoute listId
listId = fromMaybe 0 $ Array.head listIds -- listIds should be singleton list in main lists view
convOrderBy :: T.OrderByDirection T.ColumnName -> OrderBy convOrderBy :: T.OrderByDirection T.ColumnName -> OrderBy
convOrderBy (T.ASC (T.ColumnName "Score")) = ScoreAsc convOrderBy (T.ASC (T.ColumnName "Score")) = ScoreAsc
......
...@@ -136,6 +136,12 @@ sessionPath (R.GetNgrams opts i) = ...@@ -136,6 +136,12 @@ sessionPath (R.GetNgrams opts i) =
termSizeFilter MultiTerm = "&minTermSize=2" termSizeFilter MultiTerm = "&minTermSize=2"
search "" = "" search "" = ""
search s = "&search=" <> s search s = "&search=" <> s
sessionPath (R.GetNgramsTableAll opts i) =
sessionPath $ R.NodeAPI Node i
$ "ngrams?ngramsType="
<> showTabType' opts.tabType
<> foldMap (\x -> "&list=" <> show x) opts.listIds
<> limitUrl 100000
sessionPath (R.ListDocument lId dId) = sessionPath (R.ListDocument lId dId) =
sessionPath $ R.NodeAPI NodeList lId ("document/" <> (show $ maybe 0 identity dId)) sessionPath $ R.NodeAPI NodeList lId ("document/" <> (show $ maybe 0 identity dId))
sessionPath (R.ListsRoute lId) = "lists/" <> show lId sessionPath (R.ListsRoute lId) = "lists/" <> show lId
......
module Gargantext.Routes where module Gargantext.Routes where
import Prelude import Prelude
import Data.Maybe (Maybe) import Data.Maybe (Maybe)
import Gargantext.Types (ChartOpts, CorpusMetricOpts, Id, Limit, ListId, NgramsGetOpts, NodeType, Offset, OrderBy, SearchOpts, SessionId, TabType, TermList) import Gargantext.Types (ChartOpts, CorpusMetricOpts, Id, Limit, ListId, NgramsGetOpts, NodeType, Offset, OrderBy, SearchOpts, SessionId, TabType, TermList, NgramsGetTableAllOpts)
data AppRoute data AppRoute
= Home = Home
...@@ -32,6 +33,7 @@ data SessionRoute ...@@ -32,6 +33,7 @@ data SessionRoute
= Tab TabType (Maybe Id) = Tab TabType (Maybe Id)
| Children NodeType Offset Limit (Maybe OrderBy) (Maybe Id) | Children NodeType Offset Limit (Maybe OrderBy) (Maybe Id)
| GetNgrams NgramsGetOpts (Maybe Id) | GetNgrams NgramsGetOpts (Maybe Id)
| GetNgramsTableAll NgramsGetTableAllOpts (Maybe Id)
| PutNgrams TabType (Maybe ListId) (Maybe TermList) (Maybe Id) | PutNgrams TabType (Maybe ListId) (Maybe TermList) (Maybe Id)
-- ^ This name is not good. In particular this URL is used both in PUT and POST. -- ^ This name is not good. In particular this URL is used both in PUT and POST.
| NodeAPI NodeType (Maybe Id) String | NodeAPI NodeType (Maybe Id) String
......
...@@ -299,6 +299,12 @@ type NgramsGetOpts = ...@@ -299,6 +299,12 @@ type NgramsGetOpts =
, searchQuery :: String , searchQuery :: String
} }
type NgramsGetTableAllOpts =
{ tabType :: TabType
, listIds :: Array ListId
, scoreType :: ScoreType
}
type SearchOpts = type SearchOpts =
{ {-id :: Int { {-id :: Int
, query :: Array String , query :: Array String
...@@ -380,6 +386,7 @@ instance decodeJsonApiVersion :: DecodeJson ApiVersion where ...@@ -380,6 +386,7 @@ instance decodeJsonApiVersion :: DecodeJson ApiVersion where
data CTabNgramType = CTabTerms | CTabSources | CTabAuthors | CTabInstitutes data CTabNgramType = CTabTerms | CTabSources | CTabAuthors | CTabInstitutes
derive instance eqCTabNgramType :: Eq CTabNgramType derive instance eqCTabNgramType :: Eq CTabNgramType
derive instance ordCTabNgramType :: Ord CTabNgramType
instance showCTabNgramType :: Show CTabNgramType where instance showCTabNgramType :: Show CTabNgramType where
show CTabTerms = "Terms" show CTabTerms = "Terms"
...@@ -390,6 +397,7 @@ instance showCTabNgramType :: Show CTabNgramType where ...@@ -390,6 +397,7 @@ instance showCTabNgramType :: Show CTabNgramType where
data PTabNgramType = PTabPatents | PTabBooks | PTabCommunication data PTabNgramType = PTabPatents | PTabBooks | PTabCommunication
derive instance eqPTabNgramType :: Eq PTabNgramType derive instance eqPTabNgramType :: Eq PTabNgramType
derive instance ordPTabNgramType :: Ord PTabNgramType
instance showPTabNgramType :: Show PTabNgramType where instance showPTabNgramType :: Show PTabNgramType where
show PTabPatents = "Patents" show PTabPatents = "Patents"
...@@ -399,6 +407,7 @@ instance showPTabNgramType :: Show PTabNgramType where ...@@ -399,6 +407,7 @@ instance showPTabNgramType :: Show PTabNgramType where
data TabSubType a = TabDocs | TabNgramType a | TabTrash | TabMoreLikeFav | TabMoreLikeTrash data TabSubType a = TabDocs | TabNgramType a | TabTrash | TabMoreLikeFav | TabMoreLikeTrash
derive instance eqTabSubType :: Eq a => Eq (TabSubType a) derive instance eqTabSubType :: Eq a => Eq (TabSubType a)
derive instance ordTabSubType :: Ord a => Ord (TabSubType a)
instance showTabSubType :: Show a => Show (TabSubType a) where instance showTabSubType :: Show a => Show (TabSubType a) where
show TabDocs = "Docs" show TabDocs = "Docs"
...@@ -413,6 +422,7 @@ data TabType ...@@ -413,6 +422,7 @@ data TabType
| TabDocument (TabSubType CTabNgramType) | TabDocument (TabSubType CTabNgramType)
derive instance eqTabType :: Eq TabType derive instance eqTabType :: Eq TabType
derive instance ordTabType :: Ord TabType
derive instance genericTabType :: Generic TabType _ derive instance genericTabType :: Generic TabType _
......
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