using Mogafa.Common; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Threading.Tasks; namespace Mogafa.App.LogEvents.Intercepts { public class ReportCountEventParameterIntercept : MogafaBase, ILogEventParameterIntercept { private readonly string fileName = "mogafaReportCount.db"; private readonly string filePath; private Dictionary counts = new Dictionary(); private readonly Dictionary currentCounts = new Dictionary(); public ReportCountEventParameterIntercept(string filePath) { this.filePath = filePath; counts = GetDb(); } private Dictionary GetDb() { var file = Path.Combine(filePath, fileName); if (!File.Exists(file)) { File.WriteAllText(file, "{}"); } var content = File.ReadAllText(file); var oldData = new Dictionary(); try { oldData = JsonConvert.DeserializeObject>(content); } catch (Exception ex) { Logger.LogError($"Get data from file {file} error: {ex.Message}"); return oldData; } return oldData; } private void SaveDb() { var file = Path.Combine(filePath, fileName); if (!File.Exists(file)) { File.WriteAllText(file, "{}"); } var json = JsonConvert.SerializeObject(counts); File.WriteAllText(file, json); } public string Name => "EventReportCount"; public int Order => 100; public Task> Execute(string eventName, Dictionary parameters) { var newParameters = new Dictionary(); var count = 0; if (!counts.ContainsKey(eventName)) { counts.Add(eventName, count); } count = counts[eventName]; count++; counts[eventName] = count; SaveDb(); newParameters.Add("ddbEventCumulativeCount", count.ToString()); count = 0; if (!currentCounts.ContainsKey(eventName)) { currentCounts.Add(eventName, count); } count = currentCounts[eventName]; count++; currentCounts[eventName] = count; newParameters.Add("ddbEventCount", count.ToString()); return Task.FromResult(newParameters); } } }