[WebApi]歷史資料查詢加入5萬筆限制(超過容易網頁崩潰)

This commit is contained in:
張家睿 2024-08-12 10:57:15 +08:00
parent 08f341ca39
commit 101b6d21f9

View File

@ -1436,6 +1436,35 @@ namespace FrontendWebApi.ApiControllers
inner join variable v2 on temp.device_name_tag COLLATE utf8mb4_unicode_ci = v2.system_value and v2.deleted = 0 and v2.system_type = 'device_system_category_layer3' inner join variable v2 on temp.device_name_tag COLLATE utf8mb4_unicode_ci = v2.system_value and v2.deleted = 0 and v2.system_type = 'device_system_category_layer3'
order by b.priority, v1.system_priority, v2.system_priority, temp.priority;"; order by b.priority, v1.system_priority, v2.system_priority, temp.priority;";
int totalRecordCount = 0;
// 先計算總記錄數
foreach (var hi in input.HistoryItems)
{
var device_number = hi.Device_number_point.Split(":")[0];
var point = hi.Device_number_point.Split(":")[1];
var stationName = await backendRepository.GetOneAsync<string>($"select distinct parent_path from import_niagara_item_history where device_building_tag = '{device_number.Split('_')[0]}' limit 1");
var tableName = await backgroundServiceMsSqlRepository.GetOneAsync<string>($"select table_name from INFORMATION_SCHEMA.TABLES where table_name like '%{stationName}_{device_number}_{point}%'");
if (!string.IsNullOrEmpty(tableName))
{
var sqlCount = $@"select count(*) from {tableName}
where replace(convert(varchar, [timestamp], 111), '/', '-') >= @startTime
and replace(convert(varchar, [timestamp], 111), '/', '-') <= @endTime";
var recordCount = await backgroundServiceMsSqlRepository.GetOneAsync<int>(sqlCount, new { startTime = input.Start_timestamp, endTime = input.End_timestamp });
totalRecordCount += recordCount;
if (totalRecordCount > 50000)
{
// 如果總記錄數超過5萬筆返回提示信息
apiResult.Code = "5000";
apiResult.Msg = "資料量超過5萬筆請減少選擇區間或設備";
return apiResult;
}
}
}
foreach (var hi in input.HistoryItems) foreach (var hi in input.HistoryItems)
{ {
var device_number = hi.Device_number_point.Split(":")[0]; var device_number = hi.Device_number_point.Split(":")[0];