using Mogafa.Common; using Mogafa.Common.Storages; using Newtonsoft.Json; using System; using System.Linq; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; namespace Mogafa.App.LogEvents.Intercepts { public class EventCounterIntercept : MogafaBase, ILogEventIntercept { private readonly string EventCounterStorageKey = "MogafaEventCounterStorageKey"; private readonly List eventCounterConfigurations; private readonly Dictionary maxDigits = new Dictionary(); public EventCounterIntercept(List eventCounterConfigurations) { this.eventCounterConfigurations = eventCounterConfigurations; if (this.eventCounterConfigurations == null) { this.eventCounterConfigurations = new List(); } foreach(var ecc in this.eventCounterConfigurations) { if (string.IsNullOrEmpty(ecc.Name) || ecc.CountList == null || ecc.CountList.Count == 0) { continue; } var maxCount = ecc.CountList.Max(); var maxLength = maxCount.ToString().Length; if (maxDigits.ContainsKey(ecc.Name)) { var oldLength = maxDigits[ecc.Name]; if(oldLength > maxLength) { maxLength = oldLength; } maxDigits.Remove(ecc.Name); } maxDigits.Add(ecc.Name, maxLength); Logger.LogDebug($"event name:{ecc.Name}, max length:{maxLength}"); } } public string Name => "EventCounterIntercept"; public int Order => 100; public async Task>> Execute(string eventName, Dictionary parameters) { var events = new Dictionary> { { eventName, parameters } }; if (eventCounterConfigurations.Count == 0) { return events; } var needCountEvent = eventCounterConfigurations.FirstOrDefault(ec => (ec.Name.EndsWith("_") && (eventName.StartsWith(ec.Name) || eventName == ec.Name.Substring(0, ec.Name.Length - 1)) || (!ec.Name.EndsWith("_") && eventName == ec.Name))); if(needCountEvent == null) { return events; } var eventCountList = await LocalStorage.Get>(EventCounterStorageKey); if(eventCountList == null) { eventCountList = new List(); } var eventCount = eventCountList.FirstOrDefault(ec => ec.Name == eventName); if(eventCount == null) { eventCount = new EventCounterDao { Name = eventName }; eventCountList.Add(eventCount); } eventCount.Count += 1; await LocalStorage.Set(EventCounterStorageKey, eventCountList); await LocalStorage.Save(); if (needCountEvent.CountList.Contains(eventCount.Count)) { var counter = CountString(needCountEvent.Name, eventCount.Count); events.Add($"{eventName}_{counter}", parameters); } return events; } private string CountString(string eventName, int count) { if (!maxDigits.ContainsKey(eventName)) { return count.ToString(); } var digits = maxDigits[eventName]; var countString = count.ToString(); return countString.PadLeft(digits, '0'); } } public class EventCounterConfiguration { public EventCounterConfiguration() { CountList = new List(); } /// /// 需要计数的事件名称, /// 如果名称以_结束,则表示所有以此开始的事件都需要计数 /// [JsonProperty("name")] public string Name { get; set; } /// /// 需要计数个数的列表 /// [JsonProperty("countList")] public List CountList { get; set; } } public class EventCounterDao { public EventCounterDao() { Count = 0; } [JsonProperty("name")] public string Name { get; set; } [JsonProperty("count")] public int Count { get; set; } } }