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