update stats data

This commit is contained in:
2025-08-15 07:42:26 +07:00
parent 0a4a4e1398
commit 7528405845
8 changed files with 413 additions and 450 deletions

View File

@@ -40,8 +40,10 @@ public interface IPlatformSummaryGrain : IGrainWithStringKey
Task<int> GetTotalPositionCountAsync();
// Event handlers for immediate updates
Task OnStrategyDeployedAsync(StrategyDeployedEvent evt);
Task OnStrategyStoppedAsync(StrategyStoppedEvent evt);
/// <summary>
/// Updates the active strategy count
/// </summary>
Task UpdateActiveStrategyCountAsync(int newActiveCount);
Task OnPositionOpenedAsync(PositionOpenedEvent evt);
Task OnPositionClosedAsync(PositionClosedEvent evt);
Task OnTradeExecutedAsync(TradeExecutedEvent evt);
@@ -57,37 +59,7 @@ public abstract class PlatformMetricsEvent
public DateTime Timestamp { get; set; } = DateTime.UtcNow;
}
/// <summary>
/// Event fired when a new strategy is deployed
/// </summary>
[GenerateSerializer]
public class StrategyDeployedEvent : PlatformMetricsEvent
{
[Id(1)]
public Guid StrategyId { get; set; }
[Id(2)]
public string AgentName { get; set; } = string.Empty;
[Id(3)]
public string StrategyName { get; set; } = string.Empty;
}
/// <summary>
/// Event fired when a strategy is stopped
/// </summary>
[GenerateSerializer]
public class StrategyStoppedEvent : PlatformMetricsEvent
{
[Id(1)]
public Guid StrategyId { get; set; }
[Id(2)]
public string AgentName { get; set; } = string.Empty;
[Id(3)]
public string StrategyName { get; set; } = string.Empty;
}
/// <summary>
/// Event fired when a new position is opened

View File

@@ -65,8 +65,8 @@ public class LiveBotRegistryGrain : Grain, ILiveBotRegistryGrain
"Bot {Identifier} registered successfully for user {UserId}. Total bots: {TotalBots}, Active bots: {ActiveBots}",
identifier, userId, _state.State.TotalBotsCount, _state.State.ActiveBotsCount);
// Notify platform summary grain about strategy deployment
await NotifyStrategyDeployedAsync(identifier, userId);
// Notify platform summary grain about strategy count change
await NotifyPlatformSummaryAsync();
}
catch (Exception ex)
{
@@ -102,8 +102,8 @@ public class LiveBotRegistryGrain : Grain, ILiveBotRegistryGrain
"Bot {Identifier} unregistered successfully from user {UserId}. Total bots: {TotalBots}",
identifier, entryToRemove.UserId, _state.State.TotalBotsCount);
// Notify platform summary grain about strategy stopped
await NotifyStrategyStoppedAsync(identifier, entryToRemove.UserId);
// Notify platform summary grain about strategy count change
await NotifyPlatformSummaryAsync();
}
catch (Exception ex)
{
@@ -187,65 +187,19 @@ public class LiveBotRegistryGrain : Grain, ILiveBotRegistryGrain
return Task.FromResult(entry.Status);
}
private async Task NotifyStrategyDeployedAsync(Guid identifier, int userId)
private async Task NotifyPlatformSummaryAsync()
{
try
{
// Get bot details for the event
var bot = await _botService.GetBotByIdentifier(identifier);
if (bot != null)
{
var platformGrain = GrainFactory.GetGrain<IPlatformSummaryGrain>("platform-summary");
var deployedEvent = new StrategyDeployedEvent
{
StrategyId = identifier,
AgentName = bot.User.AgentName,
StrategyName = bot.Name
};
await platformGrain.OnStrategyDeployedAsync(deployedEvent);
_logger.LogDebug("Notified platform summary about strategy deployment: {StrategyName}", bot.Name);
}
else
{
_logger.LogWarning("Could not find bot {Identifier} to notify platform summary", identifier);
}
var platformGrain = GrainFactory.GetGrain<IPlatformSummaryGrain>("platform-summary");
await platformGrain.UpdateActiveStrategyCountAsync(_state.State.ActiveBotsCount);
_logger.LogDebug("Notified platform summary about active strategy count change. New count: {ActiveCount}",
_state.State.ActiveBotsCount);
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to notify platform summary about strategy deployment for bot {Identifier}", identifier);
}
}
private async Task NotifyStrategyStoppedAsync(Guid identifier, int userId)
{
try
{
// Get bot details for the event
var bot = await _botService.GetBotByIdentifier(identifier);
if (bot != null)
{
var platformGrain = GrainFactory.GetGrain<IPlatformSummaryGrain>("platform-summary");
var stoppedEvent = new StrategyStoppedEvent
{
StrategyId = identifier,
AgentName = bot.User?.Name ?? $"User-{userId}",
StrategyName = bot.Name
};
await platformGrain.OnStrategyStoppedAsync(stoppedEvent);
_logger.LogDebug("Notified platform summary about strategy stopped: {StrategyName}", bot.Name);
}
else
{
_logger.LogWarning("Could not find bot {Identifier} to notify platform summary", identifier);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to notify platform summary about strategy stopped for bot {Identifier}", identifier);
_logger.LogError(ex, "Failed to notify platform summary about strategy count change");
}
}
}

