From 9777771c7f800c0dd2819ae486caca1d41889585 Mon Sep 17 00:00:00 2001 From: Kim Date: Thu, 13 Jul 2023 16:37:52 +0200 Subject: [PATCH] Refactoring S3Explorer --- csharp/App/S3Explorer/Program.cs | 73 +++++++++++++++++++------------- 1 file changed, 44 insertions(+), 29 deletions(-) diff --git a/csharp/App/S3Explorer/Program.cs b/csharp/App/S3Explorer/Program.cs index a00764ff8..69c9d8890 100644 --- a/csharp/App/S3Explorer/Program.cs +++ b/csharp/App/S3Explorer/Program.cs @@ -8,48 +8,52 @@ public static class Program public static async Task Main(String[] args) { + // Todo refactor S3Access into Lib + + // Sssssecret if (args.Contains("-s")) { await SnakeGameSs.PlaySnake(); } + // Help message if (args.Length < 4 || args.Contains("-h")) { - Console.WriteLine("To use: $S3Explorer [BucketId] [from:Unix-time] [to:Unix-time] [#Data-points]"); - Console.WriteLine("-h shows this message"); + Console.WriteLine("Usage: S3Explorer [BucketId] [from:Unix-time] [to:Unix-time] [#Data-points]"); + Console.WriteLine("-h Shows this message."); Console.WriteLine("-s 🐍"); return 0; } - - - Console.WriteLine(""); + // Parsing Arguments var bucketName = args[0] + "-3e5b3069-214a-43ee-8d85-57d72000c19d"; - var fromTime = Int64.Parse(args[1]); - var toTime = Int64.Parse(args[2]); + var startTime = Int64.Parse(args[1]); + var endTime = Int64.Parse(args[2]); var numberOfDataPoints = Int64.Parse(args[3]); - - var time = toTime - fromTime; - var timeBetweenDataPoints = time / numberOfDataPoints; - timeBetweenDataPoints = Math.Max(timeBetweenDataPoints, 2); - // var fileList = ListAllFileNamesInBucket(bucketName); - var timestampList = new List { }; + var timeBetweenDataPoints = TimeBetweenDataPoints(startTime, endTime, numberOfDataPoints); - for (var i = fromTime; i <= toTime; i += timeBetweenDataPoints) + // Building a List of the timestamps we want to grab the files for. + var timestampList = new List { }; + for (var i = startTime; i <= endTime; i += timeBetweenDataPoints) { + //Rounding to even numbers only (we only save every second second) timestampList.Add((i/2 *2).ToString()); } - await GrabFiles(bucketName,timestampList); + + await PrintFiles(bucketName,timestampList); + // Success return 0; } - private static async Task GrabFiles(String bucketName, List timestampList) + private static async Task PrintFiles(String bucketName, List timestampList) { - var last = timestampList.Last(); - var fileCsv = await S3Access.Admin.GetFileText(bucketName, last + ".csv"); - var dataKeys = fileCsv + var newestDataFilename = timestampList.Last(); + var csvFileText = await GetFileText(bucketName, newestDataFilename); + + // Building Header-Row from the newest data + csvFileText .Select(l => l.Split(";")) .Select(l => l[0]) .Prepend("Timestamp") @@ -58,22 +62,33 @@ public static class Program foreach (var timestamp in timestampList) { - - fileCsv = await S3Access.Admin.GetFileText(bucketName,timestamp + ".csv"); - - fileCsv.Select(l => l.Split(";")) + csvFileText = await GetFileText(bucketName, timestamp); + + // Writing Data below data-keys in a timestamped row + csvFileText.Select(l => l.Split(";")) .Select(l => l[1]) .Prepend(timestamp) .JoinWith(";") .WriteLine(); - - // Parse csv, build csv } - } - private static List ListAllFileNamesInBucket(String bucketName) + } + + private static Int64 TimeBetweenDataPoints(Int64 startTime, Int64 endTime, Int64 numberOfDataPoints) { - // Todo refactor S3Access into Lib - return S3Access.Admin.ListFilesInBucket(bucketName).Result.Split(',').ToList(); + // Calculating temporal distance of data files from the number of requested points. + var timeSpan = endTime - startTime; + var timeBetweenDataPoints = timeSpan / numberOfDataPoints; + + // We only upload data every second second so sampling more is impossible. + // If this ever changes we might have to change this as well. + timeBetweenDataPoints = Math.Max(timeBetweenDataPoints, 2); + return timeBetweenDataPoints; + } + + // This Method extracts the Text from a given csv file on the s3 bucket + private static async Task GetFileText(String bucketName, String filename) + { + return await S3Access.Admin.GetFileText(bucketName, filename + ".csv"); } } \ No newline at end of file