Exemplo n.º 1
0
            /// <summary>
            /// 统计作息时间表
            /// </summary>
            /// <param name="userId"></param>
            /// <param name="year"></param>
            /// <param name="month"></param>
            /// <returns></returns>
            public static void GetRestTimeReport(Guid userId, int year, int month)
            {
                DateTime beginDate = new DateTime(year, month, 1);
                DateTime endDate = beginDate.AddMonths(1);
                int      monthDays = DateTime.DaysInMonth(beginDate.Year, beginDate.Month);
                int      restHour = 22, warningHour = 3, totalHour = 24;

                using (Muse db = new Muse())
                {
                    var normalRest = db.Do <WakaTimeDatas>().
                                     Where(x => x.EndDate >= beginDate && x.EndDate < endDate &&
                                           x.EndDate.Hour >= restHour && x.EndDate.Hour < totalHour).
                                     GroupBy(x => x.EndDate.Day).
                                     Select(x => new
                    {
                        Time = x.Max(e => e.EndDate),
                    }).OrderBy(x => x.Time).ToList();
                    DateTime tempbeginDate = beginDate.AddDays(1), tempendDate = endDate.AddDays(1);
                    var      dangerRest = db.Do <WakaTimeDatas>().
                                          Where(x => x.EndDate >= tempbeginDate && x.EndDate < tempendDate &&
                                                x.EndDate.Hour >= 0 && x.EndDate.Hour < warningHour).
                                          GroupBy(x => x.EndDate.Day).
                                          Select(x => new
                    {
                        Time = x.Max(e => e.EndDate),
                    }).OrderBy(x => x.Time).ToList();
                }
            }
Exemplo n.º 2
0
            /// <summary>
            /// 统计最近七天编程语言使用占用情况
            /// </summary>
            /// <param name="userId"></param>
            /// <returns></returns>
            public static List <Tuple <string, double> > LanguageCount()
            {
                DateTime today                    = DateTime.Now;
                DateTime sevenDaysAgo             = new DateTime(today.AddDays(-6).Year, today.AddDays(-6).Month, today.AddDays(-6).Day, 0, 0, 0);
                List <Tuple <string, double> > rs = null;

                using (Muse db = new Muse())
                {
                    if (db.Any <WakaTimeDatas>(x => x.ID != null, null))
                    {
                        var languageGroup = db.Do <WakaTimeDatas>().Where(x => x.Language != null && x.BeginDate >= sevenDaysAgo).GroupBy(x => x.Language).
                                            Select(x => new
                        {
                            Language = x.Max(e => e.Language),
                            Duration = x.Sum(y => y.Duration),
                        }).OrderByDescending(x => x.Duration).Take(MAX_LANGUAGE).ToList();

                        if (ListTool.HasElements(languageGroup))
                        {
                            rs = new List <Tuple <string, double> >();
                            languageGroup.ForEach(x =>
                            {
                                Tuple <string, double> rec = new Tuple <string, double>(x.Language, x.Duration);
                                rs.Add(rec);
                            });
                        }
                    }
                }
                return(rs);
            }
Exemplo n.º 3
0
        void ShowFileDetails(int row)
        {
            if (row >= 0)
            {
                string path = R.Services.FBS.Paths[row].Path;
                UIEnableButton(false);
                DgvFile.Rows.Clear();
                Task.Factory.StartNew(() =>
                {
                    using (var db = new Muse())
                    {
                        //db.Context.Database.Log = (sql) =>
                        //{
                        //    R.Log.i(sql);
                        //};
                        try
                        {
                            var result = db.Do <BackupFiles>().
                                         Where(x => x.FullPath.Contains(path)).
                                         GroupBy(x => new { x.FullPath }).
                                         Select(x => new
                            {
                                Path     = x.Max(o => o.FullPath),
                                BackPath = x.Max(o => o.BackupFullPath),
                                Count    = x.Count(),
                                Time     = x.Max(o => o.LastWriteTime),
                            }).OrderByDescending(x => x.Time).ToList();

                            if (ListTool.HasElements(result))
                            {
                                foreach (var item in result)
                                {
                                    //BackupFiles bkfile = bkfiles.FirstOrDefault(x => x.FullPath == file);
                                    //int versioncount = bkfiles.Count(x => x.FullPath == file);
                                    //string lastwritetime = bkfile != null ? bkfile.LastWriteTime : "-";
                                    string versiondesc = "第 " + item.Count + " 版";
                                    UIDgvFileAdd(Path.GetFileName(item.Path), item.Path, FileTool.SizeFormat(item.BackPath), versiondesc, item.Time);
                                }
                            }
                        }
                        catch (Exception e) { }

                        //List<BackupFiles> bkfiles = db.Gets<BackupFiles>(x => x.FullPath.Contains(path), null).ToList();
                        //List<string> files = FileTool.GetAllFile(path);
                        //if (ListTool.HasElements(files))
                        //{
                        //    foreach (var file in files)
                        //    {
                        //        BackupFiles bkfile = bkfiles.FirstOrDefault(x => x.FullPath == file);
                        //        int versioncount = bkfiles.Count(x => x.FullPath == file);
                        //        string versiondesc = "第 " + versioncount + " 版";
                        //        string lastwritetime = bkfile != null ? bkfile.LastWriteTime : "-";
                        //        UIDgvFileAdd(Path.GetFileName(file), file, FileTool.SizeFormat(file), versiondesc, lastwritetime);
                        //    }
                        //}
                    }
                    UIEnableButton(true);
                });
            }
        }