View File

@@ -190,7 +190,8 @@ public class PlatformSummaryGrain : Grain, IPlatformSummaryGrain, IRemindable
var openInterest = openPositions
.Sum(p => (p.Open.Price * p.Open.Quantity) * p.Open.Leverage);
_logger.LogDebug("Calculated position metrics: {PositionCount} positions, {OpenInterest} leveraged open interest",
_logger.LogDebug(
"Calculated position metrics: {PositionCount} positions, {OpenInterest} leveraged open interest",
positionCount, openInterest);
return (openInterest, positionCount);
@@ -229,20 +230,11 @@ public class PlatformSummaryGrain : Grain, IPlatformSummaryGrain, IRemindable
}
// Event handlers for immediate updates
public async Task OnStrategyDeployedAsync(StrategyDeployedEvent evt)
public async Task UpdateActiveStrategyCountAsync(int newActiveCount)
{
_logger.LogInformation("Strategy deployed: {StrategyId} - {StrategyName}", evt.StrategyId, evt.StrategyName);
_logger.LogInformation("Updating active strategies count to: {NewActiveCount}", newActiveCount);
_state.State.TotalActiveStrategies++;
_state.State.HasPendingChanges = true;
await _state.WriteStateAsync();
}
public async Task OnStrategyStoppedAsync(StrategyStoppedEvent evt)
{
_logger.LogInformation("Strategy stopped: {StrategyId} - {StrategyName}", evt.StrategyId, evt.StrategyName);
_state.State.TotalActiveStrategies--;
_state.State.TotalActiveStrategies = newActiveCount;
_state.State.HasPendingChanges = true;
await _state.WriteStateAsync();
}

View File

@@ -448,6 +448,8 @@ public class ManagingDbContext : DbContext
entity.Property(e => e.Roi).HasPrecision(18, 8);
entity.Property(e => e.Volume).HasPrecision(18, 8);
entity.Property(e => e.Fees).HasPrecision(18, 8);
entity.Property(e => e.LongPositionCount).IsRequired();
entity.Property(e => e.ShortPositionCount).IsRequired();
// Create indexes
entity.HasIndex(e => e.Identifier).IsUnique();

View File

