Skip to content

Commit

Permalink
feat(api): make endpoint to return temperature predictions gold table
Browse files Browse the repository at this point in the history
  • Loading branch information
pbullhove committed Oct 2, 2024
1 parent fd17f91 commit ab8d1d8
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
3 changes: 3 additions & 0 deletions api/AquaApi/AquaApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.12.1" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.22.1" />
<PackageReference Include="CsvHelper" Version="33.0.1" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.7" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>
Expand Down
12 changes: 12 additions & 0 deletions api/AquaApi/MeasurementRow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace AquaApi;

public class MeasurementRow
{
public string lat { get; set; }
public string lon { get; set; }
public string variable_name { get; set; }
public string time { get; set; }
public string ocean_temperature { get; set; }
public string depth_meters { get; set; }
public string fetch_timestamp{ get; set; }
}
51 changes: 47 additions & 4 deletions api/AquaApi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,56 @@
using System.Globalization;
using System.Text.Json;
using AquaApi;
using Azure.Identity;
using Azure.Storage.Blobs;
using CsvHelper;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
// Add services to the container
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

// Register the BlobServiceClient with the connection string from app settings
builder.Services.AddSingleton(x =>
{
var connectionString = builder.Configuration.GetConnectionString("BlobStorage");
return new BlobServiceClient(connectionString);
});

var app = builder.Build();
app.MapGet("/", () => "Hello World!");

app.Run("http://0.0.0.0:80");
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}



// Endpoint to fetch a CSV from Blob Storage, parse it, and return JSON
app.MapGet("/temperature_predictions/latest", async (BlobServiceClient blobServiceClient) =>
{
var containerClient = blobServiceClient.GetBlobContainerClient("datalake");
var blobClient = containerClient.GetBlobClient("havvarsel/gold/havtemp-pred-latest.csv");

// Check if the file exists
if (!await blobClient.ExistsAsync())
{
return Results.NotFound(new { message = "File not found" });
}

// Download the CSV file from Blob Storage
var downloadStream = new MemoryStream();
await blobClient.DownloadToAsync(downloadStream);
downloadStream.Position = 0; // Reset the stream position

// Parse the CSV file using CsvHelper
using var reader = new StreamReader(downloadStream);
using var csv = new CsvReader(reader, CultureInfo.InvariantCulture);
var records = csv.GetRecords<MeasurementRow>().ToList(); // Dynamically read CSV data
return Results.Ok(records); // Return the JSON data
})
.WithName("DownloadCsvAndParseToJson");

app.Run();

0 comments on commit ab8d1d8

Please sign in to comment.