From fd6657a6d4d46ad99b0731211cf86406038cd98c Mon Sep 17 00:00:00 2001 From: dev02 Date: Thu, 16 Nov 2023 11:32:32 +0800 Subject: [PATCH] =?UTF-8?q?[=E5=BE=8C=E5=8F=B0-=E5=BE=8C=E7=AB=AF]=20?= =?UTF-8?q?=E8=AA=BF=E6=95=B4=E5=BE=8C=E5=8F=B0=E5=90=8C=E6=AD=A5=E8=A8=98?= =?UTF-8?q?=E9=8C=84=E6=B5=81=E7=A8=8B=20[=E5=89=8D=E5=8F=B0-=E5=89=8D?= =?UTF-8?q?=E7=AB=AF]=20=E4=BF=AE=E6=94=B9=E5=88=A4=E6=96=B7=E5=90=8D?= =?UTF-8?q?=E7=A8=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NiagaraDataSynchronizeController.cs | 128 ++++++- .../Services/Implement/webRequestService.cs | 94 +++++- Backend/appsettings.Development.json | 2 +- FrontendWebApi/Views/Shared/_Layout.cshtml | 2 +- .../NiagaraDataSynchronizeRepository.cs | 317 ++++++++++++++++-- .../INiagaraDataSynchronizeRepository.cs | 6 +- 6 files changed, 510 insertions(+), 39 deletions(-) diff --git a/Backend/Controllers/NiagaraDataSynchronizeController.cs b/Backend/Controllers/NiagaraDataSynchronizeController.cs index 45ab2ef..136d0b7 100644 --- a/Backend/Controllers/NiagaraDataSynchronizeController.cs +++ b/Backend/Controllers/NiagaraDataSynchronizeController.cs @@ -14,6 +14,7 @@ using NPOI.SS.Formula.Functions; using Repository.Helper; using Microsoft.AspNetCore.Routing.Matching; using Newtonsoft.Json; +using System.Diagnostics; namespace Backend.Controllers { @@ -23,6 +24,10 @@ namespace Backend.Controllers //private readonly IDeviceImportRepository deviceImportRepository; private readonly INiagaraDataSynchronizeRepository niagaraDataSynchronizeRepository; private readonly IBackEndConfigHelper backEndConfig; + private Stopwatch stopwatch = new Stopwatch(); + private Stopwatch stopwatchSection = new Stopwatch(); + private Stopwatch syncStopWatch = new Stopwatch(); + public NiagaraDataSynchronizeController(IBackendRepository backendRepository, INiagaraDataSynchronizeRepository niagaraDataSynchronizeRepository,IBackEndConfigHelper backEndConfig) { this.backendRepository = backendRepository; @@ -43,8 +48,9 @@ namespace Backend.Controllers [HttpPost] public Task>> RawDataList() { + stopwatch = new Stopwatch(); + stopwatch.Start(); ApiResult> apiResult = new ApiResult>(); - try { var sqlObix = $@"SELECT system_value as Value, system_key as Name FROM variable WHERE deleted = 0 AND system_type = 'obixConfig'"; @@ -62,6 +68,8 @@ namespace Backend.Controllers string top100 = "";//" top 100 "; webRequestService svc = new webRequestService(); var urlSlots = backendRepository.GetAllAsync("select obixSlot from building where deleted = 0").Result; + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); foreach (var us in urlSlots) { string bql = us + "bql:select " + top100 + " * from baja:Folder "; @@ -69,7 +77,8 @@ namespace Backend.Controllers //ds = svc.obixQuery("http://localhost:8080/obix/config/Program/ObixQuery/query/", bql); ds.AddRange(svc.obixQuery(obixApiConfig.ApiBase, bql, obixApiConfig.TagQuantity, obixApiConfig.UserName, obixApiConfig.Password));//三菱 } - + stopwatchSection.Stop(); + niagaraDataSynchronizeRepository.KeepTimeLog("GetDataFromNiagara", stopwatchSection.ElapsedMilliseconds); apiResult.Code = "0000"; apiResult.Data = ds; } @@ -82,6 +91,8 @@ namespace Backend.Controllers } //return apiResult; + stopwatchSection.Stop(); + niagaraDataSynchronizeRepository.KeepTimeLog("RawDataList", stopwatch.ElapsedMilliseconds); return Task.FromResult(apiResult); } @@ -92,8 +103,10 @@ namespace Backend.Controllers [HttpPost] public async Task> CompareData([FromBody] List ds) { + syncStopWatch.Start(); + stopwatch = new Stopwatch(); + stopwatch.Start(); bool result = false; - ApiResult apiResult = new ApiResult(); var sqlObix = $@"SELECT system_value as Value, system_key as Name FROM variable WHERE deleted = 0 AND system_type = 'obixConfig'"; var variableObix = await backendRepository.GetAllAsync(sqlObix); @@ -101,6 +114,8 @@ namespace Backend.Controllers try { + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); string ss = ds.Where(x => x.tag_name != "").FirstOrDefault().tag_name; var data = ds.Where(x => x.tag_name != ""); List building = null; @@ -114,16 +129,62 @@ namespace Backend.Controllers //building.Add(ss.Split("_")[1]); building = data.GroupBy(x => x.tag_name.Split("_")[1]).Select(x => x.Key).ToList(); } + stopwatchSection.Stop(); + await niagaraDataSynchronizeRepository.KeepTimeLog("filter building tag", stopwatchSection.ElapsedMilliseconds); + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); string LightSwitchLevel = await niagaraDataSynchronizeRepository.getLightSwitchLevel(); //獲取照明開關 是否在 device or device_node + stopwatchSection.Stop(); + await niagaraDataSynchronizeRepository.KeepTimeLog("getLightSwitchLevel", stopwatchSection.ElapsedMilliseconds); + + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); await niagaraDataSynchronizeRepository.InsertNiagaraTagList(ds, building, tag_quantity); // 匯入 MySQL table: import_niagara_tag + stopwatchSection.Stop(); + await niagaraDataSynchronizeRepository.KeepTimeLog("InsertNiagaraTagList", stopwatchSection.ElapsedMilliseconds); + + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); await niagaraDataSynchronizeRepository.DeviceComparison(LightSwitchLevel); + stopwatchSection.Stop(); + await niagaraDataSynchronizeRepository.KeepTimeLog("DeviceComparison", stopwatchSection.ElapsedMilliseconds); + + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); await niagaraDataSynchronizeRepository.CheckDiffFullNameAndCover(); // * + stopwatchSection.Stop(); + await niagaraDataSynchronizeRepository.KeepTimeLog("CheckDiffFullNameAndCover", stopwatchSection.ElapsedMilliseconds); + + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); await niagaraDataSynchronizeRepository.CheckFullNameEmptyReplaceByDeviceName(); + stopwatchSection.Stop(); + await niagaraDataSynchronizeRepository.KeepTimeLog("CheckFullNameEmptyReplaceByDeviceName", stopwatchSection.ElapsedMilliseconds); + + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); await niagaraDataSynchronizeRepository.InsertBuildingMenu(); + stopwatchSection.Stop(); + await niagaraDataSynchronizeRepository.KeepTimeLog("InsertBuildingMenu", stopwatchSection.ElapsedMilliseconds); + + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); await niagaraDataSynchronizeRepository.InsertSubSystemFloor(); + stopwatchSection.Stop(); + await niagaraDataSynchronizeRepository.KeepTimeLog("InsertSubSystemFloor", stopwatchSection.ElapsedMilliseconds); + + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); await niagaraDataSynchronizeRepository.InsertFloor(); + stopwatchSection.Stop(); + await niagaraDataSynchronizeRepository.KeepTimeLog("InsertFloor", stopwatchSection.ElapsedMilliseconds); + + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); await this.DeviceDisasterAsync(); + stopwatchSection.Stop(); + await niagaraDataSynchronizeRepository.KeepTimeLog("DeviceDisasterAsync", stopwatchSection.ElapsedMilliseconds); result = true; apiResult.Code = "0000"; @@ -136,6 +197,8 @@ namespace Backend.Controllers Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message + Environment.NewLine + exception.StackTrace); } + stopwatch.Stop(); + await niagaraDataSynchronizeRepository.KeepTimeLog("CompareData", stopwatch.ElapsedMilliseconds); return apiResult; } @@ -147,13 +210,15 @@ namespace Backend.Controllers public Task>> RawDataDevItemList([FromBody] Device_value building) { ApiResult> apiResult = new ApiResult>(); - + stopwatch = new Stopwatch(); + stopwatch.Start(); try { + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); var ds = new List(); webRequestService svc = new webRequestService(); - var sqlObix = $@"SELECT system_value as Value, system_key as Name FROM variable WHERE deleted = 0 AND system_type = 'obixConfig'"; var variableObix = backendRepository.GetAllAsync(sqlObix).Result; var obixApiConfig = new Backend.Models.ObixApiConfig(); @@ -179,6 +244,8 @@ namespace Backend.Controllers { niagaraDataSynchronizeRepository.DeviceControlPoint(data2); } + stopwatchSection.Stop(); + niagaraDataSynchronizeRepository.KeepTimeLog("get device_item from niagara and record all device point", stopwatchSection.ElapsedMilliseconds); var buildings = backendRepository.GetAllAsync("select building_tag from building where deleted = 0").Result; apiResult.Code = "0000"; apiResult.Data = ds.Where(x => buildings.Contains(x.device_building_tag)).ToList(); @@ -191,6 +258,8 @@ namespace Backend.Controllers } //return apiResult; + stopwatch.Stop(); + niagaraDataSynchronizeRepository.KeepTimeLog("RawDataDevItemList", stopwatch.ElapsedMilliseconds); return Task.FromResult(apiResult); } @@ -201,6 +270,8 @@ namespace Backend.Controllers [HttpPost] public async Task> DevIteComData([FromBody] List ds) { + stopwatch = new Stopwatch(); + stopwatch.Start(); bool result = false; var oldDs = JsonConvert.DeserializeObject>(JsonConvert.SerializeObject(ds)); ApiResult apiResult = new ApiResult(); @@ -223,21 +294,59 @@ namespace Backend.Controllers obixApiConfig.Password = variableObix.Where(x => x.Name == "Password").Select(x => x.Value).FirstOrDefault(); //obixApiConfig.UrlSlot = variableObix.Where(x => x.Name == "url_slot").Select(x => x.Value).FirstOrDefault(); webRequestService svc = new webRequestService(); + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); var data = svc.obixHisQuery_v2(obixApiConfig.ApiBase + "obix/config/Program/ObixQuery/query/", obixApiConfig.ApiBase + "obix/histories", "", obixApiConfig.UserName, obixApiConfig.Password); ds.AddRange(data); + stopwatchSection.Stop(); + await niagaraDataSynchronizeRepository.KeepTimeLog("Get history device point from niagara", stopwatchSection.ElapsedMilliseconds); // save to history + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); await niagaraDataSynchronizeRepository.InsertItemFromNiagara_History(ds, building); // insert 暫存table import_niagara_item_history + stopwatchSection.Stop(); + await niagaraDataSynchronizeRepository.KeepTimeLog("InsertItemFromNiagara_History", stopwatchSection.ElapsedMilliseconds); if (!string.IsNullOrEmpty(isDome) && isDome.Contains("ibms_dome")) + { + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); await niagaraDataSynchronizeRepository.RecoverSysTag("import_niagara_item_history"); // recover missing deivce_system_tag + stopwatchSection.Stop(); + await niagaraDataSynchronizeRepository.KeepTimeLog("RecoverSysTag import_niagara_item_history", stopwatchSection.ElapsedMilliseconds); + } #endregion + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); await niagaraDataSynchronizeRepository.InsertItemFromNiagara(oldDs, building); // insert 暫存table import_niagara_item + stopwatchSection.Stop(); + await niagaraDataSynchronizeRepository.KeepTimeLog("InsertItemFromNiagara", stopwatchSection.ElapsedMilliseconds); + + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); await niagaraDataSynchronizeRepository.DeviceItemComparison(); //insert device_item + stopwatchSection.Stop(); + await niagaraDataSynchronizeRepository.KeepTimeLog("DeviceItemComparison", stopwatchSection.ElapsedMilliseconds); + + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); await niagaraDataSynchronizeRepository.CheckItemDiffFullNameAndCover(); // update device_item.fullname + stopwatchSection.Stop(); + await niagaraDataSynchronizeRepository.KeepTimeLog("CheckItemDiffFullNameAndCover", stopwatchSection.ElapsedMilliseconds); + + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); await niagaraDataSynchronizeRepository.ItemCheckFullNameEmptyReplaceByDeviceName(); // 檢查device_item內FullName為空的值,以points取代 + stopwatchSection.Stop(); + await niagaraDataSynchronizeRepository.KeepTimeLog("ItemCheckFullNameEmptyReplaceByDeviceName", stopwatchSection.ElapsedMilliseconds); + + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); await niagaraDataSynchronizeRepository.CheckItemIsShowHistory(); + stopwatchSection.Stop(); + await niagaraDataSynchronizeRepository.KeepTimeLog("CheckItemIsShowHistory", stopwatchSection.ElapsedMilliseconds); result = true; @@ -253,11 +362,18 @@ namespace Backend.Controllers Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message + Environment.NewLine + exception.StackTrace); } + stopwatch.Stop(); + await niagaraDataSynchronizeRepository.KeepTimeLog("DevIteComData", stopwatch.ElapsedMilliseconds); + + syncStopWatch.Stop(); + await niagaraDataSynchronizeRepository.KeepTimeLog("Full sync usage record", syncStopWatch.ElapsedMilliseconds); return apiResult; } private async Task DeviceDisasterAsync() { + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); var sqlObix = $@"SELECT system_value as Value, system_key as Name FROM variable WHERE deleted = 0 AND system_type = 'obixConfig'"; var variableObix = await backendRepository.GetAllAsync(sqlObix); var obixApiConfig = new Models.ObixApiConfig(); @@ -281,6 +397,8 @@ namespace Backend.Controllers await niagaraDataSynchronizeRepository.DeviceDisasterAysnc(dv); } + stopwatchSection.Stop(); + await niagaraDataSynchronizeRepository.KeepTimeLog("DeviceDisasterAsync", stopwatchSection.ElapsedMilliseconds); } } } diff --git a/Backend/Services/Implement/webRequestService.cs b/Backend/Services/Implement/webRequestService.cs index c7c96d7..59abbaf 100644 --- a/Backend/Services/Implement/webRequestService.cs +++ b/Backend/Services/Implement/webRequestService.cs @@ -7,13 +7,27 @@ using Repository.Models; using System.IO; using System.Net; using System.Xml.Linq; +using System.Diagnostics; +using Repository.BackendRepository.Implement; +using Repository.Helper; +using System.Runtime.CompilerServices; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.AspNetCore.Components.Routing; namespace Backend.Services.Implement { public class webRequestService { + private Stopwatch stopwatch = new Stopwatch(); + private Stopwatch stopwatchSection = new Stopwatch(); + public List obixQuery(string urlString, string bql, string tag_quantity, string acc, string pass) { + stopwatch = new Stopwatch(); + stopwatch.Start(); List result = new List(); //String username = "obixUser"; //String password = "Admin123456"; @@ -24,7 +38,8 @@ namespace Backend.Services.Implement String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password)); String API_Url = urlString; - + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); HttpWebRequest Postrequest = (HttpWebRequest)WebRequest.Create(API_Url); Postrequest.Method = "POST"; Postrequest.Headers.Add("Authorization", "Basic " + encoded); @@ -38,7 +53,11 @@ namespace Backend.Services.Implement } HttpWebResponse response = (HttpWebResponse)Postrequest.GetResponse(); var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd(); + stopwatchSection.Stop(); + KeepTimeLog("request data from niagara", stopwatchSection.ElapsedMilliseconds); + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(responseString); //xmlDoc.Load("N4v1021.xml");//N4v1021 @@ -113,7 +132,11 @@ namespace Backend.Services.Implement } } } + stopwatchSection.Stop(); + KeepTimeLog("Serialize xml data, and filter data", stopwatchSection.ElapsedMilliseconds); + stopwatch.Stop(); + KeepTimeLog("obixQuery", stopwatch.ElapsedMilliseconds); return result; } @@ -126,6 +149,8 @@ namespace Backend.Services.Implement /// public List obixHisQuery(string bqlUrlString, string urlString, string slot, string acc, string pass, string AreaTag) { + stopwatch = new Stopwatch(); + stopwatch.Start(); //String username = "obixUser"; //String password = "Admin123456"; //String username = "stanGG"; @@ -139,6 +164,8 @@ namespace Backend.Services.Implement String cp_API_Url = bqlUrlString; String building_tag = slot.Split("/")[2].Replace("|", "_").Trim(); //Ex: D2_, M_ + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); HttpWebRequest cp_Postrequest = (HttpWebRequest)WebRequest.Create(cp_API_Url); cp_Postrequest.Method = "POST"; cp_Postrequest.Headers.Add("Authorization", "Basic " + encoded); @@ -153,7 +180,11 @@ namespace Backend.Services.Implement cp_Postrequest.Timeout = System.Threading.Timeout.Infinite; HttpWebResponse cp_response = (HttpWebResponse)cp_Postrequest.GetResponse(); var cp_responseString = new StreamReader(cp_response.GetResponseStream()).ReadToEnd(); + stopwatchSection.Stop(); + KeepTimeLog("request data from niagara", stopwatchSection.ElapsedMilliseconds); + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); XmlDocument cp_xmlDoc = new XmlDocument(); cp_xmlDoc.LoadXml(cp_responseString); @@ -316,12 +347,18 @@ namespace Backend.Services.Implement } } } + stopwatchSection.Stop(); + KeepTimeLog("Serialize xml data, and filter data", stopwatchSection.ElapsedMilliseconds); + stopwatch.Stop(); + KeepTimeLog("obixHisQuery", stopwatch.ElapsedMilliseconds); return result.ToList(); } public List obixDevDisaster(string urlString, string bql, string tag_quantity, string acc, string pass, string dt) { + stopwatch = new Stopwatch(); + stopwatch.Start(); List result = new List(); try { @@ -330,6 +367,8 @@ namespace Backend.Services.Implement String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(acc + ":" + pass)); String API_Url = urlString; + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); HttpWebRequest Postrequest = (HttpWebRequest)WebRequest.Create(API_Url); Postrequest.Method = "POST"; Postrequest.Headers.Add("Authorization", "Basic " + encoded); @@ -344,7 +383,11 @@ namespace Backend.Services.Implement HttpWebResponse response = (HttpWebResponse)Postrequest.GetResponse(); var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd(); + stopwatchSection.Stop(); + KeepTimeLog("get data from niagara", stopwatchSection.ElapsedMilliseconds); + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(responseString); @@ -428,8 +471,12 @@ namespace Backend.Services.Implement } catch(Exception e) { - var a = e; } + stopwatchSection.Stop(); + KeepTimeLog("Serialize xml data, and filter data", stopwatchSection.ElapsedMilliseconds); + + stopwatch.Stop(); + KeepTimeLog("obixHisQuery", stopwatch.ElapsedMilliseconds); return result; } @@ -442,6 +489,8 @@ namespace Backend.Services.Implement /// public List obixHisQuery_v2(string bqlUrlString, string urlString, string slot, string acc, string pass) { + stopwatch = new Stopwatch(); + stopwatch.Start(); //String username = "obixUser"; //String password = "Admin123456"; //String username = "stanGG"; @@ -494,6 +543,8 @@ namespace Backend.Services.Implement List result = new List(); String API_Url = urlString; + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); HttpWebRequest Postrequest = (HttpWebRequest)WebRequest.Create(API_Url); Postrequest.Method = "GET"; Postrequest.Headers.Add("Authorization", "Basic " + encoded); @@ -502,12 +553,16 @@ namespace Backend.Services.Implement HttpWebResponse response = (HttpWebResponse)Postrequest.GetResponse(); var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd(); + stopwatchSection.Stop(); + KeepTimeLog("get data from niagara", stopwatchSection.ElapsedMilliseconds); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(responseString); // 1.循環所有的 station //ref https://stackoverflow.com/questions/642293/how-do-i-read-and-parse-an-xml-file-in-c + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes) { var stationName = node.Attributes["name"].InnerText; // WSP_Supervisor @@ -573,7 +628,12 @@ namespace Backend.Services.Implement } } } + stopwatchSection.Stop(); + KeepTimeLog("Serialize xml data, and filter data", stopwatchSection.ElapsedMilliseconds); #endregion + + stopwatch.Stop(); + KeepTimeLog("obixHisQuery_v2", stopwatch.ElapsedMilliseconds); return result.ToList(); //return result.Where(x => x.full_name != null).ToList(); } @@ -739,6 +799,8 @@ namespace Backend.Services.Implement /// public List obixAllDevicePoint(string bqlUrlString, string urlString, string slot, string acc, string pass, string AreaTag) { + stopwatch = new Stopwatch(); + stopwatch.Start(); String username = acc; String password = pass; String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password)); @@ -748,6 +810,8 @@ namespace Backend.Services.Implement String cp_API_Url = bqlUrlString; String building_tag = slot.Split("/")[2].Replace("|", "_").Trim(); //Ex: D2_, M_ + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); HttpWebRequest cp_Postrequest = (HttpWebRequest)WebRequest.Create(cp_API_Url); cp_Postrequest.Method = "POST"; cp_Postrequest.Headers.Add("Authorization", "Basic " + encoded); @@ -762,7 +826,11 @@ namespace Backend.Services.Implement cp_Postrequest.Timeout = System.Threading.Timeout.Infinite; HttpWebResponse cp_response = (HttpWebResponse)cp_Postrequest.GetResponse(); var cp_responseString = new StreamReader(cp_response.GetResponseStream()).ReadToEnd(); + stopwatchSection.Stop(); + KeepTimeLog("get data from niagara", stopwatchSection.ElapsedMilliseconds); + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); XmlDocument cp_xmlDoc = new XmlDocument(); cp_xmlDoc.LoadXml(cp_responseString); @@ -833,7 +901,29 @@ namespace Backend.Services.Implement } } } + stopwatchSection.Stop(); + KeepTimeLog("Serialize xml data, and filter data", stopwatchSection.ElapsedMilliseconds); + + stopwatch.Stop(); + KeepTimeLog("obixHisQuery", stopwatch.ElapsedMilliseconds); return result.ToList(); } + + public void KeepTimeLog(string functionName, long millisecond, [CallerLineNumber] int line = 0, [CallerMemberName] string name = "") + { + string path = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "Logs"); + string filepath = System.IO.Path.Combine(path, "SyncLog" + DateTime.Now.ToString("yyyyMMdd") + ".txt"); + + using (FileStream fs = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write)) + { + StreamWriter sw = new StreamWriter(fs); + sw.BaseStream.Seek(0, SeekOrigin.End); + sw.WriteLine($"Log current time {DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")}, Log function name: {functionName}, Log function usage time: {TimeSpan.FromMilliseconds(millisecond)}, Log function record line: {line}, Log called function name: {name}"); + sw.WriteLine(Environment.NewLine); + sw.Flush(); + sw.Close(); + fs.Close(); + } + } } } diff --git a/Backend/appsettings.Development.json b/Backend/appsettings.Development.json index 2a7e0a5..16453c3 100644 --- a/Backend/appsettings.Development.json +++ b/Backend/appsettings.Development.json @@ -14,7 +14,7 @@ }, "DBConfig": { "MySqlDBConfig": { - "Server": "jbgI3Q4uFL4Kd9bbvToklA==", //0.201 + "Server": "CYGthbCeGtAXT4s1NOSJHQ==", //0.201 "Port": "mkF51jVbg40V5K5eTh2Ckw==", //3306 //"Database": "VJB2XC+lAtzuHObDGMVOAA==", //30 //"Database": "IgYBsgG2VLKKxFb64j7LOA==", //wsp diff --git a/FrontendWebApi/Views/Shared/_Layout.cshtml b/FrontendWebApi/Views/Shared/_Layout.cshtml index b9a3064..5cbeda5 100644 --- a/FrontendWebApi/Views/Shared/_Layout.cshtml +++ b/FrontendWebApi/Views/Shared/_Layout.cshtml @@ -141,7 +141,7 @@ } - @if (ViewBag.role.Contains("RescueDeviceAED")) + @if (ViewBag.role.Contains("WarningValueIndex")) {
  • diff --git a/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs b/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs index 565721d..049856d 100644 --- a/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs +++ b/Repository/BackendRepository/Implement/NiagaraDataSynchronizeRepository.cs @@ -16,11 +16,16 @@ using Org.BouncyCastle.Utilities.Collections; using System.Reflection; using System.Drawing; using Org.BouncyCastle.Asn1.Mozilla; +using System.IO; +using System.Diagnostics; +using System.Runtime.CompilerServices; namespace Repository.BackendRepository.Implement { public class NiagaraDataSynchronizeRepository : BackendRepository, INiagaraDataSynchronizeRepository { + private Stopwatch stopwatch = new Stopwatch(); + private Stopwatch stopwatchSection = new Stopwatch(); public NiagaraDataSynchronizeRepository(IDatabaseHelper databaseHelper) : base(databaseHelper) { } @@ -33,13 +38,17 @@ namespace Repository.BackendRepository.Implement /// public async Task InsertNiagaraTagList(List ds, List building, string tag_quantity) { + stopwatch = new Stopwatch(); + stopwatch.Start(); using (IDbConnection conn = GetDbConnection()) { conn.Open(); StringBuilder sb = new StringBuilder(); try { - foreach(var b in building) + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); + foreach (var b in building) { string sql = @"CREATE TABLE IF NOT EXISTS `import_niagara_tag` ( `id` int(11) NOT NULL AUTO_INCREMENT, @@ -68,6 +77,8 @@ namespace Repository.BackendRepository.Implement await conn.ExecuteAsync(sql); } } + stopwatchSection.Stop(); + await KeepTimeLog("Create or delete related building import_niagara_tag", stopwatchSection.ElapsedMilliseconds); //N4資料groupBy後放入import_niagara_tag資料表 var ds2 = ds.GroupBy(x => new @@ -83,6 +94,8 @@ namespace Repository.BackendRepository.Implement // StringBuilder sb = new StringBuilder(); bool isDome = false; //是否為巨蛋案 int count = 0; + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); foreach (var row in ds2) { if (string.IsNullOrEmpty(row.tag_name)) continue; @@ -140,18 +153,27 @@ namespace Repository.BackendRepository.Implement } } } + stopwatchSection.Stop(); + await KeepTimeLog("Insert into import_niagara_tag", stopwatchSection.ElapsedMilliseconds); + if (sb.Length > 0) { + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); var temp = sb.ToString(); using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { await conn.ExecuteAsync(temp); } sb.Clear(); + stopwatchSection.Stop(); + await KeepTimeLog("Insert into import_niagara_tag", stopwatchSection.ElapsedMilliseconds); //巨蛋特殊處理 if (isDome) { + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); sb.Append($@" -- tag 第二碼為小類(除了安全系統之外) update import_niagara_tag set device_name_tag = device_system_tag where (device_system_tag <> 'S' and device_system_tag <> 'CWB' and device_system_tag <> 'B' and @@ -167,8 +189,12 @@ namespace Repository.BackendRepository.Implement { await conn.ExecuteAsync(sb.ToString()); } + stopwatchSection.Stop(); + await KeepTimeLog("巨蛋特殊處理", stopwatchSection.ElapsedMilliseconds); } } + stopwatch.Stop(); + await KeepTimeLog("InsertNiagaraTagList", stopwatch.ElapsedMilliseconds); } catch (Exception exception) { @@ -188,12 +214,16 @@ namespace Repository.BackendRepository.Implement /// public async Task getLightSwitchLevel() { + stopwatch = new Stopwatch(); + stopwatch.Start(); using (IDbConnection conn = GetDbConnection()) { conn.Open(); var sql = "select system_value from variable where system_type = 'module_light_switch' "; var module_light_switch = conn.QueryAsync(sql).Result.FirstOrDefault(); conn.Close(); + stopwatch.Stop(); + await KeepTimeLog("getLightSwitchLevel", stopwatch.ElapsedMilliseconds); return module_light_switch; } } @@ -203,16 +233,19 @@ namespace Repository.BackendRepository.Implement /// /// /// - /// public async Task InsertItemFromNiagara(List ds, List building) { + stopwatch = new Stopwatch(); + stopwatch.Start(); using (IDbConnection conn = GetDbConnection()) { conn.Open(); try { #region 刪除 import_niagara_item資料表中選取的棟別 + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); foreach(var b in building) { string sql = @"CREATE TABLE IF NOT EXISTS `import_niagara_item` ( @@ -237,7 +270,12 @@ namespace Repository.BackendRepository.Implement await conn.ExecuteAsync(sql); } } + stopwatchSection.Stop(); + await KeepTimeLog("Create or delete related building import_niagara_tag", stopwatchSection.ElapsedMilliseconds); #endregion + + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); ds = ds.GroupBy(x => new { device_area_tag = x.device_area_tag, device_building_tag = x.device_building_tag, @@ -293,9 +331,13 @@ namespace Repository.BackendRepository.Implement } sb.Clear(); + stopwatchSection.Stop(); + await KeepTimeLog("Insert into import_niagara_item", stopwatchSection.ElapsedMilliseconds); if (isDome.Contains("ibms_dome")) { + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); sb.Append($@" update import_niagara_item a join variable b on a.device_name_tag COLLATE utf8mb4_0900_ai_ci = b.system_value COLLATE utf8mb4_0900_ai_ci and b.deleted = 0 and b.system_type = 'device_system_category_layer3' join variable v1 on b.system_parent_id = v1.id @@ -306,17 +348,30 @@ namespace Repository.BackendRepository.Implement { await conn.ExecuteAsync(sb.ToString()); } + stopwatchSection.Stop(); + await KeepTimeLog("Insert into import_niagara_item", stopwatchSection.ElapsedMilliseconds); } - sb.Clear(); + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); sb.Append($@" update import_niagara_item set full_name = device_point_name where full_name = '' or full_name is null;"); using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { await conn.ExecuteAsync(sb.ToString()); } sb.Clear(); + stopwatchSection.Stop(); + await KeepTimeLog("update device point name where full name is empty or null", stopwatchSection.ElapsedMilliseconds); + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); + await RecoverSysTag("import_niagara_item"); + stopwatchSection.Stop(); + await KeepTimeLog("update import_niagara_item system tag", stopwatchSection.ElapsedMilliseconds); + + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); sb.Append($@"SET SQL_SAFE_UPDATES = 0; update import_niagara_item ini join ( @@ -332,7 +387,11 @@ namespace Repository.BackendRepository.Implement await conn.ExecuteAsync(sb.ToString()); } sb.Clear(); + stopwatchSection.Stop(); + await KeepTimeLog("update device_item history", stopwatchSection.ElapsedMilliseconds); + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); sb.Append($@"SET SQL_SAFE_UPDATES = 0; update import_niagara_item ini join ( @@ -344,8 +403,11 @@ namespace Repository.BackendRepository.Implement { await conn.ExecuteAsync(sb.ToString()); } + stopwatchSection.Stop(); + await KeepTimeLog("update device_item parent_path", stopwatchSection.ElapsedMilliseconds); - await RecoverSysTag("import_niagara_item"); + stopwatch.Stop(); + await KeepTimeLog("InsertItemFromNiagara", stopwatch.ElapsedMilliseconds); #endregion } catch (Exception exception) @@ -359,12 +421,15 @@ namespace Repository.BackendRepository.Implement } } + /// /// 比對 device /// /// public async Task DeviceComparison(string LightSwitchLevel) { + stopwatch = new Stopwatch(); + stopwatch.Start(); using (IDbConnection conn = GetDbConnection()) { conn.Open(); @@ -373,6 +438,8 @@ namespace Repository.BackendRepository.Implement List result; StringBuilder sb = new StringBuilder(); StringBuilder sb2 = new StringBuilder(); + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); sb.Append($@"SELECT m.* FROM import_niagara_tag m WHERE NOT EXISTS ( @@ -381,9 +448,13 @@ namespace Repository.BackendRepository.Implement WHERE m.niagara_tags = d.device_number );"); result = (await conn.QueryAsync(sb.ToString())).ToList(); - sb.Clear(); + stopwatchSection.Stop(); + await KeepTimeLog("filter device item from import_niagara_tag", stopwatchSection.ElapsedMilliseconds); + #region device, device_kind 新增 + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); //新增至device, is_link = 1 if (result.Count > 0) { @@ -437,6 +508,20 @@ namespace Repository.BackendRepository.Implement } } + if (sb.Length > 0) + { + using (TransactionScope scope = new TransactionScope((TransactionScopeOption)TransactionScopeAsyncFlowOption.Enabled)) + { + await conn.ExecuteAsync(sb.ToString()); + } + + sb.Clear(); + } + stopwatchSection.Stop(); + await KeepTimeLog("insert into device", stopwatchSection.ElapsedMilliseconds); + + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); count = 0; foreach (var data in result2) { @@ -464,16 +549,6 @@ namespace Repository.BackendRepository.Implement } } - if (sb.Length > 0) - { - using (TransactionScope scope = new TransactionScope((TransactionScopeOption)TransactionScopeAsyncFlowOption.Enabled)) - { - await conn.ExecuteAsync(sb.ToString()); - } - - sb.Clear(); - } - if (sb2.Length > 0) { using (TransactionScope scope = new TransactionScope((TransactionScopeOption)TransactionScopeAsyncFlowOption.Enabled)) @@ -483,10 +558,15 @@ namespace Repository.BackendRepository.Implement sb2.Clear(); } + stopwatchSection.Stop(); + await KeepTimeLog("insert into device_kind", stopwatchSection.ElapsedMilliseconds); + } #endregion #region device_node 新增 + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); sb.Append($@" SELECT m.* FROM import_niagara_tag m WHERE NOT EXISTS ( @@ -544,9 +624,13 @@ namespace Repository.BackendRepository.Implement } sb.Clear(); } + stopwatchSection.Stop(); + await KeepTimeLog("insert into device_node", stopwatchSection.ElapsedMilliseconds); } #endregion + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); //device有,niagara有,is_link 更新成 1 sb.Append($@" SET SQL_SAFE_UPDATES = 0; UPDATE device d @@ -560,9 +644,12 @@ namespace Repository.BackendRepository.Implement { await conn.ExecuteAsync(sb.ToString()); } - sb.Clear(); + stopwatchSection.Stop(); + await KeepTimeLog("update device is_link = 1, deleted = 0", stopwatchSection.ElapsedMilliseconds); + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); //device有,niagara沒有,is_link 更新成 0 sb.Append($@" SET SQL_SAFE_UPDATES = 0; UPDATE device d @@ -577,9 +664,12 @@ namespace Repository.BackendRepository.Implement { await conn.ExecuteAsync(sb.ToString()); } - sb.Clear(); + stopwatchSection.Stop(); + await KeepTimeLog("update device is_link = 0", stopwatchSection.ElapsedMilliseconds); + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); // device_node 有, niagara沒有, is_link 更新成 0 sb.Append($@" SET SQL_SAFE_UPDATES = 0; UPDATE device_node dn @@ -595,6 +685,11 @@ namespace Repository.BackendRepository.Implement { await conn.ExecuteAsync(sb.ToString()); } + stopwatchSection.Stop(); + await KeepTimeLog("update device_node is_link = 0", stopwatchSection.ElapsedMilliseconds); + + stopwatch.Stop(); + await KeepTimeLog("DeviceComparison", stopwatch.ElapsedMilliseconds); } catch (Exception exception) { @@ -615,11 +710,15 @@ namespace Repository.BackendRepository.Implement /// public async Task DeviceItemComparison() { + stopwatch = new Stopwatch(); + stopwatch.Start(); using (IDbConnection conn = GetDbConnection()) { conn.Open(); try { + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); List result; StringBuilder sb = new StringBuilder(); sb.Append($@" SELECT m.device_point_name, m.is_history, m.device_building_tag, m.device_system_tag, m.device_name_tag, m.parent_path, m.full_name @@ -633,9 +732,13 @@ namespace Repository.BackendRepository.Implement and m.device_building_tag = d.device_building_tag where d.points is null;"); result = (await conn.QueryAsync(sb.ToString())).ToList(); - sb.Clear(); - # region 新增至device, is_link = 1 + stopwatchSection.Stop(); + await KeepTimeLog("filter device_item from import_niagara_item", stopwatchSection.ElapsedMilliseconds); + + #region 新增至device, is_link = 1 + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); if (result.Count > 0) { foreach (var data in result) @@ -674,6 +777,8 @@ namespace Repository.BackendRepository.Implement sb.Clear(); } } + stopwatchSection.Stop(); + await KeepTimeLog("insert into device_item", stopwatchSection.ElapsedMilliseconds); #endregion @@ -684,8 +789,10 @@ namespace Repository.BackendRepository.Implement // ON d.device_system_tag = m.device_system_tag and d.device_name_tag = m.device_name_tag and d.points = m.device_point_name // SET d.is_link = 0, d.is_show_history = 0 // WHERE m.device_point_name IS NULL"); - + //本次有匯入的 isLink 改為 1, 沒有的 isLink = 0 + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); sb.Append($@" SET SQL_SAFE_UPDATES = 0; UPDATE device_item d LEFT JOIN ( @@ -703,8 +810,12 @@ namespace Repository.BackendRepository.Implement { await conn.ExecuteAsync(sb.ToString()); } - sb.Clear(); + stopwatchSection.Stop(); + await KeepTimeLog("update device_item is_link", stopwatchSection.ElapsedMilliseconds); + + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); sb.Append($@" SET SQL_SAFE_UPDATES = 0; UPDATE device_item d LEFT JOIN ( @@ -723,6 +834,11 @@ namespace Repository.BackendRepository.Implement { await conn.ExecuteAsync(sb.ToString()); } + stopwatchSection.Stop(); + await KeepTimeLog("update device_item is_show_history", stopwatchSection.ElapsedMilliseconds); + + stopwatch.Stop(); + await KeepTimeLog("DeviceItemComparison", stopwatch.ElapsedMilliseconds); } catch (Exception exception) { @@ -741,6 +857,8 @@ namespace Repository.BackendRepository.Implement /// public async Task InsertBuildingMenu() { + stopwatch = new Stopwatch(); + stopwatch.Start(); using (IDbConnection conn = GetDbConnection()) { conn.Open(); @@ -749,6 +867,8 @@ namespace Repository.BackendRepository.Implement List result; StringBuilder sb = new StringBuilder(); #region comparison building_menu and import_niagara_tag + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); sb.Append($@" select a.* from ( select device_building_tag, device_system_tag, device_name_tag from import_niagara_tag @@ -762,7 +882,11 @@ namespace Repository.BackendRepository.Implement result = (await conn.QueryAsync(sb.ToString())).ToList(); #endregion sb.Clear(); + stopwatchSection.Stop(); + await KeepTimeLog("filter building_menu from import_niagara_tag", stopwatchSection.ElapsedMilliseconds); + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); if (result.Count > 0) { foreach (var data in result) @@ -792,6 +916,11 @@ namespace Repository.BackendRepository.Implement sb.Clear(); } } + stopwatchSection.Stop(); + await KeepTimeLog("insert into building_menu", stopwatchSection.ElapsedMilliseconds); + + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); //building_menu有,import_niagara_tag沒有,is_link 更新成 0 sb.Append($@" SET SQL_SAFE_UPDATES = 0; UPDATE building_menu b LEFT JOIN ( @@ -806,8 +935,12 @@ namespace Repository.BackendRepository.Implement { await conn.ExecuteAsync(sb.ToString()); } - sb.Clear(); + stopwatchSection.Stop(); + await KeepTimeLog("update building_menu is_link = 0", stopwatchSection.ElapsedMilliseconds); + + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); sb.Append($@"SET SQL_SAFE_UPDATES = 0; UPDATE building_menu b LEFT JOIN ( select device_building_tag, device_system_tag, device_name_tag @@ -822,6 +955,11 @@ namespace Repository.BackendRepository.Implement { await conn.ExecuteAsync(sb.ToString()); } + stopwatchSection.Stop(); + await KeepTimeLog("update building_menu is_link = 1", stopwatchSection.ElapsedMilliseconds); + + stopwatch.Stop(); + await KeepTimeLog("InsertBuildingMenu", stopwatch.ElapsedMilliseconds); } catch (Exception exception) { @@ -840,6 +978,8 @@ namespace Repository.BackendRepository.Implement /// public async Task InsertSubSystemFloor() { + stopwatch = new Stopwatch(); + stopwatch.Start(); using (IDbConnection conn = GetDbConnection()) { conn.Open(); @@ -848,6 +988,8 @@ namespace Repository.BackendRepository.Implement List result; StringBuilder sb = new StringBuilder(); #region comparison sub_system_floor and import_niagara_tag + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); sb.Append($@" select a.* from ( select device_building_tag, device_system_tag, device_name_tag, device_floor_tag from import_niagara_tag @@ -860,9 +1002,13 @@ namespace Repository.BackendRepository.Implement a.device_floor_tag COLLATE utf8mb4_0900_ai_ci = b.floor_tag COLLATE utf8mb4_0900_ai_ci WHERE b.building_tag IS NULL"); result = (await conn.QueryAsync(sb.ToString())).ToList(); + sb.Clear(); + stopwatchSection.Stop(); + await KeepTimeLog("filter subsystem floor from import_niagara_tag", stopwatchSection.ElapsedMilliseconds); #endregion - sb.Clear(); + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); if (result.Count > 0) { foreach (var data in result) @@ -887,7 +1033,12 @@ namespace Repository.BackendRepository.Implement sb.Clear(); } } + stopwatchSection.Stop(); + await KeepTimeLog("insert into subsystem floor", stopwatchSection.ElapsedMilliseconds); + //building_menu有,import_niagara_tag沒有,is_link 更新成 0 + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); sb.Append($@" SET SQL_SAFE_UPDATES = 0; UPDATE sub_system_floor b LEFT JOIN ( select device_building_tag, device_system_tag, device_name_tag, device_floor_tag @@ -904,9 +1055,12 @@ namespace Repository.BackendRepository.Implement { await conn.ExecuteAsync(sb.ToString()); } - sb.Clear(); + stopwatchSection.Stop(); + await KeepTimeLog("update subsystem floor is_link = 0", stopwatchSection.ElapsedMilliseconds); + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); sb.Append($@" SET SQL_SAFE_UPDATES = 0; UPDATE sub_system_floor b LEFT JOIN ( select device_building_tag, device_system_tag, device_name_tag, device_floor_tag @@ -923,6 +1077,11 @@ namespace Repository.BackendRepository.Implement { await conn.ExecuteAsync(sb.ToString()); } + stopwatchSection.Stop(); + await KeepTimeLog("update subsystem floor is_link = 1", stopwatchSection.ElapsedMilliseconds); + + stopwatch.Stop(); + await KeepTimeLog("InsertSubSystemFloor", stopwatch.ElapsedMilliseconds); } catch (Exception exception) { @@ -940,6 +1099,8 @@ namespace Repository.BackendRepository.Implement /// public async Task CheckFullNameEmptyReplaceByDeviceName() { + stopwatch = new Stopwatch(); + stopwatch.Start(); using (IDbConnection conn = GetDbConnection()) { conn.Open(); @@ -961,6 +1122,8 @@ namespace Repository.BackendRepository.Implement } } } + stopwatch.Stop(); + await KeepTimeLog("CheckFullNameEmptyReplaceByDeviceName", stopwatch.ElapsedMilliseconds); } /// @@ -969,7 +1132,8 @@ namespace Repository.BackendRepository.Implement /// public async Task ItemCheckFullNameEmptyReplaceByDeviceName() { - + stopwatch = new Stopwatch(); + stopwatch.Start(); //檢查是否有空值 using (IDbConnection conn = GetDbConnection()) { @@ -991,8 +1155,9 @@ namespace Repository.BackendRepository.Implement conn.Close(); } } - } + stopwatch.Stop(); + await KeepTimeLog("ItemCheckFullNameEmptyReplaceByDeviceName", stopwatch.ElapsedMilliseconds); } /// @@ -1001,6 +1166,8 @@ namespace Repository.BackendRepository.Implement /// public async Task CheckDiffFullNameAndCover() { + stopwatch = new Stopwatch(); + stopwatch.Start(); using (IDbConnection conn = GetDbConnection()) { conn.Open(); @@ -1032,8 +1199,9 @@ namespace Repository.BackendRepository.Implement conn.Close(); } } - } + stopwatch.Stop(); + await KeepTimeLog("CheckDiffFullNameAndCover", stopwatch.ElapsedMilliseconds); } @@ -1044,6 +1212,8 @@ namespace Repository.BackendRepository.Implement /// public async Task CheckItemDiffFullNameAndCover() { + stopwatch = new Stopwatch(); + stopwatch.Start(); //檢查 using (IDbConnection conn = GetDbConnection()) { @@ -1077,6 +1247,8 @@ namespace Repository.BackendRepository.Implement } } } + stopwatch.Stop(); + await KeepTimeLog("CheckItemDiffFullNameAndCover", stopwatch.ElapsedMilliseconds); } /// @@ -1086,6 +1258,8 @@ namespace Repository.BackendRepository.Implement /// public async Task CheckItemIsShowHistory() { + stopwatch = new Stopwatch(); + stopwatch.Start(); //先將所有is_show_history關閉,niagara有的才開啟 using (IDbConnection conn = GetDbConnection()) { @@ -1126,12 +1300,15 @@ namespace Repository.BackendRepository.Implement { conn.Close(); } - } + stopwatch.Stop(); + await KeepTimeLog("getLightSwitchLevel", stopwatch.ElapsedMilliseconds); } public async Task DeviceDisasterAysnc(List dv) { + stopwatch = new Stopwatch(); + stopwatch.Start(); using (IDbConnection conn = GetDbConnection()) { conn.Open(); @@ -1142,11 +1319,20 @@ namespace Repository.BackendRepository.Implement { try { + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); sb.Append("select * from device_disaster;"); deviceDisaster = (await conn.QueryAsync(sb.ToString())).ToList(); sb.Clear(); + stopwatchSection.Stop(); + await KeepTimeLog("select all device_disaster", stopwatchSection.ElapsedMilliseconds); + + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); sb.Append("select * from device where deleted = 0;"); device = (await conn.QueryAsync(sb.ToString())).ToList(); + stopwatchSection.Stop(); + await KeepTimeLog("select all device", stopwatchSection.ElapsedMilliseconds); } catch (Exception exception) { @@ -1158,6 +1344,8 @@ namespace Repository.BackendRepository.Implement } } + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); dv = dv.Where(x => device.Any(d => d.device_number == x.value.Split('/')[6])).ToList(); sb.Clear(); var updateList = dv.Where(x => deviceDisaster.Any(dd => dd.device_number == x.value.Split('/')[6] && dd.device_system_value != x.disasterValue)).ToList(); @@ -1201,6 +1389,11 @@ namespace Repository.BackendRepository.Implement conn.Close(); } } + stopwatchSection.Stop(); + await KeepTimeLog("update or insert into device_disaster", stopwatchSection.ElapsedMilliseconds); + + stopwatch.Stop(); + await KeepTimeLog("DeviceDisasterAysnc", stopwatch.ElapsedMilliseconds); } } @@ -1210,6 +1403,8 @@ namespace Repository.BackendRepository.Implement /// public async Task InsertFloor() { + stopwatch = new Stopwatch(); + stopwatch.Start(); using (IDbConnection conn = GetDbConnection()) { conn.Open(); @@ -1220,6 +1415,8 @@ namespace Repository.BackendRepository.Implement List result; StringBuilder sb = new StringBuilder(); #region comparison floor and sub_system_floor + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); sb.Append($@" select tag.* from ( select device_building_tag, device_floor_tag @@ -1232,10 +1429,14 @@ namespace Repository.BackendRepository.Implement and f.deleted = 0 WHERE f.building_tag IS NULL;"); result = (await conn.QueryAsync(sb.ToString())).ToList(); + sb.Clear(); + stopwatchSection.Stop(); + await KeepTimeLog("filter floor from import_niagara_tag", stopwatchSection.ElapsedMilliseconds); #endregion - sb.Clear(); #region insert floor + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); if (result.Count > 0) { Dictionary floorPriority = new Dictionary(); @@ -1261,9 +1462,13 @@ namespace Repository.BackendRepository.Implement sb.Clear(); } } + stopwatchSection.Stop(); + await KeepTimeLog("insert into floor", stopwatchSection.ElapsedMilliseconds); #endregion #region update unuse floor with niagara data + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); var floor = (await conn.QueryAsync($@"select f1.* from floor as f1 left join ( @@ -1290,6 +1495,11 @@ namespace Repository.BackendRepository.Implement await conn.ExecuteAsync(sb.ToString()); } + stopwatchSection.Stop(); + await KeepTimeLog("update floor deleted = 1", stopwatchSection.ElapsedMilliseconds); + + stopwatch.Stop(); + await KeepTimeLog("InsertFloor", stopwatch.ElapsedMilliseconds); #endregion } catch (Exception exception) @@ -1306,11 +1516,15 @@ namespace Repository.BackendRepository.Implement public async Task InsertItemFromNiagara_History(List ds, List building) { + stopwatch = new Stopwatch(); + stopwatch.Start(); using (IDbConnection conn = GetDbConnection()) { conn.Open(); try { + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); //改成每次都新增 string sql = @"SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; @@ -1343,6 +1557,8 @@ SET FOREIGN_KEY_CHECKS = 1;"; { await conn.ExecuteAsync(sql); } + stopwatchSection.Stop(); + await KeepTimeLog("recreate import_niagara_item_history", stopwatchSection.ElapsedMilliseconds); #region 刪除 import_niagara_item資料表中選取的棟別 //foreach (var b in building) @@ -1356,6 +1572,8 @@ SET FOREIGN_KEY_CHECKS = 1;"; StringBuilder sb = new StringBuilder(); int count = 0; #region 放入import_niagara_item資料表 + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); foreach (var row in ds) { string ss = $@" insert import_niagara_item_history(device_area_tag, device_building_tag, device_system_tag, device_name_tag, @@ -1394,6 +1612,8 @@ device_floor_tag, device_master_tag, device_last_name_tag, device_serial_tag, d } sb.Clear(); } + stopwatchSection.Stop(); + await KeepTimeLog("insert into import_niagara_item_history", stopwatchSection.ElapsedMilliseconds); #endregion } catch (Exception exception) @@ -1405,6 +1625,8 @@ device_floor_tag, device_master_tag, device_last_name_tag, device_serial_tag, d conn.Close(); } } + stopwatch.Stop(); + await KeepTimeLog("InsertItemFromNiagara_History", stopwatch.ElapsedMilliseconds); } /// @@ -1413,6 +1635,8 @@ device_floor_tag, device_master_tag, device_last_name_tag, device_serial_tag, d /// public async Task RecoverSysTag(string tableName) { + stopwatch = new Stopwatch(); + stopwatch.Start(); //檢查 using (IDbConnection conn = GetDbConnection()) { @@ -1440,6 +1664,8 @@ device_floor_tag, device_master_tag, device_last_name_tag, device_serial_tag, d } } } + stopwatch.Stop(); + await KeepTimeLog("RecoverSysTag", stopwatch.ElapsedMilliseconds); } /// @@ -1449,10 +1675,14 @@ device_floor_tag, device_master_tag, device_last_name_tag, device_serial_tag, d /// public async Task DeviceControlPoint (List dcp) { + stopwatch = new Stopwatch(); + stopwatch.Start(); using (IDbConnection conn = GetDbConnection()) { try { + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); string sql = @"CREATE TABLE IF NOT EXISTS `import_niagara_device_control_point` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(1000) DEFAULT NULL, @@ -1470,13 +1700,21 @@ device_floor_tag, device_master_tag, device_last_name_tag, device_serial_tag, d { await conn.ExecuteAsync(sql); } + stopwatchSection.Stop(); + await KeepTimeLog("check and create import_niagara_device_control_point", stopwatchSection.ElapsedMilliseconds); + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); sql = @"truncate table import_niagara_device_control_point"; using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { await conn.ExecuteAsync(sql); } + stopwatchSection.Stop(); + await KeepTimeLog("truncate import_niagara_device_control_point", stopwatchSection.ElapsedMilliseconds); + stopwatchSection = new Stopwatch(); + stopwatchSection.Start(); StringBuilder sb = new StringBuilder(); int count = 0; foreach (var data in dcp) @@ -1499,6 +1737,8 @@ device_floor_tag, device_master_tag, device_last_name_tag, device_serial_tag, d await conn.ExecuteAsync(sb.ToString()); sb.Clear(); } + stopwatchSection.Stop(); + await KeepTimeLog("insert into import_niagara_device_control_point", stopwatchSection.ElapsedMilliseconds); } catch (Exception e) { @@ -1509,6 +1749,25 @@ device_floor_tag, device_master_tag, device_last_name_tag, device_serial_tag, d conn.Close(); } } + stopwatch.Stop(); + await KeepTimeLog("DeviceControlPoint", stopwatch.ElapsedMilliseconds); + } + + public async Task KeepTimeLog(string functionName, long millisecond, [CallerLineNumber] int line = 0, [CallerMemberName] string name = "") + { + string path = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "Logs"); + string filepath = System.IO.Path.Combine(path, "SyncLog" + DateTime.Now.ToString("yyyyMMdd") + ".txt"); + + using (FileStream fs = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write)) + { + StreamWriter sw = new StreamWriter(fs); + sw.BaseStream.Seek(0, SeekOrigin.End); + sw.WriteLine($"Log current time {DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")}, Log function name: {functionName}, Log function usage time: {TimeSpan.FromMilliseconds(millisecond)}, Log function record line: {line}, Log called function name: {name}"); + sw.WriteLine(Environment.NewLine); + sw.Flush(); + sw.Close(); + fs.Close(); + } } } } diff --git a/Repository/BackendRepository/Interface/INiagaraDataSynchronizeRepository.cs b/Repository/BackendRepository/Interface/INiagaraDataSynchronizeRepository.cs index 351047c..4419a10 100644 --- a/Repository/BackendRepository/Interface/INiagaraDataSynchronizeRepository.cs +++ b/Repository/BackendRepository/Interface/INiagaraDataSynchronizeRepository.cs @@ -1,4 +1,6 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; +using System.Runtime.CompilerServices; using System.Threading.Tasks; using Repository.Models; @@ -89,5 +91,7 @@ namespace Repository.BackendRepository.Interface /// /// Task DeviceControlPoint(List dcp); + + Task KeepTimeLog(string functionName, long milliSecond, [CallerLineNumber] int line = 0, [CallerMemberName] string name = ""); } }