2 Module : Gargantext.API.Count
3 Description : Server API
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 Count API part of Gargantext.
13 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
15 {-# LANGUAGE NoImplicitPrelude #-}
16 {-# LANGUAGE DataKinds #-}
17 {-# LANGUAGE TemplateHaskell #-}
18 {-# LANGUAGE TypeOperators #-}
19 {-# LANGUAGE DeriveGeneric #-}
20 {-# LANGUAGE DeriveAnyClass #-}
21 {-# LANGUAGE OverloadedStrings #-}
23 module Gargantext.API.Count
27 import GHC.Generics (Generic)
28 import Prelude (Bounded, Enum, minBound, maxBound)
30 import Data.Aeson hiding (Error)
31 import Data.Aeson.TH (deriveJSON)
34 import Data.List (repeat, permutations)
36 import Data.Text (Text, pack)
39 import Test.QuickCheck.Arbitrary
40 import Test.QuickCheck (elements)
41 -- import Control.Applicative ((<*>))
43 import Gargantext.Prelude
44 import Gargantext.Core.Utils.Prefix (unPrefix, unPrefixSwagger)
46 -----------------------------------------------------------------------
47 -- TODO-ACCESS: CanCount
48 -- TODO-EVENTS: No events as this is a read only query.
49 type CountAPI = Post '[JSON] Counts
51 -----------------------------------------------------------------------
52 data Scraper = Pubmed | Hal | IsTex | Isidore
53 deriving (Eq, Show, Generic, Enum, Bounded)
56 scrapers = [minBound..maxBound]
58 instance FromJSON Scraper
59 instance ToJSON Scraper
61 instance Arbitrary Scraper where
62 arbitrary = elements scrapers
64 instance ToSchema Scraper
66 -----------------------------------------------------------------------
67 data QueryBool = QueryBool Text
68 deriving (Eq, Show, Generic)
70 queries :: [QueryBool]
71 queries = [QueryBool (pack "(X OR X') AND (Y OR Y') NOT (Z OR Z')")]
72 --queries = [QueryBool (pack "(X + X') * (Y + Y') - (Z + Z')")]
74 instance Arbitrary QueryBool where
75 arbitrary = elements queries
77 instance FromJSON QueryBool
78 instance ToJSON QueryBool
80 instance ToSchema QueryBool
81 -----------------------------------------------------------------------
83 data Query = Query { query_query :: QueryBool
84 , query_name :: Maybe [Scraper]
86 deriving (Eq, Show, Generic)
87 instance FromJSON Query
90 instance Arbitrary Query where
91 arbitrary = elements [ Query q (Just n)
93 , n <- take 10 $ permutations scrapers
96 instance ToSchema Query where
97 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "query_")
99 -----------------------------------------------------------------------
102 type Errors = [Error]
104 -----------------------------------------------------------------------
105 data Message = Message Code Errors
106 deriving (Eq, Show, Generic)
108 toMessage :: [(Code, Errors)] -> [Message]
109 toMessage = map (\(c,err) -> Message c err)
111 messages :: [Message]
112 messages = toMessage $ [ (400, ["Ill formed query "])
113 , (300, ["API connexion error "])
114 , (300, ["Internal Gargantext Error "])
115 ] <> take 10 ( repeat (200, [""]))
117 instance Arbitrary Message where
118 arbitrary = elements messages
120 instance FromJSON Message
121 instance ToJSON Message
123 instance ToSchema Message
124 -----------------------------------------------------------------------
125 data Counts = Counts { results :: [Either Message Count]
126 } deriving (Eq, Show, Generic)
129 instance FromJSON Counts
130 instance ToJSON Counts
132 instance Arbitrary Counts where
133 arbitrary = elements [Counts [ Right (Count Pubmed (Just 20 ))
134 , Right (Count IsTex (Just 150))
135 , Right (Count Hal (Just 150))
139 instance ToSchema Counts
141 -----------------------------------------------------------------------
142 data Count = Count { count_name :: Scraper
143 , count_count :: Maybe Int
145 deriving (Eq, Show, Generic)
147 $(deriveJSON (unPrefix "count_") ''Count)
149 instance ToSchema Count where
150 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "count_")
151 --instance Arbitrary Count where
152 -- arbitrary = Count <$> arbitrary <*> arbitrary <*> arbitrary
154 -----------------------------------------------------------------------
155 count :: Monad m => Query -> m Counts