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