节点在线、应用在线、配置在线使用令牌查询
大石头 authored at 2021-12-16 19:49:30
4.09 KiB
Stardust
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using NewLife;
using NewLife.Cube;
using NewLife.Cube.Extensions;
using NewLife.Cube.ViewModels;
using NewLife.Web;
using Stardust.Data;
using XCode.Membership;

namespace Stardust.Web.Areas.Registry.Controllers;

/// <summary>应用历史。记录应用的关键操作事件(登录/心跳/命令),支持按应用和操作类型检索</summary>
[RegistryArea]
[Menu(0, false)]
public class AppHistoryController : RegistryEntityController<AppHistory>
{
    static AppHistoryController()
    {
        ListFields.RemoveField("Id", "Version");
        ListFields.AddListField("Remark", null, "TraceId");

        //{
        //    var df = ListFields.GetField("TraceId") as ListField;
        //    df.DisplayName = "跟踪";
        //    df.Url = StarHelper.BuildUrl("{TraceId}");
        //    df.DataVisible = e => e is AppHistory entity && !entity.TraceId.IsNullOrEmpty();
        //}
        ListFields.LocalizedTraceUrl();

        {
            var df = ListFields.GetField("AppName") as ListField;
            df.Url = "/Registry/App?Id={AppId}";
            df.Title = "应用: {AppName}";
            df.Target = "_frame";
        }
        {
            var df = ListFields.GetField("Client") as ListField;
            df.Url = "/Registry/AppHistory?appId={AppId}&client={Client}";
        }

        {
            var df = ListFields.GetField("Action") as ListField;
            df.Url = "/Registry/AppHistory?appId={AppId}&action={Action}";
            // 数据库只存英文规范键,列表按当前界面语言显示;链接仍使用原始值,保证筛选有效
            df.GetValue = e => e is AppHistory entity ? HistoryAction.Localize(entity.Action) : null;
        }
    }

    protected override IEnumerable<AppHistory> SearchData(Pager p)
    {
        var appId = p["appId"].ToInt(-1);
        if (appId >= 0) PageSetting.OrderByKey = true;

        return base.SearchData(p);
    }

    /// <summary>列表动作前判断是否需要总数。未按应用或时间筛选时历史大表全表计数很慢</summary>
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);

        var filtered = GetRequest("appId").ToInt(-1) > 0
            || !GetRequest("client").IsNullOrEmpty()
            || !GetRequest("action").IsNullOrEmpty()
            || GetRequest("dtStart").ToDateTime().Year > 2000;
        if (!filtered) PageSetting.EnableTotalCount = false;
    }

    /// <summary>查看。详情中的操作名同样按当前界面语言显示</summary>
    /// <param name="id">编号</param>
    /// <returns></returns>
    public override ActionResult Detail(String id)
    {
        var rs = base.Detail(id);
        if (rs is ViewResult view && view.Model is AppHistory entity)
        {
            // 克隆后再翻译,避免改动单对象缓存中的实体
            var clone = entity.CloneEntity();
            clone.Action = HistoryAction.Localize(clone.Action);
            view.ViewData.Model = clone;
        }

        return rs;
    }

    /// <summary>高级搜索。按条件分页查询</summary>
    /// <param name="p">分页参数</param>
    /// <returns>实体列表</returns>
    protected override IEnumerable<AppHistory> Search(Pager p)
    {
        //PageSetting.EnableAdd = false;
        //PageSetting.EnableNavbar = false;

        var appId = p["appId"].ToInt(-1);
        var nodeId = p["nodeId"].ToInt(-1);
        var client = p["client"];
        var action = p["action"];
        var success = p["success"]?.ToBoolean();

        var start = p["dtStart"].ToDateTime();
        var end = p["dtEnd"].ToDateTime();

        //if (appId > 0 && start.Year < 2000) p["dtStart"] = (start = DateTime.Today).ToString("yyyy-MM-dd");

        // WebSocket 连接/断开数量大,默认不展示;指定 action 或 all=true 时查看
        var exclude = action.IsNullOrEmpty() && !p["all"].ToBoolean() ? HistoryAction.ConnectionActions : null;

        return AppHistory.SearchEx(appId, client, action, success, nodeId, start, end, p["Q"], p, exclude);
    }
}