import { uuid } from '@labelbee/lb-annotation' ; import { jsonParser } from '.' ; import { EToolName } from '@/data/enums/ToolType' ; import _ from 'lodash' ; import StepUtils from './StepUtils' ; import { IStepInfo } from '@/types/step' ; export default class AnnotationDataUtils { param copyResult param step param currentResult returns public static copyResultChange ( copyResult , step , currentResult ) { try { const copyData = jsonParser ( copyResult ) ; const currentData = jsonParser ( currentResult ) ; const stepName = `step_${ step }` ; if ( copyData [ stepName ] ) { const info = copyData [ stepName ] ; if ( info . result ) { info . result = info . result . map ( ( info ) => ( { ... info , id : uuid ( 8 , 62 ) , } ) ) ; currentData [ stepName ] = info ; return JSON . stringify ( currentData ) ; } } return copyResult ; } catch { return copyResult ; } } param result param basicResultList public static isResultSourceMatchedDependence ( result , basicResultList ) { const sourceIDForCurStep = result ?. map ( ( i ) => i . sourceID ) . sort ( ) ; const sourceIDForDependStep = basicResultList ?. map ( ( i ) => i . id ) . sort ( ) ; return _ . isEqual ( sourceIDForCurStep , sourceIDForDependStep ) ; } param result param basicResultList returns public static deltaUpdateBasicResultList ( result , basicResultList ) { const sourceIDForCurStep = result ?. map ( ( i ) => i . sourceID ) . sort ( ) ; return basicResultList . filter ( ( v ) => ! sourceIDForCurStep . includes ( v . id ) ) ; } param stepResult param toolInstance param stepConfig param basicResultList param isInitData returns public static getInitialResultList ( stepResult , toolInstance , stepConfig , basicResultList , isInitData ) { const resultList = stepResult ?? [ ] ; switch ( stepConfig . tool ) { case EToolName . Tag : case EToolName . Text : { /**
                 * 在依赖的情况下，检查的是否需要增量更新前面新增的结果
                 */ if ( stepConfig . dataSourceStep > 0 ) { const deltaResultList = this . deltaUpdateBasicResultList ( resultList , basicResultList ) ; if ( deltaResultList . length > 0 ) { return resultList . concat ( toolInstance . getInitResultList ( stepConfig . dataSourceStep , deltaResultList ) ) ; } } if ( isInitData !== true ) { return resultList ; } return toolInstance . getInitResultList ( stepConfig . dataSourceStep , basicResultList ) ; } default : { return resultList ; } } } param newResStr param oldResStr param step param stepList public static dataCorrection ( newResStr , oldResStr , step , stepList ) { try { const curStep = StepUtils . getStepInfo ( step , stepList ) ; const stepKey = `step_${ curStep . step }` ; const newRes = jsonParser ( newResStr ) ; const oldRes = jsonParser ( oldResStr ) ; const newResForCurStep = newRes [ stepKey ] ?. result ; const oldResForCurStep = oldRes [ stepKey ] ?. result ; /** 没有旧数据时不处理 */ if ( ! oldResForCurStep ) { return newResStr ; } if ( _ . isEqual ( newResForCurStep . sort ( this . idCmp ) , oldResForCurStep . sort ( this . idCmp ) ) ) { return newResStr ; } const deletedIds = this . findDeletedIds ( newResForCurStep , oldResForCurStep ) ; if ( deletedIds . length === 0 ) { return newResStr ; } const dataSourceStep = step ; const stepKeys = this . getStepKeys ( newRes ) . sort ( ) ; this . deleteRes ( newRes , dataSourceStep , deletedIds , stepKeys ) ; return JSON . stringify ( newRes ) ; } catch ( error ) { console . error ( error ) ; return newResStr ; } } param a param b returns public static idCmp ( a , b ) { const idA = a . id ; const idB = b . id ; if ( idA < idB ) { return - 1 ; } if ( idA > idB ) { return 1 ; } return 0 ; } param newResForCurStep param oldResForCurStep returns { Array < string > } public static findDeletedIds ( newResForCurStep , oldResForCurStep ) { return this . findDeletedItems ( oldResForCurStep , newResForCurStep ) . map ( ( i ) => i . id ) ; } param oldResForCurStep param newResForCurStep returns { Array < any > } public static findDeletedItems ( oldResForCurStep , newResForCurStep ) { const deletedItems = [ ] ; oldResForCurStep . forEach ( ( i ) => { const isNewResExisted = newResForCurStep . some ( ( r ) => r . id === i . id ) ; if ( ! isNewResExisted ) { deletedItems . push ( i ) ; } } ) ; return deletedItems ; } param res returns { Array < number > } public static getStepKeys ( res ) { return Object . keys ( res ) . map ( ( i ) => parseInt ( i . replace ( 'step_' , '' ) , 10 ) ) . filter ( ( i ) => ! isNaN ( i ) ) ; } param resData param dataSourceStep param deletedIds param stepKeys public static deleteRes ( resData , dataSourceStep , deletedIds , stepKeys ) { stepKeys . forEach ( ( s ) => { if ( s > dataSourceStep ) { const stepRes = resData [ `step_${ s }` ] ; if ( [ EToolName . FolderTag , EToolName . Segmentation ] . includes ( stepRes . tool ) ) { return ; } if ( stepRes . dataSourceStep === dataSourceStep ) { const newDeletedIds = [ ] ; stepRes . result = stepRes . result . filter ( ( i ) => { const exist = deletedIds . includes ( i . sourceID ) ; if ( exist ) { newDeletedIds . push ( i . id ) ; return false ; } return true ; } ) ; this . deleteRes ( resData , s , newDeletedIds , stepKeys ) ; } else { stepRes . result = stepRes . result . filter ( ( i ) => { const exist = deletedIds . includes ( i . sourceID ) ; if ( exist ) { return false ; } return true ; } ) ; } } } ) ; } }