This repository has been archived by the owner on Oct 20, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 637
/
AdminApiController.cs
100 lines (80 loc) · 3.5 KB
/
AdminApiController.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
using Autofac;
using Microsoft.AspNetCore.Mvc;
using Miningcore.Extensions;
using Miningcore.Mining;
using Miningcore.Persistence.Repositories;
using Miningcore.Util;
using System;
using System.Collections.Concurrent;
using System.Net;
using System.Threading.Tasks;
namespace Miningcore.Api.Controllers
{
[Route("api/admin")]
[ApiController]
public class AdminApiController : ApiControllerBase
{
public AdminApiController(IComponentContext ctx) : base(ctx)
{
gcStats = ctx.Resolve<Responses.AdminGcStats>();
minerRepo = ctx.Resolve<IMinerRepository>();
pools = ctx.Resolve<ConcurrentDictionary<string, IMiningPool>>();
paymentsRepo = ctx.Resolve<IPaymentRepository>();
balanceRepo = ctx.Resolve<IBalanceRepository>();
}
private readonly IPaymentRepository paymentsRepo;
private readonly IBalanceRepository balanceRepo;
private readonly IMinerRepository minerRepo;
private readonly ConcurrentDictionary<string, IMiningPool> pools;
private readonly Responses.AdminGcStats gcStats;
#region Actions
[HttpGet("stats/gc")]
public ActionResult<Responses.AdminGcStats> GetGcStats()
{
gcStats.GcGen0 = GC.CollectionCount(0);
gcStats.GcGen1 = GC.CollectionCount(1);
gcStats.GcGen2 = GC.CollectionCount(2);
gcStats.MemAllocated = FormatUtil.FormatCapacity(GC.GetTotalMemory(false));
return gcStats;
}
[HttpPost("forcegc")]
public ActionResult<string> ForceGc()
{
GC.Collect(2, GCCollectionMode.Forced);
return "Ok";
}
[HttpGet("pools/{poolId}/miners/{address}/getbalance")]
public async Task<decimal> GetMinerBalanceAsync(string poolId, string address)
{
return await cf.Run(con => balanceRepo.GetBalanceAsync(con, poolId, address));
}
[HttpGet("pools/{poolId}/miners/{address}/settings")]
public async Task<Responses.MinerSettings> GetMinerSettingsAsync(string poolId, string address)
{
var pool = GetPool(poolId);
if(string.IsNullOrEmpty(address))
throw new ApiException("Invalid or missing miner address", HttpStatusCode.NotFound);
var result = await cf.Run(con=> minerRepo.GetSettings(con, null, pool.Id, address));
if(result == null)
throw new ApiException("No settings found", HttpStatusCode.NotFound);
return mapper.Map<Responses.MinerSettings>(result);
}
[HttpPost("pools/{poolId}/miners/{address}/settings")]
public async Task<Responses.MinerSettings> SetMinerSettingsAsync(string poolId, string address,
[FromBody] Responses.MinerSettings settings)
{
var pool = GetPool(poolId);
if(string.IsNullOrEmpty(address))
throw new ApiException("Invalid or missing miner address", HttpStatusCode.NotFound);
if(settings == null)
throw new ApiException("Invalid or missing settings", HttpStatusCode.BadRequest);
// map settings
var mapped = mapper.Map<Persistence.Model.MinerSettings>(settings);
mapped.PoolId = pool.Id;
mapped.Address = address;
var result = await cf.RunTx((con, tx) => minerRepo.UpdateSettings(con, tx, mapped));
return mapper.Map<Responses.MinerSettings>(result);
}
#endregion // Actions
}
}