[BackendWorkService]新增日歸檔的最後歸檔日期判斷,月歸檔會從最後歸檔日期開始補做的邏輯

This commit is contained in:
張家睿 2024-02-22 16:53:33 +08:00
parent e91066df9c
commit 30aa417f78
2 changed files with 216 additions and 130 deletions

View File

@ -82,6 +82,7 @@ namespace Backend.Models
public List<DeviceDisaster> Device_disasters { get; set; } //防災類型 public List<DeviceDisaster> Device_disasters { get; set; } //防災類型
public List<DeviceNode> Device_nodes { get; set; } //設備子節點 public List<DeviceNode> Device_nodes { get; set; } //設備子節點
public System.DateTime created_at { get; set; }
/// <summary> /// <summary>
/// 前次成功日期;下次重新歸檔日期 /// 前次成功日期;下次重新歸檔日期
/// </summary> /// </summary>
@ -277,6 +278,8 @@ namespace Backend.Models
public string Point { get; set; } public string Point { get; set; }
public string FullDeviceNumberPoint { get; set; } public string FullDeviceNumberPoint { get; set; }
public System.DateTime archive_lastDate { get; set; } public System.DateTime archive_lastDate { get; set; }
public System.DateTime created_at { get; set; }
} }
public class ImportDevForCoo public class ImportDevForCoo

View File

