<commit_before>{-# LANGUAGE OverloadedStrings #-}

module Widgets.Search where

import Import hiding (parseTime)
import qualified Data.Text as T
import Debug.Trace

data FilterClaimStatus = Claimed | Unclaimed | All deriving Eq

data TimeParameters = TimeParameters
    { basedON :: Text
    , criteria :: Text
    , startTime :: Text
    , endTime :: Text
    }

data SearchParameters = SearchParameters
    { claimed :: FilterClaimStatus
    , time :: TimeParameters
    , tags :: [(Text, Text)]
    , sort :: Maybe Text
    }

searchForm :: [Text] -> Form SearchParameters
searchForm tags extra = do
    let claimedList = [("All" :: Text, All),
                    ("Claimed", Claimed),
                    ("Unclaimed", Unclaimed)]

    (claimedRes, claimedView) <- mreq 
        (radioFieldList claimedList)
        "Claimed"
        (Just All)
    (timeRes, timeView) <- mreq timeWidget "Time" Nothing
    (tagsRes, tagsView) <- mreq (tagWidget tags) "Tags" Nothing
    (sortRes, sortView) <- mopt textField "Sort" Nothing

    let searchRes = SearchParameters 
                    <$> claimedRes
                    <*> timeRes
                    <*> tagsRes
                    <*> sortRes

    let widget = do
          [whamlet|
            #{extra}
            <div>
              <p>^{fvInput claimedView}
              <p>time:
              ^{fvInput timeView}
              <p>tags:
              ^{fvInput tagsView}
            <div>
              <p>Sort: ^{fvInput sortView}
          |]

    return (searchRes, widget)

timeWidget :: Field Handler TimeParameters
timeWidget = Field
    { fieldParse = \timeSelectValues _ ->
        trace "timeWidget fieldParse\n"
        (case timeSelectValues of
            [a, b, c, d] ->
                    trace (show a ++ " " ++ show b ++ " " ++ show c ++ " " ++ show d)
                      (return $ Right $ Just $ TimeParameters a b c d)
            _ -> return $ Left "Error with time selection: Missing/Too Much input.")
    , fieldView = \_ _ _ _ _ -> $(widgetFile "time_widget")
    , fieldEnctype = UrlEncoded
    }

tagWidget :: [Text] -> Field Handler [(Text, Text)]
tagWidget tags = Field
    { fieldParse = \tagSelectValues _ ->
          trace "tagWidget fieldParse\n"
            (if (length tagSelectValues > 0) then
                traceShow (zip tags tagSelectValues)
                (return $ Right $ Just $ zip tags tagSelectValues)
             else
                return $ Left "Error with tag selection: No input.")
    , fieldView = \_ _ _ _ _ -> $(widgetFile "tag_widget")
    , fieldEnctype = UrlEncoded
    }

searchFilterString :: SearchParameters -> Text
searchFilterString (SearchParameters Claimed time tags _ ) = 
        T.intercalate " AND " $ filter (not . T.null)
            [(T.pack "CLAIMED"),
             (parseTime time),
             (parseTags tags)
            ]
        -- T.append "CLAIMED AND " $ parseTags tags
searchFilterString (SearchParameters Unclaimed time tags _ ) =
        T.intercalate " AND " $ filter (not . T.null)
            [(T.pack "UNCLAIMED"),
             (parseTime time),
             (parseTags tags)
            ]
        --T.append "UNCLAIMED AND " $ parseTags tags
searchFilterString (SearchParameters All time tags _) =
        T.intercalate " AND " $ filter (not . T.null)
            [(parseTime time),
             (parseTags tags)
            ]
        --T.append T.empty $ parseTags tags

parseTime :: TimeParameters -> Text
parseTime (TimeParameters base crit start end)
        | (base == "Created") || (base == "Last Updated") = T.toUpper $
                if (crit == "between") then
                    T.intercalate " " [base, crit, start, "AND", end]
                else
                    T.intercalate " " [base, crit, start]
        | otherwise = T.empty

parseTags :: [(Text, Text)] -> Text
parseTags t = T.intercalate " AND " $ filter (not . T.null) (map parseTag t)
    where parseTag (tag, response)
              | (tag == T.empty) = T.empty
              | (response == "include") = "(" <> tag <> ")"
              | (response == "exclude") = "(NOT " <> tag <> ")"
              | otherwise = T.empty

searchSortString :: SearchParameters -> Text
searchSortString (SearchParameters _ _ _ sortString) = fromMaybe T.empty sortString
<commit_msg>Uploading unfinished for others to work on while on hiatus<commit_after>{-# LANGUAGE OverloadedStrings #-}

module Widgets.Search where

import Import hiding (parseTime)
import qualified Data.Text as T
import Debug.Trace

data FilterClaimStatus = Claimed | Unclaimed | All deriving Eq

data TimeParameters = TimeParameters
    { basedON :: Text
    , criteria :: Text
    , startTime :: Text
    , endTime :: Text
    }

data SearchParameters = SearchParameters
    { claimed :: FilterClaimStatus
    , time :: TimeParameters
    , tags :: [(Text, Text)]
    , sort :: Maybe Text
    }

searchForm :: [Text] -> Form SearchParameters
searchForm tags extra = do
    let claimedList = [("All" :: Text, All),
                    ("Claimed", Claimed),
                    ("Unclaimed", Unclaimed)]

    (claimedRes, claimedView) <- mreq 
        (radioFieldList claimedList)
        "Claimed"
        (Just All)
    (timeRes, timeView) <- mreq timeWidget "Time" Nothing
    (tagsRes, tagsView) <- mreq (tagWidget tags) "Tags" Nothing
    (sortRes, sortView) <- mopt textField "Sort" Nothing

    let searchRes = SearchParameters 
                    <$> claimedRes
                    <*> timeRes
                    <*> tagsRes
                    <*> sortRes

    let widget = do
          [whamlet|
            #{extra}
            <div>
              <p>^{fvInput claimedView}
              <p>time:
              ^{fvInput timeView}
              <p>tags:
              ^{fvInput tagsView}
            <div>
              <p>Sort: ^{fvInput sortView}
          |]

    return (searchRes, widget)

timeWidget :: Field Handler TimeParameters
timeWidget = Field
    { fieldParse = \timeSelectValues _ ->
        trace "timeWidget fieldParse\n"
        (case timeSelectValues of
            [a, b, c, d] -> (return $ Right $ Just $ TimeParameters a b c d)
            _ -> return $ Left "Error with time selection: Missing/Too Much input.")
    , fieldView = \_ _ _ _ _ -> $(widgetFile "time_widget")
    , fieldEnctype = UrlEncoded
    }

tagWidget :: [Text] -> Field Handler [(Text, Text)]
tagWidget tags = Field
    { fieldParse = \tagSelectValues _ ->
          trace "tagWidget fieldParse\n"
            (if (length tagSelectValues > 0) then
                traceShow (zip tags tagSelectValues)
                (return $ Right $ Just $ zip tags tagSelectValues)
             else
                return $ Left "Error with tag selection: No input.")
    , fieldView = \_ _ _ _ _ -> $(widgetFile "tag_widget")
    , fieldEnctype = UrlEncoded
    }

searchFilterString :: SearchParameters -> Text
searchFilterString (SearchParameters Claimed time tags _ ) = 
        T.intercalate " AND " $ filter (not . T.null)
            [(T.pack "CLAIMED"),
             (parseTime time),
             (parseTags tags)
            ]
        -- T.append "CLAIMED AND " $ parseTags tags
searchFilterString (SearchParameters Unclaimed time tags _ ) =
        T.intercalate " AND " $ filter (not . T.null)
            [(T.pack "UNCLAIMED"),
             (parseTime time),
             (parseTags tags)
            ]
        --T.append "UNCLAIMED AND " $ parseTags tags
searchFilterString (SearchParameters All time tags _) =
        T.intercalate " AND " $ filter (not . T.null)
            [(parseTime time),
             (parseTags tags)
            ]
        --T.append T.empty $ parseTags tags

parseTime :: TimeParameters -> Text
parseTime (TimeParameters base crit start end)
        | (base == "Created") || (base == "Last Updated") = T.toUpper $
                if (crit == "between") then
                    T.intercalate " " [base, crit, start, "AND", end]
                else
                    T.intercalate " " [base, crit, start]
        | otherwise = T.empty

parseTags :: [(Text, Text)] -> Text
parseTags t = T.intercalate " AND " $ filter (not . T.null) (map parseTag t)
    where parseTag (tag, response)
              | (tag == T.empty) = T.empty
              | (response == "include") = "(" <> tag <> ")"
              | (response == "exclude") = "(NOT " <> tag <> ")"
              | otherwise = T.empty

searchSortString :: SearchParameters -> Text
searchSortString (SearchParameters _ _ _ sortString) = fromMaybe T.empty sortString

{'fraction_non_alphanumeric': 0.11375073142188415, 'fraction_numerical': 0.00023405500292568755, 'mean_word_length': 1.9197130167406902, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 57, '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': '4465390', 'n_tokens_mistral': 2426, 'n_tokens_neox': 2322, 'n_words': 692}