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.List (zip)
21 import Data.Map (Map, toList, fromList)
22 import Data.Swagger (ToSchema, declareNamedSchema, genericDeclareNamedSchema)
23 import Data.Text (Text, concat, pack)
24 import GHC.Generics (Generic)
25 import Gargantext.API.Node.Corpus.New
26 import Gargantext.API.Node.Corpus.New.File (FileType(..))
27 import Gargantext.API.Ngrams
28 import Gargantext.API.Admin.Orchestrator.Types
29 import Gargantext.API.Prelude (GargServer)
30 import Gargantext.Core.Utils.Prefix (unPrefixSwagger)
31 import Gargantext.Database.Action.Flow (FlowCmdM)
32 import Gargantext.Database.Admin.Types.Node
33 import Gargantext.Database.Schema.Ngrams (NgramsType(..), ngramsTypes)
34 import Gargantext.Prelude
35 import Network.HTTP.Media ((//), (/:))
37 import Servant.Job.Async
38 import Servant.Job.Utils (jsonOptions)
39 import Web.FormUrlEncoded (FromForm)
41 ------------------------------------------------------------------------
42 type NgramsList = (Map NgramsType (Versioned NgramsTableMap))
43 ------------------------------------------------------------------------
44 type API = Get '[JSON, HTML] (Headers '[Header "Content-Disposition" Text] NgramsList)
45 -- :<|> ReqBody '[JSON] NgramsList :> Post '[JSON] Bool
48 api :: ListId -> GargServer API
49 api l = get l :<|> postAsync l
52 instance Accept HTML where
53 contentType _ = "text" // "html" /: ("charset", "utf-8")
54 instance ToJSON a => MimeRender HTML a where
57 ------------------------------------------------------------------------
58 get :: RepoCmdM env err m =>
59 ListId -> m (Headers '[Header "Content-Disposition" Text] NgramsList)
63 return $ addHeader (concat [ "attachment; filename=GarganText_NgramsList-"
69 get' :: RepoCmdM env err m
70 => ListId -> m NgramsList
73 <$> mapM (getNgramsTableMap lId) ngramsTypes
75 ------------------------------------------------------------------------
77 post :: FlowCmdM env err m
82 -- TODO check with Version for optim
83 _ <- mapM (\(nt, Versioned _v ns) -> setListNgrams l nt ns) $ toList m
87 ------------------------------------------------------------------------
88 ------------------------------------------------------------------------
89 type PostAPI = Summary "Update List"
93 :> AsyncJobs JobLog '[FormUrlEncoded] WithFile JobLog
95 postAsync :: ListId -> GargServer PostAPI
98 JobFunction (\f log' -> postAsync' lId f (liftBase . log'))
100 postAsync' :: FlowCmdM env err m
105 postAsync' l (WithFile _ m _) logStatus = do
107 logStatus JobLog { _scst_succeeded = Just 0
108 , _scst_failed = Just 0
109 , _scst_remaining = Just 1
110 , _scst_events = Just []
114 pure JobLog { _scst_succeeded = Just 1
115 , _scst_failed = Just 0
116 , _scst_remaining = Just 0
117 , _scst_events = Just []
120 data WithFile = WithFile
121 { _wf_filetype :: !FileType
122 , _wf_data :: !NgramsList
124 } deriving (Eq, Show, Generic)
126 makeLenses ''WithFile
127 instance FromForm WithFile
128 instance FromJSON WithFile where
129 parseJSON = genericParseJSON $ jsonOptions "_wf_"
130 instance ToSchema WithFile where
131 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_wf_")