<commit_before>{-# LANGUAGE OverloadedStrings #-}
module Controller.Periodic
  ( periodicHandler
    -- viewPeriodic
  , postPeriodic
  ) where

import qualified Data.ByteString as BS
import Control.Exception (throwTo)
import Control.Monad.IO.Class (liftIO)
import Network.HTTP.Types.Method (methodGet, methodPost)
import qualified Network.HTTP.Types.Method as HTM

import Has
import Model.Periodic
import Service.Types
import Action
import HTTP.Form.Deform
import HTTP.Path.Parser
import Controller.Permission
import Controller.Form
import View.Periodic

periodicHandler :: HTM.Method -> [(BS.ByteString, BS.ByteString)] -> Action
periodicHandler method _
    | method == methodGet = viewPeriodicHandler
    | method == methodPost = postPeriodicHandler
    | otherwise = error "unhandled api/method combo" -- TODO: better error 

{-
viewPeriodic :: ActionRoute ()
viewPeriodic = action GET ("admin" >/> "periodic") $ \() -> withAuth $ do
  checkMemberADMIN
  peeks $ blankForm . htmlPeriodic
-}
viewPeriodicHandler :: Action
viewPeriodicHandler = withAuth $ do
  checkMemberADMIN
  peeks $ blankForm . htmlPeriodic

postPeriodic :: ActionRoute ()
postPeriodic = action POST ("admin" >/> "periodic") $ \() -> postPeriodicHandler

postPeriodicHandler :: Action
postPeriodicHandler = withAuth $ do
  checkMemberADMIN
  t <- peeks servicePeriodic
  w <- runForm (Just htmlPeriodic) $ "weekly" .:> deform
  liftIO $ mapM_ (`throwTo` if w then PeriodWeekly else PeriodDaily) t
  return $ okResponse [] (maybe "no" (const "ok") t :: String)

<commit_msg>Explicit type for post periodic request<commit_after>{-# LANGUAGE OverloadedStrings #-}
module Controller.Periodic
  ( periodicHandler
    -- viewPeriodic
  , postPeriodic
  ) where

import qualified Data.ByteString as BS
import Control.Exception (throwTo)
import Control.Monad.IO.Class (liftIO)
import Network.HTTP.Types.Method (methodGet, methodPost)
import qualified Network.HTTP.Types.Method as HTM

import Has
import Model.Periodic
import Service.Types
import Action
import HTTP.Form.Deform
import HTTP.Path.Parser
import Controller.Permission
import Controller.Form
import View.Periodic

periodicHandler :: HTM.Method -> [(BS.ByteString, BS.ByteString)] -> Action
periodicHandler method _
    | method == methodGet = viewPeriodicHandler
    | method == methodPost = postPeriodicHandler
    | otherwise = error "unhandled api/method combo" -- TODO: better error 

{-
viewPeriodic :: ActionRoute ()
viewPeriodic = action GET ("admin" >/> "periodic") $ \() -> withAuth $ do
  checkMemberADMIN
  peeks $ blankForm . htmlPeriodic
-}
viewPeriodicHandler :: Action
viewPeriodicHandler = withAuth $ do
  checkMemberADMIN
  peeks $ blankForm . htmlPeriodic

postPeriodic :: ActionRoute ()
postPeriodic = action POST ("admin" >/> "periodic") $ \() -> postPeriodicHandler

data RunPeriodicRequest = RunPeriodicRequest Bool

postPeriodicHandler :: Action
postPeriodicHandler = withAuth $ do
  checkMemberADMIN
  t <- peeks servicePeriodic
  RunPeriodicRequest w <- runForm (Just htmlPeriodic) $ (RunPeriodicRequest <$> ("weekly" .:> deform))
  liftIO $ mapM_ (`throwTo` if w then PeriodWeekly else PeriodDaily) t
  return $ okResponse [] (maybe "no" (const "ok") t :: String)


{'fraction_non_alphanumeric': 0.10127368748058403, 'fraction_numerical': 0.0, 'mean_word_length': 4.63922942206655, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '211051', 'n_tokens_mistral': 962, 'n_tokens_neox': 931, 'n_words': 273}