Exemplo n.º 4
0
            /// <summary>
            /// 获取所有年份
            /// </summary>
            /// <returns></returns>
            public static List<int> AllYears()
            {
                List<int> years = new List<int>();
                using (Muse db = new Muse())
                {
                    if (db.Any<WakaTimeDatas>(x => x.ID != null, null))
                    {
                        var data = db.Do<WakaTimeDatas>().
                              GroupBy(x => new { x.BeginDate.Year }).
                              Select(x => new
                              {
                                  Date = x.Max(y => y.BeginDate),
                              }).OrderByDescending(x => x.Date).ToList();

                        if (ListTool.HasElements(data))
                        {
                            data.ForEach(x =>
                            {
                                years.Add(x.Date.Year);
                            });
                        }
                    }
                }
                int year = DateTime.Now.Year;
                if (!years.Contains(year)) years.Add(year);
                return years;
            }
Exemplo n.º 5
0
            /// <summary>
            /// 统计所有编程语言使用占用情况
            /// </summary>
            /// <param name="userId"></param>
            /// <returns></returns>
            public static List <Tuple <string, double> > LanguageCount()
            {
                List <Tuple <string, double> > rs = null;

                using (Muse db = new Muse())
                {
                    if (db.Any <WakaTimeDatas>(x => x.ID != null, null))
                    {
                        var languageGroup = db.Do <WakaTimeDatas>().Where(x => x.Language != null).GroupBy(x => x.Language).
                                            Select(x => new
                        {
                            Language = x.Max(e => e.Language),
                            Duration = x.Sum(y => y.Duration),
                        }).OrderByDescending(x => x.Duration).Take(MAX_LANGUAGE).ToList();

                        if (ListTool.HasElements(languageGroup))
                        {
                            rs = new List <Tuple <string, double> >();
                            languageGroup.ForEach(x =>
                            {
                                Tuple <string, double> rec = new Tuple <string, double>(x.Language, x.Duration);
                                rs.Add(rec);
                            });
                        }
                    }
                }
                return(rs);
            }
Exemplo n.º 6
0
            /// <summary>
            /// 统计月度所有项目时间段情况(散点图)
            /// </summary>
            /// <param name="userId"></param>
            /// <param name="year"></param>
            /// <param name="month"></param>
            /// <returns></returns>
            public static List <Tuple <string, List <Tuple <DateTime, double> > > > BubbleReport(int year, int month)
            {
                List <Tuple <string, List <Tuple <DateTime, double> > > > rs = null;
                DateTime monthStart = new DateTime(year, month, 1);
                DateTime monthEnd   = monthStart.AddMonths(1);

                using (Muse db = new Muse())
                {
                    if (db.Any <WakaTimeDatas>(x => x.BeginDate >= monthStart && x.BeginDate < monthEnd, null))
                    {
                        var heartbeats = db.Do <WakaTimeDatas>().Where(x => x.BeginDate >= monthStart && x.BeginDate < monthEnd).OrderByDescending(x => x.Duration).Take(1000).ToList();
                        if (ListTool.HasElements(heartbeats))
                        {
                            rs = new List <Tuple <string, List <Tuple <DateTime, double> > > >();
                            heartbeats.ForEach(x =>
                            {
                                if (!rs.Any(y => y.Item1 == x.Project))
                                {
                                    rs.Add(new Tuple <string, List <Tuple <DateTime, double> > >(x.Project, new List <Tuple <DateTime, double> >()));
                                }
                                else
                                {
                                    var item = rs.FirstOrDefault(y => y.Item1 == x.Project);
                                    if (item != null)
                                    {
                                        item.Item2.Add(new Tuple <DateTime, double>(x.BeginDate, x.Duration));
                                    }
                                }
                            });
                        }
                    }
                }
                return(rs);
            }
