Skip to content

Instantly share code, notes, and snippets.

@sdcb
Last active March 23, 2018 02:22
Show Gist options
  • Select an option

  • Save sdcb/76d44e5fbe1e8bb5e8d49ad9651cd491 to your computer and use it in GitHub Desktop.

Select an option

Save sdcb/76d44e5fbe1e8bb5e8d49ad9651cd491 to your computer and use it in GitHub Desktop.
F# iis log top N
open System
open System.IO
type IISLogItem = {
date :DateTime;
time :DateTime;
``s-ip`` :string;
``cs-method`` :string;
``cs-uri-stem`` :string;
``cs-uri-query`` :string;
``s-port`` :int;
``cs-username`` :string;
``c-ip`` :string;
``cs(User-Agent)`` :string;
``cs(Referer)`` :string;
``sc-status`` :int;
``sc-substatus`` :int;
``sc-win32-status``:int;
``time-taken`` :int;
}
let parseLogItem (log: string) =
' '
|> log.Split
|> fun x ->
{
date = x.[0] |> DateTime.Parse;
time = x.[1] |> DateTime.Parse;
``s-ip`` = x.[2]
``cs-method`` = x.[3]
``cs-uri-stem`` = x.[4]
``cs-uri-query`` = x.[5]
``s-port`` = x.[6] |> int
``cs-username`` = x.[7]
``c-ip`` = x.[8]
``cs(User-Agent)`` = x.[9]
``cs(Referer)`` = x.[10]
``sc-status`` = x.[11] |> int
``sc-substatus`` = x.[12] |> int
``sc-win32-status``= x.[13] |> int
``time-taken`` = x.[14] |> int
}
let readIISLogs file =
file
|> File.ReadLines
|> Seq.filter (fun x -> "#" |> (x.StartsWith >> not))
|> Seq.map parseLogItem
let totalTime v =
v |> Seq.sumBy (fun i -> i.``time-taken``)
let avgTime v =
v |> Seq.averageBy (fun i -> i.``time-taken`` |> double)
let printTopN fn n logs =
logs
|> Seq.groupBy (fun x -> x.``cs-uri-stem``)
|> Seq.sortByDescending (fun (k, v) -> v |> fn)
|> Seq.take n
|> Seq.iter (fun (k, v) -> printfn "(%f) %s" (v |> fn |> double) k)
readIISLogs """C:\Users\flysha.zhou\source\repos\fscon\fscon\u_ex180322.log"""
|> printTopN totalTime 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment