Statistic.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. 'use client'
  2. import cn from '@/utils/classnames'
  3. import ReactECharts from 'echarts-for-react'
  4. import * as echarts from 'echarts'
  5. import useSWR from 'swr'
  6. import { fetchStatistic } from '@/service/common'
  7. import { useAppContext } from '@/context/app-context'
  8. const cardClass = 'bg-[#FCFCFC] border rounded-[10px] border-[#E6E7EB] p-6 flex'
  9. const Statistic = () => {
  10. const { currentWorkspace, isCurrentWorkspaceOwner } = useAppContext()
  11. const { data: data1 }: any = useSWR(
  12. {
  13. url: '/datasets/count',
  14. params: {
  15. tenant_id: currentWorkspace.id,
  16. },
  17. },
  18. fetchStatistic,
  19. )
  20. const datasetsCount = data1?.data?.datasets_count || 0
  21. const deptsCount = data1?.data?.depts_count || 0
  22. const tagsCount = data1?.data?.tags_count || 0
  23. const { data: data2 }: any = useSWR(
  24. {
  25. url: '/datasets/type-stats',
  26. params: {
  27. tenant_id: currentWorkspace.id,
  28. },
  29. },
  30. fetchStatistic,
  31. )
  32. const typeData = data2?.data || []
  33. const typeOptions = {
  34. grid: {
  35. top: 0,
  36. bottom: 0,
  37. left: 0,
  38. right: 0,
  39. },
  40. tooltip: {
  41. trigger: 'item',
  42. },
  43. legend: {
  44. orient: 'horizontal',
  45. top: 0,
  46. itemWidth: 8,
  47. itemHeight: 8,
  48. },
  49. series: [
  50. {
  51. type: 'pie',
  52. radius: ['26%', '50%'],
  53. data: typeData.map((v: any) => {
  54. v.value = v.value || 100
  55. v.name = v.type
  56. return v
  57. }),
  58. label: {
  59. formatter: '{b} {d}%',
  60. },
  61. emphasis: {
  62. disabled: true,
  63. },
  64. },
  65. ],
  66. }
  67. const { data: data3 }: any = useSWR(
  68. {
  69. url: '/datasets/update-stats',
  70. params: {
  71. tenant_id: currentWorkspace.id,
  72. },
  73. },
  74. fetchStatistic,
  75. )
  76. const updateData = data3?.data || []
  77. const updateOptions = {
  78. grid: {
  79. top: 40,
  80. bottom: 20,
  81. left: 40,
  82. right: 0,
  83. },
  84. tooltip: {
  85. trigger: 'axis',
  86. },
  87. xAxis: {
  88. type: 'category',
  89. data: updateData.map((v: any) => v.period),
  90. axisLine: { show: false },
  91. axisTick: { show: false },
  92. splitLine: { show: false },
  93. axisLabel: {
  94. fontSize: 10,
  95. interval: 0,
  96. color: 'rgba(87, 98, 117, 1)',
  97. },
  98. },
  99. yAxis: {
  100. type: 'value',
  101. axisLine: { show: false },
  102. axisTick: { show: false },
  103. splitNumber: 3,
  104. splitLine: {
  105. lineStyle: {
  106. type: 'dashed',
  107. color: 'rgba(239, 241, 248, 1)',
  108. },
  109. },
  110. axisLabel: {
  111. fontSize: 10,
  112. },
  113. },
  114. series: [
  115. {
  116. type: 'bar',
  117. data: updateData.map((v: any) => v.count),
  118. itemStyle: {
  119. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  120. { offset: 0, color: '#2078FC' },
  121. { offset: 1, color: '#5DC9F9' },
  122. ]),
  123. },
  124. },
  125. ],
  126. }
  127. const { data: data4 }: any = useSWR(
  128. {
  129. url: '/tags',
  130. params: {
  131. type: 'knowledge',
  132. label_map: true,
  133. },
  134. },
  135. fetchStatistic,
  136. )
  137. const tagData = data4 || []
  138. const tagColors = ['#7185fb', '#4bc97d', '#2079fe', '#dba53c', '#4ec5f9']
  139. const tagOptions = {
  140. tooltip: {
  141. trigger: 'item',
  142. },
  143. series: [
  144. {
  145. type: 'graph',
  146. layout: 'force',
  147. data: tagData.map((v: any) => ({ name: v.name, value: Number(v.binding_count) })).sort((a: any, b: any) => b.value - a.value).map((v: any, i: number) => {
  148. if (i < Math.floor(tagData.length / 4)) {
  149. v.symbolSize = 100
  150. v.label = {
  151. fontSize: 30,
  152. }
  153. }
  154. else if (i < Math.floor(tagData.length / 2)) {
  155. v.symbolSize = 90
  156. v.label = {
  157. fontSize: 24,
  158. }
  159. }
  160. else if (i < Math.floor(tagData.length * 3 / 4)) {
  161. v.symbolSize = 80
  162. v.label = {
  163. fontSize: 20,
  164. }
  165. }
  166. else {
  167. v.symbolSize = 70
  168. v.label = {
  169. fontSize: 16,
  170. }
  171. }
  172. v.itemStyle = {
  173. color: tagColors[i % tagColors.length],
  174. }
  175. return v
  176. }),
  177. links: [],
  178. roam: true,
  179. force: {
  180. repulsion: 110,
  181. edgeLength: [10, 50],
  182. },
  183. symbolSize: 60,
  184. label: {
  185. show: true,
  186. color: '#ffffff',
  187. },
  188. emphasis: {
  189. disabled: true,
  190. },
  191. },
  192. ],
  193. }
  194. return (
  195. <div className="w-full px-12">
  196. <div className={cn('w-full gap-6', cardClass)}>
  197. <div className="flex h-[120px] grow items-center bg-[url('/imgs/statistic-bg.png')] bg-[length:100%_100%] bg-no-repeat pl-6">
  198. <img src="/imgs/statistic-icon-1.png" className="mr-6 h-16 w-16"/>
  199. <div>
  200. <div className="text-[36px] text-[#111111]">{datasetsCount}</div>
  201. <div className="text-[#111111]">知识库总量</div>
  202. </div>
  203. </div>
  204. <div className="flex h-[120px] grow items-center bg-[url('/imgs/statistic-bg.png')] bg-[length:100%_100%] bg-no-repeat pl-6">
  205. <img src="/imgs/statistic-icon-2.png" className="mr-6 h-16 w-16"/>
  206. <div>
  207. <div className="text-[36px] text-[#111111]">{deptsCount}</div>
  208. <div className="text-[#111111]">部门总量</div>
  209. </div>
  210. </div>
  211. <div className="flex h-[120px] grow items-center bg-[url('/imgs/statistic-bg.png')] bg-[length:100%_100%] bg-no-repeat pl-6">
  212. <img src="/imgs/statistic-icon-3.png" className="mr-6 h-16 w-16"/>
  213. <div>
  214. <div className="text-[36px] text-[#111111]">{tagsCount}</div>
  215. <div className="text-[#111111]">标签总量</div>
  216. </div>
  217. </div>
  218. </div>
  219. <div className='mt-6 flex w-full gap-8'>
  220. <div className={cn(cardClass, 'h-[360px] flex-1 flex-col overflow-hidden')}>
  221. <div className="text-2xl font-bold text-[#333333]">知识库类别统计</div>
  222. <div className="relative mt-3 flex flex-1 items-center justify-center">
  223. <img src="/imgs/statistic-icon-4.png" className="absolute"/>
  224. <ReactECharts option={typeOptions} className="flex-1" />
  225. </div>
  226. </div>
  227. <div className={cn(cardClass, 'h-[360px] flex-1 flex-col overflow-hidden')}>
  228. <div className="text-2xl font-bold text-[#333333]">知识库更新情况统计</div>
  229. <ReactECharts option={updateOptions} className="flex-1" />
  230. </div>
  231. <div className={cn(cardClass, 'h-[360px] flex-1 flex-col overflow-hidden bg-[url("/imgs/statistic-icon-5.png")] bg-[length:100%_100%] bg-no-repeat')}>
  232. <div className="text-2xl font-bold text-[#333333]">标签云图</div>
  233. <ReactECharts option={tagOptions} className="flex-1" />
  234. </div>
  235. </div>
  236. </div>
  237. )
  238. }
  239. export default Statistic