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
, _wq_databases = dbs
, _wq_datafield = datafield
, _wq_lang = l
, _wq_filters = filters
, _wq_flowListWith = flw
, _wq_pubmedAPIKey = mPubmedAPIKey
, .. }) maybeLimit jobHandle = do
......@@ -192,7 +193,7 @@ addToCorpusWithQuery user cid (WithQuery { _wq_query = q
let db = database2origin dbs
-- mPubmedAPIKey <- getUserPubmedAPIKey user
-- 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
case eTxt of
......
......@@ -87,6 +87,7 @@ data WithQuery = WithQuery
, _wq_databases :: !Database
, _wq_datafield :: !(Maybe Datafield)
, _wq_lang :: !Lang
, _wq_filters :: !(Maybe Text)
, _wq_node_id :: !Int
, _wq_flowListWith :: !FlowSocialListWith
, _wq_pubmedAPIKey :: !(Maybe Text)
......
......@@ -38,6 +38,7 @@ import Gargantext.Prelude hiding (get)
import Gargantext.Utils.Jobs.Error
import PUBMED.Types qualified as PUBMED
import Servant.Client (ClientError)
import Debug.Pretty.Simple
data GetCorpusError
= -- | We couldn't parse the user input query into something meaningful.
......@@ -53,12 +54,15 @@ instance ToHumanFriendlyError GetCorpusError where
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."
-- | Get External API metadata main function
get :: ExternalAPIs
-> Lang
-- ^ 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
-- is not important, the frontend will simply send 'EN' to the backend.
-> Maybe Text
-> Corpus.RawQuery
-> Maybe PUBMED.APIKey
-> Maybe EPO.AuthKey
......@@ -66,14 +70,14 @@ get :: ExternalAPIs
-> Maybe Corpus.Limit
-- -> IO [HyperdataDocument]
-> 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 Arxiv we parse the query into a structured boolean query we submit over.
case externalAPI of
PubMed ->
first (ExternalAPIError externalAPI) <$> PUBMED.get (fromMaybe "" mPubmedAPIKey) q limit
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
corpusQuery <- ExceptT (pure parse_query)
ExceptT $ fmap Right (Arxiv.get lang corpusQuery limit)
......
......@@ -23,13 +23,19 @@ import Servant.Client (ClientError)
get :: Text
-> Corpus.RawQuery
-> Maybe ISO639.ISO639_1
-> Maybe Text
-> Maybe Limit
-> 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 mFilter = (\l -> "language:" <> iso639ToText l) <$> lang
eRes <- OA.fetchWorksC Nothing mFilter $ Just $ Corpus.getRawQuery q
let maybeJoinedFilters =
[ 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
toDoc :: OA.Work -> HyperdataDocument
......
......@@ -132,15 +132,16 @@ getDataText :: (HasNodeError err)
=> DataOrigin
-> TermType Lang
-> API.RawQuery
-> Maybe Text
-> Maybe PUBMED.APIKey
-> Maybe EPO.AuthKey
-> Maybe API.Limit
-> 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
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
getDataText (InternalOrigin _) la q _ _ _li = do
getDataText (InternalOrigin _) la q _ _ _ _li = do
(_masterUserId, _masterRootId, cId) <- getOrMkRootWithCorpus MkCorpusUserMaster (Nothing :: Maybe HyperdataCorpus)
ids <- map fst <$> searchDocInDatabase cId (stem (_tt_lang la) GargPorterAlgorithm $ API.getRawQuery q)
pure $ Right $ DataOld ids
......@@ -152,7 +153,7 @@ getDataText_Debug :: (HasNodeError err)
-> Maybe API.Limit
-> DBCmd' env err ()
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
Left err -> liftBase $ putText $ show err
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