Skip to content

Instantly share code, notes, and snippets.

@bohdanszymanik
Created November 18, 2015 02:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bohdanszymanik/6b7966a7658e5b2fefc6 to your computer and use it in GitHub Desktop.
Save bohdanszymanik/6b7966a7658e5b2fefc6 to your computer and use it in GitHub Desktop.
open System
open System.IO
open System.Text.RegularExpressions
open System.Globalization
(*
For this example let's say we have data describing the processing of a batch of steps eg something like this:
BatchId
Start DateTime, End DateTime, Step Description
Start DateTime, End DateTime, Step Description
Start DateTime, End DateTime, Step Description
Start DateTime, End DateTime, Step Description
And repeating eg
1
2015-10-01 00:01:22, 2015-10-01 00:01:25, Step1
2015-10-01 00:01:29, 2015-10-01 00:03:12, Step2
2015-10-01 00:03:13, 2015-10-01 01:23:25, Step3
2
2015-10-01 00:02:22,2015-10-01 00:02:56,Step1
2015-10-01 00:03:29,2015-10-01 00:04:05,Step2
2015-10-01 00:04:13,2015-10-01 02:01:05,Step3
*)
let txt = File.ReadAllText("c:\\temp\\somedata.txt")
let rawTimes = Regex.Split(txt, "\n\r\n")
type RunTime = {
Id: string;
BatchDuration: TimeSpan;
MaxStepDuration: TimeSpan;
Step: string}
let stepParser (s:string) =
let csvs = Regex.Split(s, ",")
let start = DateTime.ParseExact(csvs.[0], "yyyy-MM-dd HH:mm:ss", CultureInfo.CurrentCulture)
let ``end`` = DateTime.ParseExact(csvs.[1], "yyyy-MM-dd HH:mm:ss", CultureInfo.CurrentCulture)
start, ``end``, ``end``.Subtract(start), csvs.[2]
let batchDurations = rawTimes
|> Array.map (fun rt ->
let splitLines = Regex.Split(rt, "\n",RegexOptions.Multiline)
let id = splitLines.[0].Trim()
let stepDurations = splitLines.[1..] |> Array.map stepParser
let maxStepDurationTime, maxStepDurationStep = stepDurations
|> Array.maxBy (fun (_,_,ts,_) -> ts)
|> fun (_,_,ts,st) -> ts,st
let batchStart = stepDurations |> Array.map (fun (s,_,_,_) -> s) |> Array.min
let batchEnd = stepDurations |> Array.map (fun (_,e,_,_) -> e) |> Array.max
{Id = id; BatchDuration = batchEnd.Subtract(batchStart); MaxStepDuration = maxStepDurationTime; Step = maxStepDurationStep.Trim() }
)
#r @"packages\Deedle.1.2.4\lib\net40\Deedle.dll"
open Deedle
let frame = Frame.ofRecords batchDurations
frame.SaveCsv(@"c:\temp\data.csv")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment