-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re-introduce Mediator with GetDiskMetricsQuery
- Loading branch information
1 parent
ef95503
commit 04b2f92
Showing
7 changed files
with
137 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 30 additions & 58 deletions
88
...ter.Core/Services/ServerMetricsService.cs → Crypter.Core/AssemblyInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,30 @@ | ||
/* | ||
* Copyright (C) 2023 Crypter File Transfer | ||
* | ||
* This file is part of the Crypter file transfer project. | ||
* | ||
* Crypter is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* The Crypter source code is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* You can be released from the requirements of the aforementioned license | ||
* by purchasing a commercial license. Buying such a license is mandatory | ||
* as soon as you develop commercial activities involving the Crypter source | ||
* code without disclosing the source code of your own applications. | ||
* | ||
* Contact the current copyright holder to discuss commercial license options. | ||
*/ | ||
|
||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Crypter.Common.Contracts.Features.Metrics; | ||
using Crypter.Core.Features.Metrics; | ||
using Crypter.Core.Settings; | ||
using Crypter.DataAccess; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Crypter.Core.Services; | ||
|
||
public interface IServerMetricsService | ||
{ | ||
Task<PublicStorageMetricsResponse> GetAggregateDiskMetricsAsync(CancellationToken cancellationToken = default); | ||
} | ||
|
||
public class ServerMetricsService : IServerMetricsService | ||
{ | ||
private readonly DataContext _context; | ||
private readonly TransferStorageSettings _transferStorageSettings; | ||
|
||
public ServerMetricsService(DataContext context, IOptions<TransferStorageSettings> transferStorageSettings) | ||
{ | ||
_context = context; | ||
_transferStorageSettings = transferStorageSettings.Value; | ||
} | ||
|
||
public Task<PublicStorageMetricsResponse> GetAggregateDiskMetricsAsync( | ||
CancellationToken cancellationToken = default) | ||
{ | ||
return MetricsQueries.GetAggregateDiskMetricsAsync(_context, _transferStorageSettings, cancellationToken); | ||
} | ||
} | ||
/* | ||
* Copyright (C) 2023 Crypter File Transfer | ||
* | ||
* This file is part of the Crypter file transfer project. | ||
* | ||
* Crypter is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* The Crypter source code is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* You can be released from the requirements of the aforementioned license | ||
* by purchasing a commercial license. Buying such a license is mandatory | ||
* as soon as you develop commercial activities involving the Crypter source | ||
* code without disclosing the source code of your own applications. | ||
* | ||
* Contact the current copyright holder to discuss commercial license options. | ||
*/ | ||
|
||
namespace Crypter.Core; | ||
|
||
public class AssemblyInfo | ||
{ } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 86 additions & 66 deletions
152
...r.Core/Features/Metrics/MetricsQueries.cs → ...es/Metrics/Queries/GetDiskMetricsQuery.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,86 @@ | ||
/* | ||
* Copyright (C) 2023 Crypter File Transfer | ||
* | ||
* This file is part of the Crypter file transfer project. | ||
* | ||
* Crypter is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* The Crypter source code is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* You can be released from the requirements of the aforementioned license | ||
* by purchasing a commercial license. Buying such a license is mandatory | ||
* as soon as you develop commercial activities involving the Crypter source | ||
* code without disclosing the source code of your own applications. | ||
* | ||
* Contact the current copyright holder to discuss commercial license options. | ||
*/ | ||
|
||
using System; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Crypter.Common.Contracts.Features.Metrics; | ||
using Crypter.Core.Settings; | ||
using Crypter.DataAccess; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace Crypter.Core.Features.Metrics; | ||
|
||
internal static class MetricsQueries | ||
{ | ||
internal static async Task<PublicStorageMetricsResponse> GetAggregateDiskMetricsAsync(DataContext dataContext, | ||
TransferStorageSettings transferStorageSettings, CancellationToken cancellationToken = default) | ||
{ | ||
IQueryable<long> anonymousMessageSizes = dataContext.AnonymousMessageTransfers | ||
.Select(x => x.Size); | ||
|
||
IQueryable<long> userMessageSizes = dataContext.UserMessageTransfers | ||
.Select(x => x.Size); | ||
|
||
IQueryable<long> anonymousFileSizes = dataContext.AnonymousFileTransfers | ||
.Select(x => x.Size); | ||
|
||
IQueryable<long> userFileSizes = dataContext.UserFileTransfers | ||
.Select(x => x.Size); | ||
|
||
long usedBytes = await anonymousMessageSizes | ||
.Concat(userMessageSizes) | ||
.Concat(anonymousFileSizes) | ||
.Concat(userFileSizes) | ||
.SumAsync(cancellationToken); | ||
|
||
long allocatedBytes = transferStorageSettings.AllocatedGB * Convert.ToInt64(Math.Pow(2, 30)); | ||
long freeBytes = allocatedBytes - usedBytes; | ||
|
||
return new PublicStorageMetricsResponse(allocatedBytes, freeBytes); | ||
} | ||
} | ||
/* | ||
* Copyright (C) 2023 Crypter File Transfer | ||
* | ||
* This file is part of the Crypter file transfer project. | ||
* | ||
* Crypter is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* The Crypter source code is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* You can be released from the requirements of the aforementioned license | ||
* by purchasing a commercial license. Buying such a license is mandatory | ||
* as soon as you develop commercial activities involving the Crypter source | ||
* code without disclosing the source code of your own applications. | ||
* | ||
* Contact the current copyright holder to discuss commercial license options. | ||
*/ | ||
|
||
using System; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Crypter.Core.Settings; | ||
using Crypter.DataAccess; | ||
using MediatR; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Crypter.Core.Features.Metrics.Queries; | ||
|
||
public record GetDiskMetricsQuery : IRequest<GetDiskMetricsResult>; | ||
|
||
public record GetDiskMetricsResult(long AllocatedBytes, long FreeBytes); | ||
|
||
// ReSharper disable once ClassNeverInstantiated.Global | ||
internal sealed class GetDiskMetricsQueryHandler : IRequestHandler<GetDiskMetricsQuery, GetDiskMetricsResult> | ||
{ | ||
private readonly DataContext _dataContext; | ||
private readonly TransferStorageSettings _transferStorageSettings; | ||
|
||
public GetDiskMetricsQueryHandler(DataContext dataContext, IOptions<TransferStorageSettings> transferStorageSettings) | ||
{ | ||
_dataContext = dataContext; | ||
_transferStorageSettings = transferStorageSettings.Value; | ||
} | ||
|
||
public async Task<GetDiskMetricsResult> Handle(GetDiskMetricsQuery request, CancellationToken cancellationToken) | ||
{ | ||
return await RunQueryAsync(_dataContext, _transferStorageSettings, cancellationToken); | ||
} | ||
|
||
public static async Task<GetDiskMetricsResult> RunQueryAsync(DataContext dataContext, | ||
TransferStorageSettings transferStorageSettings, CancellationToken cancellationToken = default) | ||
{ | ||
IQueryable<long> anonymousMessageSizes = dataContext.AnonymousMessageTransfers | ||
.Select(x => x.Size); | ||
|
||
IQueryable<long> userMessageSizes = dataContext.UserMessageTransfers | ||
.Select(x => x.Size); | ||
|
||
IQueryable<long> anonymousFileSizes = dataContext.AnonymousFileTransfers | ||
.Select(x => x.Size); | ||
|
||
IQueryable<long> userFileSizes = dataContext.UserFileTransfers | ||
.Select(x => x.Size); | ||
|
||
long usedBytes = await anonymousMessageSizes | ||
.Concat(userMessageSizes) | ||
.Concat(anonymousFileSizes) | ||
.Concat(userFileSizes) | ||
.SumAsync(cancellationToken); | ||
|
||
long allocatedBytes = transferStorageSettings.AllocatedGB * Convert.ToInt64(Math.Pow(2, 30)); | ||
long freeBytes = allocatedBytes - usedBytes; | ||
|
||
return new GetDiskMetricsResult(allocatedBytes, freeBytes); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters