using Mogafa.App.LogEvents; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; namespace Mogafa.App.Assets.EventIntercepts { public class AssetChangedEventGenerator { private readonly List assetChangedEventConfigurations; private readonly Dictionary maxDigits = new Dictionary(); public AssetChangedEventGenerator(List assetChangedEventConfigurations) { this.assetChangedEventConfigurations = assetChangedEventConfigurations; if(this.assetChangedEventConfigurations == null) { this.assetChangedEventConfigurations = new List(); } foreach(var ace in this.assetChangedEventConfigurations) { if (ace.Values.Count == 0) { continue; } var maxValue = ace.Values.Max(); var maxLength = maxValue.ToString().Length; if (maxDigits.ContainsKey(ace.AssetCode)) { var oldLength = maxDigits[ace.AssetCode]; if (oldLength > maxLength) { maxLength = oldLength; } maxDigits.Remove(ace.AssetCode); } maxDigits.Add(ace.AssetCode, maxLength); } } public async void AssetIncreasedCallback(string code, long newValue, long oldValue) { var config = assetChangedEventConfigurations.FirstOrDefault(a => a.AssetCode == code); if(config == null) { return; } var targets = config.Values.Where(v => v > oldValue && v <= newValue); foreach(var target in targets) { var value = CountString(code, target); await LogEventReporter.LogEvent($"{config.EventName}_{value}", new Dictionary()); } } private string CountString(string code, long value) { if (!maxDigits.ContainsKey(code)) { return value.ToString(); } var digits = maxDigits[code]; var countString = value.ToString(); return countString.PadLeft(digits, '0'); } public void AssetReducedCallback(string code, long newValue, long oldValue) { } } public class AssetChangedEventConfiguration { [JsonProperty("assetCode")] public string AssetCode; [JsonProperty("eventName")] public string EventName; [JsonProperty("values")] public List Values; } }