Exemplo n.º 7
0
 /// <summary>
 /// 读取备份文件总数
 /// </summary>
 private void ReadBackupFileCount()
 {
     //统计备份文件总数
     using (var db = new Muse())
     {
         _FileCount = db.Do <BackupFiles>().Count();
     }
 }
Exemplo n.º 8
0
        private void GetFileToDatabase()
        {
            var drives = FileQueryEngine.GetReadyNtfsDrives().OrderBy(x => x.Name);

            if (ListTool.HasElements(drives))
            {
                foreach (var drive in drives)
                {
                    NewFileCount = 0;
                    //if (!drive.Name.Contains("J")) continue;//测试只读取D盘
                    //if (drive.Name.Contains("D")) continue;//测试时跳过D盘
                    //if (drive.Name.Contains("F")) continue;//测试时跳过F盘

                    using (var db = new Muse())
                    {
                        //检测磁盘是否格式化,如果格式化则清空USN记录
                        DateTime dt1 = DriveTool.GetLastFormatTime(drive.Name);
                        var      ds  = db.Get <UsnDrives>(x => x.Name == drive.Name, null);
                        if ((ds == null) || (ds != null && ds.LastFormatTime != dt1.ToString()))
                        {
                            var deleteSql = db.Context.Database.ExecuteSqlCommand("DELETE FROM usnfiles WHERE drive = @p0;", drive.Name);

                            if (ds == null)
                            {
                                db.Add(new UsnDrives()
                                {
                                    Name = drive.Name, LastFormatTime = dt1.ToString()
                                });
                            }
                            else
                            {
                                ds.LastFormatTime = dt1.ToString();
                                db.Update(ds, true);
                            }
                        }

                        //查询上次读取到的位置(最后一条记录)
                        ulong filenumber = 0;
                        long  usn        = 0;
                        if (db.Any <UsnFiles>(x => x.Drive == drive.Name, null))
                        {
                            int      lastId  = db.Do <UsnFiles>().Where(x => x.Drive == drive.Name).Max(x => x.Id);
                            UsnFiles lastRec = db.Get <UsnFiles>(x => x.Id == lastId, null);

                            usn        = lastRec.Usn;
                            filenumber = NumberStringTool.ToUlong(lastRec.Number);

                            //usn = db.Do<UsnFiles>().Where(x => x.Drive == drive.Name).Max(x => x.Usn);
                            //string filenumberstr = db.Do<UsnFiles>().Where(x => x.Drive == drive.Name).Max(x => x.Number);
                            //filenumber = NumberStringTool.ToUlong(filenumberstr);
                        }
                        //从上次FileNumber记录开始读取
                        var usnOperator = new UsnOperator(drive);
                        usnOperator.GetEntries(usn, filenumber, GetFileToDatabaseEvent, 1000);
                    }
                }
            }
        }
Exemplo n.º 9
0
 /// <summary>
 /// 获取所有项目统计列表
 /// </summary>
 /// <param name="userId"></param>
 /// <returns></returns>
 public static void GetProjectList(Guid userId)
 {
     using (Muse db = new Muse())
     {
         var projectCount = db.Do <WakaTimeDatas>().
                            GroupBy(x => new { x.Project }).
                            Select(x => new
         {
             Project  = x.Max(y => y.Project),
             Duration = x.Sum(y => y.Duration),
         }).OrderByDescending(x => x.Duration).ToList();
     }
 }
Exemplo n.º 10
0
            /// <summary>
            /// 获取编程总时长
            /// </summary>
            /// <returns></returns>
            public static double Career()
            {
                double career = 0;

                using (Muse db = new Muse())
                {
                    if (db.Any <WakaTimeDatas>(x => x.ID != null, null))
                    {
                        career = db.Do <WakaTimeDatas>().Sum(x => x.Duration);
                    }
                }
                return(career);
            }
