-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEndpointService.cs
More file actions
141 lines (122 loc) · 4.78 KB
/
EndpointService.cs
File metadata and controls
141 lines (122 loc) · 4.78 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
133
134
135
136
137
138
139
140
141
using Microsoft.EntityFrameworkCore;
using ThingConnect.Pulse.Server.Data;
using ThingConnect.Pulse.Server.Helpers;
using ThingConnect.Pulse.Server.Models;
namespace ThingConnect.Pulse.Server.Services;
public interface IEndpointService
{
Task<EndpointDetailDto?> GetEndpointDetailAsync(Guid id, int windowMinutes = 60);
}
public sealed class EndpointService : IEndpointService
{
private readonly PulseDbContext _context;
private const int RecentFetchLimit = 2000;
public EndpointService(PulseDbContext context)
{
_context = context;
}
public async Task<EndpointDetailDto?> GetEndpointDetailAsync(Guid id, int windowMinutes = 60)
{
// Load endpoint with group
var endpoint = await _context.Endpoints
.Include(e => e.Group)
.FirstOrDefaultAsync(e => e.Id == id);
if (endpoint == null) return null;
var windowStart = DateTimeOffset.UtcNow.AddMinutes(-windowMinutes);
// --- Fetch recent raw checks ---
var rawChecks = await _context.CheckResultsRaw
.Where(c => c.EndpointId == id)
.OrderByDescending(c => c.Ts)
.Take(RecentFetchLimit)
.ToListAsync();
var recent = rawChecks
.Select(c => new RawCheckDto
{
Ts = ConvertToDateTimeOffset(c.Ts),
Status = c.Status.ToString().ToLower(),
RttMs = c.RttMs,
Error = c.Error
})
.Where(r => r.Ts >= windowStart)
.OrderByDescending(r => r.Ts)
.ToList();
// --- Fetch outages within window ---
var outageRaw = await _context.Outages
.Where(o => o.EndpointId == id)
.ToListAsync();
var outages = outageRaw
.Where(o =>
{
var started = ConvertToDateTimeOffset(o.StartedTs);
var ended = o.EndedTs != null ? ConvertToDateTimeOffset(o.EndedTs) : (DateTimeOffset?)null;
return started <= DateTimeOffset.UtcNow && (ended == null || ended >= windowStart);
})
.OrderByDescending(o => ConvertToDateTimeOffset(o.StartedTs))
.Select(o => new OutageDto
{
StartedTs = ConvertToDateTimeOffset(o.StartedTs),
EndedTs = o.EndedTs != null ? ConvertToDateTimeOffset(o.EndedTs) : null,
DurationS = NormalizeDurationToInt(o.DurationSeconds),
LastError = o.LastError
})
.ToList();
// --- Map endpoint DTO ---
var endpointDto = MapToEndpointDto(endpoint);
return new EndpointDetailDto
{
Endpoint = endpointDto,
Recent = recent,
Outages = outages
};
}
private EndpointDto MapToEndpointDto(Data.Endpoint endpoint)
{
return new EndpointDto
{
Id = endpoint.Id,
Name = endpoint.Name,
Group = new GroupDto
{
Id = endpoint.Group.Id,
Name = endpoint.Group.Name,
ParentId = endpoint.Group.ParentId,
Color = endpoint.Group.Color,
SortOrder = endpoint.Group.SortOrder
},
Type = endpoint.Type.ToString().ToLower(),
Host = endpoint.Host,
Port = endpoint.Port,
HttpPath = endpoint.HttpPath,
HttpMatch = endpoint.HttpMatch,
IntervalSeconds = endpoint.IntervalSeconds,
TimeoutMs = endpoint.TimeoutMs,
Retries = endpoint.Retries,
Enabled = endpoint.Enabled
};
}
// --- Helper to convert timestamp to DateTimeOffset ---
private static DateTimeOffset ConvertToDateTimeOffset<T>(T value)
{
if (value is DateTimeOffset dto) return dto;
if (value is DateTime dt) return new DateTimeOffset(dt.Kind == DateTimeKind.Utc ? dt : dt.ToUniversalTime());
if (value is long l) return DateTimeOffset.FromUnixTimeSeconds(l);
if (value is int i) return DateTimeOffset.FromUnixTimeSeconds(i);
var s = value?.ToString();
if (!string.IsNullOrEmpty(s) && DateTimeOffset.TryParse(s, out var parsed)) return parsed;
throw new InvalidOperationException($"Unsupported timestamp type: {value?.GetType().FullName ?? "null"}");
}
// --- Helper to normalize duration to int seconds ---
private static int? NormalizeDurationToInt(object? value)
{
if (value == null) return null;
return value switch
{
int i => i,
long l => (int)l,
TimeSpan t => (int)t.TotalSeconds,
double d => (int)Math.Round(d),
float f => (int)Math.Round(f),
_ => int.TryParse(value.ToString(), out var v) ? v : null
};
}
}