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

loop-log-trigger.tsx 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { useTranslation } from 'react-i18next'
  2. import { RiArrowRightSLine } from '@remixicon/react'
  3. import Button from '@/app/components/base/button'
  4. import type {
  5. LoopDurationMap,
  6. LoopVariableMap,
  7. NodeTracing,
  8. } from '@/types/workflow'
  9. import { Loop } from '@/app/components/base/icons/src/vender/workflow'
  10. type LoopLogTriggerProps = {
  11. nodeInfo: NodeTracing
  12. allExecutions?: NodeTracing[]
  13. onShowLoopResultList: (loopResultList: NodeTracing[][], loopResultDurationMap: LoopDurationMap, loopVariableMap: LoopVariableMap) => void
  14. }
  15. const LoopLogTrigger = ({
  16. nodeInfo,
  17. allExecutions,
  18. onShowLoopResultList,
  19. }: LoopLogTriggerProps) => {
  20. const { t } = useTranslation()
  21. const filterNodesForInstance = (key: string): NodeTracing[] => {
  22. if (!allExecutions) return []
  23. const parallelNodes = allExecutions.filter(exec =>
  24. exec.execution_metadata?.parallel_mode_run_id === key,
  25. )
  26. if (parallelNodes.length > 0)
  27. return parallelNodes
  28. const serialIndex = Number.parseInt(key, 10)
  29. if (!isNaN(serialIndex)) {
  30. const serialNodes = allExecutions.filter(exec =>
  31. exec.execution_metadata?.loop_id === nodeInfo.node_id
  32. && exec.execution_metadata?.loop_index === serialIndex,
  33. )
  34. if (serialNodes.length > 0)
  35. return serialNodes
  36. }
  37. return []
  38. }
  39. const handleOnShowLoopDetail = (e: React.MouseEvent<HTMLButtonElement>) => {
  40. e.stopPropagation()
  41. e.nativeEvent.stopImmediatePropagation()
  42. const loopNodeMeta = nodeInfo.execution_metadata
  43. const loopDurMap = nodeInfo?.loopDurationMap || loopNodeMeta?.loop_duration_map || {}
  44. const loopVarMap = loopNodeMeta?.loop_variable_map || {}
  45. let structuredList: NodeTracing[][] = []
  46. if (nodeInfo.details?.length) {
  47. structuredList = nodeInfo.details
  48. }
  49. else if (loopNodeMeta?.loop_duration_map) {
  50. const instanceKeys = Object.keys(loopNodeMeta.loop_duration_map)
  51. structuredList = instanceKeys
  52. .map(key => filterNodesForInstance(key))
  53. .filter(branchNodes => branchNodes.length > 0)
  54. }
  55. onShowLoopResultList(
  56. structuredList,
  57. loopDurMap,
  58. loopVarMap,
  59. )
  60. }
  61. let displayLoopCount = 0
  62. const loopMap = nodeInfo.execution_metadata?.loop_duration_map
  63. if (loopMap)
  64. displayLoopCount = Object.keys(loopMap).length
  65. else if (nodeInfo.details?.length)
  66. displayLoopCount = nodeInfo.details.length
  67. else if (nodeInfo.metadata?.loop_length)
  68. displayLoopCount = nodeInfo.metadata.loop_length
  69. const getErrorCount = (details: NodeTracing[][] | undefined) => {
  70. if (!details || details.length === 0)
  71. return 0
  72. return details.reduce((acc, loop) => {
  73. if (loop.some(item => item.status === 'failed'))
  74. acc++
  75. return acc
  76. }, 0)
  77. }
  78. const errorCount = getErrorCount(nodeInfo.details)
  79. return (
  80. <Button
  81. 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'
  82. onClick={handleOnShowLoopDetail}
  83. >
  84. <Loop className='h-4 w-4 shrink-0 text-components-button-tertiary-text' />
  85. <div className='system-sm-medium flex-1 text-left text-components-button-tertiary-text'>{t('workflow.nodes.loop.loop', { count: displayLoopCount })}{errorCount > 0 && (
  86. <>
  87. {t('workflow.nodes.loop.comma')}
  88. {t('workflow.nodes.loop.error', { count: errorCount })}
  89. </>
  90. )}</div>
  91. <RiArrowRightSLine className='h-4 w-4 shrink-0 text-components-button-tertiary-text' />
  92. </Button>
  93. )
  94. }
  95. export default LoopLogTrigger