Exemplo n.º 11
0
 /// <summary>
 /// 获取当月项目计时
 /// </summary>
 public static void GetMonthProjectTime()
 {
     using (Muse db = new Muse())
     {
         var projectCount = db.Do <WakaTimeDatas>().
                            Where(x => x.BeginDate.Year == DateTime.Now.Year && x.BeginDate.Month == DateTime.Now.Month).
                            GroupBy(x => new { x.Project }).
                            Select(x => new
         {
             Project  = x.Max(y => y.Project),
             Duration = x.Sum(y => y.Duration),
         }).OrderByDescending(x => x.Duration).Take(MAX_PROJECT).ToList();
     }
 }
Exemplo n.º 12
0
 /// <summary>
 /// 获取项目所有文件详细统计信息
 /// </summary>
 /// <param name="userId"></param>
 /// <param name="project"></param>
 /// <returns></returns>
 public static void GetProjectDetail(Guid userId, string project)
 {
     using (Muse db = new Muse())
     {
         var projectCount = db.Do <WakaTimeDatas>().
                            Where(x => x.Project == project).
                            GroupBy(x => new { x.Entity }).
                            Select(x => new
         {
             Entity   = x.Max(y => y.Entity),
             Duration = x.Sum(y => y.Duration),
         }).OrderByDescending(x => x.Duration).ToList();
     }
 }
Exemplo n.º 13
0
            /// <summary>
            /// 统计月总时长
            /// </summary>
            /// <returns></returns>
            public static double Career(int year, int month)
            {
                double career = 0;

                using (Muse db = new Muse())
                {
                    if (db.Any <WakaTimeDatas>(x => x.BeginDate.Year == year && x.BeginDate.Month == month, null))
                    {
                        career = db.Do <WakaTimeDatas>().Where(x => x.BeginDate.Year == year &&
                                                               x.BeginDate.Month == month).Sum(x => x.Duration);
                    }
                }
                return(career);
            }
Exemplo n.º 14
0
            /// <summary>
            /// 获取最近七天时长
            /// </summary>
            public static double Career()
            {
                double   career       = 0;
                DateTime today        = DateTime.Now;
                DateTime sevenDaysAgo = new DateTime(today.AddDays(-6).Year, today.AddDays(-6).Month, today.AddDays(-6).Day, 0, 0, 0);

                using (Muse db = new Muse())
                {
                    if (db.Any <WakaTimeDatas>(x => x.BeginDate >= sevenDaysAgo, null))
                    {
                        career = db.Do <WakaTimeDatas>().Where(x => x.BeginDate >= sevenDaysAgo).Sum(x => x.Duration);
                    }
                }
                return(career);
            }
Exemplo n.º 15
0
        private void BtAddPath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();

            dialog.Description = "请选择要备份的文件夹";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string selPath = dialog.SelectedPath;                                                                                    //获取选中的目录
                string path    = DirTool.Combine(selPath, "\\");                                                                         //格式化选中的目录
                string name    = DirTool.GetPathName(selPath);                                                                           //获取目录名称

                List <BackupPaths> clashPath = R.Services.FBS.Paths.Where(x => x.Path.Contains(path) || path.Contains(x.Path)).ToList(); //查询冲突项
                if (ListTool.HasElements(clashPath))
                {
                    string cp = "";
                    clashPath.ForEach(x => cp += (x.Path + ";"));
                    //存在重合目录
                    MessageBox.Show(string.Format("您当前选择路径:{0},与之前选择的目录:{1},存在嵌套包含关系,请先从备份目录中移除,然后重新添加。", path, cp));
                }
                else
                {
                    UIEnableButton(false);
                    Task.Factory.StartNew(() =>
                    {
                        using (var db = new Muse())
                        {
                            if (!db.Do <BackupPaths>().Any(x => x.Path == path))
                            {
                                BackupPaths bp = new BackupPaths()
                                {
                                    Path = path, Alias = Guid.NewGuid().ToString()
                                };
                                if (db.Add(bp) > 0)
                                {
                                    R.Services.FBS.Paths.Add(bp);             //添加到列表
                                    R.Services.FBS.AddToWatcherPath(bp.Path); //添加到监听
                                    UIDgvPathAdd(name, null);                 //添加到列表UI
                                }
                            }
                        }
                        UIEnableButton(true);
                    });
                }
            }
        }
