forked from ddrilling/AsbCloudServer
#создана миграция новых таблиц подсистем и наработок
#дополнен SubsystemOperationTimeService
This commit is contained in:
parent
d3bbbd5bd6
commit
9f23a45be7
@ -23,14 +23,14 @@ namespace AsbCloudApp.Data.Subsystems
|
||||
/// <summary>
|
||||
/// наработка подсистемы
|
||||
/// </summary>
|
||||
public TimeSpan UsedTime { get; set; }
|
||||
public TimeSpan UsedTimeHours { get; set; }
|
||||
/// <summary>
|
||||
/// коэффициент использования
|
||||
/// </summary>
|
||||
public TimeSpan KUsage { get; set; }
|
||||
public double KUsage { get; set; }
|
||||
/// <summary>
|
||||
/// коэффициент применения
|
||||
/// </summary>
|
||||
public TimeSpan K2 { get; set; }
|
||||
public double K2 { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -48,10 +48,16 @@ namespace AsbCloudApp.Requests
|
||||
/// </summary>
|
||||
public const int SelectModeOuter = 0;
|
||||
/// <summary>
|
||||
///
|
||||
/// информация попадает в выборку, если интервал выборки строго полностью пересекается с запрашиваемым интервалом.
|
||||
/// </summary>
|
||||
public const int SelectModeInner = 1;
|
||||
/// <summary>
|
||||
/// аналогично outer, но интервалы в частично пересекающиеся укорачиваются по границам интервала выборки.
|
||||
/// </summary>
|
||||
public const int SelectModeTrim = 2;
|
||||
/// <summary>
|
||||
/// Режим выборки элементов
|
||||
/// </summary>
|
||||
public int SelectMode { get; set; } = SelectModeOuter;
|
||||
}
|
||||
#nullable disable
|
||||
|
6200
AsbCloudDb/Migrations/20220802095637_Add_Table_Subsystem_and_SubsystemOperationTime.Designer.cs
generated
Normal file
6200
AsbCloudDb/Migrations/20220802095637_Add_Table_Subsystem_and_SubsystemOperationTime.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,104 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace AsbCloudDb.Migrations
|
||||
{
|
||||
public partial class Add_Table_Subsystem_and_SubsystemOperationTime : Migration
|
||||
{
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "t_subsystem",
|
||||
columns: table => new
|
||||
{
|
||||
id = table.Column<int>(type: "integer", nullable: false)
|
||||
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
|
||||
name = table.Column<string>(type: "character varying(255)", maxLength: 255, nullable: true),
|
||||
description = table.Column<string>(type: "character varying(255)", maxLength: 255, nullable: true)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_t_subsystem", x => x.id);
|
||||
},
|
||||
comment: "Описание подсистем");
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "t_subsystem_operation_time",
|
||||
columns: table => new
|
||||
{
|
||||
id = table.Column<int>(type: "integer", nullable: false)
|
||||
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
|
||||
id_telemetry = table.Column<int>(type: "integer", nullable: false, comment: "ИД телеметрии по которой выдается информация"),
|
||||
id_subsystem = table.Column<int>(type: "integer", nullable: false),
|
||||
date_start = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, comment: "дата/время включения подсистемы"),
|
||||
date_end = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, comment: "дата/время выключения подсистемы"),
|
||||
depth_start = table.Column<float>(type: "real", nullable: true, comment: "глубина забоя на момент включения подсистемы"),
|
||||
depth_end = table.Column<float>(type: "real", nullable: true, comment: "глубина забоя на момент выключения подсистемы")
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_t_subsystem_operation_time", x => x.id);
|
||||
table.ForeignKey(
|
||||
name: "FK_t_subsystem_operation_time_t_subsystem_id_subsystem",
|
||||
column: x => x.id_subsystem,
|
||||
principalTable: "t_subsystem",
|
||||
principalColumn: "id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
table.ForeignKey(
|
||||
name: "FK_t_subsystem_operation_time_t_telemetry_id_telemetry",
|
||||
column: x => x.id_telemetry,
|
||||
principalTable: "t_telemetry",
|
||||
principalColumn: "id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
},
|
||||
comment: "наработки подсистем");
|
||||
|
||||
migrationBuilder.InsertData(
|
||||
table: "t_permission",
|
||||
columns: new[] { "id", "description", "name" },
|
||||
values: new object[,]
|
||||
{
|
||||
{ 127, "Разрешение удалять админ. Подсистемы", "AdminSubsytem.delete" },
|
||||
{ 128, "Разрешение редактировать админ. Подсистемы", "AdminSubsytem.edit" },
|
||||
{ 129, "Разрешение просматривать админ. Подсистемы", "AdminSubsytem.get" }
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_t_subsystem_operation_time_id_subsystem",
|
||||
table: "t_subsystem_operation_time",
|
||||
column: "id_subsystem");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_t_subsystem_operation_time_id_telemetry",
|
||||
table: "t_subsystem_operation_time",
|
||||
column: "id_telemetry");
|
||||
}
|
||||
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "t_subsystem_operation_time");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "t_subsystem");
|
||||
|
||||
migrationBuilder.DeleteData(
|
||||
table: "t_permission",
|
||||
keyColumn: "id",
|
||||
keyValue: 127);
|
||||
|
||||
migrationBuilder.DeleteData(
|
||||
table: "t_permission",
|
||||
keyColumn: "id",
|
||||
keyValue: 128);
|
||||
|
||||
migrationBuilder.DeleteData(
|
||||
table: "t_permission",
|
||||
keyColumn: "id",
|
||||
keyValue: 129);
|
||||
}
|
||||
}
|
||||
}
|
@ -1114,6 +1114,24 @@ namespace AsbCloudDb.Migrations
|
||||
Name = "AdminWell.get"
|
||||
},
|
||||
new
|
||||
{
|
||||
Id = 127,
|
||||
Description = "Разрешение удалять админ. Подсистемы",
|
||||
Name = "AdminSubsytem.delete"
|
||||
},
|
||||
new
|
||||
{
|
||||
Id = 128,
|
||||
Description = "Разрешение редактировать админ. Подсистемы",
|
||||
Name = "AdminSubsytem.edit"
|
||||
},
|
||||
new
|
||||
{
|
||||
Id = 129,
|
||||
Description = "Разрешение просматривать админ. Подсистемы",
|
||||
Name = "AdminSubsytem.get"
|
||||
},
|
||||
new
|
||||
{
|
||||
Id = 200,
|
||||
Description = "Разрешение редактировать 0",
|
||||
@ -2746,6 +2764,81 @@ namespace AsbCloudDb.Migrations
|
||||
b.HasComment("Запросы на изменение уставок панели оператора");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("AsbCloudDb.Model.Subsystems.Subsystem", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("integer")
|
||||
.HasColumnName("id");
|
||||
|
||||
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<string>("Description")
|
||||
.HasMaxLength(255)
|
||||
.HasColumnType("character varying(255)")
|
||||
.HasColumnName("description");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.HasMaxLength(255)
|
||||
.HasColumnType("character varying(255)")
|
||||
.HasColumnName("name");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("t_subsystem");
|
||||
|
||||
b.HasComment("Описание подсистем");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("AsbCloudDb.Model.Subsystems.SubsystemOperationTime", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("integer")
|
||||
.HasColumnName("id");
|
||||
|
||||
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<DateTimeOffset>("DateEnd")
|
||||
.HasColumnType("timestamp with time zone")
|
||||
.HasColumnName("date_end")
|
||||
.HasComment("дата/время выключения подсистемы");
|
||||
|
||||
b.Property<DateTimeOffset>("DateStart")
|
||||
.HasColumnType("timestamp with time zone")
|
||||
.HasColumnName("date_start")
|
||||
.HasComment("дата/время включения подсистемы");
|
||||
|
||||
b.Property<float?>("DepthEnd")
|
||||
.HasColumnType("real")
|
||||
.HasColumnName("depth_end")
|
||||
.HasComment("глубина забоя на момент выключения подсистемы");
|
||||
|
||||
b.Property<float?>("DepthStart")
|
||||
.HasColumnType("real")
|
||||
.HasColumnName("depth_start")
|
||||
.HasComment("глубина забоя на момент включения подсистемы");
|
||||
|
||||
b.Property<int>("IdSubsystem")
|
||||
.HasColumnType("integer")
|
||||
.HasColumnName("id_subsystem");
|
||||
|
||||
b.Property<int>("IdTelemetry")
|
||||
.HasColumnType("integer")
|
||||
.HasColumnName("id_telemetry")
|
||||
.HasComment("ИД телеметрии по которой выдается информация");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("IdSubsystem");
|
||||
|
||||
b.HasIndex("IdTelemetry");
|
||||
|
||||
b.ToTable("t_subsystem_operation_time");
|
||||
|
||||
b.HasComment("наработки подсистем");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("AsbCloudDb.Model.Telemetry", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
@ -5727,6 +5820,25 @@ namespace AsbCloudDb.Migrations
|
||||
b.Navigation("Well");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("AsbCloudDb.Model.Subsystems.SubsystemOperationTime", b =>
|
||||
{
|
||||
b.HasOne("AsbCloudDb.Model.Subsystems.Subsystem", "Subsystem")
|
||||
.WithMany("SubsystemOperationTimes")
|
||||
.HasForeignKey("IdSubsystem")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("AsbCloudDb.Model.Telemetry", "Telemetry")
|
||||
.WithMany()
|
||||
.HasForeignKey("IdTelemetry")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Subsystem");
|
||||
|
||||
b.Navigation("Telemetry");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("AsbCloudDb.Model.TelemetryDataSaub", b =>
|
||||
{
|
||||
b.HasOne("AsbCloudDb.Model.Telemetry", "Telemetry")
|
||||
@ -6016,6 +6128,11 @@ namespace AsbCloudDb.Migrations
|
||||
b.Navigation("RelationUserRolePermissions");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("AsbCloudDb.Model.Subsystems.Subsystem", b =>
|
||||
{
|
||||
b.Navigation("SubsystemOperationTimes");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("AsbCloudDb.Model.Telemetry", b =>
|
||||
{
|
||||
b.Navigation("DataSaub");
|
||||
|
@ -19,7 +19,7 @@ namespace AsbCloudDb.Model.Subsystems
|
||||
[Column("id_subsystem")]
|
||||
public int IdSubsystem { get; set; }
|
||||
|
||||
[Column("date_end"), Comment("дата/время включения подсистемы")]
|
||||
[Column("date_start"), Comment("дата/время включения подсистемы")]
|
||||
public DateTimeOffset DateStart { get; set; }
|
||||
[Column("date_end"), Comment("дата/время выключения подсистемы")]
|
||||
public DateTimeOffset DateEnd { get; set; }
|
||||
|
@ -15,6 +15,7 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace AsbCloudInfrastructure.Services.Subsystems
|
||||
{
|
||||
#nullable enable
|
||||
internal class SubsystemOperationTimeService : ISubsystemOperationTimeService
|
||||
{
|
||||
|
||||
@ -61,63 +62,86 @@ namespace AsbCloudInfrastructure.Services.Subsystems
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<SubsystemOperationTimeDto>> GetOperationTimeAsync(SubsystemOperationTimeRequest request, CancellationToken token)
|
||||
{
|
||||
|
||||
{
|
||||
var query = BuildQuery(request)
|
||||
.AsNoTracking();
|
||||
if (query is null)
|
||||
return null;
|
||||
var data = await query.ToListAsync(token);
|
||||
|
||||
var data = await query.ToListAsync(token);
|
||||
|
||||
if (request.SelectMode == SubsystemOperationTimeRequest.SelectModeInner)
|
||||
{
|
||||
if (request.GtDate is not null)
|
||||
query = query.Where(o => o.DateStart >= request.GtDate.Value || o.DateEnd >= request.GtDate.Value);
|
||||
|
||||
if (request.LtDate is not null)
|
||||
query = query.Where(o => o.DateEnd <= request.LtDate.Value || o.DateStart <= request.LtDate.Value);
|
||||
}
|
||||
if (request.SelectMode == SubsystemOperationTimeRequest.SelectModeTrim)
|
||||
{
|
||||
var begin = request.GtDate ?? throw new ArgumentNullException(nameof(request.GtDate));
|
||||
var end = request.GtDate ?? throw new ArgumentNullException(nameof(request.LtDate));
|
||||
data = Trim(data, begin,end );
|
||||
}
|
||||
var dtos = data.Select(o => Convert(o));
|
||||
|
||||
return dtos;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public async Task<IEnumerable<SubsystemStatisticsDto>?> GetStatAsync(SubsystemOperationTimeRequest request, CancellationToken token)
|
||||
{
|
||||
var query = BuildQuery(request)
|
||||
?.AsNoTracking();
|
||||
if (query is null)
|
||||
return null;
|
||||
var groupedData = await query
|
||||
.GroupBy(o => o.IdSubsystem)
|
||||
.ToListAsync(token);
|
||||
var statList = CalcStat(groupedData,request);
|
||||
{
|
||||
var data = await GetOperationTimeAsync(request, token);
|
||||
var statList = CalcStat(data, request);
|
||||
return statList;
|
||||
}
|
||||
|
||||
|
||||
private List<SubsystemOperationTime> Trim(List<SubsystemOperationTime> data, DateTime gtDate, DateTime ltDate)
|
||||
{
|
||||
var itemsToTrim = data.Where(q => true)
|
||||
.Select(o => new SubsystemOperationTime
|
||||
{
|
||||
Id = o.Id,
|
||||
DateStart = gtDate < o.DateStart ? gtDate : o.DateStart,
|
||||
DateEnd = ltDate > o.DateEnd ? ltDate : o.DateEnd,
|
||||
IdSubsystem = o.IdSubsystem,
|
||||
IdTelemetry = o.IdTelemetry,
|
||||
DepthEnd = o.DepthEnd,
|
||||
DepthStart = o.DepthStart,
|
||||
Subsystem = o.Subsystem,
|
||||
Telemetry = o.Telemetry
|
||||
})
|
||||
.ToList();
|
||||
return itemsToTrim;
|
||||
}
|
||||
|
||||
private IEnumerable<SubsystemStatisticsDto> CalcStat(List<IGrouping<int, SubsystemOperationTime>> groupedData, SubsystemOperationTimeRequest request)
|
||||
private IEnumerable<SubsystemStatisticsDto> CalcStat(IEnumerable<SubsystemOperationTimeDto> groupedData, SubsystemOperationTimeRequest request)
|
||||
{
|
||||
var result = new List<SubsystemStatisticsDto>();
|
||||
var periodGroupTotal = groupedData.Sum(g => g.Sum(o=> o.DateEnd.ToUnixTimeSeconds()-o.DateStart.ToUnixTimeSeconds()));
|
||||
var periodGroupTotal = groupedData
|
||||
.GroupBy(x => x.IdSubsystem)
|
||||
.Sum(g => g.Sum(o=> (o.DateEnd - o.DateStart).TotalHours));
|
||||
foreach (IEnumerable<SubsystemOperationTime> item in groupedData)
|
||||
{
|
||||
var periodGroup = item.Sum(g => g.DateEnd.ToUnixTimeSeconds() - g.DateStart.ToUnixTimeSeconds());
|
||||
|
||||
var periodGroup = item.Sum(g => (g.DateEnd - g.DateStart).TotalHours);
|
||||
var periodRequest = item.Where(o => o.DateStart==request.GtDate & o.DateEnd == request.LtDate)
|
||||
.Sum(o => o.DateEnd.ToUnixTimeSeconds() - o.DateStart.ToUnixTimeSeconds());
|
||||
.Sum(o => (o.DateEnd - o.DateStart).TotalHours);
|
||||
int idSubsystem = item.First().IdSubsystem;
|
||||
var subsystemStat = new SubsystemStatisticsDto()
|
||||
{
|
||||
IdSubsystem = idSubsystem,
|
||||
Subsystem = subsystemService.GetOrDefault(idSubsystem)?.Name ?? "unknown",
|
||||
UsedTime = ConvertToTimeSpan(periodGroup),
|
||||
KUsage = ConvertToTimeSpan(periodGroup / periodRequest),
|
||||
K2 = ConvertToTimeSpan(periodGroup / periodGroupTotal),
|
||||
UsedTimeHours = TimeSpan.FromHours(periodGroup),
|
||||
KUsage = 1d*periodGroup / periodRequest,
|
||||
K2 = 1d*periodGroup / periodGroupTotal,
|
||||
};
|
||||
result.Add(subsystemStat);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private TimeSpan ConvertToTimeSpan(long sec)
|
||||
{
|
||||
TimeSpan ts = TimeSpan.FromSeconds(sec);
|
||||
return ts;
|
||||
}
|
||||
|
||||
|
||||
private IQueryable<SubsystemOperationTime>? BuildQuery(SubsystemOperationTimeRequest request)
|
||||
{
|
||||
@ -132,26 +156,13 @@ namespace AsbCloudInfrastructure.Services.Subsystems
|
||||
if(request.IdsSubsystems?.Any() == true)
|
||||
query = query.Where(o => request.IdsSubsystems.Contains(o.IdSubsystem));
|
||||
|
||||
switch(request.SelectMode)
|
||||
{
|
||||
case 0:
|
||||
case 2:
|
||||
if (request.GtDate is not null)
|
||||
query = query.Where(o => o.DateStart >= request.GtDate.Value||o.DateEnd >= request.GtDate.Value);
|
||||
|
||||
if (request.GtDate is not null)
|
||||
query = query.Where(o => o.DateStart >= request.GtDate.Value);
|
||||
|
||||
if (request.LtDate is not null)
|
||||
query = query.Where(o => o.DateEnd <= request.LtDate.Value||o.DateStart <= request.LtDate.Value);
|
||||
break;
|
||||
|
||||
case 1:
|
||||
if (request.GtDate is not null)
|
||||
query = query.Where(o => o.DateStart >= request.GtDate.Value);
|
||||
|
||||
if (request.LtDate is not null)
|
||||
query = query.Where(o => o.DateEnd <= request.LtDate.Value);
|
||||
break;
|
||||
|
||||
}
|
||||
if (request.LtDate is not null)
|
||||
query = query.Where(o => o.DateEnd <= request.LtDate.Value);
|
||||
|
||||
|
||||
if (request.GtDepth is not null)
|
||||
query = query.Where(o => o.DepthStart >= request.GtDepth.Value);
|
||||
@ -189,4 +200,5 @@ namespace AsbCloudInfrastructure.Services.Subsystems
|
||||
|
||||
|
||||
}
|
||||
#nullable disable
|
||||
}
|
||||
|
@ -20,12 +20,12 @@ namespace AsbCloudWebApi.Controllers.Subsystems
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
|
||||
public class OperatingTimeSubsystemController : ControllerBase
|
||||
public class SubsystemOperationTimeController : ControllerBase
|
||||
{
|
||||
private readonly ISubsystemOperationTimeService subsystemOperationTimeService;
|
||||
private readonly IWellService wellService;
|
||||
|
||||
public OperatingTimeSubsystemController(ISubsystemOperationTimeService subsystemOperationTimeService, IWellService wellService)
|
||||
public SubsystemOperationTimeController(ISubsystemOperationTimeService subsystemOperationTimeService, IWellService wellService)
|
||||
{
|
||||
this.subsystemOperationTimeService = subsystemOperationTimeService;
|
||||
this.wellService = wellService;
|
||||
@ -39,28 +39,7 @@ namespace AsbCloudWebApi.Controllers.Subsystems
|
||||
{
|
||||
var subsystemResult = new List<SubsystemStatisticsDto>()
|
||||
{
|
||||
new SubsystemStatisticsDto(){
|
||||
IdSubsystem = 1,
|
||||
Subsystem = "test1",
|
||||
UsedTime = System.TimeSpan.MinValue,
|
||||
K2 = System.TimeSpan.MinValue,
|
||||
KUsage = System.TimeSpan.MinValue
|
||||
|
||||
},
|
||||
new SubsystemStatisticsDto(){
|
||||
IdSubsystem = 2,
|
||||
Subsystem = "test2",
|
||||
UsedTime = System.TimeSpan.MinValue,
|
||||
K2 = System.TimeSpan.Zero,
|
||||
KUsage = System.TimeSpan.Zero
|
||||
},
|
||||
new SubsystemStatisticsDto(){
|
||||
IdSubsystem = 3,
|
||||
Subsystem = "test3",
|
||||
UsedTime = System.TimeSpan.MinValue,
|
||||
K2 = System.TimeSpan.MaxValue,
|
||||
KUsage = System.TimeSpan.MaxValue
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
return Ok(subsystemResult);
|
Loading…
Reference in New Issue
Block a user