Statistic.tsx 6.8 KB

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