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 #-}
16 {-# LANGUAGE NoImplicitPrelude #-}
17 {-# LANGUAGE DataKinds #-}
18 {-# LANGUAGE TemplateHaskell #-}
19 {-# LANGUAGE TypeOperators #-}
20 {-# LANGUAGE DeriveGeneric #-}
21 {-# LANGUAGE DeriveAnyClass #-}
22 {-# LANGUAGE OverloadedStrings #-}
24 module Gargantext.API.Count
28 import GHC.Generics (Generic)
29 import Prelude (Bounded, Enum, minBound, maxBound)
31 import Data.Aeson hiding (Error)
32 import Data.Aeson.TH (deriveJSON)
35 import Data.List (repeat, permutations)
37 import Data.Text (Text, pack)
40 import Test.QuickCheck.Arbitrary
41 import Test.QuickCheck (elements)
42 -- import Control.Applicative ((<*>))
44 import Gargantext.Prelude
45 import Gargantext.Core.Utils.Prefix (unPrefix)
47 -----------------------------------------------------------------------
48 type CountAPI = Post '[JSON] Counts
50 -----------------------------------------------------------------------
51 data Scraper = Pubmed | Hal | IsTex | Isidore
52 deriving (Eq, Show, Generic, Enum, Bounded)
55 scrapers = [minBound..maxBound]
57 instance FromJSON Scraper
58 instance ToJSON Scraper
60 instance Arbitrary Scraper where
61 arbitrary = elements scrapers
63 instance ToSchema Scraper
65 -----------------------------------------------------------------------
66 data QueryBool = QueryBool Text
67 deriving (Eq, Show, Generic)
69 queries :: [QueryBool]
70 queries = [QueryBool (pack "(X OR X') AND (Y OR Y') NOT (Z OR Z')")]
72 instance Arbitrary QueryBool where
73 arbitrary = elements queries
75 instance FromJSON QueryBool
76 instance ToJSON QueryBool
78 instance ToSchema QueryBool
79 -----------------------------------------------------------------------
81 data Query = Query { query_query :: QueryBool
82 , query_name :: Maybe [Scraper]
84 deriving (Eq, Show, Generic)
85 instance FromJSON Query
88 instance Arbitrary Query where
89 arbitrary = elements [ Query q (Just n)
91 , n <- take 10 $ permutations scrapers
94 instance ToSchema Query
95 -----------------------------------------------------------------------
100 -----------------------------------------------------------------------
101 data Message = Message Code Errors
102 deriving (Eq, Show, Generic)
104 toMessage :: [(Code, Errors)] -> [Message]
105 toMessage = map (\(c,err) -> Message c err)
107 messages :: [Message]
108 messages = toMessage $ [ (400, ["Ill formed query "])
109 , (300, ["API connexion error "])
110 , (300, ["Internal Gargantext Error "])
111 ] <> take 10 ( repeat (200, [""]))
113 instance Arbitrary Message where
114 arbitrary = elements messages
116 instance FromJSON Message
117 instance ToJSON Message
119 instance ToSchema Message
120 -----------------------------------------------------------------------
121 data Counts = Counts { results :: [Either Message Count]
122 } deriving (Eq, Show, Generic)
125 instance FromJSON Counts
126 instance ToJSON Counts
128 instance Arbitrary Counts where
129 arbitrary = elements [Counts [ Right (Count Pubmed (Just 20 ))
130 , Right (Count IsTex (Just 150))
131 , Right (Count Hal (Just 150))
135 instance ToSchema Counts
137 -----------------------------------------------------------------------
138 data Count = Count { count_name :: Scraper
139 , count_count :: Maybe Int
141 deriving (Eq, Show, Generic)
143 $(deriveJSON (unPrefix "count_") ''Count)
145 instance ToSchema Count
146 --instance Arbitrary Count where
147 -- arbitrary = Count <$> arbitrary <*> arbitrary <*> arbitrary
149 -----------------------------------------------------------------------
150 count :: Query -> Handler Counts