Created
December 17, 2021 02:08
-
-
Save sporto/c716048c9edffae9f61a7956cd865486 to your computer and use it in GitHub Desktop.
Get stats from cost page usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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