]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/API/Job.hs
[openalex] Database type fixed
[gargantext.git] / src / Gargantext / API / Job.hs
1 module Gargantext.API.Job where
2
3 import Control.Lens (over, _Just)
4 import Data.Maybe
5 import qualified Data.Text as T
6
7 import Gargantext.Prelude
8
9 import Gargantext.API.Admin.Orchestrator.Types
10
11 newtype RemainingSteps = RemainingSteps { _RemainingSteps :: Int }
12 deriving (Show, Eq, Num)
13
14 jobLogStart :: RemainingSteps -> JobLog
15 jobLogStart rem =
16 JobLog { _scst_succeeded = Just 0
17 , _scst_remaining = Just (_RemainingSteps rem)
18 , _scst_failed = Just 0
19 , _scst_events = Just [] }
20
21 addEvent :: T.Text -> T.Text -> JobLog -> JobLog
22 addEvent level message (JobLog { _scst_events = mEvts, .. }) = JobLog { _scst_events = Just (evts <> [ newEvt ]), .. }
23 where
24 evts = fromMaybe [] mEvts
25 newEvt = ScraperEvent { _scev_message = Just message
26 , _scev_level = Just level
27 , _scev_date = Nothing }
28
29 addErrorEvent :: T.Text -> JobLog -> JobLog
30 addErrorEvent message = addEvent "ERROR" message
31
32 jobLogProgress :: Int -> JobLog -> JobLog
33 jobLogProgress n jl = over (scst_succeeded . _Just) (+ n) $
34 over (scst_remaining . _Just) (\x -> x - n) jl
35
36 -- | Mark a job as completely done, by adding the 'remaining' into 'succeeded'.
37 -- At the end 'scst_remaining' will be 0, and 'scst_succeeded' will be 'oldvalue + remaining'.
38 jobLogComplete :: JobLog -> JobLog
39 jobLogComplete jl =
40 let remainingNow = fromMaybe 0 (_scst_remaining jl)
41 in jl & over scst_succeeded (Just . maybe remainingNow ((+) remainingNow))
42 & over scst_remaining (const (Just 0))
43
44 jobLogFailures :: Int -> JobLog -> JobLog
45 jobLogFailures n jl = over (scst_failed . _Just) (+ n) $
46 over (scst_remaining . _Just) (\x -> x - n) jl
47
48 jobLogFailTotal :: JobLog -> JobLog
49 jobLogFailTotal (JobLog { _scst_succeeded = mSucc
50 , _scst_remaining = mRem
51 , _scst_failed = mFail
52 , _scst_events = evt }) =
53 JobLog { _scst_succeeded = mSucc
54 , _scst_remaining = newRem
55 , _scst_failed = newFail
56 , _scst_events = evt }
57 where
58 (newRem, newFail) = case mRem of
59 Nothing -> (Nothing, mFail)
60 Just rem -> (Just 0, (+ rem) <$> mFail)
61
62 jobLogFailTotalWithMessage :: T.Text -> JobLog -> JobLog
63 jobLogFailTotalWithMessage message jl = addErrorEvent message $ jobLogFailTotal jl
64
65 jobLogEvt :: JobLog -> ScraperEvent -> JobLog
66 jobLogEvt jl evt = over (scst_events . _Just) (\evts -> (evt:evts)) jl