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 (permutations)
35 -----------------------------------------------------------------------
36 type CountAPI = Post '[JSON] Counts
38 -----------------------------------------------------------------------
39 data Scraper = Pubmed | Hal | IsTex | Isidore
40 deriving (Eq, Show, Generic, Enum, Bounded)
43 scrapers = [minBound..maxBound]
45 instance FromJSON Scraper
46 instance ToJSON Scraper
48 instance Arbitrary Scraper where
49 arbitrary = elements scrapers
51 -----------------------------------------------------------------------
52 -----------------------------------------------------------------------
54 data QueryBool = QueryBool Text
55 deriving (Eq, Show, Generic)
57 queries :: [QueryBool]
58 queries = [QueryBool (pack "(X OR X') AND (Y OR Y') NOT (Z OR Z')")]
60 instance Arbitrary QueryBool where
61 arbitrary = elements queries
63 instance FromJSON QueryBool
64 instance ToJSON QueryBool
68 data Query = Query { query_query :: QueryBool
69 , query_name :: Maybe [Scraper]
71 deriving (Eq, Show, Generic)
72 instance FromJSON Query
74 instance Arbitrary Query where
75 arbitrary = elements [ Query q (Just n)
77 , n <- take 10 $ permutations scrapers
80 -----------------------------------------------------------------------
81 -----------------------------------------------------------------------
86 data Message = Message Code Errors
87 deriving (Eq, Show, Generic)
89 toMessage :: [(Code, Errors)] -> [Message]
90 toMessage = map (\(c,err) -> Message c err)
93 messages = toMessage $ [ (400, ["Ill formed query "])
94 , (300, ["API connexion error "])
95 , (300, ["Internal Gargantext Error "])
96 , (300, ["Connexion to Gargantext Error"])
97 , (300, ["Token has expired "])
98 ] -- <> take 10 ( repeat (200, [""]))
100 instance Arbitrary Message where
101 arbitrary = elements messages
103 instance FromJSON Message
104 instance ToJSON Message
106 -----------------------------------------------------------------------
107 -----------------------------------------------------------------------
108 data Counts = Counts [Count]
109 deriving (Eq, Show, Generic)
111 instance FromJSON Counts
112 instance ToJSON Counts
114 data Count = Count { count_name :: Scraper
115 , count_count :: Maybe Int
116 , count_message :: Maybe Message
118 deriving (Eq, Show, Generic)
120 instance FromJSON Count
121 instance ToJSON Count
123 instance Arbitrary Counts where
124 arbitrary = elements $ select
126 $ map (\xs -> zipWith (\s (c,m) -> Count s c m) scrapers xs)
127 $ chunkAlong (length scrapers) 1 $ (map filter' countOrErrors)
129 select xs = (take 10 xs) <> (take 10 $ drop 100 xs)
130 countOrErrors = [ (c,e) | c <- [500..1000], e <- reverse messages]
131 filter' (c,e) = case e of
132 Message 200 _ -> (Just c , Nothing )
133 message -> (Nothing, Just message)
135 -----------------------------------------------------------------------
136 count :: Query -> Handler Counts