修改巨蛋水電歸檔邏輯,新增月歸檔sql會自動建立table語法

This commit is contained in:
張家睿 2024-02-01 15:06:59 +08:00
parent 58e7ec1be9
commit c35aeb2f2e
3 changed files with 160 additions and 36 deletions

View File

@ -121,12 +121,12 @@ namespace BackendWorkerService
//); //);
//#endregion //#endregion
#region ( 5 ) //#region 停車場管理(設定每 5秒 執行一次)
services.AddSingleton<ParkingJob>(); //services.AddSingleton<ParkingJob>();
services.AddSingleton( //services.AddSingleton(
new JobSchedule(jobType: typeof(ParkingJob), cronExpression: configuration.GetValue<string>("BackgroundServiceCron:ParkingJob")) //new JobSchedule(jobType: typeof(ParkingJob), cronExpression: configuration.GetValue<string>("BackgroundServiceCron:ParkingJob"))
); //);
#endregion //#endregion
//#region 電錶歸檔(設定每 小時 執行一次) //#region 電錶歸檔(設定每 小時 執行一次)
//services.AddSingleton<ArchiveElectricMeterHourJob>(); //services.AddSingleton<ArchiveElectricMeterHourJob>();
@ -142,12 +142,12 @@ namespace BackendWorkerService
); );
#endregion #endregion
#region API //#region 定時取得氣象API
services.AddSingleton<Quartz.Jobs.WeatherAPIJob>(); //services.AddSingleton<Quartz.Jobs.WeatherAPIJob>();
services.AddSingleton( //services.AddSingleton(
new JobSchedule(jobType: typeof(Quartz.Jobs.WeatherAPIJob), cronExpression: configuration.GetValue<string>("BackgroundServiceCron:WeatherAPIJob")) //new JobSchedule(jobType: typeof(Quartz.Jobs.WeatherAPIJob), cronExpression: configuration.GetValue<string>("BackgroundServiceCron:WeatherAPIJob"))
); //);
#endregion //#endregion
}).ConfigureLogging((hostContext, logFactory) => { }).ConfigureLogging((hostContext, logFactory) => {
IConfiguration configuration = hostContext.Configuration; IConfiguration configuration = hostContext.Configuration;

View File

@ -141,8 +141,8 @@ namespace BackendWorkerService.Quartz.Jobs
try try
{ {
await task_Detail.InsertWorkTime("ArchiveElectricMeterDayJob", "Day", "水電表天任務開始"); await task_Detail.InsertWorkTime("ArchiveElectricMeterDayJob", "Day", "水電表天任務開始");
//var preDay = now.AddDays(-1); //取得前一天 var preDay = now.AddDays(-1); //取得前一天
//var dbDateName = preDay.Year.ToString().PadLeft(4, '0') + preDay.Month.ToString().PadLeft(2, '0'); var dbDateName = preDay.Year.ToString().PadLeft(4, '0') + preDay.Month.ToString().PadLeft(2, '0');
//var startTimestamp = string.Format("{0}T00:00:00.000+08:00", preDay.ToString("yyyy-MM-dd")); //var startTimestamp = string.Format("{0}T00:00:00.000+08:00", preDay.ToString("yyyy-MM-dd"));
//var endTimestamp = string.Format("{0}T23:59:59.000+08:00", preDay.ToString("yyyy-MM-dd")); //var endTimestamp = string.Format("{0}T23:59:59.000+08:00", preDay.ToString("yyyy-MM-dd"));
@ -153,17 +153,11 @@ namespace BackendWorkerService.Quartz.Jobs
// <reltime name='interval' val = 'PT1D' /> // <reltime name='interval' val = 'PT1D' />
// </obj>"; // </obj>";
#region
var preDay = now.AddDays(-1); //取得前一天
var dbDateName = preDay.Year.ToString().PadLeft(4, '0') + preDay.Month.ToString().PadLeft(2, '0');
#endregion
//Stopwatch stopWatch = new Stopwatch(); //Stopwatch stopWatch = new Stopwatch();
//stopWatch.Start(); //stopWatch.Start();
#region
//抓取每個設備的資料 //抓取每個設備的資料
List<Dictionary<string, object>> electericArchiveDayRawDatas = new List<Dictionary<string, object>>(); List<Dictionary<string, object>> electericArchiveDayRawDatas = new List<Dictionary<string, object>>();
List<Dictionary<string, object>> waterArchiveDayRawDatas = new List<Dictionary<string, object>>(); List<Dictionary<string, object>> waterArchiveDayRawDatas = new List<Dictionary<string, object>>();
@ -173,8 +167,10 @@ namespace BackendWorkerService.Quartz.Jobs
var endDay = System.DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"); var endDay = System.DateTime.Now.AddDays(-1).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")); //var sDay = string.Format("{0}T00:00:00.000+08:00", day.ToString("yyyy-MM-dd").Replace(" ", "T")); // 巨蛋用此時間抓到的實際是T00:15:00的資料
var eDay = string.Format("{0}T00:15:00.000+08:00", day.AddDays(1).ToString("yyyy-MM-dd").Replace(" ", "T")); var sDay = string.Format("{0}T23:59:59.000+08:00", day.AddDays(-1).ToString("yyyy-MM-dd").Replace(" ", "T")); // 巨蛋用此時間抓到的實際是T00:00:00的資料
var eDay = string.Format("{0}T00:00:00.000+08:00", day.AddDays(1).ToString("yyyy-MM-dd").Replace(" ", "T"));
string historyQueryFilter = $@"<obj is='obix: HistoryFilter'> string historyQueryFilter = $@"<obj is='obix: HistoryFilter'>
@ -252,11 +248,13 @@ namespace BackendWorkerService.Quartz.Jobs
foreach (var deviceNumberPoint in waterDeviceNumberPoints) foreach (var deviceNumberPoint in waterDeviceNumberPoints)
{ {
var startDay = deviceNumberPoint.archive_lastDate.ToString("yyyy-MM-dd"); var startDay = deviceNumberPoint.archive_lastDate.ToString("yyyy-MM-dd");
var endDay = System.DateTime.Now.ToString("yyyy-MM-dd"); var endDay = System.DateTime.Now.AddDays(-1).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")); //var sDay = string.Format("{0}T00:00:00.000+08:00", day.ToString("yyyy-MM-dd").Replace(" ", "T")); // 巨蛋用此時間抓到的實際是T00:15:00的資料
var eDay = string.Format("{0}T00:15:00.000+08:00", day.AddDays(1).ToString("yyyy-MM-dd").Replace(" ", "T")); var sDay = string.Format("{0}T23:59:59.000+08:00", day.AddDays(-1).ToString("yyyy-MM-dd").Replace(" ", "T")); // 巨蛋用此時間抓到的實際是T00:00:00的資料
var eDay = string.Format("{0}T00:00:00.000+08:00", day.AddDays(1).ToString("yyyy-MM-dd").Replace(" ", "T"));
string historyQueryFilter = $@"<obj is='obix: HistoryFilter'> string historyQueryFilter = $@"<obj is='obix: HistoryFilter'>
@ -334,7 +332,7 @@ namespace BackendWorkerService.Quartz.Jobs
} }
} }
#endregion
//stopWatch.Stop(); //stopWatch.Stop();
//logger.LogInformation("【ArchiveElectricMeterDayJob】【天歸檔】【效能檢驗】[取得資料花費時間]{0} 毫秒", stopWatch.ElapsedMilliseconds); //logger.LogInformation("【ArchiveElectricMeterDayJob】【天歸檔】【效能檢驗】[取得資料花費時間]{0} 毫秒", stopWatch.ElapsedMilliseconds);
@ -1090,22 +1088,24 @@ namespace BackendWorkerService.Quartz.Jobs
try try
{ {
await task_Detail.InsertWorkTime("ArchiveElectricMeterDayJob", "Month", "水電表月任務開始"); await task_Detail.InsertWorkTime("ArchiveElectricMeterDayJob", "Month", "水電表月任務開始");
var FirstDay = now.AddDays(-now.Day + 1); var preDay = now.AddDays(-1); //取得前一天
//var LastDay = now.AddMonths(1).AddDays(-now.AddMonths(1).Day);
var LastDay = FirstDay.AddMonths(1); var dayInMonth = DateTime.DaysInMonth(preDay.Year, preDay.Month);
var FirstDay = preDay.AddDays(- dayInMonth + 1);
//var LastDay = now.AddMonths(1).AddDays(-now.AddMonths(1).Day); // 這是抓到該月最後一天
var LastDay = FirstDay.AddMonths(1); //但實際應該抓隔月第一天
var dayInMonth = DateTime.DaysInMonth(now.Year, now.Month);
var startTimestamp = string.Format("{0}T00:00:00.000+08:00", FirstDay.ToString("yyyy-MM-dd")); var startTimestamp = string.Format("{0}T23:59:59.000+08:00", FirstDay.AddDays(-1).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}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 endTimestamp = string.Format("{0}T00:00:00.000+08:00", LastDay.ToString("yyyy-MM-dd")); // 240131 jay for dome
var historyQueryFilter = $@"<obj is='obix: HistoryFilter'> var historyQueryFilter = $@"<obj is='obix: HistoryFilter'>
<abstime name='start' val='{startTimestamp}' /> <abstime name='start' val='{startTimestamp}' />
<abstime name='end' val='{endTimestamp}' /> <abstime name='end' val='{endTimestamp}' />
<reltime name='interval' val = 'PT{dayInMonth}D' /> <reltime name='interval' val = 'PT{dayInMonth+1}D' />
</obj>"; </obj>";
//Stopwatch stopWatch = new Stopwatch(); //Stopwatch stopWatch = new Stopwatch();
@ -1258,7 +1258,29 @@ namespace BackendWorkerService.Quartz.Jobs
if (electricArchiveMonthRawDatas.Count() > 0) if (electricArchiveMonthRawDatas.Count() > 0)
{ {
var sql = $@" var sql = $@"
CREATE TABLE IF NOT EXISTS `archive_electric_water_meter_month` (
`device_number` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`point` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`start_timestamp` datetime(6) NOT NULL,
`end_timestamp` datetime(6) NULL DEFAULT NULL,
`count_rawdata` int(11) NULL DEFAULT NULL,
`min_rawdata` decimal(15, 3) NULL DEFAULT NULL,
`max_rawdata` decimal(15, 3) NULL DEFAULT NULL,
`sub_result` decimal(15, 3) NULL DEFAULT NULL,
`avg_rawdata` decimal(15, 3) NULL DEFAULT NULL,
`sum_rawdata` decimal(15, 3) NULL DEFAULT NULL,
`is_complete` tinyint(3) UNSIGNED NULL DEFAULT NULL COMMENT '0: 1:',
`repeat_times` int(11) NULL DEFAULT 0 COMMENT '',
`fail_reason` varchar(4000) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '',
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime(6) NULL DEFAULT NULL,
PRIMARY KEY (`device_number`, `point`, `start_timestamp`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = DYNAMIC;
SET FOREIGN_KEY_CHECKS = 1;
UPDATE archive_electric_water_meter_month SET UPDATE archive_electric_water_meter_month SET
start_timestamp = @start_timestamp,
end_timestamp = @end_timestamp,
count_rawdata = @count_rawdata, count_rawdata = @count_rawdata,
min_rawdata = @min_rawdata, min_rawdata = @min_rawdata,
max_rawdata = @max_rawdata, max_rawdata = @max_rawdata,
@ -1304,8 +1326,48 @@ namespace BackendWorkerService.Quartz.Jobs
WHERE ROW_COUNT() = 0;"; WHERE ROW_COUNT() = 0;";
var mySql = $@"BEGIN TRANSACTION; var mySql = $@"BEGIN TRANSACTION;
IF OBJECT_ID(N'dbo.archive_electric_water_meter_month', N'U') is null
BEGIN
CREATE TABLE [dbo].[archive_electric_water_meter_month](
[device_number] [varchar](50) NOT NULL,
[point] [varchar](20) NOT NULL,
[start_timestamp] [datetime] NOT NULL,
[end_timestamp] [datetime] NULL,
[count_rawdata] [int] NULL,
[min_rawdata] [decimal](15, 3) NULL,
[max_rawdata] [decimal](15, 3) NULL,
[sub_result] [decimal](15, 3) NULL,
[avg_rawdata] [decimal](15, 3) NULL,
[sum_rawdata] [decimal](15, 3) NULL,
[is_complete] [tinyint] NULL,
[repeat_times] [int] NULL,
[fail_reason] [nvarchar](max) NULL,
[created_at] [datetime] NULL,
[updated_at] [datetime] NULL,
CONSTRAINT [PK_archive_electric_water_meter_month] PRIMARY KEY CLUSTERED
(
[device_number] ASC,
[point] ASC,
[start_timestamp] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
ALTER TABLE [dbo].[archive_electric_water_meter_month] ADD CONSTRAINT [DF_archive_electric_water_meter_month_repeat_times] DEFAULT ((0)) FOR [repeat_times]
ALTER TABLE [dbo].[archive_electric_water_meter_month] ADD CONSTRAINT [DF_archive_electric_water_meter_month_created_at] DEFAULT (getdate()) FOR [created_at]
ALTER TABLE [dbo].[archive_electric_water_meter_month] ADD CONSTRAINT [DF_archive_electric_water_meter_month_updated_at] DEFAULT (NULL) FOR [updated_at]
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'0: 1:' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'archive_electric_water_meter_month', @level2type=N'COLUMN',@level2name=N'is_complete'
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'archive_electric_water_meter_month', @level2type=N'COLUMN',@level2name=N'repeat_times'
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'archive_electric_water_meter_month', @level2type=N'COLUMN',@level2name=N'fail_reason'
END
UPDATE archive_electric_water_meter_month SET UPDATE archive_electric_water_meter_month SET
start_timestamp = @start_timestamp,
end_timestamp = @end_timestamp,
count_rawdata = @count_rawdata, count_rawdata = @count_rawdata,
min_rawdata = @min_rawdata, min_rawdata = @min_rawdata,
max_rawdata = @max_rawdata, max_rawdata = @max_rawdata,
@ -1362,7 +1424,29 @@ namespace BackendWorkerService.Quartz.Jobs
if (waterArchiveMonthRawDatas.Count() > 0) if (waterArchiveMonthRawDatas.Count() > 0)
{ {
var sql = $@" var sql = $@"
CREATE TABLE IF NOT EXISTS `archive_electric_water_meter_month` (
`device_number` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`point` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`start_timestamp` datetime(6) NOT NULL,
`end_timestamp` datetime(6) NULL DEFAULT NULL,
`count_rawdata` int(11) NULL DEFAULT NULL,
`min_rawdata` decimal(15, 3) NULL DEFAULT NULL,
`max_rawdata` decimal(15, 3) NULL DEFAULT NULL,
`sub_result` decimal(15, 3) NULL DEFAULT NULL,
`avg_rawdata` decimal(15, 3) NULL DEFAULT NULL,
`sum_rawdata` decimal(15, 3) NULL DEFAULT NULL,
`is_complete` tinyint(3) UNSIGNED NULL DEFAULT NULL COMMENT '0: 1:',
`repeat_times` int(11) NULL DEFAULT 0 COMMENT '',
`fail_reason` varchar(4000) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '',
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime(6) NULL DEFAULT NULL,
PRIMARY KEY (`device_number`, `point`, `start_timestamp`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = DYNAMIC;
SET FOREIGN_KEY_CHECKS = 1;
UPDATE archive_electric_water_meter_month SET UPDATE archive_electric_water_meter_month SET
start_timestamp = @start_timestamp,
end_timestamp = @end_timestamp,
count_rawdata = @count_rawdata, count_rawdata = @count_rawdata,
min_rawdata = @min_rawdata, min_rawdata = @min_rawdata,
max_rawdata = @max_rawdata, max_rawdata = @max_rawdata,
@ -1408,8 +1492,48 @@ namespace BackendWorkerService.Quartz.Jobs
WHERE ROW_COUNT() = 0;"; WHERE ROW_COUNT() = 0;";
var mySql = $@"BEGIN TRANSACTION; var mySql = $@"BEGIN TRANSACTION;
IF OBJECT_ID(N'dbo.archive_electric_water_meter_month', N'U') is null
BEGIN
CREATE TABLE [dbo].[archive_electric_water_meter_month](
[device_number] [varchar](50) NOT NULL,
[point] [varchar](20) NOT NULL,
[start_timestamp] [datetime] NOT NULL,
[end_timestamp] [datetime] NULL,
[count_rawdata] [int] NULL,
[min_rawdata] [decimal](15, 3) NULL,
[max_rawdata] [decimal](15, 3) NULL,
[sub_result] [decimal](15, 3) NULL,
[avg_rawdata] [decimal](15, 3) NULL,
[sum_rawdata] [decimal](15, 3) NULL,
[is_complete] [tinyint] NULL,
[repeat_times] [int] NULL,
[fail_reason] [nvarchar](max) NULL,
[created_at] [datetime] NULL,
[updated_at] [datetime] NULL,
CONSTRAINT [PK_archive_electric_water_meter_month] PRIMARY KEY CLUSTERED
(
[device_number] ASC,
[point] ASC,
[start_timestamp] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
ALTER TABLE [dbo].[archive_electric_water_meter_month] ADD CONSTRAINT [DF_archive_electric_water_meter_month_repeat_times] DEFAULT ((0)) FOR [repeat_times]
ALTER TABLE [dbo].[archive_electric_water_meter_month] ADD CONSTRAINT [DF_archive_electric_water_meter_month_created_at] DEFAULT (getdate()) FOR [created_at]
ALTER TABLE [dbo].[archive_electric_water_meter_month] ADD CONSTRAINT [DF_archive_electric_water_meter_month_updated_at] DEFAULT (NULL) FOR [updated_at]
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'0: 1:' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'archive_electric_water_meter_month', @level2type=N'COLUMN',@level2name=N'is_complete'
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'archive_electric_water_meter_month', @level2type=N'COLUMN',@level2name=N'repeat_times'
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'archive_electric_water_meter_month', @level2type=N'COLUMN',@level2name=N'fail_reason'
END
UPDATE archive_electric_water_meter_month SET UPDATE archive_electric_water_meter_month SET
start_timestamp = @start_timestamp,
end_timestamp = @end_timestamp,
count_rawdata = @count_rawdata, count_rawdata = @count_rawdata,
min_rawdata = @min_rawdata, min_rawdata = @min_rawdata,
max_rawdata = @max_rawdata, max_rawdata = @max_rawdata,

View File

@ -19,7 +19,7 @@
}, },
"DBConfig": { "DBConfig": {
"MySqlDBConfig": { "MySqlDBConfig": {
"Server": "CYGthbCeGtAXT4s1NOSJHQ==", //0.202 "Server": "CYGthbCeGtAXT4s1NOSJHQ==", //0.132
"Port": "mkF51jVbg40V5K5eTh2Ckw==", //3306 "Port": "mkF51jVbg40V5K5eTh2Ckw==", //3306
//"Database": "VJB2XC+lAtzuHObDGMVOAA==", //30 //"Database": "VJB2XC+lAtzuHObDGMVOAA==", //30
//"Database": "IgYBsgG2VLKKxFb64j7LOA==", //wsp //"Database": "IgYBsgG2VLKKxFb64j7LOA==", //wsp