using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using New_College.Common.Helper; using New_College.Common.LogHelper; using New_College.Hubs; using New_College.Middlewares; using New_College.Model; using New_College.Model.ViewModels; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.SignalR; using Newtonsoft.Json; namespace New_College.Controllers { [Route("api/[Controller]/[action]")] [ApiController] [AllowAnonymous] public class MonitorController : Controller { private readonly IHubContext _hubContext; private readonly IWebHostEnvironment _env; public MonitorController(IHubContext hubContext, IWebHostEnvironment env) { _hubContext = hubContext; _env = env; } /// /// 服务器配置信息 /// /// [HttpGet] public MessageModel Server() { return new MessageModel() { 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) } }; } /// /// SignalR send data /// /// // GET: api/Logs [HttpGet] public MessageModel> Get() { _hubContext.Clients.All.SendAsync("ReceiveUpdate", LogLock.GetLogData()).Wait(); return new MessageModel>() { msg = "获取成功", success = true, response = null }; } [HttpGet] public MessageModel GetRequestApiinfoByWeek() { return new MessageModel() { msg = "获取成功", success = true, response = LogLock.RequestApiinfoByWeek() }; } [HttpGet] public MessageModel GetAccessApiByDate() { return new MessageModel() { msg = "获取成功", success = true, response = LogLock.AccessApiByDate() }; } [HttpGet] public MessageModel GetAccessApiByHour() { return new MessageModel() { msg = "获取成功", success = true, response = LogLock.AccessApiByHour() }; } [HttpGet] public MessageModel> GetAccessLogs([FromServices]IWebHostEnvironment environment) { var Logs = JsonConvert.DeserializeObject>("[" + 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>() { msg = "获取成功", success = true, response = Logs }; } } }