Update bot name

This commit is contained in:
2025-06-09 02:00:33 +07:00
parent 64183fface
commit 26098d8c42
3 changed files with 88 additions and 10 deletions

View File

@@ -629,6 +629,20 @@ public class BotController : BaseController
} }
} }
// If the bot name is being changed, check for conflicts
var isNameChanging = !string.IsNullOrEmpty(request.Config.Name) &&
request.Config.Name != request.Identifier;
if (isNameChanging)
{
// Check if new name already exists
var existingBotWithNewName = bots.FirstOrDefault(b => b.Identifier == request.Config.Name);
if (existingBotWithNewName != null)
{
return BadRequest($"A bot with the name '{request.Config.Name}' already exists");
}
}
// Validate the money management if provided // Validate the money management if provided
if (request.Config.MoneyManagement != null) if (request.Config.MoneyManagement != null)
{ {
@@ -662,19 +676,27 @@ public class BotController : BaseController
return BadRequest("CloseEarlyWhenProfitable requires MaxPositionTimeHours to be set"); return BadRequest("CloseEarlyWhenProfitable requires MaxPositionTimeHours to be set");
} }
// Update the bot configuration using the new method // Update the bot configuration using the enhanced method
var success = await _botService.UpdateBotConfiguration(request.Identifier, request.Config); var success = await _botService.UpdateBotConfiguration(request.Identifier, request.Config);
if (success) if (success)
{ {
await _hubContext.Clients.All.SendAsync("SendNotification", var finalBotName = isNameChanging ? request.Config.Name : request.Identifier;
$"Bot {request.Identifier} configuration updated successfully by {user.Name}.", "Info");
return Ok("Bot configuration updated successfully"); await _hubContext.Clients.All.SendAsync("SendNotification",
$"Bot {finalBotName} configuration updated successfully by {user.Name}." +
(isNameChanging ? $" (renamed from {request.Identifier})" : ""), "Info");
await NotifyBotSubscriberAsync();
return Ok(isNameChanging
? $"Bot configuration updated successfully and renamed to '{request.Config.Name}'"
: "Bot configuration updated successfully");
} }
else else
{ {
return BadRequest("Failed to update bot configuration"); return BadRequest("Failed to update bot configuration. " +
(isNameChanging ? "The new name might already be in use." : ""));
} }
} }
catch (Exception ex) catch (Exception ex)

View File