@@ -73,6 +73,8 @@ public class PostgreSqlBotRepository : IBotRepository
existingEntity.Volume = bot.Volume;
existingEntity.Fees = bot.Fees;
existingEntity.UpdatedAt = DateTime.UtcNow;
existingEntity.LongPositionCount = bot.LongPositionCount;
existingEntity.ShortPositionCount = bot.ShortPositionCount;
await _context.SaveChangesAsync().ConfigureAwait(false);
}
@@ -158,13 +160,13 @@ public class PostgreSqlBotRepository : IBotRepository
}
public async Task<(IEnumerable<Bot> Bots, int TotalCount)> GetBotsPaginatedAsync(
int pageNumber,
int pageSize,
BotStatus? status = null,
string? name = null,
string? ticker = null,
string? agentName = null,
string sortBy = "CreateDate",
int pageNumber,
int pageSize,
BotStatus? status = null,
string? name = null,
string? ticker = null,
string? agentName = null,
string sortBy = "CreateDate",
string sortDirection = "Desc")
{
// Build the query with filters
@@ -200,29 +202,29 @@ public class PostgreSqlBotRepository : IBotRepository
// Apply sorting
query = sortBy.ToLower() switch
{
"name" => sortDirection.ToLower() == "asc"
? query.OrderBy(b => b.Name)
"name" => sortDirection.ToLower() == "asc"
? query.OrderBy(b => b.Name)
: query.OrderByDescending(b => b.Name),
"ticker" => sortDirection.ToLower() == "asc"
? query.OrderBy(b => b.Ticker)
"ticker" => sortDirection.ToLower() == "asc"
? query.OrderBy(b => b.Ticker)
: query.OrderByDescending(b => b.Ticker),
"status" => sortDirection.ToLower() == "asc"
? query.OrderBy(b => b.Status)
"status" => sortDirection.ToLower() == "asc"
? query.OrderBy(b => b.Status)
: query.OrderByDescending(b => b.Status),
"startuptime" => sortDirection.ToLower() == "asc"
? query.OrderBy(b => b.StartupTime)
"startuptime" => sortDirection.ToLower() == "asc"
? query.OrderBy(b => b.StartupTime)
: query.OrderByDescending(b => b.StartupTime),
"pnl" => sortDirection.ToLower() == "asc"
? query.OrderBy(b => b.Pnl)
"pnl" => sortDirection.ToLower() == "asc"
? query.OrderBy(b => b.Pnl)
: query.OrderByDescending(b => b.Pnl),
"winrate" => sortDirection.ToLower() == "asc"
? query.OrderBy(b => b.TradeWins / (b.TradeWins + b.TradeLosses))
"winrate" => sortDirection.ToLower() == "asc"
? query.OrderBy(b => b.TradeWins / (b.TradeWins + b.TradeLosses))
: query.OrderByDescending(b => b.TradeWins / (b.TradeWins + b.TradeLosses)),
"agentname" => sortDirection.ToLower() == "asc"
? query.OrderBy(b => b.User.AgentName)
"agentname" => sortDirection.ToLower() == "asc"
? query.OrderBy(b => b.User.AgentName)
: query.OrderByDescending(b => b.User.AgentName),
_ => sortDirection.ToLower() == "asc"
? query.OrderBy(b => b.CreateDate)
_ => sortDirection.ToLower() == "asc"
? query.OrderBy(b => b.CreateDate)
: query.OrderByDescending(b => b.CreateDate) // Default to CreateDate
};

View File

@@ -72,7 +72,7 @@ const BotList: React.FC<IBotList> = ({ list }) => {
const [showManualPositionModal, setShowManualPositionModal] = useState(false)
const [selectedBotForManualPosition, setSelectedBotForManualPosition] = useState<string | null>(null)
const [showTradesModal, setShowTradesModal] = useState(false)
const [selectedBotForTrades, setSelectedBotForTrades] = useState<{ identifier: string; agentName: string } | null>(null)
const [selectedBotForTrades, setSelectedBotForTrades] = useState<{ name: string; agentName: string } | null>(null)
const [showBotConfigModal, setShowBotConfigModal] = useState(false)
const [selectedBotForUpdate, setSelectedBotForUpdate] = useState<{
identifier: string
@@ -144,10 +144,10 @@ const BotList: React.FC<IBotList> = ({ list }) => {
)
}
function getTradesBadge(botIdentifier: string, agentName: string) {
function getTradesBadge(name: string, agentName: string) {
const classes = baseBadgeClass() + ' bg-secondary'
return (
<button className={classes} onClick={() => openTradesModal(botIdentifier, agentName)}>
<button className={classes} onClick={() => openTradesModal(name, agentName)}>
<p className="text-primary-content flex">
<ChartBarIcon width={15}></ChartBarIcon>
</p>
@@ -160,8 +160,8 @@ const BotList: React.FC<IBotList> = ({ list }) => {
setShowManualPositionModal(true)
}
function openTradesModal(botIdentifier: string, agentName: string) {
setSelectedBotForTrades({ identifier: botIdentifier, agentName })
function openTradesModal(name: string, agentName: string) {
setSelectedBotForTrades({ name: name, agentName })
setShowTradesModal(true)
}
@@ -311,7 +311,7 @@ const BotList: React.FC<IBotList> = ({ list }) => {
<div className={baseBadgeClass(true)}>
PNL {bot.profitAndLoss.toFixed(2).toString()} $
</div>
{getTradesBadge(bot.identifier, bot.agentName)}
{getTradesBadge(bot.name, bot.agentName)}
</div>
</div>
</div>
@@ -335,7 +335,7 @@ const BotList: React.FC<IBotList> = ({ list }) => {
/>
<TradesModal
showModal={showTradesModal}
strategyName={selectedBotForTrades?.identifier ?? null}
strategyName={selectedBotForTrades?.name ?? null}
agentName={selectedBotForTrades?.agentName ?? null}
onClose={() => {
setShowTradesModal(false)

View File

@@ -1,373 +1,385 @@
import React, {useEffect, useState} from 'react'
import React from 'react'
import {useQuery} from '@tanstack/react-query'
import useApiUrlStore from '../../app/store/apiStore'
import {
DataClient,
type PlatformSummaryViewModel,
type TopStrategiesByRoiViewModel,
type TopStrategiesViewModel
} from '../../generated/ManagingApi'
import {fetchPlatformData} from '../../services/platformService'
function PlatformSummary({ index }: { index: number }) {
const { apiUrl } = useApiUrlStore()
const [platformData, setPlatformData] = useState<PlatformSummaryViewModel | null>(null)
function PlatformSummary({index}: { index: number }) {
const {apiUrl} = useApiUrlStore()
const [topStrategies, setTopStrategies] = useState<TopStrategiesViewModel | null>(null)
const [topStrategiesByRoi, setTopStrategiesByRoi] = useState<TopStrategiesByRoiViewModel | null>(null)
const [isLoading, setIsLoading] = useState(true)
const [error, setError] = useState<string | null>(null)
const {
data,
isLoading,
error,
isFetching
} = useQuery({
queryKey: ['platformData', apiUrl],
queryFn: () => fetchPlatformData(apiUrl),
refetchInterval: 30000, // Refetch every 30 seconds
staleTime: 25000, // Consider data stale after 25 seconds
placeholderData: (previousData) => previousData, // Keep previous data while fetching
enabled: !!apiUrl
})
const fetchPlatformData = async () => {
setIsLoading(true)
setError(null)
try {
const client = new DataClient({}, apiUrl)
// Fetch all platform data in parallel
const [platform, top, topRoi] = await Promise.all([
client.data_GetPlatformSummary(),
client.data_GetTopStrategies(),
client.data_GetTopStrategiesByRoi()
])
setPlatformData(platform)
setTopStrategies(top)
setTopStrategiesByRoi(topRoi)
} catch (err) {
setError('Failed to fetch platform data')
console.error('Error fetching platform data:', err)
} finally {
setIsLoading(false)
const platformData = data?.platform
const topStrategies = data?.topStrategies
const topStrategiesByRoi = data?.topStrategiesByRoi
const formatCurrency = (value: number) => {
if (value >= 1000000) {
return `$${(value / 1000000).toFixed(1)}M`
} else if (value >= 1000) {
return `$${(value / 1000).toFixed(1)}K`
}
return `$${value.toFixed(0)}`
}
}
useEffect(() => {
fetchPlatformData()
// Set up refresh interval (every 30 seconds)
const interval = setInterval(fetchPlatformData, 30000)
return () => clearInterval(interval)
}, [apiUrl])
const formatCurrency = (value: number) => {
if (value >= 1000000) {
return `$${(value / 1000000).toFixed(1)}M`
} else if (value >= 1000) {
return `$${(value / 1000).toFixed(1)}K`
const formatNumber = (value: number) => {
if (value >= 1000) {
return value.toLocaleString()
}
return value.toString()
}
return `$${value.toFixed(0)}`
}
const formatNumber = (value: number) => {
if (value >= 1000) {
return value.toLocaleString()
}
return value.toString()
}
const formatChangeIndicator = (change: number) => {
if (change > 0) {
return (
<span className="text-green-500">
const formatChangeIndicator = (change: number) => {
if (change > 0) {
return (
<span className="text-green-500">
+{change} Today
</span>
)
} else if (change < 0) {
return (
<span className="text-red-500">
)
} else if (change < 0) {
return (
<span className="text-red-500">
{change} Today
</span>
)
}
return (
<span className="text-gray-500">
)
}
return (
<span className="text-gray-500">
No change
</span>
)
}
)
}
const formatPercentageChange = (current: number, change: number) => {
if (current === 0) return '0%'
const percentage = (change / (current - change)) * 100
return `${percentage >= 0 ? '+' : ''}${percentage.toFixed(1)}%`
}
const formatPercentageChange = (current: number, change: number) => {
if (current === 0) return '0%'
const percentage = (change / (current - change)) * 100
return `${percentage >= 0 ? '+' : ''}${percentage.toFixed(1)}%`
}
// Show loading spinner only on initial load
if (isLoading && !data) {
return (
<div className="flex justify-center items-center min-h-96">
<div className="loading loading-spinner loading-lg"></div>
</div>
)
}
if (error && !data) {
return (
<div className="alert alert-error">
<span>Failed to fetch platform data</span>
<details className="text-sm mt-2">
{error instanceof Error ? error.message : 'Unknown error occurred'}
</details>
</div>
)
}
if (isLoading) {
return (
<div className="flex justify-center items-center min-h-96">
<div className="loading loading-spinner loading-lg"></div>
</div>
)
}
<div className="p-6 bg-base-100 min-h-screen">
{/* Subtle refetching indicator */}
{isFetching && data && (
<div className="fixed top-4 right-4 z-50">
<div className="bg-blue-500 text-white px-3 py-1 rounded-full text-sm flex items-center gap-2">
<div className="loading loading-spinner loading-xs"></div>
Updating...
</div>
</div>
)}
if (error) {
return (
<div className="alert alert-error">
<span>{error}</span>
</div>
)
}
{/* Header */}
<div className="mb-8">
<h1 className="text-4xl font-bold text-white mb-2">
{formatNumber(platformData?.totalActiveStrategies || 0)} Strategies Deployed
</h1>
<div className="text-lg">
{platformData && formatChangeIndicator(platformData.strategiesChange24h || 0)}
</div>
</div>
return (
<div className="p-6 bg-base-100 min-h-screen">
{/* Header */}
<div className="mb-8">
<h1 className="text-4xl font-bold text-white mb-2">
{formatNumber(platformData?.totalActiveStrategies || 0)} Strategies Deployed
</h1>
<div className="text-lg">
{platformData && formatChangeIndicator(platformData.strategiesChange24h || 0)}
</div>
</div>
{/* Main Stats Grid */}
<div className="grid grid-cols-1 lg:grid-cols-4 gap-6 mb-8">
{/* Total Volume Traded */}
<div className="bg-base-200 rounded-lg p-6">
<h3 className="text-lg font-semibold text-gray-400 mb-2">Total Volume Traded</h3>
<div className="text-3xl font-bold text-white mb-1">
{formatCurrency(platformData?.totalPlatformVolume || 0)}
</div>
<div className={`text-sm ${(platformData?.volumeChange24h || 0) >= 0 ? 'text-green-500' : 'text-red-500'}`}>
{(platformData?.volumeChange24h || 0) >= 0 ? '+' : ''}{formatCurrency(platformData?.volumeChange24h || 0)} Today
<span className="ml-2 text-gray-400">
{/* Main Stats Grid */}
<div className="grid grid-cols-1 lg:grid-cols-4 gap-6 mb-8">
{/* Total Volume Traded */}
<div className="bg-base-200 rounded-lg p-6">
<h3 className="text-lg font-semibold text-gray-400 mb-2">Total Volume Traded</h3>
<div className="text-3xl font-bold text-white mb-1">
{formatCurrency(platformData?.totalPlatformVolume || 0)}
</div>
<div
className={`text-sm ${(platformData?.volumeChange24h || 0) >= 0 ? 'text-green-500' : 'text-red-500'}`}>
{(platformData?.volumeChange24h || 0) >= 0 ? '+' : ''}{formatCurrency(platformData?.volumeChange24h || 0)} Today
<span className="ml-2 text-gray-400">
({formatPercentageChange(platformData?.totalPlatformVolume || 0, platformData?.volumeChange24h || 0)})
</span>
</div>
{/* Simple chart placeholder - you can replace with actual chart */}
<div className="mt-4 h-16 flex items-end">
<div className="w-full h-8 bg-green-500 rounded opacity-20"></div>
</div>
</div>
</div>
{/* Simple chart placeholder - you can replace with actual chart */}
<div className="mt-4 h-16 flex items-end">
<div className="w-full h-8 bg-green-500 rounded opacity-20"></div>
</div>
</div>
{/* Top 3 Most Profitable */}
<div className="bg-base-200 rounded-lg p-6">
<div className="flex items-center gap-2 mb-4">
<span className="text-2xl">🤑</span>
<h3 className="text-lg font-semibold text-gray-400">Top 3 Most Profitable</h3>
</div>
<div className="space-y-3">
{topStrategies?.topStrategies?.slice(0, 3).map((strategy, index) => (
<div key={index} className="flex items-center justify-between">
<div className="flex items-center gap-3">
<div className="w-8 h-8 bg-blue-500 rounded-full flex items-center justify-center">
{/* Top 3 Most Profitable */}
<div className="bg-base-200 rounded-lg p-6">
<div className="flex items-center gap-2 mb-4">
<span className="text-2xl">🤑</span>
<h3 className="text-lg font-semibold text-gray-400">Top 3 Most Profitable</h3>
</div>
<div className="space-y-3">
{topStrategies?.topStrategies?.slice(0, 3).map((strategy, index) => (
<div key={index} className="flex items-center justify-between">
<div className="flex items-center gap-3">
<div className="w-8 h-8 bg-blue-500 rounded-full flex items-center justify-center">
<span className="text-xs font-bold text-white">
{strategy.strategyName?.charAt(0) || 'S'}
</span>
</div>
<div>
<div className="text-sm text-white font-medium">
{strategy.strategyName || '[Strategy Name]'}
</div>
<div>
<div className="text-sm text-white font-medium">
{strategy.strategyName || '[Strategy Name]'}
</div>
<div className="text-xs text-gray-400">📧</div>
</div>
</div>
<div
className={`text-sm font-bold ${strategy.pnL && strategy.pnL >= 0 ? 'text-green-500' : 'text-red-500'}`}>
{strategy.pnL && strategy.pnL >= 0 ? '+' : ''}{formatCurrency(strategy.pnL || 0)}
</div>
</div>
)) || (
<div className="text-gray-500 text-sm">No profitable strategies found</div>
)}
</div>
<div className="text-xs text-gray-400">📧</div>
</div>
</div>
<div className={`text-sm font-bold ${strategy.pnL && strategy.pnL >= 0 ? 'text-green-500' : 'text-red-500'}`}>
{strategy.pnL && strategy.pnL >= 0 ? '+' : ''}{formatCurrency(strategy.pnL || 0)}
</div>
</div>
)) || (
<div className="text-gray-500 text-sm">No profitable strategies found</div>
)}
</div>
</div>
{/* Top 3 Rising (by ROI) */}
<div className="bg-base-200 rounded-lg p-6">
<div className="flex items-center gap-2 mb-4">
<span className="text-2xl">📈</span>
<h3 className="text-lg font-semibold text-gray-400">Top 3 by ROI</h3>
</div>
<div className="space-y-3">
{topStrategiesByRoi?.topStrategiesByRoi?.slice(0, 3).map((strategy, index) => (
<div key={index} className="flex items-center justify-between">
<div className="flex items-center gap-3">
<div className="w-8 h-8 bg-green-500 rounded-full flex items-center justify-center">
{/* Top 3 Rising (by ROI) */}
<div className="bg-base-200 rounded-lg p-6">
<div className="flex items-center gap-2 mb-4">
<span className="text-2xl">📈</span>
<h3 className="text-lg font-semibold text-gray-400">Top 3 by ROI</h3>
</div>
<div className="space-y-3">
{topStrategiesByRoi?.topStrategiesByRoi?.slice(0, 3).map((strategy, index) => (
<div key={index} className="flex items-center justify-between">
<div className="flex items-center gap-3">
<div className="w-8 h-8 bg-green-500 rounded-full flex items-center justify-center">
<span className="text-xs font-bold text-white">
{strategy.strategyName?.charAt(0) || 'S'}
</span>
</div>
<div>
<div className="text-sm text-white font-medium">
{strategy.strategyName || '[Strategy Name]'}
</div>
<div>
<div className="text-sm text-white font-medium">
{strategy.strategyName || '[Strategy Name]'}
</div>
<div className="text-xs text-gray-400">
Vol: {formatCurrency(strategy.volume || 0)}
</div>
</div>
</div>
<div className="text-right">
<div
className={`text-sm font-bold ${(strategy.roi || 0) >= 0 ? 'text-green-500' : 'text-red-500'}`}>
{(strategy.roi || 0) >= 0 ? '+' : ''}{strategy.roi?.toFixed(2) || 0}%
</div>
<div
className={`text-xs ${(strategy.pnL || 0) >= 0 ? 'text-green-400' : 'text-red-400'}`}>
{(strategy.pnL || 0) >= 0 ? '+' : ''}{formatCurrency(strategy.pnL || 0)}
</div>
</div>
</div>
)) || (
<div className="text-gray-500 text-sm">No ROI data available</div>
)}
</div>
<div className="text-xs text-gray-400">
Vol: {formatCurrency(strategy.volume || 0)}
</div>
</div>
{/* Platform Summary Stats */}
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-8">
<div className="bg-base-200 rounded-lg p-6">
<h3 className="text-lg font-semibold text-gray-400 mb-2">Total Agents</h3>
<div className="text-3xl font-bold text-white mb-1">
{formatNumber(platformData?.totalAgents || 0)}
</div>
<div
className={`text-sm ${(platformData?.agentsChange24h || 0) >= 0 ? 'text-green-500' : 'text-red-500'}`}>
{formatChangeIndicator(platformData?.agentsChange24h || 0)}
</div>
</div>
</div>
<div className="text-right">
<div className={`text-sm font-bold ${(strategy.roi || 0) >= 0 ? 'text-green-500' : 'text-red-500'}`}>
{(strategy.roi || 0) >= 0 ? '+' : ''}{strategy.roi?.toFixed(2) || 0}%
</div>
<div className={`text-xs ${(strategy.pnL || 0) >= 0 ? 'text-green-400' : 'text-red-400'}`}>
{(strategy.pnL || 0) >= 0 ? '+' : ''}{formatCurrency(strategy.pnL || 0)}
</div>
<div className="bg-base-200 rounded-lg p-6">
<h3 className="text-lg font-semibold text-gray-400 mb-2">Active Strategies</h3>
<div className="text-3xl font-bold text-white mb-1">
{formatNumber(platformData?.totalActiveStrategies || 0)}
</div>
<div
className={`text-sm ${(platformData?.strategiesChange24h || 0) >= 0 ? 'text-green-500' : 'text-red-500'}`}>
{formatChangeIndicator(platformData?.strategiesChange24h || 0)}
</div>
</div>
</div>
)) || (
<div className="text-gray-500 text-sm">No ROI data available</div>
)}
</div>
</div>
</div>
{/* Platform Summary Stats */}
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-8">
<div className="bg-base-200 rounded-lg p-6">
<h3 className="text-lg font-semibold text-gray-400 mb-2">Total Agents</h3>
<div className="text-3xl font-bold text-white mb-1">
{formatNumber(platformData?.totalAgents || 0)}
</div>
<div className={`text-sm ${(platformData?.agentsChange24h || 0) >= 0 ? 'text-green-500' : 'text-red-500'}`}>
{formatChangeIndicator(platformData?.agentsChange24h || 0)}
</div>
</div>
<div className="bg-base-200 rounded-lg p-6">
<h3 className="text-lg font-semibold text-gray-400 mb-2">Total Platform PnL</h3>
<div
className={`text-3xl font-bold ${(platformData?.totalPlatformPnL || 0) >= 0 ? 'text-green-500' : 'text-red-500'}`}>
{(platformData?.totalPlatformPnL || 0) >= 0 ? '+' : ''}{formatCurrency(platformData?.totalPlatformPnL || 0)}
</div>
<div
className={`text-sm ${(platformData?.pnLChange24h || 0) >= 0 ? 'text-green-500' : 'text-red-500'}`}>
{(platformData?.pnLChange24h || 0) >= 0 ? '+' : ''}{formatCurrency(platformData?.pnLChange24h || 0)} Today
</div>
</div>
<div className="bg-base-200 rounded-lg p-6">
<h3 className="text-lg font-semibold text-gray-400 mb-2">Active Strategies</h3>
<div className="text-3xl font-bold text-white mb-1">
{formatNumber(platformData?.totalActiveStrategies || 0)}
</div>
<div className={`text-sm ${(platformData?.strategiesChange24h || 0) >= 0 ? 'text-green-500' : 'text-red-500'}`}>
{formatChangeIndicator(platformData?.strategiesChange24h || 0)}
</div>
</div>
<div className="bg-base-200 rounded-lg p-6">
<h3 className="text-lg font-semibold text-gray-400 mb-2">Open Interest</h3>
<div className="text-3xl font-bold text-white mb-1">
{formatCurrency(platformData?.totalOpenInterest || 0)}
</div>
<div
className={`text-sm ${(platformData?.openInterestChange24h || 0) >= 0 ? 'text-green-500' : 'text-red-500'}`}>
{(platformData?.openInterestChange24h || 0) >= 0 ? '+' : ''}{formatCurrency(platformData?.openInterestChange24h || 0)} Today
</div>
</div>
</div>
<div className="bg-base-200 rounded-lg p-6">
<h3 className="text-lg font-semibold text-gray-400 mb-2">Total Platform PnL</h3>
<div className={`text-3xl font-bold ${(platformData?.totalPlatformPnL || 0) >= 0 ? 'text-green-500' : 'text-red-500'}`}>
{(platformData?.totalPlatformPnL || 0) >= 0 ? '+' : ''}{formatCurrency(platformData?.totalPlatformPnL || 0)}
</div>
<div className={`text-sm ${(platformData?.pnLChange24h || 0) >= 0 ? 'text-green-500' : 'text-red-500'}`}>
{(platformData?.pnLChange24h || 0) >= 0 ? '+' : ''}{formatCurrency(platformData?.pnLChange24h || 0)} Today
</div>
</div>
{/* Position Metrics */}
<div className="grid grid-cols-1 md:grid-cols-3 gap-6 mb-8">
<div className="bg-base-200 rounded-lg p-6">
<h3 className="text-lg font-semibold text-gray-400 mb-2">Total Positions</h3>
<div className="text-3xl font-bold text-white mb-1">
{formatNumber(platformData?.totalPositionCount || 0)}
</div>
<div
className={`text-sm ${(platformData?.positionCountChange24h || 0) >= 0 ? 'text-green-500' : 'text-red-500'}`}>
{formatChangeIndicator(platformData?.positionCountChange24h || 0)}
</div>
</div>
<div className="bg-base-200 rounded-lg p-6">
<h3 className="text-lg font-semibold text-gray-400 mb-2">Open Interest</h3>
<div className="text-3xl font-bold text-white mb-1">
{formatCurrency(platformData?.totalOpenInterest || 0)}
</div>
<div className={`text-sm ${(platformData?.openInterestChange24h || 0) >= 0 ? 'text-green-500' : 'text-red-500'}`}>
{(platformData?.openInterestChange24h || 0) >= 0 ? '+' : ''}{formatCurrency(platformData?.openInterestChange24h || 0)} Today
</div>
</div>
</div>
<div className="bg-base-200 rounded-lg p-6">
<h3 className="text-lg font-semibold text-gray-400 mb-2">Long Positions</h3>
<div className="text-3xl font-bold text-green-400 mb-1">
{formatNumber(platformData?.positionCountByDirection?.Long || 0)}
</div>
<div className="text-sm text-gray-400">
{platformData?.totalPositionCount ?
((platformData.positionCountByDirection?.Long || 0) / platformData.totalPositionCount * 100).toFixed(1) : 0}%
of total
</div>
</div>
{/* Position Metrics */}
<div className="grid grid-cols-1 md:grid-cols-3 gap-6 mb-8">
<div className="bg-base-200 rounded-lg p-6">
<h3 className="text-lg font-semibold text-gray-400 mb-2">Total Positions</h3>
<div className="text-3xl font-bold text-white mb-1">
{formatNumber(platformData?.totalPositionCount || 0)}
</div>
<div className={`text-sm ${(platformData?.positionCountChange24h || 0) >= 0 ? 'text-green-500' : 'text-red-500'}`}>
{formatChangeIndicator(platformData?.positionCountChange24h || 0)}
</div>
</div>
<div className="bg-base-200 rounded-lg p-6">
<h3 className="text-lg font-semibold text-gray-400 mb-2">Short Positions</h3>
<div className="text-3xl font-bold text-red-400 mb-1">
{formatNumber(platformData?.positionCountByDirection?.Short || 0)}
</div>
<div className="text-sm text-gray-400">
{platformData?.totalPositionCount ?
((platformData.positionCountByDirection?.Short || 0) / platformData.totalPositionCount * 100).toFixed(1) : 0}%
of total
</div>
</div>
</div>
<div className="bg-base-200 rounded-lg p-6">
<h3 className="text-lg font-semibold text-gray-400 mb-2">Long Positions</h3>
<div className="text-3xl font-bold text-green-400 mb-1">
{formatNumber(platformData?.positionCountByDirection?.Long || 0)}
</div>
<div className="text-sm text-gray-400">
{platformData?.totalPositionCount ?
((platformData.positionCountByDirection?.Long || 0) / platformData.totalPositionCount * 100).toFixed(1) : 0}% of total
</div>
</div>
<div className="bg-base-200 rounded-lg p-6">
<h3 className="text-lg font-semibold text-gray-400 mb-2">Short Positions</h3>
<div className="text-3xl font-bold text-red-400 mb-1">
{formatNumber(platformData?.positionCountByDirection?.Short || 0)}
</div>
<div className="text-sm text-gray-400">
{platformData?.totalPositionCount ?
((platformData.positionCountByDirection?.Short || 0) / platformData.totalPositionCount * 100).toFixed(1) : 0}% of total
</div>
</div>
</div>
{/* Volume and Positions by Asset */}
<div className="grid grid-cols-1 lg:grid-cols-2 gap-6 mb-8">
{/* Volume by Asset */}
<div className="bg-base-200 rounded-lg p-6">
<h3 className="text-lg font-semibold text-gray-400 mb-4">Volume by Asset</h3>
<div className="space-y-3 max-h-80 overflow-y-auto">
{platformData?.volumeByAsset && Object.keys(platformData.volumeByAsset).length > 0 ? (
Object.entries(platformData.volumeByAsset)
.sort(([,a], [,b]) => b - a)
.slice(0, 10)
.map(([asset, volume]) => (
<div key={asset} className="flex items-center justify-between">
<div className="flex items-center gap-2">
<div className="w-8 h-8 bg-blue-500 rounded-full flex items-center justify-center">
{/* Volume and Positions by Asset */}
<div className="grid grid-cols-1 lg:grid-cols-2 gap-6 mb-8">
{/* Volume by Asset */}
<div className="bg-base-200 rounded-lg p-6">
<h3 className="text-lg font-semibold text-gray-400 mb-4">Volume by Asset</h3>
<div className="space-y-3 max-h-80 overflow-y-auto">
{platformData?.volumeByAsset && Object.keys(platformData.volumeByAsset).length > 0 ? (
Object.entries(platformData.volumeByAsset)
.sort(([, a], [, b]) => b - a)
.slice(0, 10)
.map(([asset, volume]) => (
<div key={asset} className="flex items-center justify-between">
<div className="flex items-center gap-2">
<div
className="w-8 h-8 bg-blue-500 rounded-full flex items-center justify-center">
<span className="text-xs font-bold text-white">
{asset.substring(0, 2)}
</span>
</div>
<span className="text-white font-medium">{asset}</span>
</div>
<span className="text-white font-medium">{asset}</span>
</div>
<div className="text-right">
<div className="text-white font-semibold">
{formatCurrency(volume)}
</div>
</div>
</div>
))
) : (
<div className="text-gray-500 text-sm">No volume data available</div>
)}
</div>
<div className="text-right">
<div className="text-white font-semibold">
{formatCurrency(volume)}
</div>
</div>
</div>
))
) : (
<div className="text-gray-500 text-sm">No volume data available</div>
)}
</div>
</div>
</div>
{/* Positions by Asset */}
<div className="bg-base-200 rounded-lg p-6">
<h3 className="text-lg font-semibold text-gray-400 mb-4">Positions by Asset</h3>
<div className="space-y-3 max-h-80 overflow-y-auto">
{platformData?.positionCountByAsset && Object.keys(platformData.positionCountByAsset).length > 0 ? (
Object.entries(platformData.positionCountByAsset)
.sort(([,a], [,b]) => b - a)
.slice(0, 10)
.map(([asset, count]) => (
<div key={asset} className="flex items-center justify-between">
<div className="flex items-center gap-2">
<div className="w-8 h-8 bg-purple-500 rounded-full flex items-center justify-center">
{/* Positions by Asset */}
<div className="bg-base-200 rounded-lg p-6">
<h3 className="text-lg font-semibold text-gray-400 mb-4">Positions by Asset</h3>
<div className="space-y-3 max-h-80 overflow-y-auto">
{platformData?.positionCountByAsset && Object.keys(platformData.positionCountByAsset).length > 0 ? (
Object.entries(platformData.positionCountByAsset)
.sort(([, a], [, b]) => b - a)
.slice(0, 10)
.map(([asset, count]) => (
<div key={asset} className="flex items-center justify-between">
<div className="flex items-center gap-2">
<div
className="w-8 h-8 bg-purple-500 rounded-full flex items-center justify-center">
<span className="text-xs font-bold text-white">
{asset.substring(0, 2)}
</span>
</div>
<span className="text-white font-medium">{asset}</span>
</div>
<span className="text-white font-medium">{asset}</span>
</div>
<div className="text-right">
<div className="text-white font-semibold">
{formatNumber(count)} positions
</div>
<div className="text-xs text-gray-400">
{platformData?.totalPositionCount ?
(count / platformData.totalPositionCount * 100).toFixed(1) : 0}% of
total
</div>
</div>
</div>
))
) : (
<div className="text-gray-500 text-sm">No position data available</div>
)}
</div>
<div className="text-right">
<div className="text-white font-semibold">
{formatNumber(count)} positions
</div>
<div className="text-xs text-gray-400">
{platformData?.totalPositionCount ?
(count / platformData.totalPositionCount * 100).toFixed(1) : 0}% of total
</div>
</div>
</div>
))
) : (
<div className="text-gray-500 text-sm">No position data available</div>
)}
</div>
</div>
</div>
</div>
</div>
{/* Data Freshness Indicator */}
<div className="bg-base-200 rounded-lg p-4">
<div className="flex items-center justify-between text-sm text-gray-400">
<span>Last updated: {platformData?.lastUpdated ? new Date(platformData.lastUpdated).toLocaleString() : 'Unknown'}</span>
<span>24h snapshot: {platformData?.last24HourSnapshot ? new Date(platformData.last24HourSnapshot).toLocaleString() : 'Unknown'}</span>
{/* Data Freshness Indicator */}
<div className="bg-base-200 rounded-lg p-4">
<div className="flex items-center justify-between text-sm text-gray-400">
<div className="flex items-center gap-2">
<span>Last updated: {platformData?.lastUpdated ? new Date(platformData.lastUpdated).toLocaleString() : 'Unknown'}</span>
{isFetching && (
<div className="flex items-center gap-1 text-blue-400">
<div className="loading loading-spinner loading-xs"></div>
<span>Refreshing...</span>
</div>
)}
</div>
<span>24h snapshot: {platformData?.last24HourSnapshot ? new Date(platformData.last24HourSnapshot).toLocaleString() : 'Unknown'}</span>
</div>
</div>
</div>
</div>
</div>
)
)
}
export default PlatformSummary

View File

@@ -0,0 +1,29 @@
import {
DataClient,
type PlatformSummaryViewModel,
type TopStrategiesByRoiViewModel,
type TopStrategiesViewModel
} from '../generated/ManagingApi'
export interface PlatformData {
platform: PlatformSummaryViewModel
topStrategies: TopStrategiesViewModel
topStrategiesByRoi: TopStrategiesByRoiViewModel
}
export const fetchPlatformData = async (apiUrl: string): Promise<PlatformData> => {
const client = new DataClient({}, apiUrl)
// Fetch all platform data in parallel
const [platform, topStrategies, topStrategiesByRoi] = await Promise.all([
client.data_GetPlatformSummary(),
client.data_GetTopStrategies(),
client.data_GetTopStrategiesByRoi()
])
return {
platform,
topStrategies,
topStrategiesByRoi
}
}