useDashboardStats.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. Copyright (C) 2025 QuantumNous
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. For commercial licensing, please contact support@quantumnous.com
  14. */
  15. import { useMemo } from 'react';
  16. import { Wallet, Activity, Zap, Gauge } from 'lucide-react';
  17. import {
  18. IconMoneyExchangeStroked,
  19. IconHistogram,
  20. IconCoinMoneyStroked,
  21. IconTextStroked,
  22. IconPulse,
  23. IconStopwatchStroked,
  24. IconTypograph,
  25. IconSend
  26. } from '@douyinfe/semi-icons';
  27. import { renderQuota } from '../../helpers';
  28. import { createSectionTitle } from '../../helpers/dashboard';
  29. export const useDashboardStats = (
  30. userState,
  31. consumeQuota,
  32. consumeTokens,
  33. times,
  34. trendData,
  35. performanceMetrics,
  36. navigate,
  37. t
  38. ) => {
  39. const groupedStatsData = useMemo(() => [
  40. {
  41. title: createSectionTitle(Wallet, t('账户数据')),
  42. color: 'bg-blue-50',
  43. items: [
  44. {
  45. title: t('当前余额'),
  46. value: renderQuota(userState?.user?.quota),
  47. icon: <IconMoneyExchangeStroked />,
  48. avatarColor: 'blue',
  49. onClick: () => navigate('/console/topup'),
  50. trendData: [],
  51. trendColor: '#3b82f6'
  52. },
  53. {
  54. title: t('历史消耗'),
  55. value: renderQuota(userState?.user?.used_quota),
  56. icon: <IconHistogram />,
  57. avatarColor: 'purple',
  58. trendData: [],
  59. trendColor: '#8b5cf6'
  60. }
  61. ]
  62. },
  63. {
  64. title: createSectionTitle(Activity, t('使用统计')),
  65. color: 'bg-green-50',
  66. items: [
  67. {
  68. title: t('请求次数'),
  69. value: userState.user?.request_count,
  70. icon: <IconSend />,
  71. avatarColor: 'green',
  72. trendData: [],
  73. trendColor: '#10b981'
  74. },
  75. {
  76. title: t('统计次数'),
  77. value: times,
  78. icon: <IconPulse />,
  79. avatarColor: 'cyan',
  80. trendData: trendData.times,
  81. trendColor: '#06b6d4'
  82. }
  83. ]
  84. },
  85. {
  86. title: createSectionTitle(Zap, t('资源消耗')),
  87. color: 'bg-yellow-50',
  88. items: [
  89. {
  90. title: t('统计额度'),
  91. value: renderQuota(consumeQuota),
  92. icon: <IconCoinMoneyStroked />,
  93. avatarColor: 'yellow',
  94. trendData: trendData.consumeQuota,
  95. trendColor: '#f59e0b'
  96. },
  97. {
  98. title: t('统计Tokens'),
  99. value: isNaN(consumeTokens) ? 0 : consumeTokens,
  100. icon: <IconTextStroked />,
  101. avatarColor: 'pink',
  102. trendData: trendData.tokens,
  103. trendColor: '#ec4899'
  104. }
  105. ]
  106. },
  107. {
  108. title: createSectionTitle(Gauge, t('性能指标')),
  109. color: 'bg-indigo-50',
  110. items: [
  111. {
  112. title: t('平均RPM'),
  113. value: performanceMetrics.avgRPM,
  114. icon: <IconStopwatchStroked />,
  115. avatarColor: 'indigo',
  116. trendData: trendData.rpm,
  117. trendColor: '#6366f1'
  118. },
  119. {
  120. title: t('平均TPM'),
  121. value: performanceMetrics.avgTPM,
  122. icon: <IconTypograph />,
  123. avatarColor: 'orange',
  124. trendData: trendData.tpm,
  125. trendColor: '#f97316'
  126. }
  127. ]
  128. }
  129. ], [
  130. userState?.user?.quota,
  131. userState?.user?.used_quota,
  132. userState?.user?.request_count,
  133. times,
  134. consumeQuota,
  135. consumeTokens,
  136. trendData,
  137. performanceMetrics,
  138. navigate,
  139. t
  140. ]);
  141. return {
  142. groupedStatsData
  143. };
  144. };