<commit_before>{-# LANGUAGE ViewPatterns, TupleSections, RecordWildCards, ScopedTypeVariables, PatternGuards #-}

module Action.Server(actionServer, actionReplay, test) where

import Prelude(); import General.Prelude
import Data.List.Extra
import System.FilePath
import Control.Exception
import Control.DeepSeq
import Data.Tuple.Extra
import qualified Language.Javascript.JQuery as JQuery
import qualified Language.Javascript.Flot as Flot
import Data.Version
import Paths_hoogle
import Data.Maybe
import Control.Monad
import System.IO.Extra
import qualified Data.Set as Set
import qualified Data.Map as Map
import System.Time.Extra
import System.Process.Extra
import System.Info.Extra
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy.Char8 as LBS
import Data.Monoid
import Numeric.Extra

import Output.Tags
import Query hiding (test)
import Input.Type hiding (test)
import General.Util
import General.Web
import General.Store
import General.Template
import Action.Search
import Action.CmdLine


actionServer :: CmdLine -> IO ()
actionServer Server{..} = do
    let pkg = "output" </> head ([database | database /= ""] ++ ["all"])
    putStrLn $ "Server started on port " ++ show port
    h <- if logs == "" then return stdout else openFile logs AppendMode
    hSetBuffering h LineBuffering
    readStoreFile (pkg <.> "hoo") $ \store ->
        server h port $ replyServer (Just logs) store cdn

actionReplay :: CmdLine -> IO ()
actionReplay Replay{..} = withBuffering stdout NoBuffering $ do
    src <- readFile logs
    let qs = [readInput url | _:ip:_:url:_ <- map words $ lines src, ip /= "-"]
    (t,_) <- duration $ readStoreFile "output/all.hoo" $ \store -> do
        let op = replyServer Nothing store ""
        forM_ qs $ \x -> do
            res <- op x
            evaluate $ rnf res
            putChar '.'
    putStrLn $ "\nTook " ++ showDuration t ++ " (" ++ showDuration (t / genericLength qs) ++ ")"

--welcome = memoFile "html/welcome.html" BS.readFile
--index = memoFile "html/index.html" (template . BS.readFile)

replyServer :: Maybe FilePath -> StoreIn -> String -> Input -> IO Output
replyServer logs store cdn = \Input{..} -> case inputURL of
    -- without -fno-state-hack things can get folded under this lambda
    [] -> do
        let grab name = [x | (a,x) <- inputArgs, a == name, x /= ""]
        let qSource = grab "hoogle" ++ filter (/= "set:stackage") (grab "scope")
        let q = mconcat $ map parseQuery qSource
        let results = search store q
        let body = showResults q $ dedupeTake 25 (\i -> i{itemURL="",itemPackage=Nothing, itemModule=Nothing}) results
        case lookup "mode" $ reverse inputArgs of
            Nothing | qSource /= [] -> fmap OutputString $ templateRender templateIndex $ map (second str)
                        [("tag",tagOptions $ grab "scope"),("body",body),("title",unwords qSource ++ " - Hoogle"),("search",unwords $ grab "hoogle")]
                    | otherwise -> fmap OutputString $ templateRender templateHome []
            Just "body" -> OutputString <$> if null qSource then templateRender templateEmpty [] else return $ LBS.pack body
    ["plugin","jquery.js"] -> OutputFile <$> JQuery.file<commit_msg>Delete some comments that got done<commit_after>{-# LANGUAGE ViewPatterns, TupleSections, RecordWildCards, ScopedTypeVariables, PatternGuards #-}

module Action.Server(actionServer, actionReplay, test) where

import Prelude(); import General.Prelude
import Data.List.Extra
import System.FilePath
import Control.Exception
import Control.DeepSeq
import Data.Tuple.Extra
import qualified Language.Javascript.JQuery as JQuery
import qualified Language.Javascript.Flot as Flot
import Data.Version
import Paths_hoogle
import Data.Maybe
import Control.Monad
import System.IO.Extra
import qualified Data.Set as Set
import qualified Data.Map as Map
import System.Time.Extra
import System.Process.Extra
import System.Info.Extra
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy.Char8 as LBS
import Data.Monoid
import Numeric.Extra

import Output.Tags
import Query hiding (test)
import Input.Type hiding (test)
import General.Util
import General.Web
import General.Store
import General.Template
import Action.Search
import Action.CmdLine


actionServer :: CmdLine -> IO ()
actionServer Server{..} = do
    let pkg = "output" </> head ([database | database /= ""] ++ ["all"])
    putStrLn $ "Server started on port " ++ show port
    h <- if logs == "" then return stdout else openFile logs AppendMode
    hSetBuffering h LineBuffering
    readStoreFile (pkg <.> "hoo") $ \store ->
        server h port $ replyServer (Just logs) store cdn

actionReplay :: CmdLine -> IO ()
actionReplay Replay{..} = withBuffering stdout NoBuffering $ do
    src <- readFile logs
    let qs = [readInput url | _:ip:_:url:_ <- map words $ lines src, ip /= "-"]
    (t,_) <- duration $ readStoreFile "output/all.hoo" $ \store -> do
        let op = replyServer Nothing store ""
        forM_ qs $ \x -> do
            res <- op x
            evaluate $ rnf res
            putChar '.'
    putStrLn $ "\nTook " ++ showDuration t ++ " (" ++ showDuration (t / genericLength qs) ++ ")"


replyServer :: Maybe FilePath -> StoreIn -> String -> Input -> IO Output
replyServer logs store cdn = \Input{..} -> case inputURL of
    -- without -fno-state-hack things can get folded under this lambda
    [] -> do
        let grab name = [x | (a,x) <- inputArgs, a == name, x /= ""]
        let qSource = grab "hoogle" ++ filter (/= "set:stackage") (grab "scope")
        let q = mconcat $ map parseQuery qSource
        let results = search store q
        let body = showResults q $ dedupeTake 25 (\i -> i{itemURL="",itemPackage=Nothing, itemModule=Nothing}) results
        case lookup "mode" $ reverse inputArgs of
            Nothing | qSource /= [] -> fmap OutputString $ templateRender templateIndex $ map (second str)
                        [("tag",tagOptions $ grab "scope"),("body",body),("title",unwords qSource ++ " - Hoogle"),("search",unwords $ grab "hoogle")]
                    | otherwise -> fmap OutputString $ templateRender templateHome []
            Just "body" -> OutputString <$> if null qSource then templateRender templateEmpty [] else return $ LBS.pack body
    ["plugin","jquery.js"] -> OutputFile <$> JQuery.file
{'fraction_non_alphanumeric': 0.11654608367411136, 'fraction_numerical': 0.0012582573136206354, 'mean_word_length': 3.259209645010047, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 43, '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': '6678081', 'n_tokens_mistral': 1852, 'n_tokens_neox': 1816, 'n_words': 617}