forked from anjoy8/Blog.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMonitorController.cs
More file actions
132 lines (115 loc) · 4.04 KB
/
MonitorController.cs
File metadata and controls
132 lines (115 loc) · 4.04 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Blog.Core.Common.Helper;
using Blog.Core.Common.LogHelper;
using Blog.Core.Hubs;
using Blog.Core.Middlewares;
using Blog.Core.Model;
using Blog.Core.Model.ViewModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using Newtonsoft.Json;
namespace Blog.Core.Controllers
{
[Route("api/[Controller]/[action]")]
[ApiController]
[AllowAnonymous]
public class MonitorController : Controller
{
private readonly IHubContext<ChatHub> _hubContext;
private readonly IWebHostEnvironment _env;
public MonitorController(IHubContext<ChatHub> hubContext, IWebHostEnvironment env)
{
_hubContext = hubContext;
_env = env;
}
/// <summary>
/// 服务器配置信息
/// </summary>
/// <returns></returns>
[HttpGet]
public MessageModel<ServerViewModel> Server()
{
return new MessageModel<ServerViewModel>()
{
msg = "获取成功",
success = true,
response = new ServerViewModel()
{
EnvironmentName = _env.EnvironmentName,
OSArchitecture = RuntimeInformation.OSArchitecture.ObjToString(),
ContentRootPath = _env.ContentRootPath,
WebRootPath = _env.WebRootPath,
FrameworkDescription = RuntimeInformation.FrameworkDescription,
MemoryFootprint = (Process.GetCurrentProcess().WorkingSet64 / 1048576).ToString("N2") + " MB",
WorkingTime = DateHelper.TimeSubTract(DateTime.Now, Process.GetCurrentProcess().StartTime)
}
};
}
/// <summary>
/// SignalR send data
/// </summary>
/// <returns></returns>
// GET: api/Logs
[HttpGet]
public MessageModel<List<LogInfo>> Get()
{
_hubContext.Clients.All.SendAsync("ReceiveUpdate", LogLock.GetLogData()).Wait();
return new MessageModel<List<LogInfo>>()
{
msg = "获取成功",
success = true,
response = null
};
}
[HttpGet]
public MessageModel<RequestApiWeekView> GetRequestApiinfoByWeek()
{
return new MessageModel<RequestApiWeekView>()
{
msg = "获取成功",
success = true,
response = LogLock.RequestApiinfoByWeek()
};
}
[HttpGet]
public MessageModel<AccessApiDateView> GetAccessApiByDate()
{
return new MessageModel<AccessApiDateView>()
{
msg = "获取成功",
success = true,
response = LogLock.AccessApiByDate()
};
}
[HttpGet]
public MessageModel<AccessApiDateView> GetAccessApiByHour()
{
return new MessageModel<AccessApiDateView>()
{
msg = "获取成功",
success = true,
response = LogLock.AccessApiByHour()
};
}
[HttpGet]
public MessageModel<List<UserAccessModel>> GetAccessLogs([FromServices]IWebHostEnvironment environment)
{
var Logs = JsonConvert.DeserializeObject<List<UserAccessModel>>("[" + LogLock.ReadLog(Path.Combine(environment.ContentRootPath, "Log"), "RecordAccessLogs_", Encoding.UTF8, ReadType.Prefix) + "]");
Logs = Logs.Where(d => d.BeginTime.ObjToDate() >= DateTime.Today).OrderByDescending(d => d.BeginTime).Take(50).ToList();
return new MessageModel<List<UserAccessModel>>()
{
msg = "获取成功",
success = true,
response = Logs
};
}
}
}