2 Module : Gargantext.API.Ngrams.List
3 Description : Get Ngrams (lists)
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
12 {-# LANGUAGE TemplateHaskell #-}
13 {-# LANGUAGE TypeOperators #-}
15 module Gargantext.API.Ngrams.List
18 import Control.Lens hiding (elements)
20 import Data.Map (toList, fromList)
21 import Data.Swagger (ToSchema, declareNamedSchema, genericDeclareNamedSchema)
22 import Data.Text (Text, concat, pack)
23 import GHC.Generics (Generic)
24 import Gargantext.API.Admin.Orchestrator.Types
25 import Gargantext.API.Ngrams (getNgramsTableMap, setListNgrams)
26 import Gargantext.API.Ngrams.Types (RepoCmdM, Versioned(..), NgramsList)
27 import Gargantext.API.Node.Corpus.New.File (FileType(..))
28 import Gargantext.API.Prelude (GargServer)
29 import Gargantext.Core.Utils.Prefix (unPrefixSwagger)
30 import Gargantext.Database.Action.Flow.Types (FlowCmdM)
31 import Gargantext.Database.Admin.Types.Node
32 import Gargantext.Database.Schema.Ngrams (ngramsTypes)
33 import Gargantext.Prelude
34 import Network.HTTP.Media ((//), (/:))
36 import Servant.Job.Async
37 import Servant.Job.Utils (jsonOptions)
38 import Web.FormUrlEncoded (FromForm)
39 ------------------------------------------------------------------------
41 ------------------------------------------------------------------------
42 type API = Get '[JSON, HTML] (Headers '[Header "Content-Disposition" Text] NgramsList)
43 -- :<|> ReqBody '[JSON] NgramsList :> Post '[JSON] Bool
46 api :: ListId -> GargServer API
47 api l = get l :<|> postAsync l
50 instance Accept HTML where
51 contentType _ = "text" // "html" /: ("charset", "utf-8")
52 instance ToJSON a => MimeRender HTML a where
55 ------------------------------------------------------------------------
56 get :: RepoCmdM env err m =>
57 ListId -> m (Headers '[Header "Content-Disposition" Text] NgramsList)
61 return $ addHeader (concat [ "attachment; filename=GarganText_NgramsList-"
67 get' :: RepoCmdM env err m
68 => ListId -> m NgramsList
71 <$> mapM (getNgramsTableMap lId) ngramsTypes
73 ------------------------------------------------------------------------
76 post :: FlowCmdM env err m
81 -- TODO check with Version for optim
82 _ <- mapM (\(nt, Versioned _v ns) -> setListNgrams l nt ns) $ toList m
86 ------------------------------------------------------------------------
87 ------------------------------------------------------------------------
88 type PostAPI = Summary "Update List"
92 :> AsyncJobs JobLog '[FormUrlEncoded] WithFile JobLog
94 postAsync :: ListId -> GargServer PostAPI
97 JobFunction (\f log' -> postAsync' lId f (liftBase . log'))
99 postAsync' :: FlowCmdM env err m
104 postAsync' l (WithFile _ m _) logStatus = do
106 logStatus JobLog { _scst_succeeded = Just 0
107 , _scst_failed = Just 0
108 , _scst_remaining = Just 1
109 , _scst_events = Just []
113 pure JobLog { _scst_succeeded = Just 1
114 , _scst_failed = Just 0
115 , _scst_remaining = Just 0
116 , _scst_events = Just []
119 data WithFile = WithFile
120 { _wf_filetype :: !FileType
121 , _wf_data :: !NgramsList
123 } deriving (Eq, Show, Generic)
125 makeLenses ''WithFile
126 instance FromForm WithFile
127 instance FromJSON WithFile where
128 parseJSON = genericParseJSON $ jsonOptions "_wf_"
129 instance ToSchema WithFile where
130 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_wf_")