2023-10-08 21:18:59 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Aitex.Core.RT.DataCenter;
|
|
|
|
|
using Aitex.Core.RT.DBCore;
|
|
|
|
|
using Aitex.Core.RT.Log;
|
|
|
|
|
using Aitex.Core.RT.SCCore;
|
|
|
|
|
using Aitex.Core.Util;
|
|
|
|
|
using MECF.Framework.Common.Equipment;
|
|
|
|
|
|
|
|
|
|
namespace Aitex.Core.RT.DataCollection.HighPerformance
|
2024-01-07 11:29:22 +08:00
|
|
|
|
{
|
2023-10-08 21:18:59 +08:00
|
|
|
|
|
|
|
|
|
public class DataRecorderManager : Singleton<DataRecorderManager>
|
|
|
|
|
{
|
|
|
|
|
#region Variables
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 当前记录器支持的数据类型。
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly Type[] _supportedDataTypes =
|
|
|
|
|
{ typeof(bool), typeof(double), typeof(float), typeof(int), typeof(ushort), typeof(short) };
|
|
|
|
|
|
2024-01-07 11:29:22 +08:00
|
|
|
|
|
2023-10-08 21:18:59 +08:00
|
|
|
|
private readonly object _lock = new();
|
|
|
|
|
private readonly int _dataRecorderInterval;
|
|
|
|
|
private readonly int _dataRecorderCacheSize;
|
|
|
|
|
private readonly int _dataRecorderCachePeriodMs;
|
2024-01-07 11:29:22 +08:00
|
|
|
|
private string[] _dataTableCategory = { ModuleName.System.ToString() };
|
2023-10-08 21:18:59 +08:00
|
|
|
|
private readonly Dictionary<string, Func<object>> _subscribedRecordedDataGetter = new();
|
2024-01-07 11:29:22 +08:00
|
|
|
|
private readonly CancellationTokenSource _ctsDataRecorderThread;
|
|
|
|
|
private readonly CancellationToken _ctDataRecorderThread;
|
2024-01-07 11:45:22 +08:00
|
|
|
|
private Dictionary<string, DataRecorderCache> _dictDataRecCachePerDataTable;
|
|
|
|
|
private DateTime _dateDataTable;
|
2023-10-08 21:18:59 +08:00
|
|
|
|
private IDataRecorderCallback _callback;
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Constructors
|
|
|
|
|
|
|
|
|
|
public DataRecorderManager()
|
|
|
|
|
{
|
|
|
|
|
_dataRecorderInterval = (SC.ContainsItem("System.DataRecorderInterval")
|
|
|
|
|
? SC.GetValue<int>("System.DataCollectionInterval")
|
|
|
|
|
: 1000);
|
|
|
|
|
|
|
|
|
|
_dataRecorderCacheSize = (SC.ContainsItem("System.DataRecorderCacheSize")
|
|
|
|
|
? SC.GetValue<int>("System.DataRecorderCacheSize")
|
|
|
|
|
: DataRecorderCache.MIN_CACHE_SIZE);
|
|
|
|
|
|
|
|
|
|
_dataRecorderCachePeriodMs = (SC.ContainsItem("System.DataRecorderCachePeriod")
|
|
|
|
|
? SC.GetValue<int>("System.DataRecorderCachePeriod")
|
|
|
|
|
: DataRecorderCache.MIN_CACHE_PERIOD_MS);
|
2024-01-07 11:29:22 +08:00
|
|
|
|
|
|
|
|
|
_ctsDataRecorderThread = new();
|
|
|
|
|
_ctDataRecorderThread = _ctsDataRecorderThread.Token;
|
2023-10-08 21:18:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Methods
|
|
|
|
|
|
2024-01-07 11:29:22 +08:00
|
|
|
|
public void Initialize(string[] dataTableCategory)
|
2023-10-08 21:18:59 +08:00
|
|
|
|
{
|
2024-01-07 11:29:22 +08:00
|
|
|
|
_dataTableCategory = dataTableCategory;
|
|
|
|
|
Task.Run(DataRecorderThread, _ctDataRecorderThread);
|
2023-10-08 21:18:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 立即缓存所有模组的数据。
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void ImmediateCache()
|
|
|
|
|
{
|
|
|
|
|
DoCache();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DataRecorderThread()
|
|
|
|
|
{
|
2024-01-07 11:29:22 +08:00
|
|
|
|
try
|
2023-10-08 21:18:59 +08:00
|
|
|
|
{
|
2024-01-07 11:29:22 +08:00
|
|
|
|
while (true)
|
2023-10-08 21:18:59 +08:00
|
|
|
|
{
|
2024-01-07 11:29:22 +08:00
|
|
|
|
Thread.Sleep(_dataRecorderInterval);
|
|
|
|
|
|
|
|
|
|
_ctDataRecorderThread.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
|
2023-10-08 21:18:59 +08:00
|
|
|
|
CreateCache();
|
|
|
|
|
|
|
|
|
|
// 开始周期性数据采集
|
|
|
|
|
var lastFlushTime = DateTime.Now;
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
2024-01-07 11:29:22 +08:00
|
|
|
|
_ctDataRecorderThread.ThrowIfCancellationRequested();
|
|
|
|
|
|
2023-10-08 21:18:59 +08:00
|
|
|
|
Thread.Sleep((int)(_dataRecorderInterval * 0.99));
|
|
|
|
|
|
|
|
|
|
// 跨天,退出循环并创建新表
|
2024-01-07 11:45:22 +08:00
|
|
|
|
if (DateTime.Now.Date != _dateDataTable)
|
2023-10-08 21:18:59 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// 立即缓存一次数据
|
|
|
|
|
DoCache();
|
|
|
|
|
|
|
|
|
|
// 每隔5秒钟将缓存写入数据库
|
|
|
|
|
if (lastFlushTime.AddSeconds(5) < DateTime.Now)
|
|
|
|
|
{
|
|
|
|
|
var ret = Persist();
|
|
|
|
|
lastFlushTime = DateTime.Now;
|
|
|
|
|
if (ret < 0) // 插入数据错误,退出
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-07 11:29:22 +08:00
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
|
|
|
|
LOG.Info($"{nameof(DataRecorderManager)}.{nameof(DataRecorderThread)} terminated.");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
LOG.Write(ex, $"{nameof(DataRecorderManager)}.{nameof(DataRecorderThread)}线程意外终止,{ex.Message}");
|
2023-10-08 21:18:59 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 扫描<see cref="DataManager"/>,获取所有可记录的数据。
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// 仅数据管理器中注册的数据,且类型为<see cref="bool"/>、<see cref="double"/>、<see cref="float"/>、<see cref="int"/>、<see cref="ushort"/>、<see cref="short"/>的数据支持保存到数据库。
|
|
|
|
|
/// </remarks>>
|
|
|
|
|
private void GetRecordableDataSource()
|
|
|
|
|
{
|
|
|
|
|
var dBRecorderList = DATA.GetDBRecorderList();
|
|
|
|
|
foreach (var item in dBRecorderList)
|
|
|
|
|
{
|
|
|
|
|
if (_subscribedRecordedDataGetter.ContainsKey(item.Key))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var obj = item.Value();
|
|
|
|
|
if (obj != null)
|
|
|
|
|
{
|
|
|
|
|
var type = obj.GetType();
|
|
|
|
|
if (type == typeof(bool) || type == typeof(double) || type == typeof(float) ||
|
|
|
|
|
type == typeof(int) || type == typeof(ushort) || type == typeof(short))
|
|
|
|
|
{
|
|
|
|
|
_subscribedRecordedDataGetter[item.Key] = item.Value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 创建数据缓存对象。
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void CreateCache()
|
|
|
|
|
{
|
|
|
|
|
lock (_lock)
|
|
|
|
|
{
|
|
|
|
|
GetRecordableDataSource();
|
|
|
|
|
|
2024-01-07 11:45:22 +08:00
|
|
|
|
_dictDataRecCachePerDataTable = new();
|
|
|
|
|
Dictionary<string, List<DataHolder>> dictDataHolderPerDataTable = new();
|
2023-10-08 21:18:59 +08:00
|
|
|
|
|
2024-01-07 11:29:22 +08:00
|
|
|
|
var defaultModuleName = (_dataTableCategory.Contains(ModuleName.System.ToString())
|
2023-10-08 21:18:59 +08:00
|
|
|
|
? ModuleName.System.ToString()
|
2024-01-07 11:29:22 +08:00
|
|
|
|
: (_dataTableCategory.Contains("Data") ? "Data" : _dataTableCategory[0]));
|
2023-10-08 21:18:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (_subscribedRecordedDataGetter is { Count: > 0 })
|
|
|
|
|
{
|
|
|
|
|
foreach (var dataName in _subscribedRecordedDataGetter.Keys)
|
|
|
|
|
{
|
|
|
|
|
var isAvailableModuleName = false;
|
2024-01-07 11:29:22 +08:00
|
|
|
|
foreach (var categoryName in _dataTableCategory)
|
2023-10-08 21:18:59 +08:00
|
|
|
|
{
|
2024-01-07 11:29:22 +08:00
|
|
|
|
if (dataName.StartsWith(categoryName + ".") ||
|
|
|
|
|
dataName.StartsWith("IO." + categoryName + "."))
|
2023-10-08 21:18:59 +08:00
|
|
|
|
{
|
2024-01-07 11:45:22 +08:00
|
|
|
|
if (!dictDataHolderPerDataTable.ContainsKey(categoryName))
|
|
|
|
|
dictDataHolderPerDataTable[categoryName] = new List<DataHolder>();
|
2023-10-08 21:18:59 +08:00
|
|
|
|
|
2024-01-07 11:45:22 +08:00
|
|
|
|
dictDataHolderPerDataTable[categoryName].Add(new DataHolder(dictDataHolderPerDataTable[categoryName].Count,
|
2023-10-08 21:18:59 +08:00
|
|
|
|
dataName, _subscribedRecordedDataGetter[dataName]));
|
|
|
|
|
isAvailableModuleName = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isAvailableModuleName)
|
|
|
|
|
{
|
|
|
|
|
|
2024-01-07 11:45:22 +08:00
|
|
|
|
if (!dictDataHolderPerDataTable.ContainsKey(defaultModuleName))
|
|
|
|
|
dictDataHolderPerDataTable[defaultModuleName] = new List<DataHolder>();
|
2023-10-08 21:18:59 +08:00
|
|
|
|
|
2024-01-07 11:45:22 +08:00
|
|
|
|
dictDataHolderPerDataTable[defaultModuleName].Add(new DataHolder(
|
|
|
|
|
dictDataHolderPerDataTable[defaultModuleName].Count,
|
2023-10-08 21:18:59 +08:00
|
|
|
|
dataName, _subscribedRecordedDataGetter[dataName]));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-07 11:45:22 +08:00
|
|
|
|
_dateDataTable = DateTime.Now.Date;
|
|
|
|
|
foreach (var category in dictDataHolderPerDataTable.Keys)
|
2023-10-08 21:18:59 +08:00
|
|
|
|
{
|
2024-01-07 11:45:22 +08:00
|
|
|
|
var holders = dictDataHolderPerDataTable[category];
|
|
|
|
|
var tableName = $"{_dateDataTable:yyyyMMdd}.{category}";
|
|
|
|
|
_dictDataRecCachePerDataTable[category] =
|
|
|
|
|
new DataRecorderCache(tableName, category, holders, minCachePeriodMs: _dataRecorderCachePeriodMs,
|
2023-10-08 21:18:59 +08:00
|
|
|
|
maxCacheSize: _dataRecorderCacheSize);
|
2024-01-07 11:29:22 +08:00
|
|
|
|
|
2024-01-07 11:45:22 +08:00
|
|
|
|
UpdateTableSchema(tableName, holders);
|
2023-10-08 21:18:59 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 立即缓存所有模组的数据。
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private void DoCache()
|
|
|
|
|
{
|
|
|
|
|
lock (_lock)
|
|
|
|
|
{
|
|
|
|
|
// DO调用ForceCache方法可能发生在当前对象初始化之前,此时_dictCachePerModule为null
|
2024-01-07 11:45:22 +08:00
|
|
|
|
if (_dictDataRecCachePerDataTable == null)
|
2023-10-08 21:18:59 +08:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 立即缓存一次数据
|
2024-01-07 11:45:22 +08:00
|
|
|
|
foreach (var drc in _dictDataRecCachePerDataTable.Values)
|
2023-10-08 21:18:59 +08:00
|
|
|
|
drc.Cache();
|
|
|
|
|
}
|
2024-01-07 11:29:22 +08:00
|
|
|
|
|
2023-10-08 21:18:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 缓存写入数据库。
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>插入数据库的行数。-1 表示写入数据库错误。</returns>
|
|
|
|
|
public int Persist()
|
|
|
|
|
{
|
|
|
|
|
var num = 0;
|
|
|
|
|
|
|
|
|
|
lock (_lock)
|
|
|
|
|
{
|
2024-01-07 11:45:22 +08:00
|
|
|
|
foreach (var dcc in _dictDataRecCachePerDataTable.Values)
|
2023-10-08 21:18:59 +08:00
|
|
|
|
{
|
|
|
|
|
var sql = dcc.GetInsertSql();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(sql))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
num = DB.ExecuteNonQuery(sql);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
LOG.Write(ex, $"{ModuleName.System.ToString()}写入数据库发生异常");
|
|
|
|
|
num = -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return num;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-07 11:29:22 +08:00
|
|
|
|
public void Terminate()
|
|
|
|
|
{
|
|
|
|
|
_ctsDataRecorderThread.Cancel();
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-07 11:45:22 +08:00
|
|
|
|
private bool UpdateTableSchema(string tblName, IEnumerable<DataHolder> holders)
|
2023-10-08 21:18:59 +08:00
|
|
|
|
{
|
|
|
|
|
var cmdText = $"select column_name from information_schema.columns where table_name = '{tblName}';";
|
|
|
|
|
var reader = DB.ExecuteReader(cmdText);
|
|
|
|
|
var sqlExprAddColumn = string.Empty;
|
|
|
|
|
var existedColumns = new List<string>();
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
for (var i = 0; i < reader.FieldCount; i++)
|
|
|
|
|
{
|
|
|
|
|
existedColumns.Add(reader[i].ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reader.Close();
|
|
|
|
|
if (existedColumns.Count > 0)
|
|
|
|
|
{
|
2024-01-07 11:45:22 +08:00
|
|
|
|
foreach (var dataItem in holders)
|
2023-10-08 21:18:59 +08:00
|
|
|
|
{
|
|
|
|
|
var colName = dataItem.Name;
|
|
|
|
|
if (!existedColumns.Contains(colName))
|
|
|
|
|
{
|
|
|
|
|
var type = dataItem.Read().GetType();
|
|
|
|
|
if (type == typeof(bool))
|
|
|
|
|
{
|
|
|
|
|
sqlExprAddColumn += $"ALTER TABLE \"{tblName}\" ADD COLUMN \"{colName}\" {"Boolean"};";
|
|
|
|
|
}
|
|
|
|
|
else if (_supportedDataTypes.Contains(type))
|
|
|
|
|
{
|
|
|
|
|
sqlExprAddColumn += $"ALTER TABLE \"{tblName}\" ADD COLUMN \"{colName}\" {"Real"};";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(sqlExprAddColumn))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
DB.ExecuteNonQuery(sqlExprAddColumn);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
LOG.Write(ex);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var sqlExprCreateTable = $"CREATE TABLE \"{tblName}\"(Time bigint NOT NULL,";
|
2024-01-07 11:45:22 +08:00
|
|
|
|
foreach (var dataItem in holders)
|
2023-10-08 21:18:59 +08:00
|
|
|
|
{
|
|
|
|
|
var colName = dataItem.Name;
|
|
|
|
|
var dataType = dataItem.Read().GetType();
|
|
|
|
|
if (dataType == typeof(bool))
|
|
|
|
|
{
|
|
|
|
|
sqlExprCreateTable += $"\"{colName}\" Boolean,\n";
|
|
|
|
|
}
|
|
|
|
|
else if (_supportedDataTypes.Contains(dataType))
|
|
|
|
|
{
|
|
|
|
|
sqlExprCreateTable += $"\"{colName}\" Real,\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sqlExprCreateTable += $"CONSTRAINT \"{tblName}_pkey\" PRIMARY KEY (Time));";
|
|
|
|
|
sqlExprCreateTable += $"GRANT SELECT ON TABLE \"{tblName}\" TO postgres;";
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
DB.ExecuteNonQuery(sqlExprCreateTable.ToString());
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex2)
|
|
|
|
|
{
|
|
|
|
|
LOG.Write(ex2, "创建数据库表格失败");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-07 11:29:22 +08:00
|
|
|
|
#endregion
|
2023-10-08 21:18:59 +08:00
|
|
|
|
|
2024-01-07 11:29:22 +08:00
|
|
|
|
}
|
2023-10-08 21:18:59 +08:00
|
|
|
|
}
|