ConnectionLogdbService.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. using Dapper;
  2. using EVCB_OCPP.Domain;
  3. using EVCB_OCPP.Domain.ConnectionFactory;
  4. using EVCB_OCPP.WSServer.Helper;
  5. using EVCB_OCPP.WSServer.Service.WsService;
  6. using log4net;
  7. using Microsoft.Data.SqlClient;
  8. using Microsoft.EntityFrameworkCore;
  9. using Microsoft.Extensions.Configuration;
  10. using Microsoft.Extensions.Logging;
  11. using NLog.Fluent;
  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. ISqlConnectionFactory<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 ISqlConnectionFactory<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.MachineConnectionLogs.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. //BulkInsertWithBulkCopy,
  125. BundleInsertWithDapper,
  126. //loggerFactory.CreateLogger("InsertMeterValueHandler")
  127. logger,
  128. workerCnt: 20
  129. );
  130. }
  131. private async Task WriteMachineLogEF(MachineLog log)
  132. {
  133. var watcher = Stopwatch.StartNew();
  134. try
  135. {
  136. if (log.clientData == null || string.IsNullOrEmpty(log.data)) return;
  137. if (log.clientData.ChargeBoxId == null)
  138. {
  139. logger.LogCritical(log.clientData.Path.ToString() + "]********************session ChargeBoxId null sessionId=" + log.clientData.SessionID);
  140. }
  141. string sp = "[dbo].[uspInsertMachineConnectionLog] @CreatedOn," +
  142. "@ChargeBoxId,@MessageType,@Data,@Msg,@IsSent,@EVSEEndPoint,@Session";
  143. var dd = DateTime.UtcNow;
  144. SqlParameter[] parameter =
  145. {
  146. new SqlParameter("CreatedOn", SqlDbType.DateTime){ Value = dd },
  147. new SqlParameter("ChargeBoxId", SqlDbType.NVarChar, 50){ Value= log.clientData.ChargeBoxId==null?"unknown":log.clientData.ChargeBoxId.Replace("'","''") },
  148. new SqlParameter("MessageType", SqlDbType.NVarChar , 50){ Value = log.messageType.Replace("'","''")},
  149. new SqlParameter("Data", SqlDbType.NVarChar, -1) { Value = log.data.Replace("'", "''") },
  150. new SqlParameter("Msg", SqlDbType.NVarChar, 200) { Value = log.errorMsg.Replace("'", "''") },
  151. new SqlParameter("IsSent", SqlDbType.Bit) { Value = log.isSent },
  152. new SqlParameter("EVSEEndPoint", SqlDbType.NVarChar, 25) { Value = log.clientData.Endpoint == null ? "123" : log.clientData.Endpoint.ToString() },
  153. new SqlParameter("Session", SqlDbType.NVarChar, 36) { Value = log.clientData.SessionID == null ? "123" : log.clientData.SessionID }
  154. };
  155. using (var db = await connectionLogdbContextFactory.CreateDbContextAsync())
  156. {
  157. await db.Database.ExecuteSqlRawAsync(sp, parameter);
  158. }
  159. }
  160. catch (Exception ex)
  161. {
  162. logger.LogError(ex.ToString());
  163. }
  164. watcher.Stop();
  165. if (watcher.ElapsedMilliseconds > 1000)
  166. {
  167. logger.LogWarning("WriteMachineLog too long {0}", watcher.ElapsedMilliseconds);
  168. }
  169. }
  170. private async Task BundleInsertWithDapper(BundleHandlerData<MachineLog> bundleHandlerData)
  171. {
  172. var watch = Stopwatch.StartNew();
  173. long t0, t1, t2, t3, t4;
  174. var workTime = DateTime.UtcNow;
  175. var parmsList = bundleHandlerData.Datas.ToList();
  176. if (parmsList.Count == 0)
  177. {
  178. return;
  179. }
  180. var candidate = parmsList[0];
  181. if (!await GetTableExist(workTime))
  182. {
  183. t0 = watch.ElapsedMilliseconds;
  184. await WriteMachineLogEF(candidate);
  185. watch.Stop();
  186. t1 = watch.ElapsedMilliseconds;
  187. if (t1 > 500)
  188. {
  189. logger.LogWarning("ConnectionLog InsertWithDapper {0}/{1}", t0, t1);
  190. }
  191. parmsList.Remove(candidate);
  192. bundleHandlerData.AddCompletedData(candidate);
  193. }
  194. t0 = watch.ElapsedMilliseconds;
  195. t1 = watch.ElapsedMilliseconds;
  196. using SqlConnection sqlConnection = await sqlConnectionFactory.CreateAsync();
  197. //using var trans = await sqlConnection.BeginTransactionAsync();
  198. t2 = watch.ElapsedMilliseconds;
  199. var tableName = GetTableName(workTime);
  200. string command = $"""
  201. INSERT INTO {tableName} (CreatedOn, ChargeBoxId, MessageType, Data, Msg, IsSent, EVSEEndPoint, Session)
  202. VALUES (@CreatedOn, @ChargeBoxId, @MessageType, @Data, @Msg, @IsSent, @EVSEEndPoint, @Session);
  203. """;
  204. foreach (var log in parmsList)
  205. {
  206. var parameters = new DynamicParameters();
  207. parameters.Add("CreatedOn", workTime, DbType.DateTime);
  208. parameters.Add("ChargeBoxId", log.clientData.ChargeBoxId == null ? "unknown" : log.clientData.ChargeBoxId.Replace("'", "''"), DbType.String, size: 50); ;
  209. parameters.Add("MessageType", log.messageType.Replace("'", "''"), DbType.String, size: 50);
  210. parameters.Add("Data", log.data.Replace("'", "''"), DbType.String);
  211. parameters.Add("Msg", log.errorMsg.Replace("'", "''"), DbType.String, size: 200);
  212. parameters.Add("IsSent", log.isSent, DbType.Boolean);
  213. parameters.Add("EVSEEndPoint", log.clientData.Endpoint == null ? "123" : log.clientData.Endpoint.ToString(), DbType.String, size: 25);
  214. parameters.Add("Session", log.clientData.SessionID == null ? "123" : log.clientData.SessionID, DbType.String, size: 36);
  215. await sqlConnection.ExecuteAsync(command, parameters
  216. //, trans
  217. );
  218. bundleHandlerData.AddCompletedData(log);
  219. }
  220. t3 = watch.ElapsedMilliseconds;
  221. //await trans.CommitAsync();
  222. watch.Stop();
  223. t4 = watch.ElapsedMilliseconds;
  224. if (t4 > 1000)
  225. {
  226. logger.LogWarning("MachineLog Bundle Dapper {0}/{1}/{2}/{3}/{4}/{5}", t0, t1, t2, t3, t4, bundleHandlerData.Datas.Count());
  227. }
  228. }
  229. private async Task BulkInsertWithBulkCopy(IEnumerable<MachineLog> parms)
  230. {
  231. var watcher = Stopwatch.StartNew();
  232. long t0 = 0, t1 = 0, t2 = 0, t3 = 0, t4 = 0;
  233. var parmsList = parms.ToList();
  234. if (parmsList.Count == 0)
  235. {
  236. return;
  237. }
  238. var workTime = DateTime.UtcNow;
  239. if (!await GetTableExist(workTime))
  240. {
  241. var candidate = parmsList.First();
  242. await WriteMachineLogEF(candidate);
  243. parmsList.Remove(candidate);
  244. }
  245. t0 = watcher.ElapsedMilliseconds;
  246. var table = new DataTable();
  247. table.Columns.Add("CreatedOn");
  248. table.Columns.Add("ChargeBoxId");
  249. table.Columns.Add("MessageType");
  250. table.Columns.Add("Data");
  251. table.Columns.Add("Msg");
  252. table.Columns.Add("IsSent");
  253. table.Columns.Add("EVSEEndPoint");
  254. table.Columns.Add("Session");
  255. foreach (var param in parmsList)
  256. {
  257. var row = table.NewRow();
  258. row["CreatedOn"] = workTime;
  259. row["ChargeBoxId"] = param.clientData.ChargeBoxId == null ? "unknown" : param.clientData.ChargeBoxId.Replace("'", "''");
  260. row["MessageType"] = param.messageType.Replace("'", "''");
  261. row["Data"] = param.data.Replace("'", "''");
  262. row["Msg"] = param.errorMsg.Replace("'", "''");
  263. row["IsSent"] = param.isSent;
  264. row["EVSEEndPoint"] = param.clientData.Endpoint == null ? "123" : param.clientData.Endpoint.ToString();
  265. row["Session"] = param.clientData.SessionID == null ? "123" : param.clientData.SessionID;
  266. table.Rows.Add(row);
  267. }
  268. t1 = watcher.ElapsedMilliseconds;
  269. using SqlConnection sqlConnection = await sqlConnectionFactory.CreateAsync();
  270. using SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(sqlConnection as SqlConnection);
  271. t2 = watcher.ElapsedMilliseconds;
  272. sqlBulkCopy.BatchSize = parmsList.Count();
  273. sqlBulkCopy.DestinationTableName = GetTableName(workTime);
  274. sqlBulkCopy.ColumnMappings.Add("CreatedOn", "CreatedOn");
  275. sqlBulkCopy.ColumnMappings.Add("ChargeBoxId", "ChargeBoxId");
  276. sqlBulkCopy.ColumnMappings.Add("MessageType", "MessageType");
  277. sqlBulkCopy.ColumnMappings.Add("Data", "Data");
  278. sqlBulkCopy.ColumnMappings.Add("Msg", "Msg");
  279. sqlBulkCopy.ColumnMappings.Add("IsSent", "IsSent");
  280. sqlBulkCopy.ColumnMappings.Add("EVSEEndPoint", "EVSEEndPoint");
  281. sqlBulkCopy.ColumnMappings.Add("Session", "Session");
  282. t3 = watcher.ElapsedMilliseconds;
  283. await sqlBulkCopy.WriteToServerAsync(table);
  284. watcher.Stop();
  285. t4 = watcher.ElapsedMilliseconds;
  286. if (t4 > 500)
  287. {
  288. logger.LogWarning("ConnectionLog BulkInsertWithBulkCopy Slow {0}/{1}/{2}/{3}/{4}/{5}", t0, t1, t2, t3, t4, parms.Count());
  289. }
  290. }
  291. private async ValueTask<bool> GetTableExist(DateTime tableDateTime)
  292. {
  293. var tableName = GetTableName(tableDateTime);
  294. if (_existTables.Contains(tableName))
  295. {
  296. return true;
  297. }
  298. FormattableString checkTableSql = $"SELECT Count(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = {tableName}";
  299. using var db = await connectionLogdbContextFactory.CreateDbContextAsync();
  300. var resultList = await db.Database.SqlQuery<int>(checkTableSql)?.ToListAsync();
  301. if (resultList is not null && resultList.Count > 0 && resultList[0] > 0)
  302. {
  303. _existTables.Enqueue(tableName);
  304. if (_existTables.Count > 30)
  305. {
  306. _existTables.TryDequeue(out _);
  307. }
  308. return true;
  309. }
  310. return false;
  311. }
  312. private static string GetTableName(DateTime dateTime)
  313. => $"MachineConnectionLog{dateTime:yyMMdd}";
  314. private int GetLimit(IConfiguration configuration)
  315. {
  316. var limitConfig = configuration[LimitConfigKey];
  317. int limit = 10;
  318. if (limitConfig != default)
  319. {
  320. int.TryParse(limitConfig, out limit);
  321. }
  322. return limit;
  323. }
  324. }
  325. internal record MachineLog(WsClientData clientData, string data, string messageType, string errorMsg, bool isSent);