Compare commits

..

3 Commits

Author SHA1 Message Date
Diego
dcba7b9810 10.7.50 2025-06-11 21:50:46 +08:00
Diego
7f1a741ce6 优化sql实时表执行性能 2025-06-11 21:39:14 +08:00
Diego
ca2b17d433 10.7.47 2025-06-11 17:08:19 +08:00
7 changed files with 53 additions and 75 deletions

View File

@@ -1,8 +1,8 @@
<Project>
<PropertyGroup>
<PluginVersion>10.7.46</PluginVersion>
<ProPluginVersion>10.7.46</ProPluginVersion>
<PluginVersion>10.7.50</PluginVersion>
<ProPluginVersion>10.7.50</ProPluginVersion>
<AuthenticationVersion>2.5.0</AuthenticationVersion>
<NET8Version>8.0.17</NET8Version>
<NET9Version>9.0.6</NET9Version>

View File

@@ -220,6 +220,23 @@ public class ControlController : ControllerBase
{
return GlobalData.VariableRuntimeService.InsertTestDataAsync(testVariableCount, testDeviceCount, slaveUrl, businessEnable, restart, default);
}
/// <summary>
/// 确认实时报警
/// </summary>
/// <returns></returns>
[HttpPost("checkRealAlarm")]
[RequestAudit]
[DisplayName("确认实时报警")]
public async Task CheckRealAlarm(long variableId)
{
if (GlobalData.ReadOnlyRealAlarmIdVariables.TryGetValue(variableId, out var variable))
{
await GlobalData.SysUserService.CheckApiDataScopeAsync(variable.CreateOrgId, variable.CreateUserId).ConfigureAwait(false);
GlobalData.AlarmHostedService.ConfirmAlarm(variableId);
}
}
}
public class ChannelInput
{

View File

@@ -34,7 +34,7 @@ public class RuntimeInfoController : ControllerBase
/// <returns></returns>
[HttpGet("channelList")]
[DisplayName("获取通道信息")]
public async Task<SqlSugarPagedList<ChannelRuntime>> GetChannelListAsync(ChannelPageInput input)
public async Task<SqlSugarPagedList<ChannelRuntime>> GetChannelListAsync([FromQuery] ChannelPageInput input)
{
var channelRuntimes = await GlobalData.GetCurrentUserChannels().ConfigureAwait(false);
@@ -54,7 +54,7 @@ public class RuntimeInfoController : ControllerBase
/// <returns></returns>
[HttpGet("deviceList")]
[DisplayName("获取设备信息")]
public async Task<SqlSugarPagedList<DeviceRuntime>> GetDeviceListAsync(DevicePageInput input)
public async Task<SqlSugarPagedList<DeviceRuntime>> GetDeviceListAsync([FromQuery] DevicePageInput input)
{
var deviceRuntimes = await GlobalData.GetCurrentUserDevices().ConfigureAwait(false);
var data = deviceRuntimes
@@ -72,7 +72,7 @@ public class RuntimeInfoController : ControllerBase
/// <returns></returns>
[HttpGet("realAlarmList")]
[DisplayName("获取实时报警变量信息")]
public async Task<SqlSugarPagedList<AlarmVariable>> GetRealAlarmList(AlarmVariablePageInput input)
public async Task<SqlSugarPagedList<AlarmVariable>> GetRealAlarmList([FromQuery] AlarmVariablePageInput input)
{
var realAlarmVariables = await GlobalData.GetCurrentUserRealAlarmVariables().ConfigureAwait(false);
@@ -84,29 +84,13 @@ public class RuntimeInfoController : ControllerBase
return data;
}
/// <summary>
/// 确认实时报警
/// </summary>
/// <returns></returns>
[HttpPost("checkRealAlarm")]
[RequestAudit]
[DisplayName("确认实时报警")]
public async Task CheckRealAlarm(long variableId)
{
if (GlobalData.ReadOnlyRealAlarmIdVariables.TryGetValue(variableId, out var variable))
{
await GlobalData.SysUserService.CheckApiDataScopeAsync(variable.CreateOrgId, variable.CreateUserId).ConfigureAwait(false);
GlobalData.AlarmHostedService.ConfirmAlarm(variableId);
}
}
/// <summary>
/// 获取变量信息
/// </summary>
/// <returns></returns>
[HttpGet("variableList")]
[DisplayName("获取变量信息")]
public async Task<SqlSugarPagedList<VariableRuntime>> GetVariableList(VariablePageInput input)
public async Task<SqlSugarPagedList<VariableRuntime>> GetVariableList([FromQuery] VariablePageInput input)
{
var variables = await GlobalData.GetCurrentUserIdVariables().ConfigureAwait(false);
var data = variables
@@ -144,7 +128,7 @@ public class RuntimeInfoController : ControllerBase
/// </summary>
[HttpGet("getPluginInfos")]
[DisplayName("获取插件")]
public SqlSugarPagedList<PluginInfo> GetPluginInfos(PluginInfoPageInput input)
public SqlSugarPagedList<PluginInfo> GetPluginInfos([FromQuery] PluginInfoPageInput input)
{
//指定关键词搜索为插件FullName
return GlobalData.PluginService.GetList().WhereIF(!input.Name.IsNullOrWhiteSpace(), a => a.Name == input.Name)

View File

@@ -164,12 +164,15 @@ public partial class SqlDBProducer : BusinessBaseWithCacheIntervalVariableModel<
.Map(dest => dest.Value, src => src.Value == null ? string.Empty : src.Value.ToString() ?? string.Empty)
.Map(dest => dest.CreateTime, (src) => DateTime.Now);
_exRealTimerTick = new(_driverPropertys.RealTableBusinessInterval);
await base.InitChannelAsync(channel, cancellationToken).ConfigureAwait(false);
}
public override Task AfterVariablesChangedAsync(CancellationToken cancellationToken)
{
RealTimeVariables.Clear();
_initRealData = false;
return base.AfterVariablesChangedAsync(cancellationToken);
}
protected override async Task ProtectedStartAsync(CancellationToken cancellationToken)
{
@@ -215,42 +218,25 @@ public partial class SqlDBProducer : BusinessBaseWithCacheIntervalVariableModel<
{
if (_driverPropertys.IsReadDB)
{
if (_exRealTimerTick.IsTickHappen())
try
{
try
var varList = RealTimeVariables.ToListWithDequeue();
if (varList.Count > 0)
{
var varList = IdVariableRuntimes.Select(a => a.Value);
if (_driverPropertys.GroupUpdate)
var result = await UpdateAsync(varList.Adapt<List<SQLRealValue>>(), cancellationToken).ConfigureAwait(false);
if (success != result.IsSuccess)
{
var groups = varList.GroupBy(a => a.BusinessGroup);
foreach (var item in groups)
{
var result = await UpdateAsync(item.Adapt<List<SQLRealValue>>(), cancellationToken).ConfigureAwait(false);
if (success != result.IsSuccess)
{
if (!result.IsSuccess)
LogMessage?.LogWarning(result.ToString());
success = result.IsSuccess;
}
}
}
else
{
var result = await UpdateAsync(varList.Adapt<List<SQLRealValue>>(), cancellationToken).ConfigureAwait(false);
if (success != result.IsSuccess)
{
if (!result.IsSuccess)
LogMessage?.LogWarning(result.ToString());
success = result.IsSuccess;
}
if (!result.IsSuccess)
LogMessage?.LogWarning(result.ToString());
success = result.IsSuccess;
}
}
catch (Exception ex)
{
if (success)
LogMessage?.LogWarning(ex);
success = false;
}
}
catch (Exception ex)
{
if (success)
LogMessage?.LogWarning(ex);
success = false;
}
}

View File

@@ -58,13 +58,6 @@ public class SqlDBProducerProperty : BusinessPropertyWithCacheInterval
[AutoGenerateColumn(ComponentType = typeof(Textarea), Rows = 1)]
public string BigTextConnectStr { get; set; } = "server=.;uid=sa;pwd=111111;database=test;";
/// <summary>
/// 实时表间隔上传时间
/// </summary>
[DynamicProperty]
public virtual string RealTableBusinessInterval { get; set; } = "3000";
/// <summary>
/// 实时表脚本
/// </summary>

View File

@@ -10,11 +10,11 @@
using Mapster;
using System.Collections.Concurrent;
using System.Diagnostics;
using ThingsGateway.Extension.Generic;
using ThingsGateway.Foundation;
using ThingsGateway.NewLife;
using ThingsGateway.Plugin.DB;
using TouchSocket.Core;
@@ -27,8 +27,8 @@ namespace ThingsGateway.Plugin.SqlDB;
public partial class SqlDBProducer : BusinessBaseWithCacheIntervalVariableModel<SQLHistoryValue>
{
private TypeAdapterConfig _config;
private TimeTick _exRealTimerTick;
private volatile bool _initRealData;
private ConcurrentDictionary<long, VariableBasicData> RealTimeVariables { get; } = new ConcurrentDictionary<long, VariableBasicData>();
protected override ValueTask<OperResult> UpdateVarModel(IEnumerable<CacheDBItem<SQLHistoryValue>> item, CancellationToken cancellationToken)
{
@@ -93,6 +93,11 @@ public partial class SqlDBProducer : BusinessBaseWithCacheIntervalVariableModel<
AddQueueVarModel(new CacheDBItem<SQLHistoryValue>(variableRuntime.Adapt<SQLHistoryValue>(_config)));
}
}
if (_driverPropertys.IsReadDB)
{
RealTimeVariables.AddOrUpdate(variable.Id, variable, (key, oldValue) => variable);
}
}
@@ -169,25 +174,18 @@ public partial class SqlDBProducer : BusinessBaseWithCacheIntervalVariableModel<
if (!_initRealData)
{
if (datas?.Count > 0)
{
Stopwatch stopwatch = new();
stopwatch.Start();
var ids = (await db.Queryable<SQLRealValue>().AS(_driverPropertys.ReadDBTableName).Select(a => a.Id).ToListAsync(cancellationToken).ConfigureAwait(false)).ToHashSet();
var InsertData = datas.Where(a => !ids.Contains(a.Id)).ToList();
var InsertData = IdVariableRuntimes.Where(a => !ids.Contains(a.Key)).Select(a=>a.Value).Adapt<List<SQLRealValue>>();
var result = await db.Fastest<SQLRealValue>().AS(_driverPropertys.ReadDBTableName).PageSize(100000).BulkCopyAsync(InsertData).ConfigureAwait(false);
//var result = await db.Storageable(datas).As(_driverPropertys.ReadDBTableName).PageSize(5000).ExecuteSqlBulkCopyAsync(cancellationToken).ConfigureAwait(false);
_initRealData = true;
stopwatch.Stop();
if (result > 0)
{
LogMessage?.Trace($"RealTable Insert Data Count{result}watchTime: {stopwatch.ElapsedMilliseconds} ms");
}
return OperResult.Success;
}
return OperResult.Success;
}
else
{
if (datas?.Count > 0)
{

View File

@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>10.7.46</Version>
<Version>10.7.50</Version>
</PropertyGroup>
<ItemGroup>