@ -99,6 +99,7 @@ namespace BackendWorkerService.Quartz.Jobs
deviceNumberPoint.Point = point.points; deviceNumberPoint.Point = point.points;
deviceNumberPoint.FullDeviceNumberPoint = string.Format("{0}_{1}", electricMeter.Device_number, point.points); deviceNumberPoint.FullDeviceNumberPoint = string.Format("{0}_{1}", electricMeter.Device_number, point.points);
deviceNumberPoint.archive_lastDate = electricMeter.archive_lastDate; deviceNumberPoint.archive_lastDate = electricMeter.archive_lastDate;
deviceNumberPoint.created_at = electricMeter.created_at;
electricDeviceNumberPoints.Add(deviceNumberPoint); electricDeviceNumberPoints.Add(deviceNumberPoint);
} }
} }
@ -117,7 +118,7 @@ namespace BackendWorkerService.Quartz.Jobs
deviceNumberPoint.Point = point.points; deviceNumberPoint.Point = point.points;
deviceNumberPoint.FullDeviceNumberPoint = string.Format("{0}_{1}", waterMeter.Device_number, point.points); deviceNumberPoint.FullDeviceNumberPoint = string.Format("{0}_{1}", waterMeter.Device_number, point.points);
deviceNumberPoint.archive_lastDate = waterMeter.archive_lastDate; deviceNumberPoint.archive_lastDate = waterMeter.archive_lastDate;
deviceNumberPoint.created_at = waterMeter.created_at;
waterDeviceNumberPoints.Add(deviceNumberPoint); waterDeviceNumberPoints.Add(deviceNumberPoint);
} }
} }
@ -168,6 +169,10 @@ namespace BackendWorkerService.Quartz.Jobs
{ {
var startDay = deviceNumberPoint.archive_lastDate.ToString("yyyy-MM-dd"); var startDay = deviceNumberPoint.archive_lastDate.ToString("yyyy-MM-dd");
var endDay = System.DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"); var endDay = System.DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
if (startDay == "0001-01-01") // 假設archive_lastDate為null
{
startDay = deviceNumberPoint.created_at.ToString("yyyy-MM-dd");
}
foreach (DateTime day in EachDay(startDay, endDay)) foreach (DateTime day in EachDay(startDay, endDay))
{ {
var sDay = string.Format("{0}T00:00:00.000+08:00", day.ToString("yyyy-MM-dd").Replace(" ", "T")); // 巨蛋用此時間抓到的實際是T00:15:00的資料 var sDay = string.Format("{0}T00:00:00.000+08:00", day.ToString("yyyy-MM-dd").Replace(" ", "T")); // 巨蛋用此時間抓到的實際是T00:15:00的資料
@ -252,6 +257,10 @@ namespace BackendWorkerService.Quartz.Jobs
{ {
var startDay = deviceNumberPoint.archive_lastDate.ToString("yyyy-MM-dd"); var startDay = deviceNumberPoint.archive_lastDate.ToString("yyyy-MM-dd");
var endDay = System.DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"); var endDay = System.DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
if (startDay == "0001-01-01") // 假設archive_lastDate為null
{
startDay = deviceNumberPoint.created_at.ToString("yyyy-MM-dd");
}
foreach (DateTime day in EachDay(startDay, endDay)) foreach (DateTime day in EachDay(startDay, endDay))
{ {
var sDay = string.Format("{0}T00:00:00.000+08:00", day.ToString("yyyy-MM-dd").Replace(" ", "T")); // 巨蛋用此時間抓到的實際是T00:15:00的資料 var sDay = string.Format("{0}T00:00:00.000+08:00", day.ToString("yyyy-MM-dd").Replace(" ", "T")); // 巨蛋用此時間抓到的實際是T00:15:00的資料
@ -1101,27 +1110,10 @@ namespace BackendWorkerService.Quartz.Jobs
try try
{ {
await task_Detail.InsertWorkTime("ArchiveElectricMeterDayJob", "Month", "水電表月任務開始"); await task_Detail.InsertWorkTime("ArchiveElectricMeterDayJob", "Month", "水電表月任務開始");
var preDay = now.AddDays(-1); //取得前一天
var dayInMonth = DateTime.DaysInMonth(preDay.Year, preDay.Month); string startTimestamp = "";
var FirstDay = new DateTime(preDay.Year, preDay.Month, 1); string endTimestamp = "";
//var LastDay = now.AddMonths(1).AddDays(-now.AddMonths(1).Day); // 這是抓到該月最後一天 string historyQueryFilter = "";
var LastDay = now;
//var startTimestamp = string.Format("{0}T23:59:59.000+08:00", FirstDay.AddDays(-1).ToString("yyyy-MM-dd")); // 巨蛋要這樣抓數據才是對的
var startTimestamp = string.Format("{0}T00:00:00.000+08:00", FirstDay.ToString("yyyy-MM-dd"));
//var endTimestamp = string.Format("{0}T23:59:59.000+08:00", LastDay.ToString("yyyy-MM-dd"));
var endTimestamp = string.Format("{0}T00:15:00.000+08:00", LastDay.ToString("yyyy-MM-dd")); // 240131 jay for dome
var historyQueryFilter = $@"<obj is='obix: HistoryFilter'>
<abstime name='start' val='{startTimestamp}' />
<abstime name='end' val='{endTimestamp}' />
<reltime name='interval' val = 'PT{dayInMonth+1}D' />
</obj>";
//Stopwatch stopWatch = new Stopwatch(); //Stopwatch stopWatch = new Stopwatch();
//stopWatch.Start(); //stopWatch.Start();
@ -1131,6 +1123,51 @@ namespace BackendWorkerService.Quartz.Jobs
List<Dictionary<string, object>> waterArchiveMonthRawDatas = new List<Dictionary<string, object>>(); List<Dictionary<string, object>> waterArchiveMonthRawDatas = new List<Dictionary<string, object>>();
foreach (var deviceNumberPoint in electricDeviceNumberPoints) foreach (var deviceNumberPoint in electricDeviceNumberPoints)
{ {
var startDay = deviceNumberPoint.archive_lastDate.ToString("yyyy-MM-dd");
var endDay = System.DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
foreach (DateTime day in EachMonth(startDay, endDay))
{
if (day.ToString("yyyy-MM") == System.DateTime.Now.AddDays(-1).ToString("yyyy-MM"))
{
var preDay = now.AddDays(-1); //取得前一天
var dayInMonth = DateTime.DaysInMonth(preDay.Year, preDay.Month);
var FirstDay = new DateTime(preDay.Year, preDay.Month, 1);
//var LastDay = now.AddMonths(1).AddDays(-now.AddMonths(1).Day); // 這是抓到該月最後一天
var LastDay = now;
//var startTimestamp = string.Format("{0}T23:59:59.000+08:00", FirstDay.AddDays(-1).ToString("yyyy-MM-dd")); // 巨蛋要這樣抓數據才是對的
startTimestamp = string.Format("{0}T00:00:00.000+08:00", FirstDay.ToString("yyyy-MM-dd"));
//var endTimestamp = string.Format("{0}T23:59:59.000+08:00", LastDay.ToString("yyyy-MM-dd"));
endTimestamp = string.Format("{0}T00:15:00.000+08:00", LastDay.ToString("yyyy-MM-dd")); // 240131 jay for dome
historyQueryFilter = $@"<obj is='obix: HistoryFilter'>
<abstime name='start' val='{startTimestamp}' />
<abstime name='end' val='{endTimestamp}' />
<reltime name='interval' val = 'PT{dayInMonth + 1}D' />
</obj>";
}
else
{
var dayInMonth = DateTime.DaysInMonth(day.Year, day.Month);
var FirstDay = new DateTime(day.Year, day.Month, 1);
var LastDay = new DateTime(day.Year, day.Month, dayInMonth);
startTimestamp = string.Format("{0}T00:00:00.000+08:00", FirstDay.ToString("yyyy-MM-dd"));
endTimestamp = string.Format("{0}T00:15:00.000+08:00", LastDay.ToString("yyyy-MM-dd"));
historyQueryFilter = $@"<obj is='obix: HistoryFilter'>
<abstime name='start' val='{startTimestamp}' />
<abstime name='end' val='{endTimestamp}' />
<reltime name='interval' val = 'PT{dayInMonth + 1}D' />
</obj>";
}
device_number = deviceNumberPoint.FullDeviceNumberPoint; device_number = deviceNumberPoint.FullDeviceNumberPoint;
if (tagQuantity.Equals("5")) if (tagQuantity.Equals("5"))
station = await backgroundServiceRepository.GetOneAsync<string>($@"select parent_path from import_niagara_item_history station = await backgroundServiceRepository.GetOneAsync<string>($@"select parent_path from import_niagara_item_history
@ -1197,8 +1234,53 @@ namespace BackendWorkerService.Quartz.Jobs
} }
} }
} }
}
foreach (var deviceNumberPoint in waterDeviceNumberPoints) foreach (var deviceNumberPoint in waterDeviceNumberPoints)
{ {
var startDay = deviceNumberPoint.archive_lastDate.ToString("yyyy-MM-dd");
var endDay = System.DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
foreach (DateTime day in EachMonth(startDay, endDay))
{
if (day.ToString("yyyy-MM") == System.DateTime.Now.AddDays(-1).ToString("yyyy-MM"))
{
var preDay = now.AddDays(-1); //取得前一天
var dayInMonth = DateTime.DaysInMonth(preDay.Year, preDay.Month);
var FirstDay = new DateTime(preDay.Year, preDay.Month, 1);
//var LastDay = now.AddMonths(1).AddDays(-now.AddMonths(1).Day); // 這是抓到該月最後一天
var LastDay = now;
//var startTimestamp = string.Format("{0}T23:59:59.000+08:00", FirstDay.AddDays(-1).ToString("yyyy-MM-dd")); // 巨蛋要這樣抓數據才是對的
startTimestamp = string.Format("{0}T00:00:00.000+08:00", FirstDay.ToString("yyyy-MM-dd"));
//var endTimestamp = string.Format("{0}T23:59:59.000+08:00", LastDay.ToString("yyyy-MM-dd"));
endTimestamp = string.Format("{0}T00:15:00.000+08:00", LastDay.ToString("yyyy-MM-dd")); // 240131 jay for dome
historyQueryFilter = $@"<obj is='obix: HistoryFilter'>
<abstime name='start' val='{startTimestamp}' />
<abstime name='end' val='{endTimestamp}' />
<reltime name='interval' val = 'PT{dayInMonth + 1}D' />
</obj>";
}
else
{
var dayInMonth = DateTime.DaysInMonth(day.Year, day.Month);
var FirstDay = new DateTime(day.Year, day.Month, 1);
var LastDay = new DateTime(day.Year, day.Month, dayInMonth);
startTimestamp = string.Format("{0}T00:00:00.000+08:00", FirstDay.ToString("yyyy-MM-dd"));
endTimestamp = string.Format("{0}T00:15:00.000+08:00", LastDay.ToString("yyyy-MM-dd"));
historyQueryFilter = $@"<obj is='obix: HistoryFilter'>
<abstime name='start' val='{startTimestamp}' />
<abstime name='end' val='{endTimestamp}' />
<reltime name='interval' val = 'PT{dayInMonth + 1}D' />
</obj>";
}
device_number = deviceNumberPoint.FullDeviceNumberPoint; device_number = deviceNumberPoint.FullDeviceNumberPoint;
if (tagQuantity.Equals("5")) if (tagQuantity.Equals("5"))
station = await backgroundServiceRepository.GetOneAsync<string>($@"select parent_path from import_niagara_item_history station = await backgroundServiceRepository.GetOneAsync<string>($@"select parent_path from import_niagara_item_history
@ -1266,6 +1348,7 @@ namespace BackendWorkerService.Quartz.Jobs
} }
} }
} }
}
//stopWatch.Stop(); //stopWatch.Stop();
//logger.LogInformation("【ArchiveElectricMeterDayJob】【月歸檔效能檢驗】[取得資料花費時間]{0} 毫秒", stopWatch.ElapsedMilliseconds); //logger.LogInformation("【ArchiveElectricMeterDayJob】【月歸檔效能檢驗】[取得資料花費時間]{0} 毫秒", stopWatch.ElapsedMilliseconds);
@ -1663,7 +1746,7 @@ namespace BackendWorkerService.Quartz.Jobs
{ {
var strtday = DateTime.Parse(from); var strtday = DateTime.Parse(from);
var endday = DateTime.Parse(thru); var endday = DateTime.Parse(thru);
for (var _month = strtday.Date; _month.Month <= endday.Month; _month = _month.AddDays(1)) for (var _month = strtday.Date; _month.Month <= endday.Month; _month = _month.AddMonths(1))
yield return _month; yield return _month;
} }