zhaolei
2020-11-20 4a2e5b9a21940f11757be37d99f0944e240e908b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using Bootstrap.DataAccess;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
using Prow.Tasks;
using Microsoft.AspNetCore.Authorization;
 
namespace Bootstrap.Admin.Controllers.Api
{
    /// <summary>
    /// 系统通知控制器
    /// </summary>
    [Route("api/[controller]")]
    [Authorize]
    [ApiController]
    public class NotificationsController : ControllerBase
    {
        /// <summary>
        /// 后台 Header 状态条调用
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public object Get()
        {
            if (!User.IsInRole("Administrators")) return false;
 
            // New Users
            var user = UserHelper.RetrieveNewUsers();
            var usersCount = user.Count();
 
            user = user.Take(6);
            user.AsParallel().ForAll(n =>
            {
                var ts = DateTime.Now - n.RegisterTime;
                if (ts.TotalMinutes < 5) n.Period = "刚刚";
                else if (ts.Days > 0) n.Period = string.Format("{0}天", ts.Days);
                else if (ts.Hours > 0) n.Period = string.Format("{0}小时", ts.Hours);
                else if (ts.Minutes > 0) n.Period = string.Format("{0}分钟", ts.Minutes);
            });
 
            // Tasks
            var task = TaskServicesManager.ToList().Where(s => s.NextRuntime != null).Select(s => new { s.Name, s.LastRuntime, s.LastRunResult });
            var tasksCount = task.Count();
 
            //Message
            var message = MessageHelper.Retrieves(User.Identity.Name);
            var messagesCount = message.Count();
 
            message = message.Take(6);
            message.AsParallel().ForAll(m => m.FromIcon = Url.Content(m.FromIcon));
 
            //Apps
            var apps = ExceptionsHelper.Retrieves().Where(n => n.Category != "DB");
            var appExceptionsCount = apps.Count();
 
            apps = apps.Take(6);
            apps.AsParallel().ForAll(n =>
            {
                n.ExceptionType = n.ExceptionType?.Split('.').Last();
                var ts = DateTime.Now - n.LogTime;
                if (ts.TotalMinutes < 5) n.Period = "刚刚";
                else if (ts.Days > 0) n.Period = string.Format("{0}天", ts.Days);
                else if (ts.Hours > 0) n.Period = string.Format("{0}小时", ts.Hours);
                else if (ts.Minutes > 0) n.Period = string.Format("{0}分钟", ts.Minutes);
            });
 
            //Dbs
            var dbs = ExceptionsHelper.Retrieves().Where(n => n.Category == "DB");
            var dbExceptionsCount = dbs.Count();
 
            dbs = dbs.Take(6);
            dbs.AsParallel().ForAll(n =>
            {
                var ts = DateTime.Now - n.LogTime;
                if (ts.TotalMinutes < 5) n.Period = "刚刚";
                else if (ts.Days > 0) n.Period = string.Format("{0}天", ts.Days);
                else if (ts.Hours > 0) n.Period = string.Format("{0}小时", ts.Hours);
                else if (ts.Minutes > 0) n.Period = string.Format("{0}分钟", ts.Minutes);
            });
 
            return new
            {
                NewUsersCount = usersCount,
                TasksCount = tasksCount,
                MessagesCount = messagesCount,
                AppExceptionsCount = appExceptionsCount,
                DbExceptionsCount = dbExceptionsCount,
                Users = user.Select(i => new { i.Period, i.UserName, i.DisplayName, i.Description }),
                Tasks = task.Take(6),
                Messages = message,
                Apps = apps.Select(n => new { n.ExceptionType, n.Message, n.Period }),
                Dbs = dbs.Select(n => new { n.ErrorPage, n.Message, n.Period })
            };
        }
    }
}