-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): make endpoint to return temperature predictions gold table
- Loading branch information
Showing
3 changed files
with
62 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |