-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
115 lines (93 loc) · 4.23 KB
/
Program.cs
File metadata and controls
115 lines (93 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using Microsoft.EntityFrameworkCore;
using StromAPI.Models;
using StromAPI.Tasks;
namespace StromAPI;
public class Program
{
public static async Task Main(string[] args)
{
if (!Directory.Exists(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot")))
{
Directory.CreateDirectory(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot"));
}
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
Args = args,
WebRootPath = "wwwroot"
});
builder.Services.AddDbContext<HourlyPriceDB>(opt => opt.UseSqlite("Data Source=PriceData.db"));
builder.Services.AddCors();
var app = builder.Build();
app.UseCors(options =>options.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());
using var scope = app.Services.CreateScope();
var services = scope.ServiceProvider;
var db = services.GetRequiredService<HourlyPriceDB>();
var predictor = new PricePredictorService(db);
app.UseStaticFiles();
app.MapGet("/pris/{area}/{date}", async (HourlyPriceDB priceDb, string area, string date) =>
{
if (date.Length != 10)
{
return Results.BadRequest();
}
DateTime parsedDate = DateTime.Parse(date);
DateOnly dateOnly = DateOnly.FromDateTime(parsedDate);
var prices = await priceDb.Prices
.Where(res => res.Area == area && res.Date == dateOnly)
.ToListAsync();
if (prices.Count == 0)
{
Console.WriteLine($"No prices found for {dateOnly}: Predictor model used");
prices.AddRange(predictor.PredictDate(dateOnly,area));
}
return Results.Ok(prices);
});
app.MapGet("/pris/{area}/{fromDate}/{toDate}",
async (HourlyPriceDB priceDb, string area, string fromDate, string toDate) =>
{
if (fromDate.Length != 10 || toDate.Length != 10)
{
return Results.BadRequest();
}
DateTime parsedFromDate = DateTime.Parse(fromDate);
DateOnly fromDateOnly = DateOnly.FromDateTime(parsedFromDate);
DateTime parsedToDate = DateTime.Parse(toDate);
DateOnly toDateOnly = DateOnly.FromDateTime(parsedToDate);
var prices = await priceDb.Prices
.Where(res => res.Area == area && res.Date >= fromDateOnly && res.Date <= toDateOnly)
.ToListAsync();
var dateIterator = fromDateOnly;
while (dateIterator < toDateOnly)
{
var iterator = dateIterator;
if (prices.All(res => res.Date != iterator))
{
Console.WriteLine($"No prices found for {iterator}: Predictor model used");
prices.AddRange(predictor.PredictDate(dateIterator,area));
}
dateIterator = dateIterator.AddDays(1);
}
return Results.Ok(prices);
});
app.MapGet("/pris/gjennomsnitt/{area}",
async (HourlyPriceDB priceDb, string area) =>
{
var priceList = await priceDb.Prices.Where(res => res.Area == area).ToArrayAsync();
return Results.Ok(priceList.Sum(e => e.Price) / priceList.Length);
});
app.MapFallback(async context =>
{
var webRootPath = app.Environment.WebRootPath;
context.Response.ContentType = "text/html";
await context.Response.SendFileAsync(Path.Combine(webRootPath, "index.html"));
});
var updater = new PriceUpdateTask(db);
await updater.LoadHistoricalPricesFromHks(720);
if(DateTime.Now > new DateTime(DateTime.Today.Year,DateTime.Today.Month,DateTime.Today.Day,15,30,0))
await updater.GetTomorrowsPricesFromHks();
var dbUpdateScheduler = new TaskSchedulerService(updater.GetTomorrowsPricesFromHks, new TimeSpan(15, 30, 0));
dbUpdateScheduler.ScheduleNextExecution();
predictor.Initialize();
await app.RunAsync();
}
}