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
24 import Gargantext.Prelude
26 import Prelude (Bounded, Enum, minBound, maxBound)
28 import Data.Text (Text, pack)
30 import GHC.Generics (Generic)
31 import Data.Aeson hiding (Error)
32 import Test.QuickCheck.Arbitrary
33 import Test.QuickCheck (elements)
34 import Data.List (repeat, permutations)
35 -- import Control.Applicative ((<*>))
37 -----------------------------------------------------------------------
38 type CountAPI = Post '[JSON] Counts
40 -----------------------------------------------------------------------
41 data Scraper = Pubmed | Hal | IsTex | Isidore
42 deriving (Eq, Show, Generic, Enum, Bounded)
45 scrapers = [minBound..maxBound]
47 instance FromJSON Scraper
48 instance ToJSON Scraper
50 instance Arbitrary Scraper where
51 arbitrary = elements scrapers
53 -----------------------------------------------------------------------
54 -----------------------------------------------------------------------
56 data QueryBool = QueryBool Text
57 deriving (Eq, Show, Generic)
59 queries :: [QueryBool]
60 queries = [QueryBool (pack "(X OR X') AND (Y OR Y') NOT (Z OR Z')")]
62 instance Arbitrary QueryBool where
63 arbitrary = elements queries
65 instance FromJSON QueryBool
66 instance ToJSON QueryBool
70 data Query = Query { query_query :: QueryBool
71 , query_name :: Maybe [Scraper]
73 deriving (Eq, Show, Generic)
74 instance FromJSON Query
77 instance Arbitrary Query where
78 arbitrary = elements [ Query q (Just n)
80 , n <- take 10 $ permutations scrapers
83 -----------------------------------------------------------------------
84 -----------------------------------------------------------------------
89 data Message = Message Code Errors
90 deriving (Eq, Show, Generic)
92 toMessage :: [(Code, Errors)] -> [Message]
93 toMessage = map (\(c,err) -> Message c err)
96 messages = toMessage $ [ (400, ["Ill formed query "])
97 , (300, ["API connexion error "])
98 , (300, ["Internal Gargantext Error "])
99 , (300, ["Connexion to Gargantext Error"])
100 , (300, ["Token has expired "])
101 ] <> take 10 ( repeat (200, [""]))
103 instance Arbitrary Message where
104 arbitrary = elements messages
106 instance FromJSON Message
107 instance ToJSON Message
109 -----------------------------------------------------------------------
110 -----------------------------------------------------------------------
111 data Counts = Counts [Count]
112 deriving (Eq, Show, Generic)
114 instance FromJSON Counts
115 instance ToJSON Counts
117 data Count = Count { count_name :: Scraper
118 , count_count :: Maybe Int
119 , count_message :: Maybe Message
121 deriving (Eq, Show, Generic)
123 instance FromJSON Count
124 instance ToJSON Count
126 --instance Arbitrary Count where
127 -- arbitrary = Count <$> arbitrary <*> arbitrary <*> arbitrary
130 instance Arbitrary Counts where
131 arbitrary = elements $ select
133 $ map (\xs -> zipWith (\s (c,m) -> Count s c m) scrapers xs)
134 $ chunkAlong (length scrapers) 1 $ (map filter' countOrErrors)
136 select xs = (take 10 xs) <> (take 10 $ drop 100 xs)
137 countOrErrors = [ (c,e) | c <- [500..1000], e <- reverse messages]
138 filter' (c,e) = case e of
139 Message 200 _ -> (Just c , Nothing )
140 message -> (Nothing, Just message)
142 -----------------------------------------------------------------------
143 count :: Query -> Handler Counts