您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

iteration-log-trigger.tsx 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { useTranslation } from 'react-i18next'
  2. import { RiArrowRightSLine } from '@remixicon/react'
  3. import Button from '@/app/components/base/button'
  4. import type {
  5. IterationDurationMap,
  6. NodeTracing,
  7. } from '@/types/workflow'
  8. import { Iteration } from '@/app/components/base/icons/src/vender/workflow'
  9. type IterationLogTriggerProps = {
  10. nodeInfo: NodeTracing
  11. allExecutions?: NodeTracing[]
  12. onShowIterationResultList: (iterationResultList: NodeTracing[][], iterationResultDurationMap: IterationDurationMap) => void
  13. }
  14. const IterationLogTrigger = ({
  15. nodeInfo,
  16. allExecutions,
  17. onShowIterationResultList,
  18. }: IterationLogTriggerProps) => {
  19. const { t } = useTranslation()
  20. const filterNodesForInstance = (key: string): NodeTracing[] => {
  21. if (!allExecutions) return []
  22. const parallelNodes = allExecutions.filter(exec =>
  23. exec.execution_metadata?.parallel_mode_run_id === key,
  24. )
  25. if (parallelNodes.length > 0)
  26. return parallelNodes
  27. const serialIndex = Number.parseInt(key, 10)
  28. if (!isNaN(serialIndex)) {
  29. const serialNodes = allExecutions.filter(exec =>
  30. exec.execution_metadata?.iteration_id === nodeInfo.node_id
  31. && exec.execution_metadata?.iteration_index === serialIndex,
  32. )
  33. if (serialNodes.length > 0)
  34. return serialNodes
  35. }
  36. return []
  37. }
  38. const handleOnShowIterationDetail = (e: React.MouseEvent<HTMLButtonElement>) => {
  39. e.stopPropagation()
  40. e.nativeEvent.stopImmediatePropagation()
  41. const iterationNodeMeta = nodeInfo.execution_metadata
  42. const iterDurationMap = nodeInfo?.iterDurationMap || iterationNodeMeta?.iteration_duration_map || {}
  43. let structuredList: NodeTracing[][] = []
  44. if (iterationNodeMeta?.iteration_duration_map) {
  45. const instanceKeys = Object.keys(iterationNodeMeta.iteration_duration_map)
  46. structuredList = instanceKeys
  47. .map(key => filterNodesForInstance(key))
  48. .filter(branchNodes => branchNodes.length > 0)
  49. }
  50. else if (nodeInfo.details?.length) {
  51. structuredList = nodeInfo.details
  52. }
  53. onShowIterationResultList(structuredList, iterDurationMap)
  54. }
  55. let displayIterationCount = 0
  56. const iterMap = nodeInfo.execution_metadata?.iteration_duration_map
  57. if (iterMap)
  58. displayIterationCount = Object.keys(iterMap).length
  59. else if (nodeInfo.details?.length)
  60. displayIterationCount = nodeInfo.details.length
  61. else if (nodeInfo.metadata?.iterator_length)
  62. displayIterationCount = nodeInfo.metadata.iterator_length
  63. const getErrorCount = (details: NodeTracing[][] | undefined) => {
  64. if (!details || details.length === 0)
  65. return 0
  66. return details.reduce((acc, iteration) => {
  67. if (iteration.some(item => item.status === 'failed'))
  68. acc++
  69. return acc
  70. }, 0)
  71. }
  72. const errorCount = getErrorCount(nodeInfo.details)
  73. return (
  74. <Button
  75. className='flex w-full cursor-pointer items-center gap-2 self-stretch rounded-lg border-none bg-components-button-tertiary-bg-hover px-3 py-2 hover:bg-components-button-tertiary-bg-hover'
  76. onClick={handleOnShowIterationDetail}
  77. >
  78. <Iteration className='h-4 w-4 shrink-0 text-components-button-tertiary-text' />
  79. <div className='system-sm-medium flex-1 text-left text-components-button-tertiary-text'>{t('workflow.nodes.iteration.iteration', { count: displayIterationCount })}{errorCount > 0 && (
  80. <>
  81. {t('workflow.nodes.iteration.comma')}
  82. {t('workflow.nodes.iteration.error', { count: errorCount })}
  83. </>
  84. )}</div>
  85. <RiArrowRightSLine className='h-4 w-4 shrink-0 text-components-button-tertiary-text' />
  86. </Button>
  87. )
  88. }
  89. export default IterationLogTrigger