Skip to content

Instantly share code, notes, and snippets.

Created October 3, 2014 08:26
Show Gist options
  • Save anonymous/45aeb30e81de16364296 to your computer and use it in GitHub Desktop.
Save anonymous/45aeb30e81de16364296 to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace sska
{
class Tuple
{
public int Day { get; set;}
public String WeekDay { get; set; }
public int WeekDayIndex { get; set;}
public int Hour { get; set;}
public int Minute { get; set; }
private Lazy<DateTime> dateTime;
internal DateTime DateTime { get { return this.dateTime.Value; } }
public String Instance { get; set; }
public int ResponseTime { get; set; }
public double? ResponseTimeAverageDay { get; set; }
public int? MaxResponseTimeDay { get; set; }
public int? MinResponseTimeDay { get; set; }
internal Tuple() {
this.dateTime = new Lazy<DateTime>(() => {
try {
// year, month, day, hour, minute, and second
return new DateTime(1, 1, 1 + this.Day, this.Hour, this.Minute, 0);
} catch {
return DateTime.Now;
}
});
}
public int InstanceId { get; set; }
public int TimeStampRelative { get; set; }
const string ToStringFormat =
"{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11}";
public override string ToString ()
{
return String.Format(ToStringFormat,
Day, WeekDay, WeekDayIndex, Hour, Minute, Instance, ResponseTime,
ResponseTimeAverageDay, MaxResponseTimeDay, MinResponseTimeDay,
InstanceId, TimeStampRelative);
}
}
class MainClass
{
const string CSVHeader = "day,weekday,weekdayIndex,hour,minute,instance,responseTime," +
"responseTimeAverageDay,maxResponseTimeDay,minResponseTimeDay," +
"instanceId,timestampRelative\n{0}";
public static void Main (string[] args)
{
if (args.Length == 0) {
Console.WriteLine("Please specify two arguments. The first argument " +
"must be the absolute path to the source file and " +
"the other might be the output file. If you omit the " +
"2nd argument, all output will be written to console.");
Environment.Exit(1);
}
var data = File.ReadAllLines(args[0]);
var tuples = data.Skip(1).Where(l => !String.IsNullOrEmpty(l)).Select(l => {
var vals = l.Split (';').Select(s => s.Replace("\"", "").Trim()).ToArray();
// the CSV-header looks like timestamp,responsetime,championServiceInstance
// after processing, it looks like this:
// day, weekday, weekdayIndex, hour, minute, instanceName, respTime
var tupleDate = default(DateTime);
DateTime.TryParse(vals[0], out tupleDate);
return new Tuple {
Day = tupleDate.Day,
WeekDay = tupleDate.DayOfWeek.ToString(),
WeekDayIndex = (int)tupleDate.DayOfWeek,
Hour = tupleDate.Hour,
Minute = tupleDate.Minute,
Instance = vals[1],
ResponseTime = int.Parse(vals[2])
};
}).ToList();
// very first thing is creating instanceIds and relative timeStamps
int autoId = 0;
var dictWithIds = tuples.GroupBy(t => t.Instance).Select(g => g.Key)
.ToDictionary(kv => kv, kv => Interlocked.Increment(ref autoId));
tuples.ForEach(t => t.InstanceId = dictWithIds[t.Instance]);
// now the relative timestamps; exact to the minute
// weekday * 1440 + hour * 60 + minute
tuples.ForEach(t => t.TimeStampRelative =
(t.WeekDayIndex * 1440 + t.Hour * 60 + t.Minute) / 10);
// first thing is to calculate the m/m response times each day for each server
// (MaxResponseTimeDay, MinResponseTimeDay)
var dayGroups = tuples.GroupBy (t => t.Day);
foreach (var dayGroup in dayGroups) {
// now we calc m/m & avg for each server
var serverGroups = dayGroup.GroupBy (t => t.Instance);
foreach (var serverGroup in serverGroups) {
// now each group corresponds to one server..
var foo = serverGroup.Select (s => s.ResponseTime).ToList ();
var max = foo.Max ();
var min = foo.Min ();
var avg = foo.Average ();
// now apply to each tuple
serverGroup.ToList ().ForEach (s => {
s.MaxResponseTimeDay = max;
s.MinResponseTimeDay = min;
s.ResponseTimeAverageDay = avg;
}
);
}
}
// this is what we write to the console (or file if specified)
var output = String.Format(CSVHeader,
String.Join("\n", tuples.Select(t => t.ToString())));
// export or print..
if (args.Length > 1) {
File.WriteAllText(args[1], output);
} else {
Console.Write(output);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment