<commit_before>{-# LANGUAGE PatternGuards #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS -fno-warn-unused-do-bind -fno-warn-type-defaults #-}

-- | Browsing and searching the logs.

module Ircbrowse.View.Browse where

import           Ircbrowse.System
import           Ircbrowse.Types.Import
import           Ircbrowse.View hiding (id)
import           Ircbrowse.View.NickColour
import           Ircbrowse.View.Template
import Ircbrowse.View.Calendar

import           Control.Arrow
import           Data.Either
import           Data.Function
import           Data.Text (Text)
import qualified Data.Text as T
import           Network.URI
import           Network.URI.Params
import           Prelude (id)
import           Prelude (min)
import qualified Text.Blaze.Html5.Attributes as A
import           Text.Links

browse channel = browser True ("Browse #" <> T.pack (showChan channel)) channel $ mempty

pdfs channel = browser False ("PDFs linked in #" <> T.pack (showChan channel)) channel $ do
  p $ a ! A.href (toValue ("/pdfs/" ++ showChan channel ++ "/unique")) $ do
    "Show unique PDF links →"

browser :: Bool -> Text -> Channel -> Html -> URI -> Maybe UTCTime -> [Event] -> PN -> Maybe Text -> Html
browser search title channel extra uri timestamp events pn q =
  template "browse" title mempty $ do
    channelNav channel
    containerFluid $ do
      when search $ searchForm q
      extra
      if null events && isJust q
         then noResults
         else paginatedTable channel uri events pn
    footer

browseDay :: Channel -> Text -> Text -> [Event] -> URI -> Html
browseDay channel current date events uri = do
  template "browse" date mempty $ do
    channelNav channel
    containerFluid $ do
      h1 $ toHtml date
      if null events
         then noResults
         else do when (current == "recent")
                      (do p "Newest at the top")
                 eventsTable events uri
    footer
  when (current == "recent") $ script ! src "/js/recent.js" $ mempty

  where mode current label title =
          li !. modeClass $ a ! A.href (toValue ("?mode=" <> label :: Text)) $ title
            where modeClass | current == label = "active"
                            | otherwise = ""

noResults = do
  p "There are no results!"

searchForm :: Maybe Text -> Html
searchForm q =
  form ! method "get" $
    fieldset $ do
      inputAppend $ do
        input ! name "q" !. "span2" !# "appendedInputButton" ! type_ "text" ! value (maybe "" toValue q)
        input !. "btn" ! type_ "submit" ! value "Go!"

paginatedTable :: Channel -> URI -> [Event] -> PN -> Html
paginatedTable channel uri events pn' = do
  let pn = pn' { pnURI = deleteQueryKey "timestamp" (deleteQueryKey "id" uri) }
  pagination pn
  eventsTable events uri
  pagination pn { pnPn = (pnPn pn) { pnShowDesc = False }
                , pnResultsPerPage = Nothing
                }

eventsTable events uri =
  table !. "events table" $
    forM_ events $ \event -> do
      let secs = formatTime defaultTimeLocale "%s" (zonedTimeToUTC (eventTimestamp event))
          anchor = ("t" ++ secs)
          eventClass | Just t <- lookup "timestamp" (uriParams uri),
                       t == secs = "event info"
                     | otherwise = "event"
          focused | eventType event `elem` ["talk","act"] = "focused"
                  | otherwise = "not-focused" :: String
          color = toValue (nickColour (fromMaybe "" (eventNick event)))
          nickLink = maybe ""
                           (\nick -> toValue ("/nick/" <> nick))
                           (eventNick event)
      tr ! name (toValue anchor) !# (toValue anchor) !. (toValue (eventClass ++ " " ++ focused)) $ do
        td  !. "timestamp" $ timestamp uri (eventId event) (eventTimestamp event) anchor secs<commit_msg>Note that search index is updated once daily<commit_after>{-# LANGUAGE PatternGuards #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS -fno-warn-unused-do-bind -fno-warn-type-defaults #-}

-- | Browsing and searching the logs.

module Ircbrowse.View.Browse where

import           Ircbrowse.System
import           Ircbrowse.Types.Import
import           Ircbrowse.View hiding (id)
import           Ircbrowse.View.NickColour
import           Ircbrowse.View.Template
import Ircbrowse.View.Calendar

import           Control.Arrow
import           Data.Either
import           Data.Function
import           Data.Text (Text)
import qualified Data.Text as T
import           Network.URI
import           Network.URI.Params
import           Prelude (id)
import           Prelude (min)
import qualified Text.Blaze.Html5.Attributes as A
import           Text.Links

browse channel = browser True ("Browse #" <> T.pack (showChan channel)) channel $ mempty

pdfs channel = browser False ("PDFs linked in #" <> T.pack (showChan channel)) channel $ do
  p $ a ! A.href (toValue ("/pdfs/" ++ showChan channel ++ "/unique")) $ do
    "Show unique PDF links →"

browser :: Bool -> Text -> Channel -> Html -> URI -> Maybe UTCTime -> [Event] -> PN -> Maybe Text -> Html
browser search title channel extra uri timestamp events pn q =
  template "browse" title mempty $ do
    channelNav channel
    containerFluid $ do
      when search $ searchForm q
      extra
      if null events && isJust q
         then noResults
         else paginatedTable channel uri events pn
    footer

browseDay :: Channel -> Text -> Text -> [Event] -> URI -> Html
browseDay channel current date events uri = do
  template "browse" date mempty $ do
    channelNav channel
    containerFluid $ do
      h1 $ toHtml date
      if null events
         then noResults
         else do when (current == "recent")
                      (do p "Newest at the top")
                 eventsTable events uri
    footer
  when (current == "recent") $ script ! src "/js/recent.js" $ mempty

  where mode current label title =
          li !. modeClass $ a ! A.href (toValue ("?mode=" <> label :: Text)) $ title
            where modeClass | current == label = "active"
                            | otherwise = ""

noResults = do
  p "There are no results!"

searchForm :: Maybe Text -> Html
searchForm q =
  form ! method "get" $
    fieldset $ do
      inputAppend $ do
        input ! name "q" !. "span2" !# "appendedInputButton" ! type_ "text" ! value (maybe "" toValue q) ! placeholder "(the search index is updated once daily)" ! A.style "width:27em"
        input !. "btn" ! type_ "submit" ! value "Go!"


paginatedTable :: Channel -> URI -> [Event] -> PN -> Html
paginatedTable channel uri events pn' = do
  let pn = pn' { pnURI = deleteQueryKey "timestamp" (deleteQueryKey "id" uri) }
  pagination pn
  eventsTable events uri
  pagination pn { pnPn = (pnPn pn) { pnShowDesc = False }
                , pnResultsPerPage = Nothing
                }

eventsTable events uri =
  table !. "events table" $
    forM_ events $ \event -> do
      let secs = formatTime defaultTimeLocale "%s" (zonedTimeToUTC (eventTimestamp event))
          anchor = ("t" ++ secs)
          eventClass | Just t <- lookup "timestamp" (uriParams uri),
                       t == secs = "event info"
                     | otherwise = "event"
          focused | eventType event `elem` ["talk","act"] = "focused"
                  | otherwise = "not-focused" :: String
          color = toValue (nickColour (fromMaybe "" (eventNick event)))
          nickLink = maybe ""
                           (\nick -> toValue ("/nick/" <> nick))
                           (eventNick event)
      tr ! name (toValue anchor) !# (toValue anchor) !. (toValue (eventClass ++ " " ++ focused)) $ do
        td  !. "timestamp" $ timestamp uri (eventId event) (eventTimestamp event) anchor secs
{'fraction_non_alphanumeric': 0.1102123356926188, 'fraction_numerical': 0.0010111223458038423, 'mean_word_length': 2.229795918367347, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 53, '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': '7632165', 'n_tokens_mistral': 2367, 'n_tokens_neox': 2280, 'n_words': 768}