Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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