Commit 56d97d26 authored by Yoelis Acourt's avatar Yoelis Acourt

feat: add backend filters handlers

parent 4327e2a6
Pipeline #7043 failed with stages
in 22 minutes and 15 seconds
...@@ -158,6 +158,7 @@ addToCorpusWithQuery user cid (WithQuery { _wq_query = q ...@@ -158,6 +158,7 @@ addToCorpusWithQuery user cid (WithQuery { _wq_query = q
, _wq_databases = dbs , _wq_databases = dbs
, _wq_datafield = datafield , _wq_datafield = datafield
, _wq_lang = l , _wq_lang = l
, _wq_filters = filters
, _wq_flowListWith = flw , _wq_flowListWith = flw
, _wq_pubmedAPIKey = mPubmedAPIKey , _wq_pubmedAPIKey = mPubmedAPIKey
, .. }) maybeLimit jobHandle = do , .. }) maybeLimit jobHandle = do
...@@ -192,7 +193,7 @@ addToCorpusWithQuery user cid (WithQuery { _wq_query = q ...@@ -192,7 +193,7 @@ addToCorpusWithQuery user cid (WithQuery { _wq_query = q
let db = database2origin dbs let db = database2origin dbs
-- mPubmedAPIKey <- getUserPubmedAPIKey user -- mPubmedAPIKey <- getUserPubmedAPIKey user
-- printDebug "[addToCorpusWithQuery] mPubmedAPIKey" mPubmedAPIKey -- printDebug "[addToCorpusWithQuery] mPubmedAPIKey" mPubmedAPIKey
eTxt <- getDataText db (Multi l) q mPubmedAPIKey mEPOAuthKey maybeLimit eTxt <- getDataText db (Multi l) q filters mPubmedAPIKey mEPOAuthKey maybeLimit
-- printDebug "[G.A.N.C.New] lTxts" lTxts -- printDebug "[G.A.N.C.New] lTxts" lTxts
case eTxt of case eTxt of
......
...@@ -87,6 +87,7 @@ data WithQuery = WithQuery ...@@ -87,6 +87,7 @@ data WithQuery = WithQuery
, _wq_databases :: !Database , _wq_databases :: !Database
, _wq_datafield :: !(Maybe Datafield) , _wq_datafield :: !(Maybe Datafield)
, _wq_lang :: !Lang , _wq_lang :: !Lang
, _wq_filters :: !(Maybe Text)
, _wq_node_id :: !Int , _wq_node_id :: !Int
, _wq_flowListWith :: !FlowSocialListWith , _wq_flowListWith :: !FlowSocialListWith
, _wq_pubmedAPIKey :: !(Maybe Text) , _wq_pubmedAPIKey :: !(Maybe Text)
......
...@@ -38,6 +38,7 @@ import Gargantext.Prelude hiding (get) ...@@ -38,6 +38,7 @@ import Gargantext.Prelude hiding (get)
import Gargantext.Utils.Jobs.Error import Gargantext.Utils.Jobs.Error
import PUBMED.Types qualified as PUBMED import PUBMED.Types qualified as PUBMED
import Servant.Client (ClientError) import Servant.Client (ClientError)
import Debug.Pretty.Simple
data GetCorpusError data GetCorpusError
= -- | We couldn't parse the user input query into something meaningful. = -- | We couldn't parse the user input query into something meaningful.
...@@ -53,12 +54,15 @@ instance ToHumanFriendlyError GetCorpusError where ...@@ -53,12 +54,15 @@ instance ToHumanFriendlyError GetCorpusError where
ExternalAPIError api _ -> ExternalAPIError api _ ->
"There was a network problem while contacting the " <> T.pack (show api) <> " API provider. Please try again later or contact your network administrator." "There was a network problem while contacting the " <> T.pack (show api) <> " API provider. Please try again later or contact your network administrator."
-- | Get External API metadata main function -- | Get External API metadata main function
get :: ExternalAPIs get :: ExternalAPIs
-> Lang -> Lang
-- ^ A user-selected language in which documents needs to be retrieved. -- ^ A user-selected language in which documents needs to be retrieved.
-- If the provider doesn't support the search filtered by language, or if the language -- If the provider doesn't support the search filtered by language, or if the language
-- is not important, the frontend will simply send 'EN' to the backend. -- is not important, the frontend will simply send 'EN' to the backend.
-> Maybe Text
-> Corpus.RawQuery -> Corpus.RawQuery
-> Maybe PUBMED.APIKey -> Maybe PUBMED.APIKey
-> Maybe EPO.AuthKey -> Maybe EPO.AuthKey
...@@ -66,14 +70,14 @@ get :: ExternalAPIs ...@@ -66,14 +70,14 @@ get :: ExternalAPIs
-> Maybe Corpus.Limit -> Maybe Corpus.Limit
-- -> IO [HyperdataDocument] -- -> IO [HyperdataDocument]
-> IO (Either GetCorpusError (Maybe Integer, ConduitT () HyperdataDocument IO ())) -> IO (Either GetCorpusError (Maybe Integer, ConduitT () HyperdataDocument IO ()))
get externalAPI lang q mPubmedAPIKey epoAuthKey epoAPIUrl limit = do get externalAPI lang maybeFilters q mPubmedAPIKey epoAuthKey epoAPIUrl limit = do
-- For PUBMED, HAL, IsTex, Isidore and OpenAlex, we want to send the query as-it. -- For PUBMED, HAL, IsTex, Isidore and OpenAlex, we want to send the query as-it.
-- For Arxiv we parse the query into a structured boolean query we submit over. -- For Arxiv we parse the query into a structured boolean query we submit over.
case externalAPI of case externalAPI of
PubMed -> PubMed ->
first (ExternalAPIError externalAPI) <$> PUBMED.get (fromMaybe "" mPubmedAPIKey) q limit first (ExternalAPIError externalAPI) <$> PUBMED.get (fromMaybe "" mPubmedAPIKey) q limit
OpenAlex -> OpenAlex ->
first (ExternalAPIError externalAPI) <$> OpenAlex.get (fromMaybe "" Nothing {- email -}) q (Just $ toISO639 lang) limit first (ExternalAPIError externalAPI) <$> OpenAlex.get (fromMaybe "" Nothing {- email -}) q maybeFilters limit
Arxiv -> runExceptT $ do Arxiv -> runExceptT $ do
corpusQuery <- ExceptT (pure parse_query) corpusQuery <- ExceptT (pure parse_query)
ExceptT $ fmap Right (Arxiv.get lang corpusQuery limit) ExceptT $ fmap Right (Arxiv.get lang corpusQuery limit)
......
...@@ -23,13 +23,19 @@ import Servant.Client (ClientError) ...@@ -23,13 +23,19 @@ import Servant.Client (ClientError)
get :: Text get :: Text
-> Corpus.RawQuery -> Corpus.RawQuery
-> Maybe ISO639.ISO639_1 -> Maybe Text
-> Maybe Limit -> Maybe Limit
-> IO (Either ClientError (Maybe Integer, ConduitT () HyperdataDocument IO ())) -> IO (Either ClientError (Maybe Integer, ConduitT () HyperdataDocument IO ()))
get _email q lang mLimit = do get _email q maybeFilters mLimit = do
let limit = getLimit $ fromMaybe 1000 mLimit let limit = getLimit $ fromMaybe 1000 mLimit
let mFilter = (\l -> "language:" <> iso639ToText l) <$> lang let maybeJoinedFilters =
eRes <- OA.fetchWorksC Nothing mFilter $ Just $ Corpus.getRawQuery q [ maybeFilters ]
& catMaybes
& intersperse ","
& mconcat
& Just
_ <- trace ("JOINED" <> (show maybeJoinedFilters::Text)) $ return ()
eRes <- OA.fetchWorksC Nothing maybeJoinedFilters $ Just $ Corpus.getRawQuery q
pure $ (\(len, docsC) -> (len, docsC .| takeC limit .| mapC toDoc)) <$> eRes pure $ (\(len, docsC) -> (len, docsC .| takeC limit .| mapC toDoc)) <$> eRes
toDoc :: OA.Work -> HyperdataDocument toDoc :: OA.Work -> HyperdataDocument
......
...@@ -132,15 +132,16 @@ getDataText :: (HasNodeError err) ...@@ -132,15 +132,16 @@ getDataText :: (HasNodeError err)
=> DataOrigin => DataOrigin
-> TermType Lang -> TermType Lang
-> API.RawQuery -> API.RawQuery
-> Maybe Text
-> Maybe PUBMED.APIKey -> Maybe PUBMED.APIKey
-> Maybe EPO.AuthKey -> Maybe EPO.AuthKey
-> Maybe API.Limit -> Maybe API.Limit
-> DBCmd' env err (Either API.GetCorpusError DataText) -> DBCmd' env err (Either API.GetCorpusError DataText)
getDataText (ExternalOrigin api) la q mPubmedAPIKey mAuthKey li = do getDataText (ExternalOrigin api) la q filters mPubmedAPIKey mAuthKey li = do
cfg <- view hasConfig cfg <- view hasConfig
eRes <- liftBase $ API.get api (_tt_lang la) q mPubmedAPIKey mAuthKey (_ac_epo_api_url $ _gc_apis cfg) li eRes <- liftBase $ API.get api (_tt_lang la) filters q mPubmedAPIKey mAuthKey (_ac_epo_api_url $ _gc_apis cfg) li
pure $ DataNew <$> eRes pure $ DataNew <$> eRes
getDataText (InternalOrigin _) la q _ _ _li = do getDataText (InternalOrigin _) la q _ _ _ _li = do
(_masterUserId, _masterRootId, cId) <- getOrMkRootWithCorpus MkCorpusUserMaster (Nothing :: Maybe HyperdataCorpus) (_masterUserId, _masterRootId, cId) <- getOrMkRootWithCorpus MkCorpusUserMaster (Nothing :: Maybe HyperdataCorpus)
ids <- map fst <$> searchDocInDatabase cId (stem (_tt_lang la) GargPorterAlgorithm $ API.getRawQuery q) ids <- map fst <$> searchDocInDatabase cId (stem (_tt_lang la) GargPorterAlgorithm $ API.getRawQuery q)
pure $ Right $ DataOld ids pure $ Right $ DataOld ids
...@@ -152,7 +153,7 @@ getDataText_Debug :: (HasNodeError err) ...@@ -152,7 +153,7 @@ getDataText_Debug :: (HasNodeError err)
-> Maybe API.Limit -> Maybe API.Limit
-> DBCmd' env err () -> DBCmd' env err ()
getDataText_Debug a l q li = do getDataText_Debug a l q li = do
result <- getDataText a l q Nothing Nothing li result <- getDataText a l q Nothing Nothing Nothing li
case result of case result of
Left err -> liftBase $ putText $ show err Left err -> liftBase $ putText $ show err
Right res -> liftBase $ printDataText res Right res -> liftBase $ printDataText res
......
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