public Task Execute(IJobExecutionContext context) { Console.WriteLine("Starting statistics persistence in database"); var calculation = (INewCalculationTask)context.MergedJobDataMap.Get("calculation"); try { using (StatisticContext dbContext = new StatisticContext()) { dbContext.Database.EnsureCreated(); foreach (var operation in calculation.GetOperations()) { foreach (KeyValuePair <String, Statistic> kvp in operation.GetComputeDictionary()) { StatisticType statisticType = dbContext.StatisticTypes.Single(st => st.Name == operation.GetName()); kvp.Value.StatisticType = statisticType; dbContext.Statistics.Add(kvp.Value); } operation.GetComputeDictionary().Clear(); } dbContext.SaveChanges(); } } catch (Exception ex) { Console.WriteLine(ex); } Console.WriteLine("Persistence finished"); return(Task.FromResult(0)); }
public ActionResult <List <StatisticType> > GetCalculationTypes() { using (StatisticContext context = new StatisticContext()) { return(context.StatisticTypes.ToList()); } }
public static void initSelectors() { using (var ctx = new StatisticContext()) { ExistingSelectors = ctx.Selectors.Select(_ => _.SelectorName).ToList(); } }
/// <summary> /// 查找日期数据,如果不存在则创建 /// </summary> /// <param name="date"></param> /// <returns></returns> public StatisticModel FindCreate(DateTime date) { if (date.Date == DateTime.Now.Date && todayStatistic != null && todayStatistic.Date == date.Date) { //当日 return todayStatistic; } else { //非当日从数据库中查找 using (var db = new StatisticContext()) { var res = db.Statistics.Where(m => m.Date == date.Date); if (res.Count() == 0) { //数据库中没有时则创建 var dateData = GetNewdayData(date); db.Statistics.Add(dateData); db.SaveChanges(); return dateData; } else { return res.ToList().FirstOrDefault(); } } } }
/// <summary> /// 查找日期数据,如果不存在则创建 /// </summary> /// <param name="date"></param> /// <returns></returns> public Models.Statistic.TomatoModel FindCreateData(DateTime date) { if (date.Date == DateTime.Now.Date && tomatoDataToday != null && tomatoDataToday.Date == date.Date) { //当日 return(tomatoDataToday); } else { //非当日从数据库中查找 using (var db = new StatisticContext()) { var res = db.Tomatos.Where(m => m.Date == date.Date); if (res.Count() == 0) { //数据库中没有时则创建 var dateData = new Models.Statistic.TomatoModel() { Date = date.Date, RestartCount = 0, TomatoCount = 0 }; db.Tomatos.Add(dateData); db.SaveChanges(); return(dateData); } else { return(res.ToList().FirstOrDefault()); } } } }
/// <summary> /// 创建本月的数据 /// </summary> private void CreateMonthlyItems(DateTime dateTime) { int days = DateTime.DaysInMonth(dateTime.Year, dateTime.Month); int today = dateTime.Day; using (var db = new StatisticContext()) { for (int i = 0; i < days; i++) { var date = dateTime.AddDays(-today + (i + 1)).Date; if (db.Statistics.Where(m => m.Date == date).Count() == 0) { //补上缺少的日期 db.Statistics.Add(new StatisticModel() { Date = date, ResetTime = 0, SkipCount = 0, WorkingTime = 0 }); } } db.SaveChanges(); } }
public ActionResult <List <GroupedStatisticDto> > GetCalculation( String sensorId, [FromQuery] String aggregationType, [FromQuery] String groupBy, [FromQuery] String startAt, [FromQuery] String endAt ) { using (StatisticContext context = new StatisticContext()) { DateTime startAtDate = DateTime.ParseExact(startAt, "yyyy-MM-dd", CultureInfo.InvariantCulture); startAtDate.AddHours(-12); DateTime endAtDate = DateTime.ParseExact(endAt, "yyyy-MM-dd", CultureInfo.InvariantCulture); endAtDate.AddHours(-12); Console.WriteLine("startDate: " + startAtDate); Console.WriteLine("endDate: " + endAtDate); IEnumerable <Statistic> statistics = context.Statistics.Where(s => s.SensorName == sensorId && s.StatisticType.Name == aggregationType && s.EndAt >= startAtDate && s.EndAt <= endAtDate ); IEnumerable <IGrouping <int, Statistic> > groupedStatistics; switch (groupBy) { case "day": groupedStatistics = statistics.GroupBy(s => s.EndAt.Day); break; case "month": groupedStatistics = statistics.GroupBy(s => s.EndAt.Month); break; case "year": groupedStatistics = statistics.GroupBy(s => s.EndAt.Year); break; default: groupedStatistics = statistics.GroupBy(s => s.EndAt.Hour); break; } return(groupedStatistics .Select(group => new GroupedStatisticDto { Key = group.Key, Value = group.Sum(e => e.Value) / group.Count() } ) .ToList()); } }
private MicroserviceInformation GetFinesInformation(StatisticContext context) { var FineStatistic = context.Statistics.Where(x => x.ServerName == ServerName.FINES).ToList(); List <int> ArrayTime = new List <int>(24); for (int i = 0; i < 24; i++) { ArrayTime.Add(0); } List <int> arrayType = new List <int>(3); for (int i = 0; i < 3; i++) { arrayType.Add(0); } foreach (var item in FineStatistic) { int num = (int)item.RequestType; if (num == 1) { arrayType[0] += 1; } if (num == 2) { arrayType[1]++; } if (num == 3) { arrayType[2]++; } //string q = item.Detail.ToString(); //string[] w = q.Split(' '); //if (w[0] == "PUT") //{ // arrayType[0] += 1; //} //if (w[0] == "POST") //{ // arrayType[1]++; //} //if (w[0] == "DELETE") //{ // arrayType[2]++; //} ArrayTime[item.Time.Value.Hour]++; } MicroserviceInformation Information = new MicroserviceInformation(); Information.StatTime = ArrayTime; Information.StatType = arrayType; return(Information); }
public void WriteToDB(string path, EntityType type) { using (var ctx = new StatisticContext()) { var selector = new Selector() { SelectorName = path, AddedDate = DateTime.Now, Type = type }; ctx.Selectors.Add(selector); ctx.SaveChanges(); } }
public void WriteToDB(string path, EntityType type) { using (var ctx = new StatisticContext()) { var saga = new SagaAction() { SagaName = path, AddedDate = DateTime.Now, Type = type }; ctx.Sagas.Add(saga); ctx.SaveChanges(); } }
public async Task <IHttpActionResult> GetAllInformation() { StatisticContext Context = new StatisticContext(); StatisticInformation Information = new StatisticInformation(); Information.GateInfo = GetGatewayInformation(Context); Information.FinesInfo = GetFinesInformation(Context); Information.UsersInfo = GetUsersInformation(Context); Information.MachineInfo = GetMachineInformation(Context); return(Ok <StatisticInformation>(Information)); }
public void WriteToDB(string path, EntityType type) { using (var ctx = new StatisticContext()) { var test = new ClientTest() { Name = path, CreationDate = DateTime.Now, Type = type }; ctx.ClientTests.Add(test); ctx.SaveChanges(); } }
/// <summary> /// 查找范围内的数据 /// </summary> /// <param name="startDate">开始日期</param> /// <param name="endDate">结束日期</param> /// <returns></returns> public List<StatisticModel> GetData(DateTime startDate, DateTime endDate) { var result = new List<StatisticModel>(); startDate = startDate.AddDays(-1); using (var db = new StatisticContext()) { if (db.Statistics.Where(m => m.Date == endDate.Date).Count() == 0) { CreateMonthlyItems(endDate); } result = db.Statistics.Where(m => m.Date > startDate && m.Date <= endDate).OrderBy(m => m.Date).ToList(); } return result; }
/// <summary> /// 获取某月的数据 /// </summary> /// <param name="year">年</param> /// <param name="month">月</param> /// <returns></returns> public List<StatisticModel> GetData(int year = 0, int month = 0) { var result = new List<StatisticModel>(); if (year == 0) { year = DateTime.Now.Year; } if (month == 0) { month = DateTime.Now.Month; } using (var db = new StatisticContext()) { result = db.Statistics.Where(m => m.Date.Year == year && m.Date.Month == month).OrderBy(m => m.Date).ToList(); } return result; }
/// <summary> /// 数据持久化 /// </summary> public void SaveData() { backgroundWorker.AddAction(() => { if (tomatoDataToday == null) { tomatoDataToday = FindCreateTodayData(); } using (var db = new StatisticContext()) { var item = (from c in db.Tomatos where c.Date == tomatoDataToday.Date select c).FirstOrDefault(); item.TomatoCount = tomatoDataToday.TomatoCount; item.RestartCount = tomatoDataToday.RestartCount; db.SaveChanges(); } }); backgroundWorker.Run(); }
private GatewayInformation GetGatewayInformation(StatisticContext context) { var GateStatistic = context.Statistics.Where(x => x.ServerName == ServerName.GATEWAY).ToList(); int CountUnauthorized = 0; int CountAccess = 0; List <int> arrayTime = new List <int>(24); for (int i = 0; i < 24; i++) { arrayTime.Add(0); } List <int> arrayType = new List <int>(5); for (int i = 0; i < 5; i++) { arrayType.Add(0); } foreach (var item in GateStatistic) { string q = item.Detail.ToString(); string[] w = q.Split(' '); if (w[0] == "Unauthorized") { CountUnauthorized++; } if (w[0] == "Access") { CountAccess++; } arrayTime[item.Time.Value.Hour]++; arrayType[(int)item.RequestType]++; } GatewayInformation GateInfo = new GatewayInformation(); GateInfo.NonAuth = CountUnauthorized; GateInfo.Auth = CountAccess; GateInfo.StatTime = arrayTime; GateInfo.StatType = arrayType; return(GateInfo); }
/// <summary> /// 数据持久化 /// </summary> public void Save() { backgroundWorker.AddAction(() => { if (todayStatistic == null) { todayStatistic = FindCreate(); } using (var db = new StatisticContext()) { //var item = db.Statistics.Where(m => m.Date == todayStatistic.Date).Single(); var item = (from c in db.Statistics where c.Date == todayStatistic.Date select c).FirstOrDefault(); item.ResetTime = todayStatistic.ResetTime; item.SkipCount = todayStatistic.SkipCount; item.WorkingTime = todayStatistic.WorkingTime; db.SaveChanges(); } }); backgroundWorker.Run(); }
private MicroserviceInformation GetUsersInformation(StatisticContext context) { var UserStatistic = context.Statistics.Where(x => x.ServerName == ServerName.USERS).ToList(); List <int> ArrayTime = new List <int>(24); for (int i = 0; i < 24; i++) { ArrayTime.Add(0); } List <int> arrayType = new List <int>(3); for (int i = 0; i < 3; i++) { arrayType.Add(0); } foreach (var item in UserStatistic) { int num = (int)item.RequestType; if (num == 1) { arrayType[0] += 1; } if (num == 2) { arrayType[1]++; } if (num == 3) { arrayType[2]++; } ArrayTime[item.Time.Value.Hour]++; } MicroserviceInformation Information = new MicroserviceInformation(); Information.StatTime = ArrayTime; Information.StatType = arrayType; return(Information); }
//new code #region 迁移数据,从xml到sqlite。下一个版本将弃用 :) /// <summary> /// 迁移数据,从xml到sqlite。下一个版本将弃用 :) /// </summary> private void MigrateXMLDataToDb() { if (File.Exists(xmlPath)) { //需要迁移 xml = new XmlExtensions(xmlPath); var data = xml.ToModel(typeof(StatisticListModel)); if (data != null) { statisticList = data as StatisticListModel; if (statisticList != null && statisticList.Data != null && statisticList.Data.Count > 0) { using (var db = new StatisticContext()) { foreach (var item in statisticList.Data) { db.Statistics.Add(item); } db.SaveChanges(); //备份数据文件 File.Copy(xmlPath, xmlPath + ".migrate.backup"); //删除原数据文件 File.Delete(xmlPath); //迁移标记文件,在用户收到迁移提示后将删除 File.WriteAllText(xmlPath + ".migrate.mark", ""); } } } } //迁移判断 IsMigrated = File.Exists(xmlPath + ".migrate.mark"); }
/// <summary> /// 创建本月的数据 /// </summary> private void CreateMonthlyData(DateTime dateTime) { int days = DateTime.DaysInMonth(dateTime.Year, dateTime.Month); int today = dateTime.Day; using (var db = new StatisticContext()) { for (int i = 0; i < days; i++) { var date = dateTime.AddDays(-today + (i + 1)).Date; if (db.Tomatos.Where(m => m.Date == date).Count() == 0) { //补上缺少的日期 db.Tomatos.Add(new Models.Statistic.TomatoModel() { Date = date, RestartCount = 0, TomatoCount = 0 }); } } db.SaveChanges(); } }
public static void WriteFirstSelector() { List <string> result = new List <string>(); StreamReader reader = File.OpenText(@"D:\selector.txt"); string line; while ((line = reader.ReadLine()) != null) { result.Add(line); } using (var ctx = new StatisticContext()) { foreach (var s in result) { var selector = new Selector() { SelectorName = s, AddedDate = DateTime.Now }; ctx.Selectors.Add(selector); ctx.SaveChanges(); } } }
public StatisticDataManager(StatisticContext context) { _context = context; }
public UserDatasController(StatisticContext context) { _context = context; }
public DbAccess(StatisticContext db) { _db = db ?? throw new ArgumentNullException(nameof(db)); }
private async void backwork() { StatisticContext db2 = new StatisticContext(); MessageQueue InputQueue; if (MessageQueue.Exists(@".\private$\InputStatistic")) { InputQueue = new MessageQueue(@".\private$\InputStatistic"); } else { InputQueue = MessageQueue.Create(".\\private$\\InputStatistic"); } MessageQueue OutputQueue; if (MessageQueue.Exists(@".\private$\OutputStatistic")) { OutputQueue = new MessageQueue(@".\private$\OutputStatistic"); } else { OutputQueue = MessageQueue.Create(".\\private$\\OutputStatistic"); } using (InputQueue) { Statistic.Models.Statistic statistic = new Statistic.Models.Statistic(); InputStatisticMessage InputMessage = new InputStatisticMessage(); OutputStatisticMessage OutputMessage = new OutputStatisticMessage(); Message msg = new Message(); InputQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(InputStatisticMessage) }); OutputQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(OutputStatisticMessage) }); while (InputQueue.CanRead) { Message msgInput = InputQueue.Receive(); InputMessage = (InputStatisticMessage)msgInput.Body; statistic.RequestType = InputMessage.RequestType; statistic.ServerName = InputMessage.ServerName; statistic.Time = InputMessage.Time; statistic.Detail = InputMessage.Detail; statistic.State = InputMessage.State; try { db2.Statistics.Add(statistic); await db2.SaveChangesAsync(); } catch (DbUpdateException ex) { OutputStatisticMessage OutputMessageCatch = new OutputStatisticMessage(); OutputMessageCatch.Status = -1; OutputMessageCatch.Error = "Error in Input Queue"; OutputMessageCatch.Message = InputMessage; Message Msg = new Message(OutputMessageCatch); switch (OutputMessageCatch.Message.ServerName) { case ServerName.AUTHENTICATION: Msg.Label = "NON_AUTH"; break; case ServerName.GATEWAY: Msg.Label = "NON_GATEWAY"; break; case ServerName.USERS: Msg.Label = "NON_USERS"; break; case ServerName.MACHINES: Msg.Label = "NON_MACHINES"; break; case ServerName.FINES: Msg.Label = "NON_FINES"; break; } OutputQueue.Send(Msg); continue; } OutputMessage.Status = 0; OutputMessage.Error = ""; OutputMessage.Message = InputMessage; msg.Body = OutputMessage; switch (OutputMessage.Message.ServerName) { case ServerName.AUTHENTICATION: msg.Label = "NON_AUTH"; break; case ServerName.GATEWAY: msg.Label = "NON_GATEWAY"; break; case ServerName.USERS: msg.Label = "NON_USERS"; break; case ServerName.MACHINES: msg.Label = "NON_MACHINES"; break; case ServerName.FINES: msg.Label = "NON_FINES"; break; } OutputQueue.Send(msg); } } }
public TokenController(StatisticContext context) { _context = context; }
public CalculateStatistics(StatisticContext statisticContext) { _statisticContext = statisticContext; }
public KafkaAPIController(StatisticContext db, IDbAccess dbAccess) { _db = db ?? throw new ArgumentNullException(nameof(db)); _dbAccess = dbAccess ?? throw new ArgumentNullException(nameof(dbAccess)); }