Exemplo n.º 16
0
        private void BtToday_Click(object sender, EventArgs e)
        {
            DgvData.Rows.Clear();

            Task.Factory.StartNew(() =>
            {
                using (var db = new Muse())
                {
                    var fls = db.Do <FaultLogs>().SqlQuery("SELECT * FROM faultlogs WHERE createtime LIKE @p0", DateTime.Now.ToString("yyyy-MM-dd") + "%");
                    if (ListTool.HasElements(fls))
                    {
                        foreach (var f in fls)
                        {
                            UIAddRow(f);
                        }
                    }
                }
            });
        }
Exemplo n.º 17
0
            public static List<Tuple<DateTime, double>> MonthlyColumn(int year)
            {
                List<Tuple<DateTime, double>> rs = null;
                DateTime date = new DateTime(year, 1, 1);
                using (Muse db = new Muse())
                {
                    if (db.Any<WakaTimeDatas>(x => x.BeginDate.Year == date.Year, null))
                    {
                        var data = db.Do<WakaTimeDatas>().
                                    Where(x => x.BeginDate.Year == date.Year).
                                    GroupBy(x => new { x.BeginDate.Year, x.BeginDate.Month }).
                                    Select(x => new
                                    {
                                        Date = x.Max(y => y.BeginDate),
                                        Duration = x.Sum(y => y.Duration),
                                    }).OrderByDescending(x => x.Duration).ToList();

                        //整理数据库数据
                        if (ListTool.HasElements(data))
                        {
                            rs = new List<Tuple<DateTime, double>>();
                            data.ForEach(x =>
                            {
                                rs.Add(new Tuple<DateTime, double>(x.Date, x.Duration));
                            });
                        }
                        //补充缺失数据
                        if (ListTool.HasElements(rs))
                        {
                            for (int i = 0; i < 12; i++)
                            {
                                var dt = date.AddMonths(i);
                                if (!rs.Any(x => x.Item1.Year == dt.Year && x.Item1.Month == dt.Month))
                                {
                                    rs.Add(new Tuple<DateTime, double>(dt, 0));
                                }
                            }
                        }
                    }
                }
                return rs;
            }
Exemplo n.º 18
0
        private int GetTypeFileCount(string[] type)
        {
            int result = 0;

            try
            {
                using (var db = new Muse())
                {
                    if (ListTool.HasElements(type))
                    {
                        foreach (var t in type)
                        {
                            var count = db.Do <UsnFiles>().Count(x => !x.IsFolder && x.Name.EndsWith(t));
                            result += count;
                        }
                    }
                }
            }
            catch (Exception e) { }
            return(result);
        }
Exemplo n.º 19
0
        private void FaultLogInputPartial_Load(object sender, EventArgs e)
        {
            UICleanInput();

            Task.Factory.StartNew(() =>
            {
                using (var db = new Muse())
                {
                    var first = db.Get <FaultLogs>(x => x.Id > 0, null);

                    List <FaultLogs> fls = db.Do <FaultLogs>().SqlQuery("SELECT * FROM faultlogs WHERE createtime LIKE @p0", DateTime.Now.ToString("yyyy-MM-dd") + "%").ToList();
                    if (ListTool.HasElements(fls))
                    {
                        foreach (var f in fls)
                        {
                            UIAddRow(f);
                        }
                    }
                }
            });
        }
Exemplo n.º 20
0
 /// <summary>
 /// 删除超过备份最大次数的项
 /// </summary>
 private void DeleteExcess(string path)
 {
     using (var db = new Muse())
     {
         int count = db.Do <BackupFiles>().Count(x => x.FullPath == path);
         if (count >= R.Settings.FileBackup.BACK_UP_COUNT)
         {
             var fs = db.Gets <BackupFiles>(x => x.FullPath == path, null).OrderBy(x => x.Id).ToList();
             if (ListTool.HasElements(fs))
             {
                 for (int i = 0; i <= count - R.Settings.FileBackup.BACK_UP_COUNT; i++)
                 {
                     try
                     {
                         File.Delete(fs[i].BackupFullPath);
                         db.Del(fs[i], true);
                     }
                     catch (Exception e) { }
                 }
             }
         }
     }
 }
