Add backup management
This commit is contained in:
@@ -151,6 +151,7 @@ ________________________________________________________________________________
|
|||||||
- [ ] Create method to update the money management use by the bot
|
- [ ] Create method to update the money management use by the bot
|
||||||
- [ ] Implement from/to tickers array pattern
|
- [ ] Implement from/to tickers array pattern
|
||||||
- [ ] Extract all managing trade method into a TradingBox class => Create composable trading bot type easily
|
- [ ] Extract all managing trade method into a TradingBox class => Create composable trading bot type easily
|
||||||
|
- [ ] Bot backup worker: Every x, get saved bots and check if still running. If not running call api to reboot bot.
|
||||||
|
|
||||||
# Front-end
|
# Front-end
|
||||||
## Improve Account page
|
## Improve Account page
|
||||||
|
|||||||
@@ -36,6 +36,9 @@
|
|||||||
<Content Update="appsettings.json">
|
<Content Update="appsettings.json">
|
||||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
</Content>
|
</Content>
|
||||||
|
<Content Update="appsettings.Prod.json">
|
||||||
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
|
</Content>
|
||||||
<Content Update="appsettings.Oda-sandbox.json">
|
<Content Update="appsettings.Oda-sandbox.json">
|
||||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
</Content>
|
</Content>
|
||||||
|
|||||||
@@ -17,12 +17,15 @@ using Serilog.Sinks.Elasticsearch;
|
|||||||
|
|
||||||
// Builder
|
// Builder
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
builder.Configuration.AddJsonFile("appsettings.Lowpro.json", optional: true, reloadOnChange: true)
|
builder.Configuration.SetBasePath(System.AppContext.BaseDirectory);
|
||||||
|
builder.Configuration.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
||||||
|
.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json")
|
||||||
.AddJsonFile($"config.{builder.Environment.EnvironmentName}.json",
|
.AddJsonFile($"config.{builder.Environment.EnvironmentName}.json",
|
||||||
optional: true, reloadOnChange: true);
|
optional: true, reloadOnChange: true);
|
||||||
|
|
||||||
builder.Configuration.AddEnvironmentVariables();
|
builder.Configuration.AddEnvironmentVariables();
|
||||||
builder.Configuration.AddUserSecrets<Program>();
|
builder.Configuration.AddUserSecrets<Program>();
|
||||||
|
|
||||||
builder.Host.UseSerilog((hostBuilder, loggerConfiguration) =>
|
builder.Host.UseSerilog((hostBuilder, loggerConfiguration) =>
|
||||||
{
|
{
|
||||||
var envName = builder.Environment.EnvironmentName.ToLower().Replace(".", "-");
|
var envName = builder.Environment.EnvironmentName.ToLower().Replace(".", "-");
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
using Managing.Domain.Bots;
|
||||||
|
|
||||||
|
public interface IBotRepository
|
||||||
|
{
|
||||||
|
Task InsertBotAsync(BotBackup bot);
|
||||||
|
IEnumerable<BotBackup> GetBots();
|
||||||
|
Task UpdateBackupBot(BotBackup bot);
|
||||||
|
}
|
||||||
4
src/Managing.Application/Abstractions/IBotService.cs
Normal file
4
src/Managing.Application/Abstractions/IBotService.cs
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
public interface IBotService
|
||||||
|
{
|
||||||
|
void SaveBotBackup(BotBackup botBackup);
|
||||||
|
}
|
||||||
@@ -1,13 +1,14 @@
|
|||||||
using Managing.Domain.Bots;
|
using Managing.Domain.Bots;
|
||||||
using Managing.Domain.Workflows;
|
using Managing.Domain.Workflows;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
namespace Managing.Application.Bots
|
namespace Managing.Application.Bots
|
||||||
{
|
{
|
||||||
public class SimpleBot : Bot
|
public class SimpleBot : Bot
|
||||||
{
|
{
|
||||||
public readonly ILogger<TradingBot> Logger;
|
public readonly ILogger<TradingBot> Logger;
|
||||||
private readonly Workflow _workflow;
|
private Workflow _workflow;
|
||||||
|
|
||||||
public SimpleBot(string name, ILogger<TradingBot> logger, Workflow workflow) : base(name)
|
public SimpleBot(string name, ILogger<TradingBot> logger, Workflow workflow) : base(name)
|
||||||
{
|
{
|
||||||
@@ -35,5 +36,15 @@ namespace Managing.Application.Bots
|
|||||||
Logger.LogInformation("__________________________________________________");
|
Logger.LogInformation("__________________________________________________");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override string GetBackup()
|
||||||
|
{
|
||||||
|
return JsonConvert.SerializeObject(_workflow);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void LoadBackup(BotBackup backup)
|
||||||
|
{
|
||||||
|
_workflow = JsonConvert.DeserializeObject<Workflow>(backup.Data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ using Managing.Domain.Shared.Helpers;
|
|||||||
using Managing.Domain.Strategies;
|
using Managing.Domain.Strategies;
|
||||||
using Managing.Domain.Trades;
|
using Managing.Domain.Trades;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
using static Managing.Common.Enums;
|
using static Managing.Common.Enums;
|
||||||
|
|
||||||
namespace Managing.Application.Bots;
|
namespace Managing.Application.Bots;
|
||||||
@@ -64,6 +65,7 @@ public class TradingBot : Bot, ITradingBot
|
|||||||
MessengerService = messengerService;
|
MessengerService = messengerService;
|
||||||
TradingService = tradingService;
|
TradingService = tradingService;
|
||||||
|
|
||||||
|
|
||||||
IsForWatchingOnly = isForWatchingOnly;
|
IsForWatchingOnly = isForWatchingOnly;
|
||||||
FlipPosition = flipPosition;
|
FlipPosition = flipPosition;
|
||||||
AccountName = accountName;
|
AccountName = accountName;
|
||||||
@@ -648,4 +650,48 @@ public class TradingBot : Bot, ITradingBot
|
|||||||
await MessengerService.SendTradeMessage(message, isBadBehavior);
|
await MessengerService.SendTradeMessage(message, isBadBehavior);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override string GetBackup()
|
||||||
|
{
|
||||||
|
return JsonConvert.SerializeObject(new TradingBotBackup
|
||||||
|
{
|
||||||
|
Name = Name,
|
||||||
|
BotType = BotType,
|
||||||
|
Strategies = Strategies,
|
||||||
|
Signals = Signals,
|
||||||
|
Positions = Positions,
|
||||||
|
Timeframe = Timeframe,
|
||||||
|
Ticker = Ticker,
|
||||||
|
Scenario = Scenario,
|
||||||
|
AccountName = AccountName,
|
||||||
|
IsForWatchingOnly = IsForWatchingOnly,
|
||||||
|
WalletBalances = WalletBalances,
|
||||||
|
MoneyManagement = MoneyManagement
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void LoadBackup(BotBackup backup)
|
||||||
|
{
|
||||||
|
var data = JsonConvert.DeserializeObject<TradingBotBackup>(backup.Data);
|
||||||
|
Strategies = data.Strategies;
|
||||||
|
Signals = data.Signals;
|
||||||
|
Positions = data.Positions;
|
||||||
|
WalletBalances = data.WalletBalances;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class TradingBotBackup
|
||||||
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
|
public BotType BotType { get; set; }
|
||||||
|
public HashSet<IStrategy> Strategies { get; set; }
|
||||||
|
public HashSet<Signal> Signals { get; set; }
|
||||||
|
public List<Position> Positions { get; set; }
|
||||||
|
public Timeframe Timeframe { get; set; }
|
||||||
|
public Ticker Ticker { get; set; }
|
||||||
|
public string Scenario { get; set; }
|
||||||
|
public string AccountName { get; set; }
|
||||||
|
public bool IsForWatchingOnly { get; set; }
|
||||||
|
public Dictionary<DateTime, decimal> WalletBalances { get; set; }
|
||||||
|
public MoneyManagement MoneyManagement { get; internal set; }
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
using Managing.Domain.Bots;
|
||||||
|
using MediatR;
|
||||||
|
using static Managing.Common.Enums;
|
||||||
|
using Managing.Application.Abstractions;
|
||||||
|
using Managing.Application.ManageBot.Commands;
|
||||||
|
using Managing.Domain.MoneyManagements;
|
||||||
|
|
||||||
|
namespace Managing.Application.ManageBot
|
||||||
|
{
|
||||||
|
public class BackupBotCommandHandler : IRequestHandler<BackupBotCommand, bool>
|
||||||
|
{
|
||||||
|
private readonly IBotFactory _botFactory;
|
||||||
|
private readonly ITaskCache _taskCache;
|
||||||
|
private readonly IMoneyManagementService _moneyManagementService;
|
||||||
|
private readonly IBotService _botService;
|
||||||
|
|
||||||
|
public BackupBotCommandHandler(IBotFactory botFactory, ITaskCache taskCache, IBotService botService)
|
||||||
|
{
|
||||||
|
_botFactory = botFactory;
|
||||||
|
_taskCache = taskCache;
|
||||||
|
_botService = botService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task<bool> Handle(BackupBotCommand request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var botBackup = new BotBackup
|
||||||
|
{
|
||||||
|
Name = request.Name,
|
||||||
|
BotType = request.BotType,
|
||||||
|
Data = ""
|
||||||
|
};
|
||||||
|
|
||||||
|
switch (request.BotType)
|
||||||
|
{
|
||||||
|
case BotType.SimpleBot:
|
||||||
|
var simpleBot = _taskCache.Get<IBot>(request.Name);
|
||||||
|
botBackup.Data = simpleBot.GetBackup();
|
||||||
|
break;
|
||||||
|
case BotType.ScalpingBot:
|
||||||
|
var scalpingBot = _taskCache.Get<ITradingBot>(request.Name);
|
||||||
|
botBackup.Data = scalpingBot.GetBackup();
|
||||||
|
break;
|
||||||
|
case BotType.FlippingBot:
|
||||||
|
var flippingBot = _taskCache.Get<ITradingBot>(request.Name);
|
||||||
|
botBackup.Data = flippingBot.GetBackup();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return Task.FromResult(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
_botService.SaveBotBackup(botBackup);
|
||||||
|
|
||||||
|
return Task.FromResult(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class BackupBotCommand : IRequest<bool>
|
||||||
|
{
|
||||||
|
public string Name { get; }
|
||||||
|
public BotType BotType { get; }
|
||||||
|
|
||||||
|
public BackupBotCommand(BotType botType, string name)
|
||||||
|
{
|
||||||
|
BotType = botType;
|
||||||
|
Name = name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
37
src/Managing.Application/ManageBot/BotService.cs
Normal file
37
src/Managing.Application/ManageBot/BotService.cs
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
using Managing.Application.Abstractions;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace Managing.Application.ManageBot
|
||||||
|
{
|
||||||
|
public class BotService : IBotService
|
||||||
|
{
|
||||||
|
private readonly IBotFactory _botFactory;
|
||||||
|
private readonly IBotRepository _botRepository;
|
||||||
|
|
||||||
|
public BotService(IBotFactory botFactory, IBotRepository botRepository)
|
||||||
|
{
|
||||||
|
_botFactory = botFactory;
|
||||||
|
_botRepository = botRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
// public void CreateBot()
|
||||||
|
// {
|
||||||
|
// // Use the factory to create a new bot
|
||||||
|
// return _botFactory.CreateBot();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// public void LoadBotBackup(BotBackup botBackup)
|
||||||
|
// {
|
||||||
|
// // Deserialize the JSON into a Bot object
|
||||||
|
// var bot = JsonConvert.DeserializeObject<Bot>(json);
|
||||||
|
|
||||||
|
// return bot;
|
||||||
|
// }
|
||||||
|
|
||||||
|
public async void SaveBotBackup(BotBackup botBackup)
|
||||||
|
{
|
||||||
|
await _botRepository.InsertBotAsync(botBackup);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,127 @@
|
|||||||
|
using Managing.Domain.Bots;
|
||||||
|
using MediatR;
|
||||||
|
using static Managing.Common.Enums;
|
||||||
|
using Managing.Application.Abstractions;
|
||||||
|
using Managing.Core;
|
||||||
|
using Managing.Domain.MoneyManagements;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Managing.Application.Bots;
|
||||||
|
|
||||||
|
namespace Managing.Application.ManageBot
|
||||||
|
{
|
||||||
|
public class LoadBackupBotCommandHandler : IRequestHandler<LoadBackupBotCommand, string>
|
||||||
|
{
|
||||||
|
private readonly IBotFactory _botFactory;
|
||||||
|
private readonly ITaskCache _taskCache;
|
||||||
|
private readonly IMoneyManagementService _moneyManagementService;
|
||||||
|
private readonly IBotRepository _botRepository;
|
||||||
|
private readonly IMediator _mediator;
|
||||||
|
|
||||||
|
public LoadBackupBotCommandHandler(
|
||||||
|
IBotFactory botFactory,
|
||||||
|
ITaskCache taskCache,
|
||||||
|
IMoneyManagementService moneyManagementService,
|
||||||
|
IBotRepository botRepository,
|
||||||
|
IMediator mediator)
|
||||||
|
{
|
||||||
|
_botFactory = botFactory;
|
||||||
|
_taskCache = taskCache;
|
||||||
|
_moneyManagementService = moneyManagementService;
|
||||||
|
_botRepository = botRepository;
|
||||||
|
_mediator = mediator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task<string> Handle(LoadBackupBotCommand request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
BotStatus botStatus = BotStatus.Down;
|
||||||
|
var backupBots = _botRepository.GetBots();
|
||||||
|
var result = new Dictionary<string, BotStatus>();
|
||||||
|
|
||||||
|
foreach (var backupBot in backupBots)
|
||||||
|
{
|
||||||
|
// Check if bot is existing in cache
|
||||||
|
switch (backupBot.BotType)
|
||||||
|
{
|
||||||
|
case BotType.SimpleBot:
|
||||||
|
var simpleBot = _taskCache.Get<IBot>(backupBot.Name);
|
||||||
|
if (simpleBot == null)
|
||||||
|
{
|
||||||
|
StartBot(request, backupBot);
|
||||||
|
simpleBot.LoadBackup(backupBot);
|
||||||
|
result.Add(simpleBot.GetName(), BotStatus.Backup);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
result.Add(simpleBot.GetName(), MiscExtensions.ParseEnum<BotStatus>(simpleBot.GetStatus()));
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
case BotType.ScalpingBot:
|
||||||
|
case BotType.FlippingBot:
|
||||||
|
var scalpingBot = _taskCache.Get<ITradingBot>(backupBot.Name);
|
||||||
|
if (scalpingBot == null)
|
||||||
|
{
|
||||||
|
StartBot(request, backupBot);
|
||||||
|
scalpingBot.LoadBackup(backupBot);
|
||||||
|
result.Add(scalpingBot.GetName(), BotStatus.Backup);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
result.Add(scalpingBot.GetName(), MiscExtensions.ParseEnum<BotStatus>(scalpingBot.GetStatus()));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
result.Add(backupBot.Name, BotStatus.Down);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return Task.FromResult(botStatus.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void StartBot(LoadBackupBotCommand request, BotBackup backupBot)
|
||||||
|
{
|
||||||
|
switch (backupBot.BotType)
|
||||||
|
{
|
||||||
|
case BotType.SimpleBot:
|
||||||
|
Func<Task<IBot>> simpleBot = () => Task.FromResult(_botFactory.CreateSimpleBot(request.Name, null));
|
||||||
|
var bot1 = _taskCache.AddOrGetExisting(request.Name, simpleBot).Result;
|
||||||
|
bot1.LoadBackup(backupBot);
|
||||||
|
break;
|
||||||
|
case BotType.ScalpingBot:
|
||||||
|
var data = JsonConvert.DeserializeObject<TradingBotBackup>(backupBot.Data);
|
||||||
|
Func<Task<ITradingBot>> scalpingBot = () => Task.FromResult(_botFactory.CreateScalpingBot(
|
||||||
|
data.AccountName,
|
||||||
|
data.MoneyManagement,
|
||||||
|
data.Name,
|
||||||
|
data.Ticker,
|
||||||
|
data.Scenario,
|
||||||
|
data.Timeframe,
|
||||||
|
data.IsForWatchingOnly));
|
||||||
|
var bot2 = _taskCache.AddOrGetExisting(request.Name, scalpingBot).Result;
|
||||||
|
bot2.LoadBackup(backupBot);
|
||||||
|
break;
|
||||||
|
case BotType.FlippingBot:
|
||||||
|
var dataFlippingBot = JsonConvert.DeserializeObject<TradingBotBackup>(backupBot.Data);
|
||||||
|
Func<Task<ITradingBot>> flippingBot = () => Task.FromResult(_botFactory.CreateFlippingBot(
|
||||||
|
dataFlippingBot.AccountName,
|
||||||
|
dataFlippingBot.MoneyManagement,
|
||||||
|
dataFlippingBot.Name,
|
||||||
|
dataFlippingBot.Ticker,
|
||||||
|
dataFlippingBot.Scenario,
|
||||||
|
dataFlippingBot.Timeframe,
|
||||||
|
dataFlippingBot.IsForWatchingOnly));
|
||||||
|
var bot3 = _taskCache.AddOrGetExisting(request.Name, flippingBot).Result;
|
||||||
|
bot3.LoadBackup(backupBot);
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class LoadBackupBotCommand : IRequest<string>
|
||||||
|
{
|
||||||
|
public string Name { get; internal set; }
|
||||||
|
public string AccountName { get; internal set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -41,6 +41,7 @@ using Binance.Net.Interfaces.Clients;
|
|||||||
using Managing.Infrastructure.Evm.Services;
|
using Managing.Infrastructure.Evm.Services;
|
||||||
using Managing.Application.Workflows;
|
using Managing.Application.Workflows;
|
||||||
using Managing.Application.Bots.Base;
|
using Managing.Application.Bots.Base;
|
||||||
|
using Managing.Application.ManageBot;
|
||||||
|
|
||||||
namespace Managing.Bootstrap;
|
namespace Managing.Bootstrap;
|
||||||
|
|
||||||
@@ -105,6 +106,7 @@ public static class ApiBootstrap
|
|||||||
services.AddTransient<IUserRepository, UserRepository>();
|
services.AddTransient<IUserRepository, UserRepository>();
|
||||||
services.AddTransient<IStatisticRepository, StatisticRepository>();
|
services.AddTransient<IStatisticRepository, StatisticRepository>();
|
||||||
services.AddTransient<IWorkflowRepository, WorkflowRepository>();
|
services.AddTransient<IWorkflowRepository, WorkflowRepository>();
|
||||||
|
services.AddTransient<IBotRepository, BotRepository>();
|
||||||
|
|
||||||
// Cache
|
// Cache
|
||||||
services.AddDistributedMemoryCache();
|
services.AddDistributedMemoryCache();
|
||||||
@@ -123,6 +125,7 @@ public static class ApiBootstrap
|
|||||||
services.AddTransient<IExchangeStream, ExchangeStream>();
|
services.AddTransient<IExchangeStream, ExchangeStream>();
|
||||||
services.AddSingleton<IMessengerService, MessengerService>();
|
services.AddSingleton<IMessengerService, MessengerService>();
|
||||||
services.AddSingleton<IDiscordService, DiscordService>();
|
services.AddSingleton<IDiscordService, DiscordService>();
|
||||||
|
services.AddSingleton<IBotService, BotService>();
|
||||||
|
|
||||||
// Stream
|
// Stream
|
||||||
services.AddSingleton<IBinanceSocketClient, BinanceSocketClient>();
|
services.AddSingleton<IBinanceSocketClient, BinanceSocketClient>();
|
||||||
|
|||||||
@@ -73,7 +73,8 @@ public static class Enums
|
|||||||
{
|
{
|
||||||
Down,
|
Down,
|
||||||
Starting,
|
Starting,
|
||||||
Up
|
Up,
|
||||||
|
Backup
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum SignalStatus
|
public enum SignalStatus
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using static Managing.Common.Enums;
|
using System.Text.Json;
|
||||||
|
using static Managing.Common.Enums;
|
||||||
|
|
||||||
namespace Managing.Domain.Bots
|
namespace Managing.Domain.Bots
|
||||||
{
|
{
|
||||||
@@ -14,6 +15,7 @@ namespace Managing.Domain.Bots
|
|||||||
public int Interval { get; set; }
|
public int Interval { get; set; }
|
||||||
public BotStatus Status { get; set; }
|
public BotStatus Status { get; set; }
|
||||||
private CancellationTokenSource CancellationToken { get; set; }
|
private CancellationTokenSource CancellationToken { get; set; }
|
||||||
|
public string Data { get; set; }
|
||||||
|
|
||||||
public Bot(string name)
|
public Bot(string name)
|
||||||
{
|
{
|
||||||
@@ -71,5 +73,8 @@ namespace Managing.Domain.Bots
|
|||||||
{
|
{
|
||||||
return Name;
|
return Name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public abstract string GetBackup();
|
||||||
|
public abstract void LoadBackup(BotBackup backup);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
9
src/Managing.Domain/Bots/BotBackup.cs
Normal file
9
src/Managing.Domain/Bots/BotBackup.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
using Managing.Domain.MoneyManagements;
|
||||||
|
using static Managing.Common.Enums;
|
||||||
|
|
||||||
|
public class BotBackup
|
||||||
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
|
public BotType BotType { get; set; }
|
||||||
|
public string Data { get; set; }
|
||||||
|
}
|
||||||
@@ -8,5 +8,7 @@
|
|||||||
void Restart();
|
void Restart();
|
||||||
string GetStatus();
|
string GetStatus();
|
||||||
string GetName();
|
string GetName();
|
||||||
|
string GetBackup();
|
||||||
|
void LoadBackup(BotBackup backup);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
41
src/Managing.Infrastructure.Database/BotRepository.cs
Normal file
41
src/Managing.Infrastructure.Database/BotRepository.cs
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
using Managing.Domain.Bots;
|
||||||
|
using Managing.Infrastructure.Databases.MongoDb;
|
||||||
|
using Managing.Infrastructure.Databases.MongoDb.Abstractions;
|
||||||
|
using Managing.Infrastructure.Databases.MongoDb.Collections;
|
||||||
|
|
||||||
|
namespace Managing.Infrastructure.Databases;
|
||||||
|
|
||||||
|
public class BotRepository : IBotRepository
|
||||||
|
{
|
||||||
|
private readonly IMongoRepository<BotDto> _botRepository;
|
||||||
|
|
||||||
|
public BotRepository(IMongoRepository<BotDto> botRepository)
|
||||||
|
{
|
||||||
|
_botRepository = botRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task InsertBotAsync(BotBackup bot)
|
||||||
|
{
|
||||||
|
await _botRepository.InsertOneAsync(MongoMappers.Map(bot));
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<BotBackup> GetBots()
|
||||||
|
{
|
||||||
|
var bots = _botRepository.FindAll();
|
||||||
|
return bots.Select(b => MongoMappers.Map(b));
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task UpdateBackupBot(BotBackup bot)
|
||||||
|
{
|
||||||
|
var b = _botRepository.FindOne(b => b.Name == bot.Name);
|
||||||
|
var dto = MongoMappers.Map(bot);
|
||||||
|
dto.Id = b.Id;
|
||||||
|
_botRepository.Update(dto);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DeleteBotByName(string name)
|
||||||
|
{
|
||||||
|
var bot = _botRepository.FindOne(b => b.Name == name);
|
||||||
|
_botRepository.DeleteById(bot.Id.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
using Managing.Infrastructure.Databases.MongoDb.Attributes;
|
||||||
|
using Managing.Infrastructure.Databases.MongoDb.Configurations;
|
||||||
|
using static Managing.Common.Enums;
|
||||||
|
|
||||||
|
namespace Managing.Infrastructure.Databases.MongoDb.Collections;
|
||||||
|
|
||||||
|
[BsonCollection("Bots")]
|
||||||
|
public class BotDto : Document
|
||||||
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
|
public string Data { get; set; }
|
||||||
|
public BotType BotType { get; set; }
|
||||||
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
using Managing.Domain.Accounts;
|
using System.Reflection.Metadata.Ecma335;
|
||||||
|
using Managing.Domain.Accounts;
|
||||||
using Managing.Domain.Backtests;
|
using Managing.Domain.Backtests;
|
||||||
|
using Managing.Domain.Bots;
|
||||||
using Managing.Domain.Candles;
|
using Managing.Domain.Candles;
|
||||||
using Managing.Domain.MoneyManagements;
|
using Managing.Domain.MoneyManagements;
|
||||||
using Managing.Domain.Scenarios;
|
using Managing.Domain.Scenarios;
|
||||||
@@ -650,5 +652,29 @@ public static class MongoMappers
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal static BotDto Map(BotBackup bot)
|
||||||
|
{
|
||||||
|
if (bot == null) return null;
|
||||||
|
|
||||||
|
return new BotDto
|
||||||
|
{
|
||||||
|
Name = bot.Name,
|
||||||
|
BotType = bot.BotType,
|
||||||
|
Data = bot.Data,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static BotBackup Map(BotDto b)
|
||||||
|
{
|
||||||
|
if (b == null) return null;
|
||||||
|
|
||||||
|
return new BotBackup
|
||||||
|
{
|
||||||
|
Name = b.Name,
|
||||||
|
BotType = b.BotType,
|
||||||
|
Data = b.Data
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user