-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathEndPointStatistics.cs
160 lines (141 loc) · 6.32 KB
/
EndPointStatistics.cs
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/Beef
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Beef.CodeGen
{
/// <summary>
/// Provides the ability to capture and report endpoint statistics.
/// </summary>
internal class EndPointStatistics()
{
private readonly List<EndPointData> _endPoints = [];
/// <summary>
/// Gets the endpoint count.
/// </summary>
public int Count => _endPoints.Count;
/// <summary>
/// Adds the <see cref="CommandType.Entity"/> endpoints.
/// </summary>
public void AddEntityEndPoints(Config.Entity.CodeGenConfig root)
{
var entities = root.Entities!.Where(x => (!x.ExcludeWebApi.HasValue || !x.ExcludeWebApi.Value) && x.Operations!.Count > 0).AsEnumerable();
foreach (var e in entities)
{
foreach (var o in e.Operations!.Where(x => !x.ExcludeWebApi.HasValue || !x.ExcludeWebApi.Value))
{
Add(new EndPointData
{
Route = o.AgentWebApiRoute,
Method = o.WebApiMethod![4..],
Status = o.WebApiStatus!,
Auth = o.WebApiAuthorize ?? e.WebApiAuthorize,
Tags = string.Join(", ", o.WebApiTags!.Count > 0 ? o.WebApiTags : e.WebApiTags!),
Name = $"{e.Name}.{o.Name}"
});
}
}
}
/// <summary>
/// Adds the <see cref="CommandType.RefData"/> endpoints.
/// </summary>
public void AddRefDataEndPoints(Config.Entity.CodeGenConfig root)
{
// Add the 'GetNamed' global endpoint.
Add(new EndPointData
{
Route = root.RefDataWebApiRoute,
Method = "GET",
Status = "OK",
Auth = root.WebApiAuthorize,
Tags = null,
Name = "ReferenceData.GetNamed"
});
// Add the configured refdata entities.
var entities = root.RefDataEntities!;
foreach (var e in entities)
{
Add(new EndPointData
{
Route = e.WebApiRoutePrefix!,
Method = "GET",
Status = "OK",
Auth = e.WebApiAuthorize,
Tags = string.Join(", ", e.WebApiTags!),
Name = $"ReferenceData.{e.Name}GetAll"
});
}
}
/// <summary>
/// Add the <paramref name="data"/> to the collection.
/// </summary>
/// <param name="data">The <see cref="EndPointData"/>.</param>
public void Add(EndPointData data) => _endPoints.Add(data);
/// <summary>
/// Write as tabulated data to the <paramref name="logger"/>.
/// </summary>
/// <param name="logger">The <see cref="ILogger"/>.</param>
public void WriteTabulated(ILogger logger)
{
if (_endPoints.Count == 0)
return;
WriteTabulated(logger, false);
logger.LogInformation("{Content}", string.Empty);
}
/// <summary>
/// Writes the tabulated data.
/// </summary>
private void WriteTabulated(ILogger logger, bool indent)
{
// Write table header.
var routeLength = Math.Max(5, _endPoints.Max(x => x.Route?.Length ?? 0));
var methodLength = Math.Max(6, _endPoints.Max(x => x.Method?.Length ?? 0));
var statusLength = Math.Max(6, _endPoints.Max(x => x.Status?.Length ?? 0));
var authLength = Math.Max(4, _endPoints.Max(x => x.Auth?.Length ?? 0));
var tagsLength = Math.Max(4, _endPoints.Max(x => x.Tags?.Length ?? 0));
var hdrRoute = string.Format("{0, -" + routeLength + "}", "Route");
var hdrMethod = string.Format("{0, -" + methodLength + "}", "Method");
var hdrStatus = string.Format("{0, -" + statusLength + "}", "Status");
var hdrAuth = string.Format("{0, -" + authLength + "}", "Auth");
var hdrTags = string.Format("{0, -" + tagsLength + "}", "Tags");
if (_endPoints.Count == 0)
return;
var prefix = indent ? new string(' ', 6) : string.Empty;
logger.LogInformation("{Content}", $"{prefix}{hdrRoute} | {hdrMethod} | {hdrStatus} | {hdrAuth} | {hdrTags} | Name");
logger.LogInformation("{Content}", $"{prefix}{new string('-', routeLength + methodLength + statusLength + authLength + tagsLength + 15 + Math.Max(4, _endPoints.Max(x => x.Name?.Length ?? 0)))}");
// Write the data.
foreach (var ep in _endPoints.OrderBy(x => x.Route).ThenBy(x => x.Method).ThenBy(x => x.Name))
{
var route = string.Format("{0, -" + routeLength + "}", ep.Route);
var method = string.Format("{0, -" + methodLength + "}", ep.Method);
var status = string.Format("{0, -" + statusLength + "}", ep.Status);
var auth = string.Format("{0, -" + authLength + "}", ep.Auth);
var tags = string.Format("{0, -" + tagsLength + "}", ep.Tags);
logger.LogInformation("{Content}", $"{prefix}{route} | {method.ToUpperInvariant()} | {status} | {auth} | {tags} | {ep.Name}");
}
}
/// <summary>
/// Writes as inline tabulated to the <paramref name="logger"/>.
/// </summary>
/// <param name="logger">The <see cref="ILogger"/>.</param>
public void WriteInline(ILogger logger)
{
logger.LogInformation("{Content}", $" Endpoints:{(_endPoints.Count == 0 ? " none" : $" ({_endPoints.Count})")}");
if (_endPoints.Count > 0)
WriteTabulated(logger, true);
}
}
/// <summary>
/// Represents the endpoint data for reporting.
/// </summary>
internal class EndPointData
{
public string? Route { get; set; }
public string? Method { get; set; }
public string? Status { get; set; }
public string? Auth { get; set; }
public string? Tags { get; set; }
public string? Name { get; set; }
}
}