forked from ddrilling/AsbCloudServer
#7887519 Статистика по ограничивающим параметрам backend
This commit is contained in:
parent
79ae2ce62c
commit
c5aef20dc0
37
AsbCloudApp/Data/LimitingParameterDto.cs
Normal file
37
AsbCloudApp/Data/LimitingParameterDto.cs
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace AsbCloudApp.Data
|
||||||
|
{
|
||||||
|
#nullable enable
|
||||||
|
/// <summary>
|
||||||
|
/// Статистика по ограничивающим параметрам
|
||||||
|
/// </summary>
|
||||||
|
public class LimitingParameterDto
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Идентификатор скважины
|
||||||
|
/// </summary>
|
||||||
|
public int IdWell { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Время бурения
|
||||||
|
/// </summary>
|
||||||
|
public float TimeMinutes { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Глубина бурения
|
||||||
|
/// </summary>
|
||||||
|
public float Depth { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Количество включений
|
||||||
|
/// </summary>
|
||||||
|
public int NumberInclusions { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Идентификатор критерия бурения
|
||||||
|
/// </summary>
|
||||||
|
public short IdFeedRegulator { get; set; }
|
||||||
|
}
|
||||||
|
#nullable disable
|
||||||
|
}
|
25
AsbCloudApp/Repositories/ILimitingParameterRepository.cs
Normal file
25
AsbCloudApp/Repositories/ILimitingParameterRepository.cs
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
using AsbCloudApp.Data;
|
||||||
|
using AsbCloudApp.Requests;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
namespace AsbCloudApp.Repositories
|
||||||
|
{
|
||||||
|
#nullable enable
|
||||||
|
/// <summary>
|
||||||
|
/// Репозиторий по ограничивающим параметрам с фильтрацией
|
||||||
|
/// </summary>
|
||||||
|
public interface ILimitingParameterRepository
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Получение информации идентификатору скважины
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
/// <param name="wellDto"></param>
|
||||||
|
/// <param name="token"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<IEnumerable<LimitingParameterDto>> GetInfosAsync(LimitingParameterRequest request, WellDto wellDto, CancellationToken token);
|
||||||
|
}
|
||||||
|
#nullable disable
|
||||||
|
}
|
41
AsbCloudApp/Requests/LimitingParameterRequest.cs
Normal file
41
AsbCloudApp/Requests/LimitingParameterRequest.cs
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace AsbCloudApp.Requests
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Класс с фильтрами для запроса по ограничивающим параметрам
|
||||||
|
/// </summary>
|
||||||
|
public class LimitingParameterRequest
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// идентификатор скважины
|
||||||
|
/// </summary>
|
||||||
|
[Required]
|
||||||
|
public int IdWell { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Больше или равно дате
|
||||||
|
/// </summary>
|
||||||
|
public DateTime? GtDate { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Меньше или равно дате
|
||||||
|
/// </summary>
|
||||||
|
public DateTime? LtDate { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Больше или равно глубины забоя
|
||||||
|
/// </summary>
|
||||||
|
public double? GtDepth { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Меньше или равно глубины забоя
|
||||||
|
/// </summary>
|
||||||
|
public double? LtDepth { get; set; }
|
||||||
|
}
|
||||||
|
}
|
@ -1,9 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace AsbCloudApp.Requests
|
namespace AsbCloudApp.Requests
|
||||||
{
|
{
|
||||||
|
24
AsbCloudApp/Services/ILimitingParameterService.cs
Normal file
24
AsbCloudApp/Services/ILimitingParameterService.cs
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
using AsbCloudApp.Data;
|
||||||
|
using AsbCloudApp.Requests;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace AsbCloudApp.Services
|
||||||
|
{
|
||||||
|
#nullable enable
|
||||||
|
/// <summary>
|
||||||
|
/// Сервис по ограничивающим параметрам с фильтрацией
|
||||||
|
/// </summary>
|
||||||
|
public interface ILimitingParameterService
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Получение информации идентификатору скважины
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
/// <param name="token"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<IEnumerable<LimitingParameterDto>?> GetInfosAsync(LimitingParameterRequest request, CancellationToken token);
|
||||||
|
}
|
||||||
|
#nullable disable
|
||||||
|
}
|
6596
AsbCloudDb/Migrations/20221109104346_AddTable_t_limiting_parameter.Designer.cs
generated
Normal file
6596
AsbCloudDb/Migrations/20221109104346_AddTable_t_limiting_parameter.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,50 @@
|
|||||||
|
using System;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace AsbCloudDb.Migrations
|
||||||
|
{
|
||||||
|
public partial class AddTable_t_limiting_parameter : Migration
|
||||||
|
{
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.CreateTable(
|
||||||
|
name: "t_limiting_parameter",
|
||||||
|
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),
|
||||||
|
date_start = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
|
||||||
|
date_end = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
|
||||||
|
depth_start = table.Column<float>(type: "real", nullable: true),
|
||||||
|
depth_end = table.Column<float>(type: "real", nullable: true),
|
||||||
|
id_feed_regulator = table.Column<short>(type: "smallint", nullable: false)
|
||||||
|
},
|
||||||
|
constraints: table =>
|
||||||
|
{
|
||||||
|
table.PrimaryKey("PK_t_limiting_parameter", x => x.id);
|
||||||
|
table.ForeignKey(
|
||||||
|
name: "FK_t_limiting_parameter_t_telemetry_id_telemetry",
|
||||||
|
column: x => x.id_telemetry,
|
||||||
|
principalTable: "t_telemetry",
|
||||||
|
principalColumn: "id",
|
||||||
|
onDelete: ReferentialAction.Cascade);
|
||||||
|
},
|
||||||
|
comment: "Ограничения по параметрам телеметрии");
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_t_limiting_parameter_id_telemetry",
|
||||||
|
table: "t_limiting_parameter",
|
||||||
|
column: "id_telemetry");
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.DropTable(
|
||||||
|
name: "t_limiting_parameter");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1002,6 +1002,48 @@ namespace AsbCloudDb.Migrations
|
|||||||
b.HasComment("Действия с файлами.");
|
b.HasComment("Действия с файлами.");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("AsbCloudDb.Model.LimitingParameter", 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");
|
||||||
|
|
||||||
|
b.Property<DateTimeOffset?>("DateStart")
|
||||||
|
.HasColumnType("timestamp with time zone")
|
||||||
|
.HasColumnName("date_start");
|
||||||
|
|
||||||
|
b.Property<float?>("DepthEnd")
|
||||||
|
.HasColumnType("real")
|
||||||
|
.HasColumnName("depth_end");
|
||||||
|
|
||||||
|
b.Property<float?>("DepthStart")
|
||||||
|
.HasColumnType("real")
|
||||||
|
.HasColumnName("depth_start");
|
||||||
|
|
||||||
|
b.Property<short>("IdFeedRegulator")
|
||||||
|
.HasColumnType("smallint")
|
||||||
|
.HasColumnName("id_feed_regulator");
|
||||||
|
|
||||||
|
b.Property<int>("IdTelemetry")
|
||||||
|
.HasColumnType("integer")
|
||||||
|
.HasColumnName("id_telemetry");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("IdTelemetry");
|
||||||
|
|
||||||
|
b.ToTable("t_limiting_parameter");
|
||||||
|
|
||||||
|
b.HasComment("Ограничения по параметрам телеметрии");
|
||||||
|
});
|
||||||
|
|
||||||
modelBuilder.Entity("AsbCloudDb.Model.Measure", b =>
|
modelBuilder.Entity("AsbCloudDb.Model.Measure", b =>
|
||||||
{
|
{
|
||||||
b.Property<int>("Id")
|
b.Property<int>("Id")
|
||||||
@ -5937,6 +5979,17 @@ namespace AsbCloudDb.Migrations
|
|||||||
b.Navigation("User");
|
b.Navigation("User");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("AsbCloudDb.Model.LimitingParameter", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("AsbCloudDb.Model.Telemetry", "Telemetry")
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey("IdTelemetry")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.Navigation("Telemetry");
|
||||||
|
});
|
||||||
|
|
||||||
modelBuilder.Entity("AsbCloudDb.Model.Measure", b =>
|
modelBuilder.Entity("AsbCloudDb.Model.Measure", b =>
|
||||||
{
|
{
|
||||||
b.HasOne("AsbCloudDb.Model.MeasureCategory", "Category")
|
b.HasOne("AsbCloudDb.Model.MeasureCategory", "Category")
|
||||||
|
@ -51,6 +51,7 @@ namespace AsbCloudDb.Model
|
|||||||
public virtual DbSet<Schedule> Schedule => Set<Schedule>();
|
public virtual DbSet<Schedule> Schedule => Set<Schedule>();
|
||||||
public virtual DbSet<OperationValue> OperationValues => Set<OperationValue>();
|
public virtual DbSet<OperationValue> OperationValues => Set<OperationValue>();
|
||||||
public virtual DbSet<WellFinalDocument> WellFinalDocuments => Set<WellFinalDocument>();
|
public virtual DbSet<WellFinalDocument> WellFinalDocuments => Set<WellFinalDocument>();
|
||||||
|
public virtual DbSet<LimitingParameter> LimitingParameter => Set<LimitingParameter>();
|
||||||
|
|
||||||
// WITS
|
// WITS
|
||||||
public DbSet<WITS.Record1> Record1 => Set<WITS.Record1>();
|
public DbSet<WITS.Record1> Record1 => Set<WITS.Record1>();
|
||||||
|
@ -51,6 +51,7 @@ namespace AsbCloudDb.Model
|
|||||||
DbSet<Schedule> Schedule { get; }
|
DbSet<Schedule> Schedule { get; }
|
||||||
DbSet<OperationValue> OperationValues { get; }
|
DbSet<OperationValue> OperationValues { get; }
|
||||||
DbSet<WellFinalDocument> WellFinalDocuments { get; }
|
DbSet<WellFinalDocument> WellFinalDocuments { get; }
|
||||||
|
DbSet<LimitingParameter> LimitingParameter { get; }
|
||||||
|
|
||||||
DbSet<Record1> Record1 { get; }
|
DbSet<Record1> Record1 { get; }
|
||||||
DbSet<Record7> Record7 { get; }
|
DbSet<Record7> Record7 { get; }
|
||||||
|
57
AsbCloudDb/Model/LimitingParameter.cs
Normal file
57
AsbCloudDb/Model/LimitingParameter.cs
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using System;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
namespace AsbCloudDb.Model
|
||||||
|
{
|
||||||
|
#nullable disable
|
||||||
|
[Table("t_limiting_parameter"), Comment("Ограничения по параметрам телеметрии")]
|
||||||
|
public class LimitingParameter : IId
|
||||||
|
{
|
||||||
|
[Column("id"), Key]
|
||||||
|
public int Id { get; set; }
|
||||||
|
|
||||||
|
[Column("id_telemetry")]
|
||||||
|
public int IdTelemetry { get; set; }
|
||||||
|
|
||||||
|
[Column("date_start")]
|
||||||
|
public DateTimeOffset? DateStart { get; set; }
|
||||||
|
|
||||||
|
[Column("date_end")]
|
||||||
|
public DateTimeOffset? DateEnd { get; set; }
|
||||||
|
|
||||||
|
[Column("depth_start")]
|
||||||
|
public float? DepthStart { get; set; }
|
||||||
|
|
||||||
|
[Column("depth_end")]
|
||||||
|
public float? DepthEnd { get; set; }
|
||||||
|
|
||||||
|
[Column("id_feed_regulator")]
|
||||||
|
public short IdFeedRegulator { get; set; }
|
||||||
|
|
||||||
|
[JsonIgnore]
|
||||||
|
[ForeignKey(nameof(IdTelemetry))]
|
||||||
|
public virtual Telemetry Telemetry { get; set; } = null!;
|
||||||
|
|
||||||
|
public float? GetDepth(DateTimeOffset date)
|
||||||
|
{
|
||||||
|
var a = (date - DateStart.Value).TotalSeconds;
|
||||||
|
var b = (DateEnd.Value - DateStart.Value).TotalSeconds;
|
||||||
|
var c = DepthEnd - DepthStart;
|
||||||
|
var result = DepthStart + (a / b) * c;
|
||||||
|
return (float?)result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DateTimeOffset? GetDate(double depth)
|
||||||
|
{
|
||||||
|
var a = depth - DepthStart;
|
||||||
|
var b = DepthEnd - DepthStart;
|
||||||
|
var c = (DateEnd.Value - DateStart.Value);
|
||||||
|
var result = DateStart + (a / b) * c;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#nullable disable
|
||||||
|
}
|
@ -97,6 +97,7 @@ namespace AsbCloudInfrastructure
|
|||||||
|
|
||||||
services.AddHostedService<OperationDetectionBackgroundService>();
|
services.AddHostedService<OperationDetectionBackgroundService>();
|
||||||
services.AddHostedService<SubsystemOperationTimeBackgroundService>();
|
services.AddHostedService<SubsystemOperationTimeBackgroundService>();
|
||||||
|
services.AddHostedService<LimitingParameterBackgroundService>();
|
||||||
services.AddSingleton(new WitsInfoService());
|
services.AddSingleton(new WitsInfoService());
|
||||||
services.AddSingleton(new CacheDb());
|
services.AddSingleton(new CacheDb());
|
||||||
services.AddSingleton(new InstantDataRepository());
|
services.AddSingleton(new InstantDataRepository());
|
||||||
@ -132,6 +133,7 @@ namespace AsbCloudInfrastructure
|
|||||||
services.AddTransient<IUserSettingsRepository, UserSettingsRepository>();
|
services.AddTransient<IUserSettingsRepository, UserSettingsRepository>();
|
||||||
services.AddTransient<IWellFinalDocumentsService, WellFinalDocumentsService>();
|
services.AddTransient<IWellFinalDocumentsService, WellFinalDocumentsService>();
|
||||||
services.AddTransient<IFileCategoryService, FileCategoryService>();
|
services.AddTransient<IFileCategoryService, FileCategoryService>();
|
||||||
|
services.AddTransient<ILimitingParameterService, LimitingParameterService>();
|
||||||
|
|
||||||
// admin crud services:
|
// admin crud services:
|
||||||
services.AddTransient<ICrudService<TelemetryDto>, CrudServiceBase<TelemetryDto, Telemetry>>(s =>
|
services.AddTransient<ICrudService<TelemetryDto>, CrudServiceBase<TelemetryDto, Telemetry>>(s =>
|
||||||
@ -163,6 +165,7 @@ namespace AsbCloudInfrastructure
|
|||||||
services.AddTransient<IWellCompositeRepository, WellCompositeRepository>();
|
services.AddTransient<IWellCompositeRepository, WellCompositeRepository>();
|
||||||
services.AddTransient<IUserRoleRepository, UserRoleRepository>();
|
services.AddTransient<IUserRoleRepository, UserRoleRepository>();
|
||||||
services.AddTransient<IUserRepository, UserRepository>();
|
services.AddTransient<IUserRepository, UserRepository>();
|
||||||
|
services.AddTransient<ILimitingParameterRepository, LimitingParameterRepository>();
|
||||||
// Subsystem service
|
// Subsystem service
|
||||||
services.AddTransient<ICrudService<SubsystemDto>, CrudCacheServiceBase<SubsystemDto, Subsystem>>();
|
services.AddTransient<ICrudService<SubsystemDto>, CrudCacheServiceBase<SubsystemDto, Subsystem>>();
|
||||||
services.AddTransient<ISubsystemService, SubsystemService>();
|
services.AddTransient<ISubsystemService, SubsystemService>();
|
||||||
|
122
AsbCloudInfrastructure/Repository/LimitingParameterRepository.cs
Normal file
122
AsbCloudInfrastructure/Repository/LimitingParameterRepository.cs
Normal file
@ -0,0 +1,122 @@
|
|||||||
|
using AsbCloudApp.Data;
|
||||||
|
using AsbCloudApp.Repositories;
|
||||||
|
using AsbCloudApp.Requests;
|
||||||
|
using AsbCloudDb;
|
||||||
|
using AsbCloudDb.Model;
|
||||||
|
using AsbCloudDb.Model.Subsystems;
|
||||||
|
using AsbCloudInfrastructure.Services;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Org.BouncyCastle.Asn1.Ocsp;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace AsbCloudInfrastructure.Repository
|
||||||
|
{
|
||||||
|
#nullable enable
|
||||||
|
public class LimitingParameterRepository : ILimitingParameterRepository
|
||||||
|
{
|
||||||
|
private readonly IAsbCloudDbContext context;
|
||||||
|
|
||||||
|
public LimitingParameterRepository(IAsbCloudDbContext context)
|
||||||
|
{
|
||||||
|
this.context = context;
|
||||||
|
}
|
||||||
|
public async Task<IEnumerable<LimitingParameterDto>> GetInfosAsync(LimitingParameterRequest request, WellDto wellDto, CancellationToken token)
|
||||||
|
{
|
||||||
|
var query = BuildQuery(request, wellDto);
|
||||||
|
|
||||||
|
if (query is null)
|
||||||
|
return Enumerable.Empty<LimitingParameterDto>();
|
||||||
|
|
||||||
|
var data = (await query.ToListAsync(token))
|
||||||
|
.GroupBy(x => x.IdFeedRegulator);
|
||||||
|
|
||||||
|
List<LimitingParameterDto> result = new List<LimitingParameterDto>();
|
||||||
|
foreach (var item in data)
|
||||||
|
{
|
||||||
|
var trimData = TrimLimitingParameters(item, request);
|
||||||
|
|
||||||
|
var allItemDepths = trimData.Sum(x => x.DepthEnd - x.DepthStart);
|
||||||
|
var allItemDates = trimData.Sum(x => {
|
||||||
|
if (x.DateEnd is not null && x.DateStart is not null)
|
||||||
|
return (x.DateEnd.Value - x.DateStart.Value).TotalMinutes;
|
||||||
|
else return default(long);
|
||||||
|
});
|
||||||
|
|
||||||
|
result.Add(new LimitingParameterDto {
|
||||||
|
IdWell = wellDto.Id,
|
||||||
|
IdFeedRegulator = item.Key,
|
||||||
|
NumberInclusions = trimData.Count(),
|
||||||
|
Depth = allItemDepths.HasValue ? allItemDepths.Value : 0,
|
||||||
|
TimeMinutes = (float)allItemDates
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private IEnumerable<LimitingParameter> TrimLimitingParameters(IEnumerable<LimitingParameter> data, LimitingParameterRequest request)
|
||||||
|
{
|
||||||
|
var result = data.Select((x) =>
|
||||||
|
{
|
||||||
|
if (request.GtDate.HasValue && x.DateStart < request.GtDate.Value)
|
||||||
|
{
|
||||||
|
x.DepthStart = x.GetDepth(request.GtDate.Value);
|
||||||
|
x.DateStart = request.GtDate.Value;
|
||||||
|
}
|
||||||
|
if (request.LtDate.HasValue && x.DateEnd > request.LtDate.Value)
|
||||||
|
{
|
||||||
|
x.DepthEnd = x.GetDepth(request.LtDate.Value);
|
||||||
|
x.DateEnd = request.LtDate.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.GtDepth.HasValue && x.DepthStart < request.GtDepth.Value)
|
||||||
|
{
|
||||||
|
x.DateStart = x.GetDate(request.GtDepth.Value);
|
||||||
|
x.DepthStart = (float)request.GtDepth.Value;
|
||||||
|
}
|
||||||
|
if (request.LtDepth.HasValue && x.DepthEnd > request.LtDepth.Value)
|
||||||
|
{
|
||||||
|
x.DateEnd = x.GetDate(request.LtDepth.Value);
|
||||||
|
x.DepthEnd = (float)request.LtDepth.Value;
|
||||||
|
}
|
||||||
|
return x;
|
||||||
|
}).ToList();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private IQueryable<LimitingParameter>? BuildQuery(LimitingParameterRequest request, WellDto wellDto)
|
||||||
|
{
|
||||||
|
var query = context.LimitingParameter
|
||||||
|
.OrderBy(x => x.Id)
|
||||||
|
.Where(x => x.IdTelemetry == wellDto.IdTelemetry)
|
||||||
|
.AsNoTracking();
|
||||||
|
|
||||||
|
if (request.GtDate.HasValue)
|
||||||
|
{
|
||||||
|
var gtDate = request.GtDate.Value.ToUtcDateTimeOffset(wellDto.Timezone.Hours);
|
||||||
|
query = query.Where(x => x.DateEnd >= gtDate);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.LtDate.HasValue)
|
||||||
|
{
|
||||||
|
var ltDate = request.LtDate.Value.ToUtcDateTimeOffset(wellDto.Timezone.Hours);
|
||||||
|
query = query.Where(x => x.DateStart <= ltDate);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.GtDepth.HasValue)
|
||||||
|
query = query.Where(x => x.DepthEnd >= request.GtDepth.Value);
|
||||||
|
|
||||||
|
if (request.LtDepth.HasValue)
|
||||||
|
query = query.Where(x => x.DepthStart <= request.LtDepth.Value);
|
||||||
|
|
||||||
|
return query;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#nullable disable
|
||||||
|
}
|
@ -0,0 +1,178 @@
|
|||||||
|
using AsbCloudDb.Model;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using System;
|
||||||
|
using System.Data.Common;
|
||||||
|
using System.Data;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace AsbCloudInfrastructure.Services
|
||||||
|
{
|
||||||
|
#nullable enable
|
||||||
|
internal class LimitingParameterBackgroundService : BackgroundService
|
||||||
|
{
|
||||||
|
private readonly string connectionString;
|
||||||
|
private readonly TimeSpan period = TimeSpan.FromHours(1);
|
||||||
|
|
||||||
|
public LimitingParameterBackgroundService(IConfiguration configuration)
|
||||||
|
{
|
||||||
|
connectionString = configuration.GetConnectionString("DefaultConnection");
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override async Task ExecuteAsync(CancellationToken token)
|
||||||
|
{
|
||||||
|
var timeToStart = DateTime.Now;
|
||||||
|
var options = new DbContextOptionsBuilder<AsbCloudDbContext>()
|
||||||
|
.UseNpgsql(connectionString)
|
||||||
|
.Options;
|
||||||
|
while (!token.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
if (DateTime.Now > timeToStart)
|
||||||
|
{
|
||||||
|
timeToStart = DateTime.Now + period;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var context = new AsbCloudDbContext(options);
|
||||||
|
var added = await LimitingParameterAsync(context, token);
|
||||||
|
Trace.TraceInformation($"Total limiting parameter complete. Added {added} limiting parameters.");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Trace.TraceError(ex.Message);
|
||||||
|
}
|
||||||
|
GC.Collect();
|
||||||
|
}
|
||||||
|
var ms = (int)(timeToStart - DateTime.Now).TotalMilliseconds;
|
||||||
|
ms = ms > 100 ? ms : 100;
|
||||||
|
await Task.Delay(ms, token).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task<int> LimitingParameterAsync(IAsbCloudDbContext context, CancellationToken token)
|
||||||
|
{
|
||||||
|
var lastDetectedDates = await context.LimitingParameter
|
||||||
|
.GroupBy(o => o.IdTelemetry)
|
||||||
|
.Select(g => new
|
||||||
|
{
|
||||||
|
IdTelemetry = g.Key,
|
||||||
|
LastDate = g.Max(o => o.DateEnd)
|
||||||
|
})
|
||||||
|
.ToListAsync(token);
|
||||||
|
|
||||||
|
var telemetryIds = await context.Telemetries
|
||||||
|
.Where(t => t.Info != null && t.TimeZone != null)
|
||||||
|
.Select(t => t.Id)
|
||||||
|
.ToListAsync(token);
|
||||||
|
|
||||||
|
var telemetryLastDetectedDates = telemetryIds
|
||||||
|
.GroupJoin(lastDetectedDates,
|
||||||
|
t => t,
|
||||||
|
o => o.IdTelemetry,
|
||||||
|
(outer, inner) => new
|
||||||
|
{
|
||||||
|
IdTelemetry = outer,
|
||||||
|
inner.SingleOrDefault()?.LastDate,
|
||||||
|
});
|
||||||
|
|
||||||
|
var affected = 0;
|
||||||
|
foreach (var item in telemetryLastDetectedDates)
|
||||||
|
{
|
||||||
|
var newLimitingParameters = await GetLimitingParameterAsync(item.IdTelemetry, item.LastDate ?? DateTimeOffset.MinValue, context, token);
|
||||||
|
if (newLimitingParameters?.Any() == true)
|
||||||
|
{
|
||||||
|
context.LimitingParameter.AddRange(newLimitingParameters);
|
||||||
|
affected += await context.SaveChangesAsync(token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return affected;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task<IEnumerable<LimitingParameter>> GetLimitingParameterAsync(int idTelemetry, DateTimeOffset begin, IAsbCloudDbContext db, CancellationToken token)
|
||||||
|
{
|
||||||
|
var query =
|
||||||
|
$"select " +
|
||||||
|
$"limiting_parameters.date, limiting_parameters.id_feed_regulator, limiting_parameters.well_depth " +
|
||||||
|
$"from ( " +
|
||||||
|
$"select " +
|
||||||
|
$"date, id_feed_regulator, well_depth, " +
|
||||||
|
$"lag(id_feed_regulator, 1) over (order by date) as id_feed_regulator_lag, " +
|
||||||
|
$"lead(id_feed_regulator, 1) over (order by date) as id_feed_regulator_lead " +
|
||||||
|
$"from t_telemetry_data_saub " +
|
||||||
|
$"where id_telemetry = {idTelemetry}" +
|
||||||
|
$"and date >= '{begin:u}'" +
|
||||||
|
$"order by date) as limiting_parameters " +
|
||||||
|
$"where id_feed_regulator_lag is null " +
|
||||||
|
$"or (id_feed_regulator != id_feed_regulator_lag and id_feed_regulator_lead != id_feed_regulator_lag) " +
|
||||||
|
$"order by date;";
|
||||||
|
|
||||||
|
var rows = new List<(short? IdLimiting, DateTimeOffset Date, float? WellDepth)>(32);
|
||||||
|
{
|
||||||
|
using var result = await ExecuteReaderAsync(db, query, token);
|
||||||
|
int? idLimitingLast = null;
|
||||||
|
while (result.Read())
|
||||||
|
{
|
||||||
|
var idLimiting = result.GetFieldValue<short?>(1);
|
||||||
|
var wellDepth = result.GetFieldValue<float?>(2);
|
||||||
|
if (idLimitingLast != idLimiting)
|
||||||
|
{
|
||||||
|
idLimitingLast = idLimiting;
|
||||||
|
var date = result.GetFieldValue<DateTimeOffset>(0);
|
||||||
|
rows.Add((idLimiting, date, wellDepth));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
await result.DisposeAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rows.Count < 2)
|
||||||
|
return Enumerable.Empty<LimitingParameter>();
|
||||||
|
|
||||||
|
var limitingParameters = new List<LimitingParameter>(32);
|
||||||
|
|
||||||
|
for (int i = 1; i < rows.Count; i++)
|
||||||
|
{
|
||||||
|
var r0 = rows[i - 1];
|
||||||
|
var r1 = rows[i];
|
||||||
|
if (r0.IdLimiting is not null && r0.IdLimiting != r1.IdLimiting)
|
||||||
|
{
|
||||||
|
var limitingParameter = new LimitingParameter()
|
||||||
|
{
|
||||||
|
IdTelemetry = idTelemetry,
|
||||||
|
DateStart = r0.Date,
|
||||||
|
DateEnd = r1.Date,
|
||||||
|
DepthStart = r0.WellDepth,
|
||||||
|
DepthEnd = r1.WellDepth,
|
||||||
|
IdFeedRegulator = (short)r0.IdLimiting
|
||||||
|
};
|
||||||
|
|
||||||
|
limitingParameters.Add(limitingParameter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return limitingParameters;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task<DbDataReader> ExecuteReaderAsync(IAsbCloudDbContext db, string query, CancellationToken token)
|
||||||
|
{
|
||||||
|
var connection = db.Database.GetDbConnection();
|
||||||
|
if (
|
||||||
|
connection?.State is null ||
|
||||||
|
connection.State == ConnectionState.Broken ||
|
||||||
|
connection.State == ConnectionState.Closed)
|
||||||
|
{
|
||||||
|
await db.Database.OpenConnectionAsync(token);
|
||||||
|
connection = db.Database.GetDbConnection();
|
||||||
|
}
|
||||||
|
using var command = connection.CreateCommand();
|
||||||
|
command.CommandText = query;
|
||||||
|
|
||||||
|
var result = await command.ExecuteReaderAsync(token);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#nullable disable
|
||||||
|
}
|
35
AsbCloudInfrastructure/Services/LimitingParameterService.cs
Normal file
35
AsbCloudInfrastructure/Services/LimitingParameterService.cs
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
using AsbCloudApp.Data;
|
||||||
|
using AsbCloudApp.Repositories;
|
||||||
|
using AsbCloudApp.Requests;
|
||||||
|
using AsbCloudApp.Services;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace AsbCloudInfrastructure.Services
|
||||||
|
{
|
||||||
|
#nullable enable
|
||||||
|
public class LimitingParameterService : ILimitingParameterService
|
||||||
|
{
|
||||||
|
private readonly ILimitingParameterRepository limitingParameterRepository;
|
||||||
|
private readonly IWellService wellService;
|
||||||
|
|
||||||
|
public LimitingParameterService(ILimitingParameterRepository limitingParameterRepository,
|
||||||
|
IWellService wellService)
|
||||||
|
{
|
||||||
|
this.limitingParameterRepository = limitingParameterRepository;
|
||||||
|
this.wellService = wellService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IEnumerable<LimitingParameterDto>?> GetInfosAsync(LimitingParameterRequest request, CancellationToken token)
|
||||||
|
{
|
||||||
|
var well = await wellService.GetOrDefaultAsync(request.IdWell, token);
|
||||||
|
if (well?.IdTelemetry is null || well.Timezone is null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
return await limitingParameterRepository.GetInfosAsync(request, well, token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#nullable disable
|
||||||
|
}
|
50
AsbCloudWebApi/Controllers/LimitingParameterController.cs
Normal file
50
AsbCloudWebApi/Controllers/LimitingParameterController.cs
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
using AsbCloudApp.Data;
|
||||||
|
using AsbCloudApp.Requests;
|
||||||
|
using AsbCloudApp.Services;
|
||||||
|
using AsbCloudApp.Services.Subsystems;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Threading;
|
||||||
|
using AsbCloudDb.Model;
|
||||||
|
using AsbCloudInfrastructure.Services;
|
||||||
|
|
||||||
|
namespace AsbCloudWebApi.Controllers
|
||||||
|
{
|
||||||
|
[Route("api/[controller]")]
|
||||||
|
[ApiController]
|
||||||
|
[Authorize]
|
||||||
|
public class LimitingParameterController : ControllerBase
|
||||||
|
{
|
||||||
|
private readonly ILimitingParameterService limitingParameterService;
|
||||||
|
private readonly IWellService wellService;
|
||||||
|
|
||||||
|
public LimitingParameterController(ILimitingParameterService limitingParameterService,
|
||||||
|
IWellService wellService)
|
||||||
|
{
|
||||||
|
this.limitingParameterService = limitingParameterService;
|
||||||
|
this.wellService = wellService;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("info")]
|
||||||
|
[ProducesResponseType(typeof(IEnumerable<LimitingParameterDto>), (int)System.Net.HttpStatusCode.OK)]
|
||||||
|
public async Task<IActionResult> GetInfosAsync([FromQuery] LimitingParameterRequest request, CancellationToken token = default)
|
||||||
|
{
|
||||||
|
if (!await UserHasAccesToWellAsync(request.IdWell, token))
|
||||||
|
return Forbid();
|
||||||
|
var subsystemResult = await limitingParameterService.GetInfosAsync(request, token);
|
||||||
|
return Ok(subsystemResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected async Task<bool> UserHasAccesToWellAsync(int idWell, CancellationToken token)
|
||||||
|
{
|
||||||
|
var idCompany = User.GetCompanyId();
|
||||||
|
if (idCompany is not null &&
|
||||||
|
await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, idWell, token)
|
||||||
|
.ConfigureAwait(false))
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user