Fix bot things

This commit is contained in:
2025-02-07 16:54:42 +07:00
parent cd797711c1
commit 898ff85eed
8 changed files with 86 additions and 86 deletions

View File

@@ -73,11 +73,11 @@ public class DataController : ControllerBase
public ActionResult<SpotlightOverview> GetSpotlight()
{
var overview = _cacheService.GetOrSave(nameof(SpotlightOverview),
() => { return _statisticService.GetLastSpotlight(DateTime.Now.AddHours(-3)); }, TimeSpan.FromMinutes(2));
() => { return _statisticService.GetLastSpotlight(DateTime.Now.AddDays(-2)); }, TimeSpan.FromMinutes(2));
if (overview?.Spotlights.Count < overview?.ScenarioCount)
if (overview?.Spotlights.Count < overview?.ScenarioCount || overview == null)
{
overview = _statisticService.GetLastSpotlight(DateTime.Now.AddHours(-3));
overview = _statisticService.GetLastSpotlight(DateTime.Now.AddDays(-2));
}
return Ok(overview);

View File

@@ -32,4 +32,5 @@ public interface IBotService
Task<bool> DeleteBot(string requestName);
Task<string> RestartBot(string requestName);
void DeleteBotBackup(string backupBotName);
void ToggleIsForWatchingOnly(string botName);
}

View File

