Merge branch 'master' into dev

This commit is contained in:
ngfrolov 2022-12-20 11:58:05 +05:00
commit ba0eaa8014
6 changed files with 50 additions and 20 deletions

View File

@ -60,27 +60,27 @@ namespace AsbCloudDb.Model
public DbSet<WITS.Record60> Record60 => Set<WITS.Record60>();
public DbSet<WITS.Record61> Record61 => Set<WITS.Record61>();
public static int ReferenceCount { get; private set; }
private static int referenceCount;
public static int ReferenceCount => referenceCount;
public AsbCloudDbContext() : base()
{
ReferenceCount++;
Interlocked.Increment(ref referenceCount);
}
public AsbCloudDbContext(DbContextOptions<AsbCloudDbContext> options)
: base(options)
{
ReferenceCount++;
Interlocked.Increment(ref referenceCount);
}
~AsbCloudDbContext()
{
ReferenceCount--;
Interlocked.Decrement(ref referenceCount);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
{
if (!optionsBuilder.IsConfigured)
optionsBuilder.UseNpgsql("Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True"
//, builder=>builder.EnableRetryOnFailure(2, System.TimeSpan.FromMinutes(1))

View File

@ -98,19 +98,33 @@ namespace AsbCloudInfrastructure.Services.DetectOperations.Detectors
protected abstract double CalcValue(DetectableTelemetry[] telemetry, int begin, int end);
public static InterpolationLine MakeInterpolationLine(
/// <summary>
/// Среднее арифметическое
/// </summary>
/// <param name="yGetter"></param>
/// <param name="telemetry"></param>
/// <param name="begin"></param>
/// <param name="fragmentLength"></param>
/// <returns></returns>
public static double CalcAvgAppr(
Func<DetectableTelemetry, double> yGetter,
DetectableTelemetry[] telemetry,
int begin,
int fragmentLength)
{
DetectableTelemetry[] data;
if (fragmentLength > 0 && (begin + fragmentLength) < telemetry.Length)
data = telemetry[begin..(begin + fragmentLength)];
else
data = telemetry[begin..];
var line = new InterpolationLine(data.Select(d => (yGetter(d), (d.DateTime - telemetry[0].DateTime).TotalHours)));
return line;
var end = begin + fragmentLength;
end = end < telemetry.Length
? end
: telemetry.Length;
var subData = telemetry[begin..end].Select(yGetter);
if (end - begin > 10)
{
var ratio = (end - begin) / 5;
subData = subData.Where((_,i) => i % ratio > 0);
}
var avg = subData.Average();
return avg;
}
/// <summary>

View File

@ -1,4 +1,5 @@
using AsbCloudDb.Model;
using System.Linq;
namespace AsbCloudInfrastructure.Services.DetectOperations.Detectors
{
@ -38,9 +39,9 @@ namespace AsbCloudInfrastructure.Services.DetectOperations.Detectors
if (point0.Pressure < 25)
return IdReasonOfEnd_PressureIsLo;
var lineRotorSpeed = MakeInterpolationLine(d => d.RotorSpeed, telemetry, position, 60);
var avgRotorSpeed = CalcAvgAppr(d => d.RotorSpeed, telemetry, position, 60);
if (lineRotorSpeed.IsAverageYLessThan(10))
if (avgRotorSpeed < 10)
return IdReasonOfEnd_AvgRotorSpeedIsLo;
if (!DeviatesFromBegin(telemetry, t => t.WellDepth, position, 150, 0.003))

View File

@ -38,9 +38,9 @@ namespace AsbCloudInfrastructure.Services.DetectOperations.Detectors
if (point0.Pressure < 25)
return IdReasonOfEnd_PressureIsLo;
var lineRotorSpeed = MakeInterpolationLine(d => d.RotorSpeed, telemetry, position, 60);
var avgRotorSpeed = CalcAvgAppr(d => d.RotorSpeed, telemetry, position, 60);
if (lineRotorSpeed.IsAverageYGreaterThan(10))
if (avgRotorSpeed > 10)
return IdReasonOfEnd_AvgRotorSpeedIsHi;
if (!DeviatesFromBegin(telemetry, t => t.WellDepth, position, 150, 0.003))

View File

@ -17,6 +17,7 @@ namespace AsbCloudInfrastructure.Services.DetectOperations
{
private const string workId = "Operation detection";
private static readonly TimeSpan workPeriod = TimeSpan.FromMinutes(30);
private static string progress = "no progress";
private static readonly DetectorAbstract[] detectors = new DetectorAbstract[]
{
@ -34,7 +35,13 @@ namespace AsbCloudInfrastructure.Services.DetectOperations
public static WorkPeriodic MakeWork()
{
var workPeriodic = new WorkPeriodic(workId, WorkAction, workPeriod);
workPeriodic.Timeout = TimeSpan.FromMinutes(30);
workPeriodic.Timeout = TimeSpan.FromSeconds(15 * 60);
workPeriodic.OnErrorAsync = (id, exception, token) =>
{
var text = $"work {id}, when {progress}, throw error:{exception.Message}";
Trace.TraceWarning(text);
return Task.CompletedTask;
};
return workPeriodic;
}
@ -86,6 +93,7 @@ namespace AsbCloudInfrastructure.Services.DetectOperations
var query = db.TelemetryDataSaub
.AsNoTracking()
.Where(d => d.IdTelemetry == idTelemetry)
.Where(d => d.BlockPosition >= 0)
.Select(d => new DetectableTelemetry
{
DateTime = d.DateTime,
@ -120,20 +128,26 @@ namespace AsbCloudInfrastructure.Services.DetectOperations
var isDetected = false;
var positionBegin = 0;
var positionEnd = data.Length - gap;
var step = 10;
while (positionEnd > positionBegin)
{
step ++;
for (int i = 0; i < detectors.Length; i++)
{
progress = $"telemetry:{idTelemetry}, date:{startDate}, pos:{positionBegin}, detector:{detectors[i]}";
if (detectors[i].TryDetect(idTelemetry, data, positionBegin, positionEnd, lastDetectedOperation, out OperationDetectorResult? result))
{
detectedOperations.Add(result!.Operation);
lastDetectedOperation = result.Operation;
isDetected = true;
step = 1;
positionBegin = result.TelemetryEnd;
break;
}
}
positionBegin++;
if (step > 20)
step = 10;
positionBegin += step;
}
if (isDetected)

View File

@ -38,6 +38,7 @@ namespace AsbCloudInfrastructure.Services.SAUB
{
using var db = MakeContext(configuration);
instance.InitializeCacheFromDB<TEntity>(db);
db.Dispose();
});
}
return instance;