ConnectionLogdbService.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. using Dapper;
  2. using EVCB_OCPP.Domain;
  3. using EVCB_OCPP.WSServer.Helper;
  4. using EVCB_OCPP.WSServer.Service.WsService;
  5. using log4net;
  6. using Microsoft.Data.SqlClient;
  7. using Microsoft.EntityFrameworkCore;
  8. using Microsoft.Extensions.Configuration;
  9. using Microsoft.Extensions.Logging;
  10. using NLog.Fluent;
  11. using OCPPServer.Protocol;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Data;
  15. using System.Diagnostics;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Threading.Tasks;
  19. using static System.Runtime.InteropServices.JavaScript.JSType;
  20. namespace EVCB_OCPP.WSServer.Service;
  21. public interface IConnectionLogdbService
  22. {
  23. void WarmUpLog();
  24. void WriteMachineLog(WsClientData clientData, string data, string messageType, string errorMsg = "", bool isSent = false);
  25. }
  26. public class ConnectionLogdbService : IConnectionLogdbService
  27. {
  28. public const string LimitConfigKey = "ConnectionLogDbLimit";
  29. public ConnectionLogdbService(
  30. IDbContextFactory<ConnectionLogDBContext> connectionLogdbContextFactory,
  31. SqlConnectionFactory<ConnectionLogDBContext> sqlConnectionFactory,
  32. ILogger<ConnectionLogdbService> logger,
  33. IConfiguration configuration)
  34. {
  35. this.connectionLogdbContextFactory = connectionLogdbContextFactory;
  36. this.sqlConnectionFactory = sqlConnectionFactory;
  37. this.logger = logger;
  38. //connectionLogdbConnectionString = configuration.GetConnectionString("MeterValueDBContext");
  39. var opLimit = GetLimit(configuration);
  40. this.queueHandler = new(WriteMachineLogEF, opLimit);
  41. InitInsertConnectonLogHandler();
  42. }
  43. private readonly IDbContextFactory<ConnectionLogDBContext> connectionLogdbContextFactory;
  44. private readonly SqlConnectionFactory<ConnectionLogDBContext> sqlConnectionFactory;
  45. private readonly ILogger<ConnectionLogdbService> logger;
  46. private readonly QueueHandler<MachineLog> queueHandler;
  47. //private readonly string connectionLogdbConnectionString;
  48. private readonly Queue<string> _existTables = new();
  49. private GroupHandler<MachineLog> insertConnectonLogHandler;
  50. public void WarmUpLog()
  51. {
  52. try
  53. {
  54. using (var log = connectionLogdbContextFactory.CreateDbContext())
  55. {
  56. log.MachineConnectionLog.ToList();
  57. }
  58. }
  59. catch (Exception ex)
  60. {
  61. logger.LogError(ex.Message);
  62. logger.LogError(ex.StackTrace);
  63. //Console.WriteLine(ex.ToString());
  64. }
  65. }
  66. public void WriteMachineLog(WsClientData clientData, string data, string messageType, string errorMsg = "", bool isSent = false)
  67. {
  68. var log = new MachineLog(clientData, data, messageType, errorMsg, isSent);
  69. //queueHandler.Enqueue(log);
  70. //_ = WriteMachineLogEF(log);
  71. insertConnectonLogHandler.HandleAsync(log);
  72. //_ = InsertWithDapper(log);
  73. }
  74. private async Task InsertWithDapper(MachineLog log)
  75. {
  76. var watch = Stopwatch.StartNew();
  77. long t0, t1, t2, t3;
  78. var workTime = DateTime.UtcNow;
  79. if (!await GetTableExist(workTime))
  80. {
  81. t0 = watch.ElapsedMilliseconds;
  82. await WriteMachineLogEF(log);
  83. watch.Stop();
  84. t1 = watch.ElapsedMilliseconds;
  85. if (t1 > 500)
  86. {
  87. logger.LogWarning("ConnectionLog InsertWithDapper {0}/{1}", t0, t1);
  88. }
  89. return;
  90. }
  91. t0 = watch.ElapsedMilliseconds;
  92. var tableName = GetTableName(workTime);
  93. string command = $"""
  94. INSERT INTO {tableName} (CreatedOn, ChargeBoxId, MessageType, Data, Msg, IsSent, EVSEEndPoint, Session)
  95. VALUES (@CreatedOn, @ChargeBoxId, @MessageType, @Data, @Msg, @IsSent, @EVSEEndPoint, @Session);
  96. """;
  97. var parameters = new DynamicParameters();
  98. parameters.Add("CreatedOn", workTime, DbType.DateTime);
  99. parameters.Add("ChargeBoxId", log.clientData.ChargeBoxId == null ? "unknown" : log.clientData.ChargeBoxId.Replace("'", "''"), DbType.String, size:50); ;
  100. parameters.Add("MessageType", log.messageType.Replace("'", "''"), DbType.String, size: 50);
  101. parameters.Add("Data", log.data.Replace("'", "''"), DbType.String);
  102. parameters.Add("Msg", log.errorMsg.Replace("'", "''"), DbType.String, size: 200);
  103. parameters.Add("IsSent", log.isSent, DbType.Boolean);
  104. parameters.Add("EVSEEndPoint", log.clientData.Endpoint == null ? "123" : log.clientData.Endpoint.ToString(), DbType.String, size: 25);
  105. parameters.Add("Session", log.clientData.SessionID == null ? "123" : log.clientData.SessionID, DbType.String, size: 36);
  106. t1 = watch.ElapsedMilliseconds;
  107. using var sqlConnection = await sqlConnectionFactory.CreateAsync();
  108. t2 = watch.ElapsedMilliseconds;
  109. await sqlConnection.ExecuteAsync(command, parameters);
  110. watch.Stop();
  111. t3 = watch.ElapsedMilliseconds;
  112. if (t3 > 1000)
  113. {
  114. logger.LogWarning("ConnectionLog Dapper {0}/{1}/{2}/{3}", t0, t1, t2, t3);
  115. }
  116. }
  117. private void InitInsertConnectonLogHandler()
  118. {
  119. if (insertConnectonLogHandler is not null)
  120. {
  121. throw new Exception($"{nameof(InitInsertConnectonLogHandler)} should only called once");
  122. }
  123. insertConnectonLogHandler = new GroupHandler<MachineLog>(
  124. BundleInsertWithDapper,
  125. logger,
  126. workerCnt: 20
  127. );
  128. }
  129. private async Task WriteMachineLogEF(MachineLog log)
  130. {
  131. var watcher = Stopwatch.StartNew();
  132. try
  133. {
  134. if (log.clientData == null || string.IsNullOrEmpty(log.data)) return;
  135. if (log.clientData.ChargeBoxId == null)
  136. {
  137. logger.LogCritical(log.clientData.Path.ToString() + "]********************session ChargeBoxId null sessionId=" + log.clientData.SessionID);
  138. }
  139. string sp = "[dbo].[uspInsertMachineConnectionLog] @CreatedOn," +
  140. "@ChargeBoxId,@MessageType,@Data,@Msg,@IsSent,@EVSEEndPoint,@Session";
  141. var dd = DateTime.UtcNow;
  142. SqlParameter[] parameter =
  143. {
  144. new SqlParameter("CreatedOn", SqlDbType.DateTime){ Value = dd },
  145. new SqlParameter("ChargeBoxId", SqlDbType.NVarChar, 50){ Value= log.clientData.ChargeBoxId==null?"unknown":log.clientData.ChargeBoxId.Replace("'","''") },
  146. new SqlParameter("MessageType", SqlDbType.NVarChar , 50){ Value = log.messageType.Replace("'","''")},
  147. new SqlParameter("Data", SqlDbType.NVarChar, -1) { Value = log.data.Replace("'", "''") },
  148. new SqlParameter("Msg", SqlDbType.NVarChar, 200) { Value = log.errorMsg.Replace("'", "''") },
  149. new SqlParameter("IsSent", SqlDbType.Bit) { Value = log.isSent },
  150. new SqlParameter("EVSEEndPoint", SqlDbType.NVarChar, 25) { Value = log.clientData.Endpoint == null ? "123" : log.clientData.Endpoint.ToString() },
  151. new SqlParameter("Session", SqlDbType.NVarChar, 36) { Value = log.clientData.SessionID == null ? "123" : log.clientData.SessionID }
  152. };
  153. using (var db = await connectionLogdbContextFactory.CreateDbContextAsync())
  154. {
  155. await db.Database.ExecuteSqlRawAsync(sp, parameter);
  156. }
  157. }
  158. catch (Exception ex)
  159. {
  160. logger.LogError(ex.ToString());
  161. }
  162. watcher.Stop();
  163. if (watcher.ElapsedMilliseconds > 1000)
  164. {
  165. logger.LogWarning("WriteMachineLog too long {0}", watcher.ElapsedMilliseconds);
  166. }
  167. }
  168. private async Task BundleInsertWithDapper(BundleHandlerData<MachineLog> bundleHandlerData)
  169. {
  170. var watch = Stopwatch.StartNew();
  171. var times = new List<long>();
  172. var workTime = DateTime.UtcNow;
  173. var parmsList = bundleHandlerData.Datas.ToList();
  174. if (parmsList.Count == 0)
  175. {
  176. return ;
  177. }
  178. var candidate = parmsList[0];
  179. if (!await GetTableExist(workTime))
  180. {
  181. times.Add(watch.ElapsedMilliseconds);
  182. await WriteMachineLogEF(candidate);
  183. times.Add(watch.ElapsedMilliseconds);
  184. if (watch.ElapsedMilliseconds > 500)
  185. {
  186. logger.LogWarning($"ConnectionLog InsertWithDapper {string.Join("/", times)}");
  187. }
  188. parmsList.Remove(candidate);
  189. bundleHandlerData.AddCompletedData(candidate);
  190. }
  191. times.Add(watch.ElapsedMilliseconds);
  192. using SqlConnection sqlConnection = await sqlConnectionFactory.CreateAsync();
  193. //using var trans = await sqlConnection.BeginTransactionAsync();
  194. times.Add(watch.ElapsedMilliseconds);
  195. var tableName = GetTableName(workTime);
  196. string command = $"""
  197. INSERT INTO {tableName} (CreatedOn, ChargeBoxId, MessageType, Data, Msg, IsSent, EVSEEndPoint, Session)
  198. VALUES (@CreatedOn, @ChargeBoxId, @MessageType, @Data, @Msg, @IsSent, @EVSEEndPoint, @Session);
  199. """;
  200. foreach (var log in parmsList)
  201. {
  202. var parameters = new DynamicParameters();
  203. parameters.Add("CreatedOn", workTime, DbType.DateTime);
  204. parameters.Add("ChargeBoxId", log.clientData.ChargeBoxId == null ? "unknown" : log.clientData.ChargeBoxId.Replace("'", "''"), DbType.String, size: 50); ;
  205. parameters.Add("MessageType", log.messageType.Replace("'", "''"), DbType.String, size: 50);
  206. parameters.Add("Data", log.data.Replace("'", "''"), DbType.String);
  207. parameters.Add("Msg", log.errorMsg.Replace("'", "''"), DbType.String, size: 200);
  208. parameters.Add("IsSent", log.isSent, DbType.Boolean);
  209. parameters.Add("EVSEEndPoint", log.clientData.Endpoint == null ? "123" : log.clientData.Endpoint.ToString(), DbType.String, size: 25);
  210. parameters.Add("Session", log.clientData.SessionID == null ? "123" : log.clientData.SessionID, DbType.String, size: 36);
  211. await sqlConnection.ExecuteAsync(command, parameters
  212. //, trans
  213. );
  214. bundleHandlerData.AddCompletedData(log);
  215. }
  216. times.Add(watch.ElapsedMilliseconds);
  217. //await trans.CommitAsync();
  218. watch.Stop();
  219. if (watch.ElapsedMilliseconds > 1000)
  220. {
  221. logger.LogWarning($"MachineLog Bundle Dapper {string.Join("/", times)} coint:{bundleHandlerData.Datas.Count()}");
  222. }
  223. return ;
  224. }
  225. private async Task BulkInsertWithBulkCopy(IEnumerable<MachineLog> parms)
  226. {
  227. var watcher = Stopwatch.StartNew();
  228. long t0 = 0, t1 = 0, t2 = 0, t3 = 0, t4 = 0;
  229. var parmsList = parms.ToList();
  230. if (parmsList.Count == 0)
  231. {
  232. return;
  233. }
  234. var workTime = DateTime.UtcNow;
  235. if (!await GetTableExist(workTime))
  236. {
  237. var candidate = parmsList.First();
  238. await WriteMachineLogEF(candidate);
  239. parmsList.Remove(candidate);
  240. }
  241. t0 = watcher.ElapsedMilliseconds;
  242. var table = new DataTable();
  243. table.Columns.Add("CreatedOn");
  244. table.Columns.Add("ChargeBoxId");
  245. table.Columns.Add("MessageType");
  246. table.Columns.Add("Data");
  247. table.Columns.Add("Msg");
  248. table.Columns.Add("IsSent");
  249. table.Columns.Add("EVSEEndPoint");
  250. table.Columns.Add("Session");
  251. foreach (var param in parmsList)
  252. {
  253. var row = table.NewRow();
  254. row["CreatedOn"] = workTime;
  255. row["ChargeBoxId"] = param.clientData.ChargeBoxId == null ? "unknown" : param.clientData.ChargeBoxId.Replace("'", "''");
  256. row["MessageType"] = param.messageType.Replace("'", "''");
  257. row["Data"] = param.data.Replace("'", "''");
  258. row["Msg"] = param.errorMsg.Replace("'", "''");
  259. row["IsSent"] = param.isSent;
  260. row["EVSEEndPoint"] = param.clientData.Endpoint == null ? "123" : param.clientData.Endpoint.ToString();
  261. row["Session"] = param.clientData.SessionID == null ? "123" : param.clientData.SessionID;
  262. table.Rows.Add(row);
  263. }
  264. t1 = watcher.ElapsedMilliseconds;
  265. using SqlConnection sqlConnection = await sqlConnectionFactory.CreateAsync();
  266. using SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(sqlConnection);
  267. t2 = watcher.ElapsedMilliseconds;
  268. sqlBulkCopy.BatchSize = parmsList.Count();
  269. sqlBulkCopy.DestinationTableName = GetTableName(workTime);
  270. sqlBulkCopy.ColumnMappings.Add("CreatedOn", "CreatedOn");
  271. sqlBulkCopy.ColumnMappings.Add("ChargeBoxId", "ChargeBoxId");
  272. sqlBulkCopy.ColumnMappings.Add("MessageType", "MessageType");
  273. sqlBulkCopy.ColumnMappings.Add("Data", "Data");
  274. sqlBulkCopy.ColumnMappings.Add("Msg", "Msg");
  275. sqlBulkCopy.ColumnMappings.Add("IsSent", "IsSent");
  276. sqlBulkCopy.ColumnMappings.Add("EVSEEndPoint", "EVSEEndPoint");
  277. sqlBulkCopy.ColumnMappings.Add("Session", "Session");
  278. t3 = watcher.ElapsedMilliseconds;
  279. await sqlBulkCopy.WriteToServerAsync(table);
  280. watcher.Stop();
  281. t4 = watcher.ElapsedMilliseconds;
  282. if (t4 > 500)
  283. {
  284. logger.LogWarning("ConnectionLog BulkInsertWithBulkCopy Slow {0}/{1}/{2}/{3}/{4}/{5}", t0, t1, t2, t3, t4, parms.Count());
  285. }
  286. }
  287. private async ValueTask<bool> GetTableExist(DateTime tableDateTime)
  288. {
  289. var tableName = GetTableName(tableDateTime);
  290. if (_existTables.Contains(tableName))
  291. {
  292. return true;
  293. }
  294. FormattableString checkTableSql = $"SELECT Count(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = {tableName}";
  295. using var db = await connectionLogdbContextFactory.CreateDbContextAsync();
  296. var resultList = await db.Database.SqlQuery<int>(checkTableSql)?.ToListAsync();
  297. if (resultList is not null && resultList.Count > 0 && resultList[0] > 0)
  298. {
  299. _existTables.Enqueue(tableName);
  300. if (_existTables.Count > 30)
  301. {
  302. _existTables.TryDequeue(out _);
  303. }
  304. return true;
  305. }
  306. return false;
  307. }
  308. private static string GetTableName(DateTime dateTime)
  309. => $"MachineConnectionLog{dateTime:yyMMdd}";
  310. private int GetLimit(IConfiguration configuration)
  311. {
  312. var limitConfig = configuration[LimitConfigKey];
  313. int limit = 10;
  314. if (limitConfig != default)
  315. {
  316. int.TryParse(limitConfig, out limit);
  317. }
  318. return limit;
  319. }
  320. }
  321. internal record MachineLog(WsClientData clientData, string data, string messageType, string errorMsg, bool isSent);