Exemplo n.º 21
0
 /// <summary>
 /// 读取备份文件目录
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void TmReadPaths_Tick(object sender, EventArgs e)
 {
     if (R.Services.FBS.StatusOfReadBackupPaths)
     {
         TmReadPaths.Enabled = false;
         Task.Factory.StartNew(() =>
         {
             if (ListTool.HasElements(R.Services.FBS.Paths))
             {
                 foreach (var p in R.Services.FBS.Paths)
                 {
                     using (var db = new Muse())
                     {
                         long size   = db.Do <BackupFiles>().Where(x => x.FullPath.Contains(p.Path)).Sum(x => x.Size);
                         string name = DirTool.GetPathName(p.Path);     //获取目录名称
                         UIDgvPathAdd(name, ByteConvertTool.Fmt(size)); //添加到列表UI
                     }
                 }
             }
             UIEnableButton(true);
         });
     }
 }
Exemplo n.º 22
0
        private void BtAdd_Click(object sender, EventArgs e)
        {
            FaultLogs fl = new FaultLogs()
            {
                Ip         = TbIp.Text,
                Phone      = TbPhone.Text,
                Address    = TbAddress.Text,
                Problem    = TbProblem.Text,
                Solution   = TbSolution.Text,
                Postscript = TbPostscript.Text,
                System     = CbSystem.Text,
                CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                IsFinish   = CbIsFinish.Checked,
                FinishTime = CbIsFinish.Checked ? DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") : "",
            };

            UICleanInput();

            Task.Factory.StartNew(() =>
            {
                UIAddButton(false);
                using (var db = new Muse())
                {
                    db.Add(fl);

                    var fls = db.Do <FaultLogs>().SqlQuery("SELECT * FROM faultlogs WHERE createtime LIKE @p0", DateTime.Now.ToString("yyyy-MM-dd") + "%");
                    if (ListTool.HasElements(fls))
                    {
                        foreach (var f in fls)
                        {
                            UIAddRow(f);
                        }
                    }
                }
                UIAddButton(true);
            });
        }
Exemplo n.º 23
0
        private void BtSearch_Click(object sender, EventArgs e)
        {
            string ip      = string.Format("%{0}%", TbIp.Text);      //TbIp.Text != "" ? string.Format("%{0}%", TbIp.Text) : Guid.NewGuid().ToString();
            string phone   = string.Format("%{0}%", TbPhone.Text);   //TbPhone.Text != "" ? string.Format("%{0}%", TbPhone.Text) : Guid.NewGuid().ToString();
            string address = string.Format("%{0}%", TbAddress.Text); //TbAddress.Text != "" ? string.Format("%{0}%", TbAddress.Text) : Guid.NewGuid().ToString();

            DgvData.Rows.Clear();
            Task.Factory.StartNew(() =>
            {
                UIAddButton(false);
                using (var db = new Muse())
                {
                    var fls = db.Do <FaultLogs>().SqlQuery("SELECT * FROM faultlogs WHERE ip LIKE @p0 and phone LIKE @p1 and address LIKE @p2", ip, phone, address);
                    if (ListTool.HasElements(fls))
                    {
                        foreach (var f in fls)
                        {
                            UIAddRow(f);
                        }
                    }
                }
                UIAddButton(true);
            });
        }