@@ -210,6 +210,17 @@ namespace Managing.Application.ManageBot
_botRepository.DeleteBotBackup(backupBotName);
}
public void ToggleIsForWatchingOnly(string botName)
{
if (_botTasks.TryGetValue(botName, out var botWrapper))
{
if (botWrapper.BotInstance is ITradingBot bot)
{
bot.IsForWatchingOnly = !bot.IsForWatchingOnly;
}
}
}
public ITradingBot CreateScalpingBot(string accountName, MoneyManagement moneyManagement, string name,
Enums.Ticker ticker, string scenario, Enums.Timeframe interval, bool isForWatchingOnly)
{

View File

@@ -6,18 +6,18 @@ namespace Managing.Application.ManageBot
{
public class ToggleIsForWatchingCommandHandler : IRequestHandler<ToggleIsForWatchingCommand, string>
{
private readonly ITaskCache _taskCache;
private readonly IBotService _botService;
public ToggleIsForWatchingCommandHandler(ITaskCache taskCache)
public ToggleIsForWatchingCommandHandler(IBotService botService)
{
_taskCache = taskCache;
_botService = botService;
}
public async Task<string> Handle(ToggleIsForWatchingCommand request, CancellationToken cancellationToken)
public Task<string> Handle(ToggleIsForWatchingCommand request, CancellationToken cancellationToken)
{
var bot = _taskCache.Get<ITradingBot>(request.Name);
await bot.ToggleIsForWatchOnly();
return bot.GetStatus();
_botService.ToggleIsForWatchingOnly(request.Name);
var bot = _botService.GetActiveBots().FirstOrDefault(b => b.Name == request.Name);
return Task.FromResult(bot?.IsForWatchingOnly.ToString());
}
}
}

View File

@@ -30,27 +30,38 @@ namespace Managing.Domain.Bots
}
public async Task InitWorker(Func<Task> action)
{
try
{
await Task.Run(async () =>
{
while (Status == BotStatus.Up)
while (Status == BotStatus.Up && !CancellationToken.IsCancellationRequested)
{
try
{
await action();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
ExecutionCount++;
await Task.Delay(Interval, CancellationToken.Token);
if (CancellationToken.IsCancellationRequested)
break;
}
catch (TaskCanceledException) when (CancellationToken.IsCancellationRequested)
{
// Graceful shutdown when cancellation is requested
break;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}, CancellationToken.Token);
}
catch (TaskCanceledException)
{
Console.WriteLine();
}
}
public void Stop()
{

View File

@@ -7,16 +7,11 @@ type UseAccountsProps = {
callback?: (data: Account[]) => void | undefined
}
const useAcconuts = ({ callback }: UseAccountsProps) => {
const useAccounts = ({ }: UseAccountsProps) => {
const { apiUrl } = useApiUrlStore()
const accountClient = new AccountClient({}, apiUrl)
const query = useQuery({
onSuccess: (data) => {
if (data && callback) {
callback(data)
}
},
queryFn: () => accountClient.account_GetAccounts(),
queryKey: ['accounts'],
})
@@ -24,4 +19,4 @@ const useAcconuts = ({ callback }: UseAccountsProps) => {
return query
}
export default useAcconuts
export default useAccounts

View File

@@ -104,7 +104,7 @@ const BotList: React.FC<IBotList> = ({ list }) => {
}
function getMoneyManagementBadge(moneyManagement: MoneyManagement) {
const classes = baseBadgeClass() + 'bg-primary'
const classes = baseBadgeClass() + ' bg-primary'
return (
<button
className={classes}
@@ -115,7 +115,7 @@ const BotList: React.FC<IBotList> = ({ list }) => {
>
<p className="text-primary-content flex">
<EyeIcon width={15}></EyeIcon>
{moneyManagement.name}
MoneyManagement
</p>
</button>
)
@@ -180,26 +180,13 @@ const BotList: React.FC<IBotList> = ({ list }) => {
</figure>
<div className="card-body">
<h2 className="card-title text-sm">
{bot.name}
{/* {exchangeBadge(bot.exchange)} */}
{bot.ticker}
{getMoneyManagementBadge(bot.moneyManagement)}
{getIsForWatchingBadge(bot.isForWatchingOnly, bot.name)}
{getToggleBotStatusBadge(bot.status, bot.name, bot.botType)}
{getDeleteBadge(bot.name)}
{getMoneyManagementBadge(bot.moneyManagement)}
</h2>
<div className="columns-2">
<div>
<div>
<CardText title="Ticker" content={bot.ticker}></CardText>
</div>
<div>
<CardText
title="Timeframe"
content={bot.timeframe}
></CardText>
</div>
</div>
</div>
<div className="columns-2">
<div>

View File

@@ -23,11 +23,9 @@ import {
} from '../../generated/ManagingApi'
import BotList from './botList'
import { useQuery } from '@tanstack/react-query'
const Bots: React.FC = () => {
const [bots, setBots] = useState<TradingBot[]>([])
const [scenarios, setScenarios] = React.useState<Scenario[]>([])
const [accounts, setAccounts] = React.useState<Account[]>([])
const [showModal, setShowModal] = useState(false)
const { register, handleSubmit } = useForm<StartBotRequest>()
const { apiUrl } = useApiUrlStore()
@@ -46,42 +44,39 @@ const Bots: React.FC = () => {
})
}
useEffect(() => {
setupHubConnection().then(() => {
if (bots.length == 0) {
botClient.bot_GetActiveBots().then((data) => {
setBots(data)
})
}
const { data: bots } = useQuery({
queryFn: () => botClient.bot_GetActiveBots(),
queryKey: ['bots'],
})
const scenarioClient = new ScenarioClient({}, apiUrl)
scenarioClient.scenario_GetScenarios().then((data) => {
setScenarios(data)
const { data: scenarios } = useQuery({
queryFn: () => scenarioClient.scenario_GetScenarios(),
queryKey: ['scenarios'],
})
const accountClient = new AccountClient({}, apiUrl)
accountClient.account_GetAccounts().then((data) => {
setAccounts(data)
})
}, [])
const setupHubConnection = async () => {
const hub = new Hub('bothub', apiUrl).hub
hub.on('BotsSubscription', (bots: TradingBot[]) => {
// eslint-disable-next-line no-console
console.log(
'bot List',
bots.map((bot) => {
return bot.name
})
)
setBots(bots)
const { data: accounts } = useQuery({
queryFn: () => accountClient.account_GetAccounts(),
queryKey: ['accounts'],
})
return hub
}
// const setupHubConnection = async () => {
// const hub = new Hub('bothub', apiUrl).hub
// hub.on('BotsSubscription', (bots: TradingBot[]) => {
// // eslint-disable-next-line no-console
// console.log(
// 'bot List',
// bots.map((bot) => {
// return bot.name
// })
// )
// setBots(bots)
// })
// return hub
// }
function openModal() {
setShowModal(true)
@@ -136,7 +131,7 @@ const Bots: React.FC = () => {
<PlayIcon width="20"></PlayIcon>
</button>
</div>
<BotList list={bots} />
<BotList list={bots || []} />
{showModal ? (
<>
<div>