@@ -1278,12 +1278,23 @@ public class TradingBot : Bot, ITradingBot
/// <summary> /// <summary>
/// Updates the trading bot configuration with new settings. /// Updates the trading bot configuration with new settings.
/// This method validates the new configuration and applies it to the running bot.
/// </summary> /// </summary>
/// <param name="newConfig">The new configuration to apply</param> /// <param name="newConfig">The new configuration to apply</param>
/// <returns>True if the configuration was successfully updated, false otherwise</returns> /// <returns>True if the configuration was successfully updated, false otherwise</returns>
/// <exception cref="ArgumentException">Thrown when the new configuration is invalid</exception>
public async Task<bool> UpdateConfiguration(TradingBotConfig newConfig) public async Task<bool> UpdateConfiguration(TradingBotConfig newConfig)
{
return await UpdateConfiguration(newConfig, allowNameChange: false);
}
/// <summary>
/// Updates the trading bot configuration with new settings.
/// This method validates the new configuration and applies it to the running bot.
/// </summary>
/// <param name="newConfig">The new configuration to apply</param>
/// <param name="allowNameChange">Whether to allow changing the bot name/identifier</param>
/// <returns>True if the configuration was successfully updated, false otherwise</returns>
/// <exception cref="ArgumentException">Thrown when the new configuration is invalid</exception>
public async Task<bool> UpdateConfiguration(TradingBotConfig newConfig, bool allowNameChange = false)
{ {
try try
{ {
@@ -1312,7 +1323,7 @@ public class TradingBot : Bot, ITradingBot
// Protect critical properties that shouldn't change for running bots // Protect critical properties that shouldn't change for running bots
var protectedBotType = Config.BotType; var protectedBotType = Config.BotType;
var protectedIsForBacktest = Config.IsForBacktest; var protectedIsForBacktest = Config.IsForBacktest;
var protectedName = Config.Name; var protectedName = allowNameChange ? newConfig.Name : Config.Name;
// Log the configuration update (before changing anything) // Log the configuration update (before changing anything)
await LogInformation("⚙️ **Configuration Update**\n" + await LogInformation("⚙️ **Configuration Update**\n" +
@@ -1321,7 +1332,8 @@ public class TradingBot : Bot, ITradingBot
$"⏱️ Max Time: {(Config.MaxPositionTimeHours?.ToString() + "h" ?? "Disabled")}\n" + $"⏱️ Max Time: {(Config.MaxPositionTimeHours?.ToString() + "h" ?? "Disabled")}\n" +
$"📈 Flip Only in Profit: {(Config.FlipOnlyWhenInProfit ? "" : "")}\n" + $"📈 Flip Only in Profit: {(Config.FlipOnlyWhenInProfit ? "" : "")}\n" +
$"⏳ Cooldown: {Config.CooldownPeriod} candles\n" + $"⏳ Cooldown: {Config.CooldownPeriod} candles\n" +
$"📉 Max Loss Streak: {Config.MaxLossStreak}"); $"📉 Max Loss Streak: {Config.MaxLossStreak}" +
(allowNameChange && newConfig.Name != Config.Name ? $"\n🏷 Name: {Config.Name} → {newConfig.Name}" : ""));
// Update the configuration // Update the configuration
Config = newConfig; Config = newConfig;
@@ -1331,6 +1343,13 @@ public class TradingBot : Bot, ITradingBot
Config.IsForBacktest = protectedIsForBacktest; Config.IsForBacktest = protectedIsForBacktest;
Config.Name = protectedName; Config.Name = protectedName;
// Update bot name and identifier if allowed
if (allowNameChange && !string.IsNullOrEmpty(newConfig.Name))
{
Name = newConfig.Name;
Identifier = newConfig.Name;
}
// If account changed, reload it // If account changed, reload it
if (Config.AccountName != Account?.Name) if (Config.AccountName != Account?.Name)
{ {

View File

@@ -247,12 +247,49 @@ namespace Managing.Application.ManageBot
if (_botTasks.TryGetValue(identifier, out var botTaskWrapper) && if (_botTasks.TryGetValue(identifier, out var botTaskWrapper) &&
botTaskWrapper.BotInstance is TradingBot tradingBot) botTaskWrapper.BotInstance is TradingBot tradingBot)
{ {
return await tradingBot.UpdateConfiguration(newConfig); // Check if the bot name is changing
if (newConfig.Name != identifier && !string.IsNullOrEmpty(newConfig.Name))
{
// Check if new name already exists
if (_botTasks.ContainsKey(newConfig.Name))
{
return false; // New name already in use
}
// Update the bot configuration first
var updateResult = await tradingBot.UpdateConfiguration(newConfig, allowNameChange: true);
if (updateResult)
{
// Update the dictionary key
if (_botTasks.TryRemove(identifier, out var removedWrapper))
{
_botTasks.TryAdd(newConfig.Name, removedWrapper);
// Update the backup with the new identifier
if (!newConfig.IsForBacktest)
{
// Delete old backup
await _botRepository.DeleteBotBackup(identifier);
// Save new backup will be handled by the bot's SaveBackup method
}
}
}
return updateResult;
}
else
{
// No name change, just update configuration
return await tradingBot.UpdateConfiguration(newConfig);
}
} }
return false; return false;
} }
public ITradingBot CreateTradingBot(TradingBotConfig config) public ITradingBot CreateTradingBot(TradingBotConfig config)
{ {
return new TradingBot( return new TradingBot(