-
-
Notifications
You must be signed in to change notification settings - Fork 508
Expand file tree
/
Copy pathAdminController.cs
More file actions
220 lines (196 loc) · 8.77 KB
/
AdminController.cs
File metadata and controls
220 lines (196 loc) · 8.77 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
using Exceptionless.Core;
using Exceptionless.Core.Authorization;
using Exceptionless.Core.Billing;
using Exceptionless.Core.Extensions;
using Exceptionless.Core.Messaging.Models;
using Exceptionless.Core.Models;
using Exceptionless.Core.Models.WorkItems;
using Exceptionless.Core.Queues.Models;
using Exceptionless.Core.Repositories;
using Exceptionless.Core.Repositories.Configuration;
using Exceptionless.Core.Utility;
using Exceptionless.DateTimeExtensions;
using Exceptionless.Web.Extensions;
using Foundatio.Jobs;
using Foundatio.Messaging;
using Foundatio.Queues;
using Foundatio.Repositories;
using Foundatio.Storage;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Exceptionless.Web.Controllers;
[Route(API_PREFIX + "/admin")]
[Authorize(Policy = AuthorizationRoles.GlobalAdminPolicy)]
[ApiExplorerSettings(IgnoreApi = true)]
public class AdminController : ExceptionlessApiController
{
private readonly ExceptionlessElasticConfiguration _configuration;
private readonly IFileStorage _fileStorage;
private readonly IMessagePublisher _messagePublisher;
private readonly IOrganizationRepository _organizationRepository;
private readonly IQueue<EventPost> _eventPostQueue;
private readonly IQueue<WorkItemData> _workItemQueue;
private readonly AppOptions _appOptions;
private readonly BillingManager _billingManager;
private readonly BillingPlans _plans;
public AdminController(
ExceptionlessElasticConfiguration configuration,
IFileStorage fileStorage,
IMessagePublisher messagePublisher,
IOrganizationRepository organizationRepository,
IQueue<EventPost> eventPostQueue,
IQueue<WorkItemData> workItemQueue,
AppOptions appOptions,
BillingManager billingManager,
BillingPlans plans,
TimeProvider timeProvider) : base(timeProvider)
{
_configuration = configuration;
_fileStorage = fileStorage;
_messagePublisher = messagePublisher;
_organizationRepository = organizationRepository;
_eventPostQueue = eventPostQueue;
_workItemQueue = workItemQueue;
_appOptions = appOptions;
_billingManager = billingManager;
_plans = plans;
}
[HttpGet("settings")]
public ActionResult SettingsRequest()
{
return Ok(_appOptions);
}
[HttpGet("echo")]
public ActionResult EchoRequest()
{
return Ok(new
{
Request.Headers,
IpAddress = Request.GetClientIpAddress()
});
}
[HttpGet("assemblies")]
public ActionResult<IEnumerable<AssemblyDetail>> Assemblies()
{
var details = AssemblyDetail.ExtractAll();
return Ok(details);
}
[HttpPost("change-plan")]
public async Task<IActionResult> ChangePlanAsync(string organizationId, string planId)
{
if (String.IsNullOrEmpty(organizationId) || !CanAccessOrganization(organizationId))
return Ok(new { Success = false, Message = "Invalid Organization Id." });
var organization = await _organizationRepository.GetByIdAsync(organizationId);
if (organization is null)
return Ok(new { Success = false, Message = "Invalid Organization Id." });
var plan = _billingManager.GetBillingPlan(planId);
if (plan is null)
return Ok(new { Success = false, Message = "Invalid PlanId." });
organization.BillingStatus = !String.Equals(plan.Id, _plans.FreePlan.Id) ? BillingStatus.Active : BillingStatus.Trialing;
organization.RemoveSuspension();
_billingManager.ApplyBillingPlan(organization, plan, CurrentUser, false);
await _organizationRepository.SaveAsync(organization, o => o.Cache().Originals());
await _messagePublisher.PublishAsync(new PlanChanged
{
OrganizationId = organization.Id
});
return Ok(new { Success = true });
}
/// <summary>
/// Applies a bonus event count to the specified organization, optionally with an expiration date.
/// </summary>
/// <param name="organizationId">The unique identifier of the organization to receive the bonus.</param>
/// <param name="bonusEvents">The number of bonus events to apply.</param>
/// <param name="expires">The optional expiration date for the bonus events.</param>
/// <response code="200">Bonus was applied successfully.</response>
/// <response code="422">Validation error occurred.</response>
[HttpPost("set-bonus")]
public async Task<IActionResult> SetBonusAsync(string organizationId, int bonusEvents, DateTime? expires = null)
{
if (String.IsNullOrEmpty(organizationId) || !CanAccessOrganization(organizationId))
{
ModelState.AddModelError(nameof(organizationId), "Invalid Organization Id");
return ValidationProblem(ModelState);
}
var organization = await _organizationRepository.GetByIdAsync(organizationId);
if (organization is null)
{
ModelState.AddModelError(nameof(organizationId), "Invalid Organization Id");
return ValidationProblem(ModelState);
}
_billingManager.ApplyBonus(organization, bonusEvents, expires);
await _organizationRepository.SaveAsync(organization, o => o.Cache().Originals());
return Ok();
}
[HttpGet("requeue")]
public async Task<IActionResult> RequeueAsync(string? path = null, bool archive = false)
{
if (String.IsNullOrEmpty(path))
path = @"q\*";
int enqueued = 0;
foreach (var file in await _fileStorage.GetFileListAsync(path))
{
await _eventPostQueue.EnqueueAsync(new EventPost(_appOptions.EnableArchive && archive) { FilePath = file.Path });
enqueued++;
}
return Ok(new { Enqueued = enqueued });
}
[HttpGet("maintenance/{name:minlength(1)}")]
public async Task<IActionResult> RunJobAsync(string name, DateTime? utcStart = null, DateTime? utcEnd = null, string? organizationId = null)
{
if (!ModelState.IsValid)
return ValidationProblem(ModelState);
switch (name.ToLowerInvariant())
{
case "fix-stack-stats":
var defaultUtcStart = new DateTime(2026, 2, 10, 0, 0, 0, DateTimeKind.Utc);
var effectiveUtcStart = utcStart ?? defaultUtcStart;
if (utcEnd.HasValue && utcEnd.Value.IsBefore(effectiveUtcStart))
{
ModelState.AddModelError(nameof(utcEnd), "utcEnd must be greater than or equal to utcStart.");
return ValidationProblem(ModelState);
}
await _workItemQueue.EnqueueAsync(new FixStackStatsWorkItem
{
UtcStart = effectiveUtcStart,
UtcEnd = utcEnd,
OrganizationId = organizationId
});
break;
case "increment-project-configuration-version":
await _workItemQueue.EnqueueAsync(new ProjectMaintenanceWorkItem { IncrementConfigurationVersion = true });
break;
case "indexes":
if (!_appOptions.ElasticsearchOptions.DisableIndexConfiguration)
await _configuration.ConfigureIndexesAsync(beginReindexingOutdated: false);
break;
case "normalize-user-email-address":
await _workItemQueue.EnqueueAsync(new UserMaintenanceWorkItem { Normalize = true });
break;
case "remove-old-organization-usage":
await _workItemQueue.EnqueueAsync(new OrganizationMaintenanceWorkItem { RemoveOldUsageStats = true });
break;
case "remove-old-project-usage":
await _workItemQueue.EnqueueAsync(new ProjectMaintenanceWorkItem { RemoveOldUsageStats = true });
break;
case "reset-verify-email-address-token-and-expiration":
await _workItemQueue.EnqueueAsync(new UserMaintenanceWorkItem { ResetVerifyEmailAddressToken = true });
break;
case "update-organization-plans":
await _workItemQueue.EnqueueAsync(new OrganizationMaintenanceWorkItem { UpgradePlans = true });
break;
case "update-project-default-bot-lists":
await _workItemQueue.EnqueueAsync(new ProjectMaintenanceWorkItem { UpdateDefaultBotList = true, IncrementConfigurationVersion = true });
break;
case "update-project-notification-settings":
await _workItemQueue.EnqueueAsync(new UpdateProjectNotificationSettingsWorkItem
{
OrganizationId = organizationId
});
break;
default:
return NotFound();
}
return Ok();
}
}