Count API part of Gargantext.
-}
-{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
-{-# LANGUAGE DataKinds #-}
-{-# LANGUAGE TemplateHaskell #-}
-{-# LANGUAGE TypeOperators #-}
-{-# LANGUAGE DeriveGeneric #-}
-{-# LANGUAGE DeriveAnyClass #-}
+{-# LANGUAGE TemplateHaskell #-}
+{-# LANGUAGE TypeOperators #-}
+{-# LANGUAGE DeriveAnyClass #-}
module Gargantext.API.Count
where
-import Gargantext.Prelude
-
-import Prelude (Bounded, Enum, minBound, maxBound)
-import Data.Eq (Eq())
+import Data.Aeson hiding (Error)
+import Data.Aeson.TH (deriveJSON)
+import Data.Either
+import Data.List (permutations)
+import Data.Swagger
import Data.Text (Text, pack)
-import Servant
import GHC.Generics (Generic)
-import Data.Aeson hiding (Error)
-import Test.QuickCheck.Arbitrary
+import Gargantext.Core.Utils.Prefix (unPrefix, unPrefixSwagger)
+import Gargantext.Prelude
+import Servant
import Test.QuickCheck (elements)
-import Data.List (repeat,permutations)
+import Test.QuickCheck.Arbitrary
+
-----------------------------------------------------------------------
-type CountAPI = Post '[JSON] [Count]
+-- TODO-ACCESS: CanCount
+-- TODO-EVENTS: No events as this is a read only query.
+type CountAPI = Post '[JSON] Counts
-----------------------------------------------------------------------
data Scraper = Pubmed | Hal | IsTex | Isidore
instance Arbitrary Scraper where
arbitrary = elements scrapers
------------------------------------------------------------------------
------------------------------------------------------------------------
+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
instance FromJSON QueryBool
instance ToJSON QueryBool
-
+instance ToSchema QueryBool
+-----------------------------------------------------------------------
data Query = Query { query_query :: QueryBool
, query_name :: Maybe [Scraper]
deriving (Eq, Show, Generic)
instance FromJSON Query
instance ToJSON Query
+
instance Arbitrary Query where
arbitrary = elements [ Query q (Just n)
| q <- queries
, n <- take 10 $ permutations scrapers
]
+instance ToSchema Query where
+ declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "query_")
+
-----------------------------------------------------------------------
+type Code = Integer
+type Error = Text
+type Errors = [Error]
+
-----------------------------------------------------------------------
-data ErrorMessage = ErrorMessage Text
+data Message = Message Code Errors
deriving (Eq, Show, Generic)
-errorMessages :: [ErrorMessage]
-errorMessages = map (\m -> ErrorMessage (pack m)) $ [ "Ill formed query "
- , "API connexion error "
- , "Internal Gargantext Error "
- , "Connexion to Gargantext Error"
- -- , "Token has expired "
- ] <> take 100 ( repeat ("No Error"))
+toMessage :: [(Code, Errors)] -> [Message]
+toMessage = map (\(c,err) -> Message c err)
+
+messages :: [Message]
+messages = toMessage $ [ (400, ["Ill formed query "])
+ , (300, ["API connexion error "])
+ , (300, ["Internal Gargantext Error "])
+ ] <> take 10 ( repeat (200, [""]))
-instance Arbitrary ErrorMessage where
- arbitrary = elements errorMessages
+instance Arbitrary Message where
+ arbitrary = elements messages
-instance FromJSON ErrorMessage
-instance ToJSON ErrorMessage
+instance FromJSON Message
+instance ToJSON Message
+instance ToSchema Message
-----------------------------------------------------------------------
-data Error = Error { error_message :: ErrorMessage
- , error_code :: Int
- }
- deriving (Eq, Show, Generic)
-instance FromJSON Error
-instance ToJSON Error
+data Counts = Counts { results :: [Either Message Count]
+ } deriving (Eq, Show, Generic)
-errorCodes :: [Int]
-errorCodes = [200,300,400,500]
-errors :: [Error]
-errors = [ Error m c | m <- errorMessages
- , c <- errorCodes
- ]
+instance FromJSON Counts
+instance ToJSON Counts
-instance Arbitrary Error where
- arbitrary = elements errors
+instance Arbitrary Counts where
+ 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_errors :: Maybe [Error]
- }
+data Count = Count { count_name :: Scraper
+ , count_count :: Maybe Int
+ }
deriving (Eq, Show, Generic)
-instance FromJSON Count
-instance ToJSON Count
-
-instance Arbitrary Count where
- arbitrary = elements [ Count n (Just c) (Just [e]) | n <- scrapers
- , c <- [100..1000]
- , e <- errors
- ]
+$(deriveJSON (unPrefix "count_") ''Count)
+instance ToSchema Count where
+ declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "count_")
+--instance Arbitrary Count where
+-- arbitrary = Count <$> arbitrary <*> arbitrary <*> arbitrary
-----------------------------------------------------------------------
-count :: Query -> Handler [Count]
+count :: Monad m => Query -> m Counts
count _ = undefined