<commit_before>module Ircbrowse.Model.Events where

import Ircbrowse.Types
import Ircbrowse.Data
import Ircbrowse.Monads

import Data.Char
import Data.String
import Data.Default
import Data.Text (Text)
import Data.Time
import Snap.App
import Sphinx

getEvents :: Maybe String -> Maybe String -> Maybe UTCTime -> Pagination -> Maybe Text
          -> Model c s (Pagination,[Event])
getEvents network channel timestamp pagination q = do
  case q of
    Just q -> do
      result <- io $ search def
        { sPath = "/opt/sphinx/bin/search"
        , sConfig = "sphinx.conf"
        , sQuery = escapeText q
        }
      case result of
        Left err -> do io $ appendFile "/tmp/sphinx-error.log" (err ++"\n")
                       return (pagination { pnResults = 0 , pnTotal = 0 },[])
        Right result -> do
          results <- getEventsByResults (map fst (rResults result))
          return (pagination { pnResults = fromIntegral (rReturned result)
                             , pnTotal = fromIntegral (rTotal result)
                             }
                 ,results)
    Nothing -> do
      case timestamp of
        Nothing -> getPaginatedEvents pagination
        Just t -> getTimestampedEvents t pagination

getEventsByResults :: [Int] -> Model c s [Event]
getEventsByResults eids = do
  query ["SELECT id,timestamp,network,channel,type,nick,text"
        ,"FROM event"
        ,"WHERE id in ("
        ,intercalate ", " (map show eids)
        ,")"]
        ()

getTimestampedEvents t pagination = do
  rowsBefore <- fmap (fromMaybe 0)
                     (single ["SELECT COUNT(*) FROM event"
                             ,"WHERE timestamp at time zone 'utc' < ?"]
                             (Only t))
  getPaginatedEvents pagination { pnPage = rowsBefore `div` pnLimit pagination + 1 }

getPaginatedEvents pagination = do
  count <- single ["SELECT count FROM event_count"] ()
  events <- query ["SELECT id,timestamp,network,channel,type,nick,text FROM event"
                  ,"WHERE id >= ? and id <= ?"
                  ,"ORDER BY timestamp ASC"
                  ,"LIMIT ?"]<commit_msg>Optimize timestamp lookup.<commit_after>module Ircbrowse.Model.Events where

import Ircbrowse.Types
import Ircbrowse.Data
import Ircbrowse.Monads

import Data.Char
import Data.String
import Data.Default
import Data.Text (Text)
import Data.Time
import Snap.App
import Sphinx

getEvents :: Maybe String -> Maybe String -> Maybe UTCTime -> Pagination -> Maybe Text
          -> Model c s (Pagination,[Event])
getEvents network channel timestamp pagination q = do
  case q of
    Just q -> do
      result <- io $ search def
        { sPath = "/opt/sphinx/bin/search"
        , sConfig = "sphinx.conf"
        , sQuery = escapeText q
        }
      case result of
        Left err -> do io $ appendFile "/tmp/sphinx-error.log" (err ++"\n")
                       return (pagination { pnResults = 0 , pnTotal = 0 },[])
        Right result -> do
          results <- getEventsByResults (map fst (rResults result))
          return (pagination { pnResults = fromIntegral (rReturned result)
                             , pnTotal = fromIntegral (rTotal result)
                             }
                 ,results)
    Nothing -> do
      case timestamp of
        Nothing -> getPaginatedEvents pagination
        Just t -> getTimestampedEvents t pagination

getEventsByResults :: [Int] -> Model c s [Event]
getEventsByResults eids = do
  query ["SELECT id,timestamp,network,channel,type,nick,text"
        ,"FROM event"
        ,"WHERE id in ("
        ,intercalate ", " (map show eids)
        ,")"]
        ()

getTimestampedEvents t pagination = do
  rowsBefore <- fmap (fromMaybe 0)
                     (single ["SELECT id FROM event"
                             ,"WHERE timestamp at time zone 'utc' = ?"]
                             (Only t))
  getPaginatedEvents pagination { pnPage = rowsBefore `div` pnLimit pagination + 1 }

getPaginatedEvents pagination = do
  count <- single ["SELECT count FROM event_count"] ()
  events <- query ["SELECT id,timestamp,network,channel,type,nick,text FROM event"
                  ,"WHERE id >= ? and id <= ?"
                  ,"ORDER BY timestamp ASC"
                  ,"LIMIT ?"]
{'fraction_non_alphanumeric': 0.09880028228652082, 'fraction_numerical': 0.0018819101387908728, 'mean_word_length': 2.067821067821068, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 29, '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': '1855793', 'n_tokens_mistral': 1213, 'n_tokens_neox': 1162, 'n_words': 374}