Skip to content

Instantly share code, notes, and snippets.

@sporto
Created December 17, 2021 02:08
Show Gist options
  • Save sporto/c716048c9edffae9f61a7956cd865486 to your computer and use it in GitHub Desktop.
Save sporto/c716048c9edffae9f61a7956cd865486 to your computer and use it in GitHub Desktop.
Get stats from cost page usage
#r "nuget: FSharp.Data,4.2.6"
#r "nuget: FSharpPlus,1.2.2"
#r "nuget: FSharp.Stats, 0.4.2"
open FSharp.Data
open FSharpPlus
open System.Web
open System
open FSharp.Stats
let csv =
CsvFile
.Load(__SOURCE_DIRECTORY__ + "/cost.csv")
.Cache()
type Entry = { Date: DateOnly; Start: DateOnly }
let extract_start query =
let uri = System.Uri($"http://foo.com{query}")
let kvps = HttpUtility.ParseQueryString uri.Query
let idStr = kvps.["tcChartStart"]
idStr
let parse_date string =
try
string |> DateOnly.Parse |> Ok
with
| _ -> Error("Unable to parse")
let parse_entry (row: CsvRow) =
let maybe_date = row.GetColumn("Date") |> parse_date
let maybe_start =
row.GetColumn("search")
|> extract_start
|> parse_date
Result.map2 (fun date start -> { Date = date; Start = start }) maybe_date maybe_start
let range_on_entry (entry: Entry) =
let date =
entry.Date.ToDateTime(new TimeOnly(0, 0))
let start =
entry.Start.ToDateTime(new TimeOnly(0, 0))
let diff = date.Subtract(start).Days
max diff 0
let max_folder (acc: int) entry =
let range =
entry
|> parse_entry
|> Result.map range_on_entry
|> Result.defaultValue 0
max acc range
let max_period = csv.Rows |> Seq.fold max_folder 0
let print_entry (entry: Entry) =
printf "%s" (entry.Start.ToString "yyyy/MM/dd")
// csv.Rows
// |> Seq.head
// |> parse_entry
// |> Result.map print_entry
// printf "%i" max_period
let all_diffs =
csv.Rows
|> Seq.choose (parse_entry >> Option.ofResult)
|> Seq.map range_on_entry
let stats =
let diffs = all_diffs
let stats = Seq.stats diffs
// let mean = (Seq.meanBy float diffs)
let median = (Seq.median diffs)
printf "Max %d Mean (Average) %d Median (Middle number) %d" stats.Max stats.Mean median
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment