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)
46 -----------------------------------------------------------------------
47 type CountAPI = Post '[JSON] Counts
49 -----------------------------------------------------------------------
50 data Scraper = Pubmed | Hal | IsTex | Isidore
51 deriving (Eq, Show, Generic, Enum, Bounded)
54 scrapers = [minBound..maxBound]
56 instance FromJSON Scraper
57 instance ToJSON Scraper
59 instance Arbitrary Scraper where
60 arbitrary = elements scrapers
62 instance ToSchema Scraper
64 -----------------------------------------------------------------------
65 data QueryBool = QueryBool Text
66 deriving (Eq, Show, Generic)
68 queries :: [QueryBool]
69 queries = [QueryBool (pack "(X OR X') AND (Y OR Y') NOT (Z OR Z')")]
71 instance Arbitrary QueryBool where
72 arbitrary = elements queries
74 instance FromJSON QueryBool
75 instance ToJSON QueryBool
77 instance ToSchema QueryBool
78 -----------------------------------------------------------------------
80 data Query = Query { query_query :: QueryBool
81 , query_name :: Maybe [Scraper]
83 deriving (Eq, Show, Generic)
84 instance FromJSON Query
87 instance Arbitrary Query where
88 arbitrary = elements [ Query q (Just n)
90 , n <- take 10 $ permutations scrapers
93 instance ToSchema Query
94 -----------------------------------------------------------------------
99 -----------------------------------------------------------------------
100 data Message = Message Code Errors
101 deriving (Eq, Show, Generic)
103 toMessage :: [(Code, Errors)] -> [Message]
104 toMessage = map (\(c,err) -> Message c err)
106 messages :: [Message]
107 messages = toMessage $ [ (400, ["Ill formed query "])
108 , (300, ["API connexion error "])
109 , (300, ["Internal Gargantext Error "])
110 ] <> take 10 ( repeat (200, [""]))
112 instance Arbitrary Message where
113 arbitrary = elements messages
115 instance FromJSON Message
116 instance ToJSON Message
118 instance ToSchema Message
119 -----------------------------------------------------------------------
120 data Counts = Counts { results :: [Either Message Count]
121 } deriving (Eq, Show, Generic)
124 instance FromJSON Counts
125 instance ToJSON Counts
127 instance Arbitrary Counts where
128 arbitrary = elements [Counts [ Right (Count Pubmed (Just 20 ))
129 , Right (Count IsTex (Just 150))
130 , Right (Count Hal (Just 150))
134 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