@model IList<TraceData>
@using Stardust.Data
@using Stardust.Data.Monitors
@using NewLife;
@using NewLife.Web;
@using Stardust.Data.Nodes
@using XCode;
@using XCode.Configuration;
@using XCode.Membership;
@using NewLife.Cube;
@using System.Web;
@{
    var fact = ViewBag.Factory as IEntityFactory;
    var page = ViewBag.Page as Pager;
    var fields = ViewBag.Fields as IList<FieldItem>;
    var set = ViewBag.PageSetting as PageSetting;
    //var provider = ManageProvider.Provider;

    var nodes = new Dictionary<String, Node>();
}
<table class="table table-bordered table-hover table-striped table-condensed">
    <thead>
        <tr>
            @if (set.EnableSelect)
            {
                <th class="text-center" style="width:10px;"><input type="checkbox" id="chkAll" title="全选" /></th>
            }
            <th class="text-center"><a href="@Html.Raw(page.GetSortUrl("AppId"))">应用</a></th>
            <th class="text-center" title="实例。应用可能多实例部署，ip@proccessid"><a href="@Html.Raw(page.GetSortUrl("ClientId"))">实例</a></th>
            <th title="应用系统所在节点">应用节点</th>
            <th class="text-center" title="操作名。接口名或埋点名"><a href="@Html.Raw(page.GetSortUrl("Name"))">操作名</a></th>
            <th title="跨多系统查看调用链">追踪</th>
            <th class="text-center"><a href="@Html.Raw(page.GetSortUrl("Total"))">总数</a></th>
            <th class="text-center"><a href="@Html.Raw(page.GetSortUrl("Errors"))">错误</a></th>
            <th class="text-center" title="平均耗时。总耗时除以总次数"><a href="@Html.Raw(page.GetSortUrl("Cost"))">平均</a></th>
            <th class="text-center" title="最大耗时。单位毫秒"><a href="@Html.Raw(page.GetSortUrl("MaxCost"))">最大</a></th>
            <th class="text-center" title="最小耗时。单位毫秒"><a href="@Html.Raw(page.GetSortUrl("MinCost"))">最小</a></th>
            <th class="text-center" title="总数值。用户自定义标量"><a href="@Html.Raw(page.GetSortUrl("TotalValue"))">数值</a></th>
            <th class="text-center"><a href="@Html.Raw(page.GetSortUrl("Samples"))">正常</a></th>
            <th class="text-center"><a href="@Html.Raw(page.GetSortUrl("ErrorSamples"))">异常</a></th>
            <th class="text-center"><a href="@Html.Raw(page.GetSortUrl("Start"))">开始时间</a></th>
            @*<th title="把该操作名加入排除名单，不再收集其信息">屏蔽</th>*@
            <th class="text-center" style="min-width:134px;"><a href="@Html.Raw(page.GetSortUrl("CreateTime"))">创建时间</a></th>
            <th class="text-center"><a href="@Html.Raw(page.GetSortUrl("CreateIP"))">创建地址</a></th>
            @if (this.Has(PermissionFlags.Detail, PermissionFlags.Update, PermissionFlags.Delete))
            {
                <th class="text-center">操作</th>
            }
        </tr>
    </thead>
    <tbody>
        @foreach (var entity in Model)
        {
            var itemId = entity.Link?.ItemId ?? entity.ItemId;
            var url1 = $"/Monitors/traceData?appid={entity.AppId}&itemId={itemId}";
            //var url2 = $"SampleData?appid={entity.AppId}&name={HttpUtility.UrlEncode(entity.Name)}&start={entity.StartTime}&end={entity.EndTime}";
            var url2 = $"/Monitors/SampleData?dataid={(entity.LinkId > 0 ? entity.LinkId : entity.Id)}";
            var url3 = $"/Monitors/traceData?appid={entity.AppId}&clientId={HttpUtility.UrlEncode(entity.ClientId)}&itemId={itemId}";

            // 带缓存，避免重复查询节点
            var node = entity.Node;
            if (node == null && !nodes.TryGetValue(entity.ClientId + "", out node))
            {
                var online = AppOnline.FindByClient(entity.ClientId);
                if (online == null && entity.App != null && entity.App.AppId > 0)
                    online = AppOnline.FindAllByAppAndIP(entity.App.AppId, entity.ClientId.Substring(null, "@")).FirstOrDefault();
                node = online?.Node;
                if (node == null)
                    node = Node.SearchByIP(entity.ClientId.Substring(null, "@")).FirstOrDefault();

                nodes.Add(entity.ClientId + "", node);
            }
            var nodeName = node?.Name ?? node?.Code;

            <tr>
                @if (set.EnableSelect)
                {
                    <td class="text-center"><input type="checkbox" name="keys" value="@entity.Id" /></td>
                }
                <td class="text-center"><a href="?appId=@entity.AppId" target="_frame">@entity.AppName</a></td>
                <td><a href="@url3" title="@(entity.AppName)埋点数据。该实例的相关埋点数据">@entity.ClientId</a></td>
                <td><a href="/Nodes/Node?Id=@node?.ID" title="@(nodeName)。应用系统所在节点" target="_blank">@nodeName</a></td>
                <td style="max-width:400px;overflow:hidden;white-space: nowrap;text-overflow: ellipsis;"><a href="@url1" title="该应用操作在最近一段时间内的监控图表">@entity.Name</a></td>
                <td><a href="/Monitors/TraceData/Trace?id=@entity.Id" title="跨多系统查看调用链">追踪</a></td>
                <td class="text-right"><a href="@url2" title="本次追踪的少量采样数据样本，作为参考，并没有收集所有埋点采样数据">@entity.Total.ToString("n0")</a></td>
                <td class="text-right">@entity.Errors.ToString("n0")</td>
                <td class="text-right">@entity.Cost.ToString("n0")</td>
                <td class="text-right">@entity.MaxCost.ToString("n0")</td>
                <td class="text-right">@entity.MinCost.ToString("n0")</td>
                <td class="text-right">@entity.TotalValue.ToString("n0")</td>
                <td class="text-right"><a href="@url2&success=1" title="本次追踪的正常调用采样数据">@entity.Samples.ToString("n0")</a></td>
                <td class="text-right"><a href="@url2&success=0" title="本次追踪的异常调用采样数据，包含错误信息">@entity.ErrorSamples.ToString("n0")</a></td>
                @*<td><a href="TraceData/Exclude?id=@entity.Id" title="把该操作名加入排除名单，不再收集其信息">屏蔽</a></td>*@
                <td class="text-center">@entity.Start.ToFullString("")</td>
                <td class="text-center">@entity.CreateTime.ToFullString("")</td>
                <td class="text-center" title="@(entity.CreateIP.IPToAddress())">@entity.CreateIP</td>
                @if (this.Has(PermissionFlags.Detail, PermissionFlags.Update, PermissionFlags.Delete))
                {
                    <td class="text-center">
                        @await Html.PartialAsync("_List_Data_Action", (Object)entity)
                    </td>
                }
            </tr>
        }
        @if (page.State is TraceData)
        {
            var entity = page.State as TraceData;
            <tr>
                @if (set.EnableSelect)
                {
                    <td></td>
                }
                <td class="text-center">总计</td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td class="text-right">@entity.Total.ToString("n0")</td>
                <td class="text-right">@entity.Errors.ToString("n0")</td>
                <td class="text-right">@entity.Cost.ToString("n0")</td>
                <td class="text-right">@entity.MaxCost.ToString("n0")</td>
                <td class="text-right">@entity.MinCost.ToString("n0")</td>
                <td class="text-right">@entity.TotalValue.ToString("n0")</td>
                <td class="text-right">@entity.Samples.ToString("n0")</td>
                <td class="text-right">@entity.ErrorSamples.ToString("n0")</td>
                @*<td></td>*@
                <td></td>
                <td></td>
                <td></td>
                <td></td>

                @if (this.Has(PermissionFlags.Detail, PermissionFlags.Update, PermissionFlags.Delete))
                {
                    <td></td>
                }
            </tr>
        }
    </tbody>
</table>