Innovenergy_trunk/csharp/App/S3Explorer/Program.cs

94 lines
3.2 KiB
C#
Raw Normal View History

using InnovEnergy.App.Backend.S3;
using InnovEnergy.Lib.Utils;
namespace S3Explorer;
public static class Program
{
public static async Task<Int32> Main(String[] args)
{
2023-07-13 14:37:52 +00:00
// Todo refactor S3Access into Lib
// Sssssecret
if (args.Contains("-s"))
{
await SnakeGameSs.PlaySnake();
}
2023-07-13 14:37:52 +00:00
// Help message
if (args.Length < 4 || args.Contains("-h"))
{
2023-07-13 14:37:52 +00:00
Console.WriteLine("Usage: S3Explorer [BucketId] [from:Unix-time] [to:Unix-time] [#Data-points]");
Console.WriteLine("-h Shows this message.");
Console.WriteLine("-s 🐍");
return 0;
}
2023-07-13 14:37:52 +00:00
// Parsing Arguments
var bucketName = args[0] + "-3e5b3069-214a-43ee-8d85-57d72000c19d";
2023-07-13 14:37:52 +00:00
var startTime = Int64.Parse(args[1]);
var endTime = Int64.Parse(args[2]);
var numberOfDataPoints = Int64.Parse(args[3]);
2023-07-13 14:37:52 +00:00
var timeBetweenDataPoints = TimeBetweenDataPoints(startTime, endTime, numberOfDataPoints);
2023-07-13 14:37:52 +00:00
// Building a List of the timestamps we want to grab the files for.
var timestampList = new List<String> { };
for (var i = startTime; i <= endTime; i += timeBetweenDataPoints)
{
2023-07-13 14:37:52 +00:00
//Rounding to even numbers only (we only save every second second)
timestampList.Add((i/2 *2).ToString());
}
2023-07-13 14:37:52 +00:00
await PrintFiles(bucketName,timestampList);
2023-07-13 14:37:52 +00:00
// Success
return 0;
}
2023-07-13 14:37:52 +00:00
private static async Task PrintFiles(String bucketName, List<String> timestampList)
{
2023-07-13 14:37:52 +00:00
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")
.JoinWith(";")
.WriteLine();
foreach (var timestamp in timestampList)
{
2023-07-13 14:37:52 +00:00
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();
}
2023-07-13 14:37:52 +00:00
}
2023-07-13 14:37:52 +00:00
private static Int64 TimeBetweenDataPoints(Int64 startTime, Int64 endTime, Int64 numberOfDataPoints)
{
// Calculating temporal distance of data files from the number of requested points.
var timeSpan = endTime - startTime;
var timeBetweenDataPoints = timeSpan / numberOfDataPoints;
2023-07-13 14:37:52 +00:00
// 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<String[]> GetFileText(String bucketName, String filename)
{
2023-07-13 14:37:52 +00:00
return await S3Access.Admin.GetFileText(bucketName, filename + ".csv");
}
}