Format .NET projects with csharpier
This commit is contained in:
@@ -15,7 +15,8 @@ public class ElectricityLogController : ControllerBase
|
||||
|
||||
public ElectricityLogController(
|
||||
ElectricityService electricityService,
|
||||
ILogger<ElectricityLogController> logger)
|
||||
ILogger<ElectricityLogController> logger
|
||||
)
|
||||
{
|
||||
this.electricityService = electricityService;
|
||||
this.logger = logger;
|
||||
@@ -25,7 +26,10 @@ public class ElectricityLogController : ControllerBase
|
||||
[Route("/day")]
|
||||
public async Task<ActionResult<Minute[]>> Get([FromQuery] string? date)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(date) || !DateOnly.TryParseExact(date, Constants.isoDateFormat, out _))
|
||||
if (
|
||||
string.IsNullOrWhiteSpace(date)
|
||||
|| !DateOnly.TryParseExact(date, Constants.isoDateFormat, out _)
|
||||
)
|
||||
{
|
||||
return BadDateParameter(nameof(date));
|
||||
}
|
||||
@@ -37,24 +41,34 @@ public class ElectricityLogController : ControllerBase
|
||||
[Route("/days")]
|
||||
public async Task<ActionResult<Day[]>> Get([FromQuery] string? start, [FromQuery] string? stop)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(start) || !DateOnly.TryParseExact(start, Constants.isoDateFormat, out _))
|
||||
if (
|
||||
string.IsNullOrWhiteSpace(start)
|
||||
|| !DateOnly.TryParseExact(start, Constants.isoDateFormat, out _)
|
||||
)
|
||||
{
|
||||
return BadDateParameter(nameof(start));
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(stop) || !DateOnly.TryParseExact(stop, Constants.isoDateFormat, out _))
|
||||
if (
|
||||
string.IsNullOrWhiteSpace(stop)
|
||||
|| !DateOnly.TryParseExact(stop, Constants.isoDateFormat, out _)
|
||||
)
|
||||
{
|
||||
return BadDateParameter(nameof(stop));
|
||||
}
|
||||
|
||||
if (string.Compare(start, stop) > 0)
|
||||
{
|
||||
return BadRequest($"The date argument of {nameof(stop)} must be greater or equal to the argument value of {nameof(start)}");
|
||||
return BadRequest(
|
||||
$"The date argument of {nameof(stop)} must be greater or equal to the argument value of {nameof(start)}"
|
||||
);
|
||||
}
|
||||
|
||||
return await electricityService.GetSummariesFor(start, stop);
|
||||
}
|
||||
|
||||
private BadRequestObjectResult BadDateParameter(string parameterName) =>
|
||||
BadRequest($"The search parameter {parameterName} is missing or is not a valid ISO date ({Constants.isoDateFormat}).");
|
||||
BadRequest(
|
||||
$"The search parameter {parameterName} is missing or is not a valid ISO date ({Constants.isoDateFormat})."
|
||||
);
|
||||
}
|
||||
|
||||
@@ -5,14 +5,10 @@ namespace Electricity.Api
|
||||
{
|
||||
public partial class DatabaseContext : DbContext
|
||||
{
|
||||
public DatabaseContext()
|
||||
{
|
||||
}
|
||||
public DatabaseContext() { }
|
||||
|
||||
public DatabaseContext(DbContextOptions<DatabaseContext> options)
|
||||
: base(options)
|
||||
{
|
||||
}
|
||||
: base(options) { }
|
||||
|
||||
public virtual DbSet<ElectricityLog> ElectricityLogs { get; set; } = null!;
|
||||
|
||||
|
||||
@@ -13,11 +13,13 @@
|
||||
public long DayTarifEnabled { get; set; }
|
||||
public double GasConsumptionInCubicMeters { get; set; }
|
||||
|
||||
public DateTime GetDateTime() => DateTime
|
||||
.Parse(
|
||||
$"{Date}T{TimeUtc}Z",
|
||||
null,
|
||||
System.Globalization.DateTimeStyles.AssumeUniversal)
|
||||
.ToUniversalTime();
|
||||
public DateTime GetDateTime() =>
|
||||
DateTime
|
||||
.Parse(
|
||||
$"{Date}T{TimeUtc}Z",
|
||||
null,
|
||||
System.Globalization.DateTimeStyles.AssumeUniversal
|
||||
)
|
||||
.ToUniversalTime();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,9 +6,7 @@ public record Day(string Date, double TotalPowerUse, double TotalPowerReturn, do
|
||||
{
|
||||
public static Day Empty(DateOnly date)
|
||||
{
|
||||
return new Day(
|
||||
date.ToString(Constants.isoDateFormat),
|
||||
0, 0, 0);
|
||||
return new Day(date.ToString(Constants.isoDateFormat), 0, 0, 0);
|
||||
}
|
||||
|
||||
public static Day FromEntity(Entities.ElectricityLog entity)
|
||||
|
||||
@@ -2,13 +2,23 @@ using Electricity.Api.Extensions;
|
||||
|
||||
namespace Electricity.Api.Models;
|
||||
|
||||
public record Minute(uint DateTime, double CurrentPowerUsage, double TotalPowerUse, double TotalPowerReturn, double TotalGasUse)
|
||||
public record Minute(
|
||||
uint DateTime,
|
||||
double CurrentPowerUsage,
|
||||
double TotalPowerUse,
|
||||
double TotalPowerReturn,
|
||||
double TotalGasUse
|
||||
)
|
||||
{
|
||||
public static Minute Empty(DateOnly date)
|
||||
{
|
||||
return new Minute(
|
||||
date.ToDateTime(new TimeOnly(0, 0), DateTimeKind.Utc).ToEpoch(),
|
||||
0, 0, 0, 0);
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
public static Minute FromEntity(Entities.ElectricityLog entity)
|
||||
|
||||
@@ -7,10 +7,13 @@ internal class Program
|
||||
{
|
||||
private static int Main(string[] args)
|
||||
{
|
||||
var rootCommand = new RootCommand("ElectricityServer is a small REST API to access the electricity log database");
|
||||
var rootCommand = new RootCommand(
|
||||
"ElectricityServer is a small REST API to access the electricity log database"
|
||||
);
|
||||
var connectionStringArgument = new Option<string>(
|
||||
name: "--connection-string",
|
||||
description: "Filepath to the Sqlite3 database file (*.db)");
|
||||
description: "Filepath to the Sqlite3 database file (*.db)"
|
||||
);
|
||||
rootCommand.AddOption(connectionStringArgument);
|
||||
|
||||
var result = rootCommand.Parse(args);
|
||||
@@ -27,15 +30,19 @@ internal class Program
|
||||
var sqlite3DatabaseFilePath = result.GetValueForOption(connectionStringArgument);
|
||||
if (!File.Exists(sqlite3DatabaseFilePath))
|
||||
{
|
||||
Console.WriteLine($"Sqlite3 database <{sqlite3DatabaseFilePath}> does not exist or is inaccessible");
|
||||
Console.WriteLine(
|
||||
$"Sqlite3 database <{sqlite3DatabaseFilePath}> does not exist or is inaccessible"
|
||||
);
|
||||
return 1;
|
||||
}
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
builder.Services.AddDbContext<DatabaseContext>((builder) =>
|
||||
{
|
||||
builder.UseSqlite($"Data Source={sqlite3DatabaseFilePath}");
|
||||
});
|
||||
builder.Services.AddDbContext<DatabaseContext>(
|
||||
(builder) =>
|
||||
{
|
||||
builder.UseSqlite($"Data Source={sqlite3DatabaseFilePath}");
|
||||
}
|
||||
);
|
||||
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
@@ -44,14 +51,19 @@ internal class Program
|
||||
policy =>
|
||||
{
|
||||
policy.WithOrigins("http://localhost:8080");
|
||||
});
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
builder.Services.AddControllers()
|
||||
builder
|
||||
.Services.AddControllers()
|
||||
.AddJsonOptions(config =>
|
||||
{
|
||||
config.JsonSerializerOptions.PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase;
|
||||
config.JsonSerializerOptions.PropertyNamingPolicy = System
|
||||
.Text
|
||||
.Json
|
||||
.JsonNamingPolicy
|
||||
.CamelCase;
|
||||
config.JsonSerializerOptions.WriteIndented = true;
|
||||
});
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
@@ -14,10 +14,12 @@ public class ElectricityService
|
||||
|
||||
public async Task<Minute[]> GetDetailsFor(string date)
|
||||
{
|
||||
return (await databaseContext.ElectricityLogs
|
||||
.Where(l => l.Date == date)
|
||||
.OrderBy(l => l.TimeUtc)
|
||||
.ToArrayAsync())
|
||||
return (
|
||||
await databaseContext
|
||||
.ElectricityLogs.Where(l => l.Date == date)
|
||||
.OrderBy(l => l.TimeUtc)
|
||||
.ToArrayAsync()
|
||||
)
|
||||
.Select(Minute.FromEntity)
|
||||
.ToArray();
|
||||
}
|
||||
@@ -39,8 +41,8 @@ public class ElectricityService
|
||||
private async Task<Day> GetSummaryFor(DateOnly date)
|
||||
{
|
||||
var databaseDate = date.ToString(Constants.isoDateFormat);
|
||||
var baseQuery = databaseContext.ElectricityLogs
|
||||
.Where(l => l.Date == databaseDate)
|
||||
var baseQuery = databaseContext
|
||||
.ElectricityLogs.Where(l => l.Date == databaseDate)
|
||||
.OrderBy(l => l.TimeUtc);
|
||||
var first = await baseQuery.FirstOrDefaultAsync();
|
||||
if (first == null)
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
using System.Net.Mime;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Solar.Api.Models;
|
||||
using Solar.Api.Services;
|
||||
using Swashbuckle.AspNetCore.Annotations;
|
||||
using System.Net.Mime;
|
||||
|
||||
namespace Solar.Api.Controllers;
|
||||
|
||||
@@ -14,9 +14,7 @@ public class SolarLogController : ControllerBase
|
||||
private readonly SolarService solarService;
|
||||
private readonly ILogger<SolarLogController> logger;
|
||||
|
||||
public SolarLogController(
|
||||
SolarService solarService,
|
||||
ILogger<SolarLogController> logger)
|
||||
public SolarLogController(SolarService solarService, ILogger<SolarLogController> logger)
|
||||
{
|
||||
this.solarService = solarService;
|
||||
this.logger = logger;
|
||||
@@ -25,10 +23,8 @@ public class SolarLogController : ControllerBase
|
||||
[HttpGet()]
|
||||
[Route("/day")]
|
||||
public async Task<ActionResult<DayResponse>> GetDayDetails(
|
||||
[SwaggerParameter(Required = true)]
|
||||
[SwaggerSchema(Format = "date")]
|
||||
[FromQuery]
|
||||
string date)
|
||||
[SwaggerParameter(Required = true)] [SwaggerSchema(Format = "date")] [FromQuery] string date
|
||||
)
|
||||
{
|
||||
var parsedDate = TryParseDate(date);
|
||||
if (!parsedDate.HasValue || parsedDate.Value.Year < 2000 || parsedDate.Value.Year > 3000)
|
||||
@@ -47,28 +43,38 @@ public class SolarLogController : ControllerBase
|
||||
[SwaggerParameter(Required = true)]
|
||||
[SwaggerSchema(Format = "date")]
|
||||
[FromQuery]
|
||||
string start,
|
||||
[SwaggerParameter(Required = true)]
|
||||
[SwaggerSchema(Format = "date")]
|
||||
[FromQuery]
|
||||
string stop)
|
||||
string start,
|
||||
[SwaggerParameter(Required = true)] [SwaggerSchema(Format = "date")] [FromQuery] string stop
|
||||
)
|
||||
{
|
||||
var parsedStartDate = TryParseDate(start);
|
||||
if (!parsedStartDate.HasValue || parsedStartDate.Value.Year < 2000 || parsedStartDate.Value.Year > 3000)
|
||||
if (
|
||||
!parsedStartDate.HasValue
|
||||
|| parsedStartDate.Value.Year < 2000
|
||||
|| parsedStartDate.Value.Year > 3000
|
||||
)
|
||||
{
|
||||
logger.LogInformation("Invalid start date {Date} requested", start);
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
var parsedStopDate = TryParseDate(stop);
|
||||
if (!parsedStopDate.HasValue || parsedStopDate.Value.Year < 2000 || parsedStopDate.Value.Year > 3000)
|
||||
if (
|
||||
!parsedStopDate.HasValue
|
||||
|| parsedStopDate.Value.Year < 2000
|
||||
|| parsedStopDate.Value.Year > 3000
|
||||
)
|
||||
{
|
||||
logger.LogInformation("Invalid stop date {Date} requested", stop);
|
||||
return BadRequest();
|
||||
}
|
||||
else if (parsedStopDate < parsedStartDate)
|
||||
{
|
||||
logger.LogInformation("Stop date {StopDate} must come before start date {StartDate} requested", stop, start);
|
||||
logger.LogInformation(
|
||||
"Stop date {StopDate} must come before start date {StartDate} requested",
|
||||
stop,
|
||||
start
|
||||
);
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
@@ -81,28 +87,41 @@ public class SolarLogController : ControllerBase
|
||||
[SwaggerParameter(Required = true)]
|
||||
[SwaggerSchema(Format = "yyyy-MM")]
|
||||
[FromQuery]
|
||||
string start,
|
||||
string start,
|
||||
[SwaggerParameter(Required = true)]
|
||||
[SwaggerSchema(Format = "yyyy-MM")]
|
||||
[FromQuery]
|
||||
string stop)
|
||||
string stop
|
||||
)
|
||||
{
|
||||
var parsedStartDate = TryParseDate($"{start}-01");
|
||||
if (!parsedStartDate.HasValue || parsedStartDate.Value.Year < 2000 || parsedStartDate.Value.Year > 3000)
|
||||
if (
|
||||
!parsedStartDate.HasValue
|
||||
|| parsedStartDate.Value.Year < 2000
|
||||
|| parsedStartDate.Value.Year > 3000
|
||||
)
|
||||
{
|
||||
logger.LogInformation("Invalid start year month {YearMonth} requested", start);
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
var parsedStopDate = TryParseDate($"{stop}-01");
|
||||
if (!parsedStopDate.HasValue || parsedStopDate.Value.Year < 2000 || parsedStopDate.Value.Year > 3000)
|
||||
if (
|
||||
!parsedStopDate.HasValue
|
||||
|| parsedStopDate.Value.Year < 2000
|
||||
|| parsedStopDate.Value.Year > 3000
|
||||
)
|
||||
{
|
||||
logger.LogInformation("Invalid stop year month {YearMonth} requested", stop);
|
||||
return BadRequest();
|
||||
}
|
||||
else if (parsedStopDate < parsedStartDate)
|
||||
{
|
||||
logger.LogInformation("Stop year month {StopYearMonth} must come before start year month {StartYearMonth} requested", stop, start);
|
||||
logger.LogInformation(
|
||||
"Stop year month {StopYearMonth} must come before start year month {StartYearMonth} requested",
|
||||
stop,
|
||||
start
|
||||
);
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
@@ -110,7 +129,8 @@ public class SolarLogController : ControllerBase
|
||||
parsedStartDate.Value.Year,
|
||||
parsedStartDate.Value.Month,
|
||||
parsedStopDate.Value.Year,
|
||||
parsedStopDate.Value.Month);
|
||||
parsedStopDate.Value.Month
|
||||
);
|
||||
}
|
||||
|
||||
private static DateOnly? TryParseDate(string value)
|
||||
|
||||
@@ -10,7 +10,8 @@ public class DateOnlyConverter : JsonConverter<DateOnly>
|
||||
public override DateOnly Read(
|
||||
ref Utf8JsonReader reader,
|
||||
Type typeToConvert,
|
||||
JsonSerializerOptions options)
|
||||
JsonSerializerOptions options
|
||||
)
|
||||
{
|
||||
var value = reader.GetString();
|
||||
return DateOnly.ParseExact(value!, serializationFormat);
|
||||
@@ -19,6 +20,6 @@ public class DateOnlyConverter : JsonConverter<DateOnly>
|
||||
public override void Write(
|
||||
Utf8JsonWriter writer,
|
||||
DateOnly value,
|
||||
JsonSerializerOptions options)
|
||||
=> writer.WriteStringValue(value.ToString(serializationFormat));
|
||||
JsonSerializerOptions options
|
||||
) => writer.WriteStringValue(value.ToString(serializationFormat));
|
||||
}
|
||||
@@ -10,7 +10,8 @@ public class TimeOnlyConverter : JsonConverter<TimeOnly>
|
||||
public override TimeOnly Read(
|
||||
ref Utf8JsonReader reader,
|
||||
Type typeToConvert,
|
||||
JsonSerializerOptions options)
|
||||
JsonSerializerOptions options
|
||||
)
|
||||
{
|
||||
var value = reader.GetString();
|
||||
return TimeOnly.ParseExact(value!, serializationFormat);
|
||||
@@ -19,6 +20,6 @@ public class TimeOnlyConverter : JsonConverter<TimeOnly>
|
||||
public override void Write(
|
||||
Utf8JsonWriter writer,
|
||||
TimeOnly value,
|
||||
JsonSerializerOptions options)
|
||||
=> writer.WriteStringValue(value.ToString(serializationFormat));
|
||||
JsonSerializerOptions options
|
||||
) => writer.WriteStringValue(value.ToString(serializationFormat));
|
||||
}
|
||||
@@ -5,14 +5,10 @@ namespace Solar.Api
|
||||
{
|
||||
public partial class DatabaseContext : DbContext
|
||||
{
|
||||
public DatabaseContext()
|
||||
{
|
||||
}
|
||||
public DatabaseContext() { }
|
||||
|
||||
public DatabaseContext(DbContextOptions<DatabaseContext> options)
|
||||
: base(options)
|
||||
{
|
||||
}
|
||||
: base(options) { }
|
||||
|
||||
public virtual DbSet<EnvoyLog> EnvoyLogs { get; set; } = null!;
|
||||
public virtual DbSet<ZeverLog> ZeverLogs { get; set; } = null!;
|
||||
@@ -46,11 +42,9 @@ namespace Solar.Api
|
||||
{
|
||||
entity.ToTable("ZeverSummary");
|
||||
|
||||
entity.HasIndex(e => e.Date, "IX_ZeverSummary_Date")
|
||||
.IsUnique();
|
||||
entity.HasIndex(e => e.Date, "IX_ZeverSummary_Date").IsUnique();
|
||||
|
||||
entity.HasIndex(e => e.Date, "idx_ZeverSummary_Date")
|
||||
.IsUnique();
|
||||
entity.HasIndex(e => e.Date, "idx_ZeverSummary_Date").IsUnique();
|
||||
|
||||
entity.Property(e => e.TotalWatts).HasColumnType("BIGINT");
|
||||
});
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
namespace Solar.Api.Models;
|
||||
|
||||
public record DayResponse(ZeverDayLog[] ZeverLogs, EnvoyDayLog[] EnvoyLogs);
|
||||
@@ -1,9 +1,9 @@
|
||||
using System.Text.Json;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Solar.Api.Converters;
|
||||
using Solar.Api.Services;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Solar.Api;
|
||||
|
||||
@@ -31,31 +31,38 @@ public partial class Program
|
||||
policy =>
|
||||
{
|
||||
policy.WithOrigins("http://localhost:8080");
|
||||
});
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
builder.Services.AddControllers().AddJsonOptions(config =>
|
||||
{
|
||||
config.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
|
||||
config.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
|
||||
config.JsonSerializerOptions.Converters.Add(new DateOnlyConverter());
|
||||
config.JsonSerializerOptions.Converters.Add(new TimeOnlyConverter());
|
||||
});
|
||||
builder
|
||||
.Services.AddControllers()
|
||||
.AddJsonOptions(config =>
|
||||
{
|
||||
config.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
|
||||
config.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
|
||||
config.JsonSerializerOptions.Converters.Add(new DateOnlyConverter());
|
||||
config.JsonSerializerOptions.Converters.Add(new TimeOnlyConverter());
|
||||
});
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen(config =>
|
||||
{
|
||||
config.MapType<DateOnly>(() => new OpenApiSchema
|
||||
{
|
||||
Type = "string",
|
||||
Format = "date",
|
||||
Example = OpenApiAnyFactory.CreateFromJson("\"2022-12-31\"")
|
||||
});
|
||||
config.MapType<TimeOnly>(() => new OpenApiSchema
|
||||
{
|
||||
Type = "string",
|
||||
Format = "time",
|
||||
Example = OpenApiAnyFactory.CreateFromJson("\"13:45:42.0000000\"")
|
||||
});
|
||||
config.MapType<DateOnly>(() =>
|
||||
new OpenApiSchema
|
||||
{
|
||||
Type = "string",
|
||||
Format = "date",
|
||||
Example = OpenApiAnyFactory.CreateFromJson("\"2022-12-31\""),
|
||||
}
|
||||
);
|
||||
config.MapType<TimeOnly>(() =>
|
||||
new OpenApiSchema
|
||||
{
|
||||
Type = "string",
|
||||
Format = "time",
|
||||
Example = OpenApiAnyFactory.CreateFromJson("\"13:45:42.0000000\""),
|
||||
}
|
||||
);
|
||||
|
||||
config.SupportNonNullableReferenceTypes();
|
||||
config.EnableAnnotations();
|
||||
|
||||
@@ -15,7 +15,8 @@ public class SolarService
|
||||
public SolarService(
|
||||
DatabaseContext databaseContext,
|
||||
IMemoryCache memoryCache,
|
||||
ILogger<SolarService> logger)
|
||||
ILogger<SolarService> logger
|
||||
)
|
||||
{
|
||||
this.databaseContext = databaseContext;
|
||||
this.memoryCache = memoryCache;
|
||||
@@ -26,13 +27,11 @@ public class SolarService
|
||||
public async Task<DayResponse> GetDayDetails(DateOnly date)
|
||||
{
|
||||
var zeverRecords = await databaseContext
|
||||
.ZeverLogs
|
||||
.Where(zl => zl.Date == date)
|
||||
.ZeverLogs.Where(zl => zl.Date == date)
|
||||
.OrderBy(zl => zl.TimeUtc)
|
||||
.ToArrayAsync();
|
||||
var envoyRecords = await databaseContext
|
||||
.EnvoyLogs
|
||||
.Where(er => er.Date == date)
|
||||
.EnvoyLogs.Where(er => er.Date == date)
|
||||
.OrderBy(er => er.TimeUtc)
|
||||
.ToArrayAsync();
|
||||
|
||||
@@ -40,17 +39,12 @@ public class SolarService
|
||||
|
||||
return new DayResponse(
|
||||
zeverRecords
|
||||
.Select(zr => new ZeverDayLog(
|
||||
zr.TimeUtc,
|
||||
(int)zr.CurrentWatts,
|
||||
(int)zr.TotalWatts))
|
||||
.Select(zr => new ZeverDayLog(zr.TimeUtc, (int)zr.CurrentWatts, (int)zr.TotalWatts))
|
||||
.ToArray(),
|
||||
envoyRecords
|
||||
.Select(er => new EnvoyDayLog(
|
||||
er.TimeUtc,
|
||||
(int)er.CurrentWatts,
|
||||
(int)er.TotalWatts))
|
||||
.ToArray());
|
||||
.Select(er => new EnvoyDayLog(er.TimeUtc, (int)er.CurrentWatts, (int)er.TotalWatts))
|
||||
.ToArray()
|
||||
);
|
||||
}
|
||||
|
||||
private static void NormalizeEnvoyLogs(EnvoyLog[] entities)
|
||||
@@ -70,21 +64,18 @@ public class SolarService
|
||||
public async Task<DaysResponse> GetDaySummaries(DateOnly start, DateOnly stop)
|
||||
{
|
||||
var zeverRecords = await databaseContext
|
||||
.ZeverSummaries
|
||||
.Where(zl => zl.Date >= start && zl.Date <= stop)
|
||||
.ZeverSummaries.Where(zl => zl.Date >= start && zl.Date <= stop)
|
||||
.ToArrayAsync();
|
||||
|
||||
List<DaySummaryLog> logs = new();
|
||||
var current = start;
|
||||
do
|
||||
{
|
||||
var zeverResult = zeverRecords.FirstOrDefault(zr => zr.Date == current)?.TotalWatts ?? 0;
|
||||
var zeverResult =
|
||||
zeverRecords.FirstOrDefault(zr => zr.Date == current)?.TotalWatts ?? 0;
|
||||
var envoyResult = await GetEnvoyDayTotalWatts(current);
|
||||
|
||||
logs.Add(new DaySummaryLog(
|
||||
current,
|
||||
(int)zeverResult,
|
||||
envoyResult));
|
||||
logs.Add(new DaySummaryLog(current, (int)zeverResult, envoyResult));
|
||||
|
||||
current = current.AddDays(1);
|
||||
} while (current <= stop);
|
||||
@@ -94,12 +85,12 @@ public class SolarService
|
||||
|
||||
private async Task<int> GetEnvoyDayTotalWatts(DateOnly date)
|
||||
{
|
||||
var min = await databaseContext.EnvoyLogs
|
||||
.Where(el => el.Date == date)
|
||||
var min = await databaseContext
|
||||
.EnvoyLogs.Where(el => el.Date == date)
|
||||
.OrderBy(el => el.TotalWatts)
|
||||
.FirstOrDefaultAsync();
|
||||
var max = await databaseContext.EnvoyLogs
|
||||
.Where(el => el.Date == date)
|
||||
var max = await databaseContext
|
||||
.EnvoyLogs.Where(el => el.Date == date)
|
||||
.OrderByDescending(el => el.TotalWatts)
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
@@ -111,14 +102,29 @@ public class SolarService
|
||||
return (int)(max.TotalWatts - min.TotalWatts);
|
||||
}
|
||||
|
||||
public async Task<MonthSummariesResponse> GetMonthSummaries(int fromYear, int fromMonth, int toYear, int toMonth)
|
||||
public async Task<MonthSummariesResponse> GetMonthSummaries(
|
||||
int fromYear,
|
||||
int fromMonth,
|
||||
int toYear,
|
||||
int toMonth
|
||||
)
|
||||
{
|
||||
var results = new List<MonthLog>();
|
||||
var current = (Year: fromYear, Month: fromMonth);
|
||||
do
|
||||
{
|
||||
var zeverResult = await GetOrAddToCache("zever", current.Year, current.Month, GetZeverMonthTotalWatts);
|
||||
var envoyYear = await GetOrAddToCache("envoy", current.Year, current.Month, GetEnvoyMonthTotalWatts);
|
||||
var zeverResult = await GetOrAddToCache(
|
||||
"zever",
|
||||
current.Year,
|
||||
current.Month,
|
||||
GetZeverMonthTotalWatts
|
||||
);
|
||||
var envoyYear = await GetOrAddToCache(
|
||||
"envoy",
|
||||
current.Year,
|
||||
current.Month,
|
||||
GetEnvoyMonthTotalWatts
|
||||
);
|
||||
results.Add(new MonthLog(current.Year, current.Month, zeverResult, envoyYear));
|
||||
|
||||
if (current.Month == 12)
|
||||
@@ -135,19 +141,20 @@ public class SolarService
|
||||
|
||||
private async Task<int> GetZeverMonthTotalWatts(int year, int month)
|
||||
{
|
||||
return (int)await databaseContext.ZeverSummaries
|
||||
.Where(zs => zs.Date.Year == year && zs.Date.Month == month)
|
||||
.SumAsync(zs => zs.TotalWatts);
|
||||
return (int)
|
||||
await databaseContext
|
||||
.ZeverSummaries.Where(zs => zs.Date.Year == year && zs.Date.Month == month)
|
||||
.SumAsync(zs => zs.TotalWatts);
|
||||
}
|
||||
|
||||
private async Task<int> GetEnvoyMonthTotalWatts(int year, int month)
|
||||
{
|
||||
var min = await databaseContext.EnvoyLogs
|
||||
.Where(el => el.Date.Year == year && el.Date.Month == month)
|
||||
var min = await databaseContext
|
||||
.EnvoyLogs.Where(el => el.Date.Year == year && el.Date.Month == month)
|
||||
.OrderBy(el => el.TotalWatts)
|
||||
.FirstOrDefaultAsync();
|
||||
var max = await databaseContext.EnvoyLogs
|
||||
.Where(el => el.Date.Year == year && el.Date.Month == month)
|
||||
var max = await databaseContext
|
||||
.EnvoyLogs.Where(el => el.Date.Year == year && el.Date.Month == month)
|
||||
.OrderByDescending(el => el.TotalWatts)
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
@@ -163,7 +170,8 @@ public class SolarService
|
||||
string source,
|
||||
int year,
|
||||
int month,
|
||||
Func<int, int, Task<int>> fetchMethod)
|
||||
Func<int, int, Task<int>> fetchMethod
|
||||
)
|
||||
{
|
||||
return await memoryCache.GetOrCreateAsync(
|
||||
CacheKeys.GetMonthSummaryCacheKey(source, year, month),
|
||||
@@ -174,6 +182,7 @@ public class SolarService
|
||||
cacheEntry.SlidingExpiration = TimeSpan.FromHours(12);
|
||||
}
|
||||
return await fetchMethod(year, month);
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user