]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/API/Count.hs
[FIX] Count improving type.
[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 {-# LANGUAGE OverloadedStrings #-}
20
21 module Gargantext.API.Count
22 where
23
24
25 import GHC.Generics (Generic)
26 import Prelude (Bounded, Enum, minBound, maxBound)
27
28 import Data.Aeson hiding (Error)
29 import Data.Aeson.TH (deriveJSON)
30 import Data.Eq (Eq())
31 import Data.Either
32 import Data.List (repeat, permutations)
33 import Data.Swagger
34 import Data.Text (Text, pack)
35
36 import Servant
37 import Test.QuickCheck.Arbitrary
38 import Test.QuickCheck (elements)
39 -- import Control.Applicative ((<*>))
40
41 import Gargantext.Prelude
42 import Gargantext.Utils.Prefix (unPrefix)
43
44 -----------------------------------------------------------------------
45 type CountAPI = Post '[JSON] Counts
46
47 -----------------------------------------------------------------------
48 data Scraper = Pubmed | Hal | IsTex | Isidore
49 deriving (Eq, Show, Generic, Enum, Bounded)
50
51 scrapers :: [Scraper]
52 scrapers = [minBound..maxBound]
53
54 instance FromJSON Scraper
55 instance ToJSON Scraper
56
57 instance Arbitrary Scraper where
58 arbitrary = elements scrapers
59
60 instance ToSchema Scraper
61
62 -----------------------------------------------------------------------
63
64 data QueryBool = QueryBool Text
65 deriving (Eq, Show, Generic)
66
67 queries :: [QueryBool]
68 queries = [QueryBool (pack "(X OR X') AND (Y OR Y') NOT (Z OR Z')")]
69
70 instance Arbitrary QueryBool where
71 arbitrary = elements queries
72
73 instance FromJSON QueryBool
74 instance ToJSON QueryBool
75
76 instance ToSchema QueryBool
77 -----------------------------------------------------------------------
78
79 data Query = Query { query_query :: QueryBool
80 , query_name :: Maybe [Scraper]
81 }
82 deriving (Eq, Show, Generic)
83 instance FromJSON Query
84 instance ToJSON Query
85
86 instance Arbitrary Query where
87 arbitrary = elements [ Query q (Just n)
88 | q <- queries
89 , n <- take 10 $ permutations scrapers
90 ]
91
92 instance ToSchema Query
93 -----------------------------------------------------------------------
94 type Code = Integer
95 type Error = Text
96 type Errors = [Error]
97
98 -----------------------------------------------------------------------
99 data Message = Message Code Errors
100 deriving (Eq, Show, Generic)
101
102 toMessage :: [(Code, Errors)] -> [Message]
103 toMessage = map (\(c,err) -> Message c err)
104
105 messages :: [Message]
106 messages = toMessage $ [ (400, ["Ill formed query "])
107 , (300, ["API connexion error "])
108 , (300, ["Internal Gargantext Error "])
109 ] <> take 10 ( repeat (200, [""]))
110
111 instance Arbitrary Message where
112 arbitrary = elements messages
113
114 instance FromJSON Message
115 instance ToJSON Message
116
117 instance ToSchema Message
118 -----------------------------------------------------------------------
119 data Counts = Counts { results :: [Either Message Count]
120 } deriving (Eq, Show, Generic)
121
122
123 instance FromJSON Counts
124 instance ToJSON Counts
125
126 instance Arbitrary Counts where
127 arbitrary = elements [Counts [ Right (Count Pubmed (Just 20 ))
128 , Right (Count IsTex (Just 150))
129 , Right (Count Hal (Just 150))
130 ]
131 ]
132
133 instance ToSchema Counts
134
135
136 -----------------------------------------------------------------------
137 data Count = Count { count_name :: Scraper
138 , count_count :: Maybe Int
139 }
140 deriving (Eq, Show, Generic)
141
142 $(deriveJSON (unPrefix "count_") ''Count)
143
144 instance ToSchema Count
145 --instance Arbitrary Count where
146 -- arbitrary = Count <$> arbitrary <*> arbitrary <*> arbitrary
147
148 -----------------------------------------------------------------------
149 count :: Query -> Handler Counts
150 count _ = undefined