Count API part of Gargantext.
-}
-{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
-{-# LANGUAGE DataKinds #-}
-{-# LANGUAGE TemplateHaskell #-}
-{-# LANGUAGE TypeOperators #-}
-{-# LANGUAGE DeriveGeneric #-}
-{-# LANGUAGE DeriveAnyClass #-}
-{-# LANGUAGE OverloadedStrings #-}
+
+
+{-# LANGUAGE TemplateHaskell #-}
+{-# LANGUAGE TypeOperators #-}
+{-# LANGUAGE DeriveAnyClass #-}
module Gargantext.API.Count
where
import GHC.Generics (Generic)
-import Prelude (Bounded, Enum, minBound, maxBound)
import Data.Aeson hiding (Error)
import Data.Aeson.TH (deriveJSON)
-import Data.Eq (Eq())
-import Data.List (repeat, permutations)
+import Data.Either
+import Data.List (permutations)
import Data.Swagger
import Data.Text (Text, pack)
-- import Control.Applicative ((<*>))
import Gargantext.Prelude
-import Gargantext.Utils.Prefix (unPrefix)
+import Gargantext.Core.Utils.Prefix (unPrefix, unPrefixSwagger)
-----------------------------------------------------------------------
+-- TODO-ACCESS: CanCount
+-- TODO-EVENTS: No events as this is a read only query.
type CountAPI = Post '[JSON] Counts
-----------------------------------------------------------------------
instance ToSchema Scraper
-----------------------------------------------------------------------
-
data QueryBool = QueryBool Text
deriving (Eq, Show, Generic)
queries :: [QueryBool]
queries = [QueryBool (pack "(X OR X') AND (Y OR Y') NOT (Z OR Z')")]
+--queries = [QueryBool (pack "(X + X') * (Y + Y') - (Z + Z')")]
instance Arbitrary QueryBool where
arbitrary = elements queries
, n <- take 10 $ permutations scrapers
]
-instance ToSchema Query
+instance ToSchema Query where
+ declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "query_")
+
-----------------------------------------------------------------------
type Code = Integer
type Error = Text
messages = toMessage $ [ (400, ["Ill formed query "])
, (300, ["API connexion error "])
, (300, ["Internal Gargantext Error "])
- , (300, ["Connexion to Gargantext Error"])
- , (300, ["Token has expired "])
] <> take 10 ( repeat (200, [""]))
instance Arbitrary Message where
instance ToSchema Message
-----------------------------------------------------------------------
-data Counts = Counts [Count]
- deriving (Eq, Show, Generic)
+data Counts = Counts { results :: [Either Message Count]
+ } deriving (Eq, Show, Generic)
+
instance FromJSON Counts
instance ToJSON Counts
instance Arbitrary Counts where
- arbitrary = elements $ select
- $ map Counts
- $ map (\xs -> zipWith (\s (c,m) -> Count s c m) scrapers xs)
- $ chunkAlong (length scrapers) 1 $ (map filter' countOrErrors)
- where
- select xs = (take 10 xs) <> (take 10 $ drop 100 xs)
- countOrErrors = [ (c,e) | c <- [500..1000], e <- reverse messages]
- filter' (c,e) = case e of
- Message 200 _ -> (Just c , Nothing )
- message -> (Nothing, Just message)
+ arbitrary = elements [Counts [ Right (Count Pubmed (Just 20 ))
+ , Right (Count IsTex (Just 150))
+ , Right (Count Hal (Just 150))
+ ]
+ ]
instance ToSchema Counts
-
-----------------------------------------------------------------------
data Count = Count { count_name :: Scraper
, count_count :: Maybe Int
- , count_message :: Maybe Message
}
deriving (Eq, Show, Generic)
$(deriveJSON (unPrefix "count_") ''Count)
-instance ToSchema Count
+instance ToSchema Count where
+ declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "count_")
--instance Arbitrary Count where
-- arbitrary = Count <$> arbitrary <*> arbitrary <*> arbitrary
-----------------------------------------------------------------------
-count :: Query -> Handler Counts
+count :: Monad m => Query -> m Counts
count _ = undefined