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.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 -----------------------------------------------------------------------
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')")]
73 instance Arbitrary QueryBool where
74 arbitrary = elements queries
76 instance FromJSON QueryBool
77 instance ToJSON QueryBool
79 instance ToSchema QueryBool
80 -----------------------------------------------------------------------
82 data Query = Query { query_query :: QueryBool
83 , query_name :: Maybe [Scraper]
85 deriving (Eq, Show, Generic)
86 instance FromJSON Query
89 instance Arbitrary Query where
90 arbitrary = elements [ Query q (Just n)
92 , n <- take 10 $ permutations scrapers
95 instance ToSchema Query
96 -----------------------------------------------------------------------
101 -----------------------------------------------------------------------
102 data Message = Message Code Errors
103 deriving (Eq, Show, Generic)
105 toMessage :: [(Code, Errors)] -> [Message]
106 toMessage = map (\(c,err) -> Message c err)
108 messages :: [Message]
109 messages = toMessage $ [ (400, ["Ill formed query "])
110 , (300, ["API connexion error "])
111 , (300, ["Internal Gargantext Error "])
112 ] <> take 10 ( repeat (200, [""]))
114 instance Arbitrary Message where
115 arbitrary = elements messages
117 instance FromJSON Message
118 instance ToJSON Message
120 instance ToSchema Message
121 -----------------------------------------------------------------------
122 data Counts = Counts { results :: [Either Message Count]
123 } deriving (Eq, Show, Generic)
126 instance FromJSON Counts
127 instance ToJSON Counts
129 instance Arbitrary Counts where
130 arbitrary = elements [Counts [ Right (Count Pubmed (Just 20 ))
131 , Right (Count IsTex (Just 150))
132 , Right (Count Hal (Just 150))
136 instance ToSchema Counts
139 -----------------------------------------------------------------------
140 data Count = Count { count_name :: Scraper
141 , count_count :: Maybe Int
143 deriving (Eq, Show, Generic)
145 $(deriveJSON (unPrefix "count_") ''Count)
147 instance ToSchema Count
148 --instance Arbitrary Count where
149 -- arbitrary = Count <$> arbitrary <*> arbitrary <*> arbitrary
151 -----------------------------------------------------------------------
152 count :: Query -> Handler Counts