]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/API/Count.hs
[CLEAN] Code.
[gargantext.git] / src / Gargantext / API / Count.hs
1 {-|
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
8 Portability : POSIX
9
10 Count API part of Gargantext.
11 -}
12
13 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
14 {-# LANGUAGE DataKinds #-}
15 {-# LANGUAGE TemplateHaskell #-}
16 {-# LANGUAGE TypeOperators #-}
17 {-# LANGUAGE DeriveGeneric #-}
18 {-# LANGUAGE DeriveAnyClass #-}
19
20 module Gargantext.API.Count
21 where
22
23 import Gargantext.Prelude
24
25 import Prelude (Bounded, Enum, minBound, maxBound)
26 import Data.Eq (Eq())
27 import Data.Text (Text, pack)
28 import Servant
29 import GHC.Generics (Generic)
30 import Data.Aeson hiding (Error)
31 import Test.QuickCheck.Arbitrary
32 import Test.QuickCheck (elements)
33 import Data.List (repeat,permutations)
34 -----------------------------------------------------------------------
35 type CountAPI = Post '[JSON] [Count]
36
37 -----------------------------------------------------------------------
38 data Scraper = Pubmed | Hal | IsTex | Isidore
39 deriving (Eq, Show, Generic, Enum, Bounded)
40
41 scrapers :: [Scraper]
42 scrapers = [minBound..maxBound]
43
44 instance FromJSON Scraper
45 instance ToJSON Scraper
46
47 instance Arbitrary Scraper where
48 arbitrary = elements scrapers
49
50 -----------------------------------------------------------------------
51 -----------------------------------------------------------------------
52
53 data QueryBool = QueryBool Text
54 deriving (Eq, Show, Generic)
55
56 queries :: [QueryBool]
57 queries = [QueryBool (pack "(X OR X') AND (Y OR Y') NOT (Z OR Z')")]
58
59 instance Arbitrary QueryBool where
60 arbitrary = elements queries
61
62 instance FromJSON QueryBool
63 instance ToJSON QueryBool
64
65
66
67 data Query = Query { query_query :: QueryBool
68 , query_name :: Maybe [Scraper]
69 }
70 deriving (Eq, Show, Generic)
71 instance FromJSON Query
72 instance ToJSON Query
73 instance Arbitrary Query where
74 arbitrary = elements [ Query q (Just n)
75 | q <- queries
76 , n <- take 10 $ permutations scrapers
77 ]
78
79 -----------------------------------------------------------------------
80 -----------------------------------------------------------------------
81 data ErrorMessage = ErrorMessage Text
82 deriving (Eq, Show, Generic)
83
84 errorMessages :: [ErrorMessage]
85 errorMessages = map (\m -> ErrorMessage (pack m)) $ [ "Ill formed query "
86 , "API connexion error "
87 , "Internal Gargantext Error "
88 , "Connexion to Gargantext Error"
89 -- , "Token has expired "
90 ] <> take 100 ( repeat ("No Error"))
91
92 instance Arbitrary ErrorMessage where
93 arbitrary = elements errorMessages
94
95 instance FromJSON ErrorMessage
96 instance ToJSON ErrorMessage
97
98 -----------------------------------------------------------------------
99 data Error = Error { error_message :: ErrorMessage
100 , error_code :: Int
101 }
102 deriving (Eq, Show, Generic)
103 instance FromJSON Error
104 instance ToJSON Error
105
106 errorCodes :: [Int]
107 errorCodes = [200,300,400,500]
108
109 errors :: [Error]
110 errors = [ Error m c | m <- errorMessages
111 , c <- errorCodes
112 ]
113
114 instance Arbitrary Error where
115 arbitrary = elements errors
116
117 -----------------------------------------------------------------------
118 -----------------------------------------------------------------------
119 data Count = Count { count_name :: Scraper
120 , count_count :: Maybe Int
121 , count_errors :: Maybe [Error]
122 }
123 deriving (Eq, Show, Generic)
124
125 instance FromJSON Count
126 instance ToJSON Count
127
128 instance Arbitrary Count where
129 arbitrary = elements [ Count n (Just c) (Just [e]) | n <- scrapers
130 , c <- [100..1000]
131 , e <- errors
132 ]
133
134
135 -----------------------------------------------------------------------
136 count :: Query -> Handler [Count]
137 count _ = undefined