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 #-}
14 {-# LANGUAGE DataKinds #-}
15 {-# LANGUAGE TemplateHaskell #-}
16 {-# LANGUAGE TypeOperators #-}
17 {-# LANGUAGE DeriveGeneric #-}
18 {-# LANGUAGE DeriveAnyClass #-}
19 {-# LANGUAGE OverloadedStrings #-}
21 module Gargantext.API.Count
25 import GHC.Generics (Generic)
26 import Prelude (Bounded, Enum, minBound, maxBound)
28 import Data.Aeson hiding (Error)
29 import Data.Aeson.TH (deriveJSON)
32 import Data.List (repeat, permutations)
34 import Data.Text (Text, pack)
37 import Test.QuickCheck.Arbitrary
38 import Test.QuickCheck (elements)
39 -- import Control.Applicative ((<*>))
41 import Gargantext.Prelude
42 import Gargantext.Utils.Prefix (unPrefix)
44 -----------------------------------------------------------------------
45 type CountAPI = Post '[JSON] Counts
47 -----------------------------------------------------------------------
48 data Scraper = Pubmed | Hal | IsTex | Isidore
49 deriving (Eq, Show, Generic, Enum, Bounded)
52 scrapers = [minBound..maxBound]
54 instance FromJSON Scraper
55 instance ToJSON Scraper
57 instance Arbitrary Scraper where
58 arbitrary = elements scrapers
60 instance ToSchema Scraper
62 -----------------------------------------------------------------------
64 data QueryBool = QueryBool Text
65 deriving (Eq, Show, Generic)
67 queries :: [QueryBool]
68 queries = [QueryBool (pack "(X OR X') AND (Y OR Y') NOT (Z OR Z')")]
70 instance Arbitrary QueryBool where
71 arbitrary = elements queries
73 instance FromJSON QueryBool
74 instance ToJSON QueryBool
76 instance ToSchema QueryBool
77 -----------------------------------------------------------------------
79 data Query = Query { query_query :: QueryBool
80 , query_name :: Maybe [Scraper]
82 deriving (Eq, Show, Generic)
83 instance FromJSON Query
86 instance Arbitrary Query where
87 arbitrary = elements [ Query q (Just n)
89 , n <- take 10 $ permutations scrapers
92 instance ToSchema Query
93 -----------------------------------------------------------------------
98 -----------------------------------------------------------------------
99 data Message = Message Code Errors
100 deriving (Eq, Show, Generic)
102 toMessage :: [(Code, Errors)] -> [Message]
103 toMessage = map (\(c,err) -> Message c err)
105 messages :: [Message]
106 messages = toMessage $ [ (400, ["Ill formed query "])
107 , (300, ["API connexion error "])
108 , (300, ["Internal Gargantext Error "])
109 ] <> take 10 ( repeat (200, [""]))
111 instance Arbitrary Message where
112 arbitrary = elements messages
114 instance FromJSON Message
115 instance ToJSON Message
117 instance ToSchema Message
118 -----------------------------------------------------------------------
119 data Counts = Counts { results :: [Either Message Count]
120 } deriving (Eq, Show, Generic)
123 instance FromJSON Counts
124 instance ToJSON Counts
126 instance Arbitrary Counts where
127 arbitrary = elements [Counts [ Right (Count Pubmed (Just 20 ))
128 , Right (Count IsTex (Just 150))
129 , Right (Count Hal (Just 150))
133 instance ToSchema Counts
136 -----------------------------------------------------------------------
137 data Count = Count { count_name :: Scraper
138 , count_count :: Maybe Int
140 deriving (Eq, Show, Generic)
142 $(deriveJSON (unPrefix "count_") ''Count)
144 instance ToSchema Count
145 --instance Arbitrary Count where
146 -- arbitrary = Count <$> arbitrary <*> arbitrary <*> arbitrary
148 -----------------------------------------------------------------------
149 count :: Query -> Handler Counts