<commit_before>{-# LANGUAGE OverloadedStrings, ScopedTypeVariables #-}

module Main where

import qualified Text.Blaze.Html5 as H
import qualified Text.Blaze.Html5.Attributes as A

import Control.Exception (tryJust)
import Control.Monad (forM_, guard)
import Control.Monad.Trans (liftIO)
import Data.Text.Lazy (unpack)
import Happstack.Lite (
    dir, nullDir, serve, ServerPart, Response, msum, toResponse, path, ok,
    notFound, method, Method (POST), lookText)
import Happstack.Server (askRq, rqUri, rqMethod, matchMethod)
import System.IO.Error (isDoesNotExistError)
import Text.Blaze.Html5 ((!), toHtml, Html)

import Helpers (
    getAllGroups, getTasksOfGroup, getCmdLine, getGroupsOfTask,
    groupExists, taskExists, readInt, addTaskToGroup)
import Templates(template, groupToLi, taskToLi, alert)


main :: IO ()
main = do
    putStrLn "Running on port 8000"
    serve Nothing myApp


myApp :: ServerPart Response
myApp = msum
  [ dir "groups" $ nullDir >> listGroups
  , dir "groups" $ showGroup
  , dir "tasks"  $ nullDir >> listTasks
  , dir "tasks"  $ showTask
  ]


listGroups :: ServerPart Response
listGroups = do
    groups <- liftIO getAllGroups
    rq <- askRq
    ok $ toResponse $ template (rqUri rq) "Groups" $ do
        H.h1 "Available CGroups"
        H.ul $ forM_ groups groupToLi


processForm :: String -> ServerPart (Maybe Html)
processForm group = do
    rq <- askRq
    if matchMethod POST (rqMethod rq)
    then do
        pid <- return . readInt . unpack =<< lookText "pid"
        maybeContents <- liftIO $ tryJust notExist $ addTaskToGroup pid group
        case maybeContents of
            Left message -> return $ Just $ alert "danger" $ do
                "Something was wrong when adding task "
                H.strong $ toHtml $ show pid
                " to group "
                H.strong $ toHtml group
                ":"
                H.br
                toHtml message
            Right contents -> return $ Just $ alert "success" $ do
                "Task "
                H.strong $ toHtml $ show pid
                " has been added to group "
                H.strong $ toHtml group
                "."
    else return Nothing
    where
        notExist :: IOError -> Maybe String
        notExist exception<commit_msg>a little cleanup<commit_after>{-# LANGUAGE OverloadedStrings, ScopedTypeVariables #-}

module Main where

import qualified Text.Blaze.Html5 as H
import qualified Text.Blaze.Html5.Attributes as A

import Control.Exception (tryJust)
import Control.Monad (forM_, guard)
import Control.Monad.Trans (liftIO)
import Data.Text.Lazy (unpack)
import Happstack.Lite (
    dir, nullDir, serve, ServerPart, Response, msum, toResponse, path, ok,
    notFound, method, Method (POST), lookText)
import Happstack.Server (askRq, rqUri, rqMethod, matchMethod)
import System.IO.Error (isDoesNotExistError)
import Text.Blaze.Html5 ((!), toHtml, Html)

import Helpers (
    getAllGroups, getTasksOfGroup, getCmdLine, getGroupsOfTask,
    groupExists, taskExists, readInt, addTaskToGroup)
import Templates(template, groupToLi, taskToLi, alert)


main :: IO ()
main = do
    putStrLn "Running on port 8000"
    serve Nothing myApp


myApp :: ServerPart Response
myApp = msum
  [ dir "groups" $ nullDir >> listGroups
  , dir "groups" $ showGroup
  , dir "tasks"  $ nullDir >> listTasks
  , dir "tasks"  $ showTask
  ]


listGroups :: ServerPart Response
listGroups = do
    groups <- liftIO getAllGroups
    rq <- askRq
    ok $ toResponse $ template (rqUri rq) "Groups" $ do
        H.h1 "Available CGroups"
        H.ul $ forM_ groups groupToLi


processForm :: String -> ServerPart (Maybe Html)
processForm group = do
    rq <- askRq
    if matchMethod POST $ rqMethod rq
    then do
        pid <- return . readInt . unpack =<< lookText "pid"
        result <- liftIO $ tryJust notExist $ addTaskToGroup pid group
        case result of
            Left message -> return $ Just $ alert "danger" $ do
                "Something was wrong when adding task "
                pidToHtml pid
                " to group "
                groupToHtml group
                ":"
                H.br
                toHtml message
            Right _ -> return $ Just $ alert "success" $ do
                "Task "
                pidToHtml pid
                " has been added to group "
                groupToHtml group
                "."
    else return Nothing
    where
        groupToHtml :: String -> Html
        groupToHtml group = H.strong $ toHtml group
        pidToHtml :: Integer -> Html
        pidToHtml pid = H.strong $ toHtml pid
        notExist :: IOError -> Maybe String
        notExist exception
{'fraction_non_alphanumeric': 0.08502891411437138, 'fraction_numerical': 0.0034268579995716426, 'mean_word_length': 2.3694083694083696, 'pattern_counts': {'":': 2, '<': 17, '<?xml version=': 0, '>': 21, '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': '2900077', 'n_tokens_mistral': 1411, 'n_tokens_neox': 1340, 'n_words': 444}