Exemplo n.º 24
0
            /// <summary>
            /// 获取最近七天信息
            /// </summary>
            public static List <Tuple <string, List <Tuple <DateTime, double> > > > Report()
            {
                List <Tuple <string, List <Tuple <DateTime, double> > > > rs = null;
                DateTime today        = DateTime.Now;
                DateTime sevenDaysAgo = new DateTime(today.AddDays(-6).Year, today.AddDays(-6).Month, today.AddDays(-6).Day, 0, 0, 0);

                using (Muse db = new Muse())
                {
                    if (db.Any <WakaTimeDatas>(x => x.BeginDate >= sevenDaysAgo, null))
                    {
                        //查询数据库
                        var weekCount = db.Do <WakaTimeDatas>().
                                        Where(x => x.BeginDate >= sevenDaysAgo).
                                        GroupBy(x => new { x.BeginDate.Year, x.BeginDate.Month, x.BeginDate.Day, x.Project }).
                                        Select(x => new
                        {
                            Date     = x.Max(y => y.BeginDate),
                            Project  = x.Max(y => y.Project),
                            Duration = x.Sum(y => y.Duration),
                        }).OrderByDescending(x => x.Date).ToList();
                        //整理数据库数据
                        if (ListTool.HasElements(weekCount))
                        {
                            rs = new List <Tuple <string, List <Tuple <DateTime, double> > > >();
                            weekCount.ForEach(x =>
                            {
                                if (!rs.Any(y => y.Item1 == x.Project))
                                {
                                    var rec = new List <Tuple <DateTime, double> >();
                                    rec.Add(new Tuple <DateTime, double>(x.Date, x.Duration));
                                    rs.Add(new Tuple <string, List <Tuple <DateTime, double> > >(x.Project, rec));
                                }
                                else
                                {
                                    var item = rs.FirstOrDefault(y => y.Item1 == x.Project);
                                    if (item != null)
                                    {
                                        item.Item2.Add(new Tuple <DateTime, double>(x.Date, x.Duration));
                                    }
                                }
                            });
                        }
                        //补充缺失数据
                        if (ListTool.HasElements(rs))
                        {
                            foreach (var project in rs)
                            {
                                if (ListTool.HasElements(project.Item2))
                                {
                                    for (int i = 0; i < 7; i++)
                                    {
                                        var dt = new DateTime(today.AddDays(-i).Year, today.AddDays(-i).Month, today.AddDays(-i).Day, 0, 0, 0);
                                        if (!project.Item2.Any(x => x.Item1.Year == dt.Year && x.Item1.Month == dt.Month && x.Item1.Day == dt.Day))
                                        {
                                            project.Item2.Add(new Tuple <DateTime, double>(dt, 0));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                return(rs);
            }
Exemplo n.º 25
0
            /// <summary>
            /// 统计月度所有项目报表(堆叠柱状图)
            /// </summary>
            /// <param name="userId"></param>
            /// <param name="year"></param>
            /// <param name="month"></param>
            /// <returns></returns>
            public static List <Tuple <string, List <Tuple <DateTime, double> > > > Report(int year, int month)
            {
                List <Tuple <string, List <Tuple <DateTime, double> > > > rs = null;
                DateTime monthStart = new DateTime(year, month, 1);
                DateTime monthEnd   = monthStart.AddMonths(1);
                int      totalDays  = (int)(monthEnd - monthStart).TotalDays;

                using (Muse db = new Muse())
                {
                    if (db.Any <WakaTimeDatas>(x => x.BeginDate >= monthStart && x.BeginDate < monthEnd, null))
                    {
                        var monthCount = db.Do <WakaTimeDatas>().
                                         Where(x => x.BeginDate >= monthStart && x.BeginDate < monthEnd).
                                         GroupBy(x => new { x.BeginDate.Year, x.BeginDate.Month, x.BeginDate.Day, x.Project }).
                                         Select(x => new
                        {
                            Date     = x.Max(y => y.BeginDate),
                            Project  = x.Max(y => y.Project),
                            Duration = x.Sum(y => y.Duration),
                        }).OrderByDescending(x => x.Duration).ToList();

                        //整理数据库数据
                        if (ListTool.HasElements(monthCount))
                        {
                            rs = new List <Tuple <string, List <Tuple <DateTime, double> > > >();
                            monthCount.ForEach(x =>
                            {
                                if (!rs.Any(y => y.Item1 == x.Project))
                                {
                                    var rec = new List <Tuple <DateTime, double> >();
                                    rec.Add(new Tuple <DateTime, double>(x.Date, x.Duration));
                                    rs.Add(new Tuple <string, List <Tuple <DateTime, double> > >(x.Project, rec));
                                }
                                else
                                {
                                    var item = rs.FirstOrDefault(y => y.Item1 == x.Project);
                                    if (item != null)
                                    {
                                        item.Item2.Add(new Tuple <DateTime, double>(x.Date, x.Duration));
                                    }
                                }
                            });
                        }
                        //补充缺失数据
                        if (ListTool.HasElements(rs))
                        {
                            foreach (var project in rs)
                            {
                                if (ListTool.HasElements(project.Item2))
                                {
                                    for (int i = 0; i < totalDays; i++)
                                    {
                                        var dt = new DateTime(monthStart.AddDays(i).Year, monthStart.AddDays(i).Month, monthStart.AddDays(i).Day, 0, 0, 0);
                                        if (!project.Item2.Any(x => x.Item1.Year == dt.Year && x.Item1.Month == dt.Month && x.Item1.Day == dt.Day))
                                        {
                                            project.Item2.Add(new Tuple <DateTime, double>(dt, 0));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                return(rs);
            }