mirror of
https://github.com/ckaczor/HomeMonitor.git
synced 2026-02-16 10:58:32 -05:00
Add aggregate API and fix (hack) time bucket
This commit is contained in:
@@ -38,4 +38,10 @@ public class ReadingsController(Database database) : ControllerBase
|
|||||||
{
|
{
|
||||||
return (await database.GetReadingsHistoryGrouped(start, end, bucketMinutes)).ToList();
|
return (await database.GetReadingsHistoryGrouped(start, end, bucketMinutes)).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpGet("aggregate")]
|
||||||
|
public async Task<ActionResult<List<ReadingsAggregate>>> GetAggregate(DateTimeOffset start, DateTimeOffset end)
|
||||||
|
{
|
||||||
|
return (await database.GetReadingsAggregate(start, end)).ToList();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
using ChrisKaczor.HomeMonitor.Environment.Service.Models;
|
using ChrisKaczor.HomeMonitor.Environment.Service.Models;
|
||||||
|
using ChrisKaczor.HomeMonitor.Environment.Service.Models.Indoor;
|
||||||
using Dapper;
|
using Dapper;
|
||||||
using DbUp;
|
using DbUp;
|
||||||
using Npgsql;
|
using Npgsql;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using ChrisKaczor.HomeMonitor.Environment.Service.Models.Indoor;
|
|
||||||
|
|
||||||
namespace ChrisKaczor.HomeMonitor.Environment.Service.Data;
|
namespace ChrisKaczor.HomeMonitor.Environment.Service.Data;
|
||||||
|
|
||||||
@@ -67,6 +67,17 @@ public class Database(IConfiguration configuration)
|
|||||||
|
|
||||||
var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Environment.Service.Data.Queries.GetReadingsHistoryGrouped.psql");
|
var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Environment.Service.Data.Queries.GetReadingsHistoryGrouped.psql");
|
||||||
|
|
||||||
|
query = query.Replace("@BucketMinutes", bucketMinutes.ToString());
|
||||||
|
|
||||||
return await connection.QueryAsync<ReadingsGrouped>(query, new { Start = start, End = end, BucketMinutes = bucketMinutes }).ConfigureAwait(false);
|
return await connection.QueryAsync<ReadingsGrouped>(query, new { Start = start, End = end, BucketMinutes = bucketMinutes }).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<IEnumerable<ReadingsAggregate>> GetReadingsAggregate(DateTimeOffset start, DateTimeOffset end)
|
||||||
|
{
|
||||||
|
await using var connection = CreateConnection();
|
||||||
|
|
||||||
|
var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Environment.Service.Data.Queries.GetReadingsAggregate.psql");
|
||||||
|
|
||||||
|
return await connection.QueryAsync<ReadingsAggregate>(query, new { Start = start, End = end }).ConfigureAwait(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
13
Environment/Service/Data/Queries/GetReadingsAggregate.psql
Normal file
13
Environment/Service/Data/Queries/GetReadingsAggregate.psql
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
SELECT name,
|
||||||
|
ROUND(min(temperature), 2) AS minimumTemperature,
|
||||||
|
ROUND(avg(temperature), 2) AS averageTemperature,
|
||||||
|
ROUND(max(temperature), 2) AS maximumTemperature,
|
||||||
|
ROUND(min(pressure), 2) AS minimumPressure,
|
||||||
|
ROUND(avg(pressure), 2) AS averagePressure,
|
||||||
|
ROUND(max(pressure), 2) AS maximumPressure,
|
||||||
|
ROUND(min(humidity), 2) AS minimumHumidity,
|
||||||
|
ROUND(avg(humidity), 2) AS averageHumidity,
|
||||||
|
ROUND(max(humidity), 2) AS maximumHumidity
|
||||||
|
FROM reading
|
||||||
|
WHERE time BETWEEN @Start AND @End
|
||||||
|
GROUP BY name;
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
SELECT
|
SELECT
|
||||||
time_bucket('15 minute', time) AS bucket,
|
time_bucket('@BucketMinutes minute', time) AS bucket,
|
||||||
name,
|
name,
|
||||||
ROUND(avg(temperature), 2) AS averageTemperature,
|
ROUND(avg(temperature), 2) AS averageTemperature,
|
||||||
ROUND(avg(pressure), 2) AS averagePressure,
|
ROUND(avg(pressure), 2) AS averagePressure,
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
@Environment_HostAddress = http://localhost:5000
|
@Environment_HostAddress = http://localhost:5050
|
||||||
|
|
||||||
GET {{Environment_HostAddress}}/readings/recent
|
GET {{Environment_HostAddress}}/readings/recent
|
||||||
Accept: application/json
|
Accept: application/json
|
||||||
@@ -10,5 +10,10 @@ Accept: application/json
|
|||||||
|
|
||||||
###
|
###
|
||||||
|
|
||||||
GET {{Environment_HostAddress}}/readings/history-grouped?start=2024-02-02T06:00&end=2024-02-02T08:00&bucketMinutes=15
|
GET {{Environment_HostAddress}}/readings/history-grouped?start=2024-03-11T00:00&end=2024-03-12T00:00&bucketMinutes=15
|
||||||
|
Accept: application/json
|
||||||
|
|
||||||
|
###
|
||||||
|
|
||||||
|
GET {{Environment_HostAddress}}/readings/aggregate?start=2024-03-11T00:00&end=2024-03-12T00:00
|
||||||
Accept: application/json
|
Accept: application/json
|
||||||
36
Environment/Service/Models/ReadingsAggregate.cs
Normal file
36
Environment/Service/Models/ReadingsAggregate.cs
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
namespace ChrisKaczor.HomeMonitor.Environment.Service.Models;
|
||||||
|
|
||||||
|
public class ReadingsAggregate
|
||||||
|
{
|
||||||
|
[JsonPropertyName("name")]
|
||||||
|
public string? Name { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("minimumTemperature")]
|
||||||
|
public decimal MinimumTemperature { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("averageTemperature")]
|
||||||
|
public decimal AverageTemperature { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("maximumTemperature")]
|
||||||
|
public decimal MaximumTemperature { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("minimumPressure")]
|
||||||
|
public decimal MinimumPressure { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("averagePressure")]
|
||||||
|
public decimal AveragePressure { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("maximumPressure")]
|
||||||
|
public decimal MaximumPressure { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("minimumHumidity")]
|
||||||
|
public decimal MinimumHumidity { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("averageHumidity")]
|
||||||
|
public decimal AverageHumidity { get; set; }
|
||||||
|
|
||||||
|
[JsonPropertyName("maximumHumidity")]
|
||||||
|
public decimal MaximumHumidity { get; set; }
|
||||||
|
}
|
||||||
@@ -11,12 +11,14 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Remove="Data\Queries\CreateReading.psql" />
|
<None Remove="Data\Queries\CreateReading.psql" />
|
||||||
|
<None Remove="Data\Queries\GetReadingsAggregate.psql" />
|
||||||
<None Remove="Data\Queries\GetReadingsHistoryGrouped.psql" />
|
<None Remove="Data\Queries\GetReadingsHistoryGrouped.psql" />
|
||||||
<None Remove="Data\Queries\GetRecentReadings.psql" />
|
<None Remove="Data\Queries\GetRecentReadings.psql" />
|
||||||
<None Remove="Data\Schema\1-Initial Schema.psql" />
|
<None Remove="Data\Schema\1-Initial Schema.psql" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<EmbeddedResource Include="Data\Queries\GetReadingsAggregate.psql" />
|
||||||
<EmbeddedResource Include="Data\Queries\GetReadingsHistoryGrouped.psql" />
|
<EmbeddedResource Include="Data\Queries\GetReadingsHistoryGrouped.psql" />
|
||||||
<EmbeddedResource Include="Data\Queries\GetRecentReadings.psql" />
|
<EmbeddedResource Include="Data\Queries\GetRecentReadings.psql" />
|
||||||
<EmbeddedResource Include="Data\Queries\CreateReading.psql" />
|
<EmbeddedResource Include="Data\Queries\CreateReading.psql" />
|
||||||
|
|||||||
Reference in New Issue
Block a user