1- import type { ClickHouse } from "@internal/clickhouse" ;
1+ import type { ClickHouse , TaskRunInsertArray , PayloadInsertArray } from "@internal/clickhouse" ;
2+ import { TASK_RUN_INDEX , PAYLOAD_INDEX } from "@internal/clickhouse" ;
23import { type RedisOptions } from "@internal/redis" ;
34import {
45 LogicalReplicationClient ,
@@ -80,7 +81,9 @@ type TaskRunInsert = {
8081
8182export type RunsReplicationServiceEvents = {
8283 message : [ { lsn : string ; message : PgoutputMessage ; service : RunsReplicationService } ] ;
83- batchFlushed : [ { flushId : string ; taskRunInserts : any [ ] [ ] ; payloadInserts : any [ ] [ ] } ] ;
84+ batchFlushed : [
85+ { flushId : string ; taskRunInserts : TaskRunInsertArray [ ] ; payloadInserts : PayloadInsertArray [ ] } ,
86+ ] ;
8487} ;
8588
8689export class RunsReplicationService {
@@ -576,35 +579,29 @@ export class RunsReplicationService {
576579 . filter ( Boolean )
577580 // batch inserts in clickhouse are more performant if the items
578581 // are pre-sorted by the primary key
579- // Array indices: [0]=environment_id, [1]=organization_id, [2]=project_id, [3]=run_id, [5]=created_at
580582 . sort ( ( a , b ) => {
581- if ( a [ 1 ] !== b [ 1 ] ) {
582- // organization_id
583- return a [ 1 ] < b [ 1 ] ? - 1 : 1 ;
583+ if ( a [ TASK_RUN_INDEX . organization_id ] !== b [ TASK_RUN_INDEX . organization_id ] ) {
584+ return a [ TASK_RUN_INDEX . organization_id ] < b [ TASK_RUN_INDEX . organization_id ] ? - 1 : 1 ;
584585 }
585- if ( a [ 2 ] !== b [ 2 ] ) {
586- // project_id
587- return a [ 2 ] < b [ 2 ] ? - 1 : 1 ;
586+ if ( a [ TASK_RUN_INDEX . project_id ] !== b [ TASK_RUN_INDEX . project_id ] ) {
587+ return a [ TASK_RUN_INDEX . project_id ] < b [ TASK_RUN_INDEX . project_id ] ? - 1 : 1 ;
588588 }
589- if ( a [ 0 ] !== b [ 0 ] ) {
590- // environment_id
591- return a [ 0 ] < b [ 0 ] ? - 1 : 1 ;
589+ if ( a [ TASK_RUN_INDEX . environment_id ] !== b [ TASK_RUN_INDEX . environment_id ] ) {
590+ return a [ TASK_RUN_INDEX . environment_id ] < b [ TASK_RUN_INDEX . environment_id ] ? - 1 : 1 ;
592591 }
593- if ( a [ 5 ] !== b [ 5 ] ) {
594- // created_at
595- return a [ 5 ] - b [ 5 ] ;
592+ if ( a [ TASK_RUN_INDEX . created_at ] !== b [ TASK_RUN_INDEX . created_at ] ) {
593+ return a [ TASK_RUN_INDEX . created_at ] - b [ TASK_RUN_INDEX . created_at ] ;
596594 }
597- return a [ 3 ] < b [ 3 ] ? - 1 : 1 ; // run_id
595+ return a [ TASK_RUN_INDEX . run_id ] < b [ TASK_RUN_INDEX . run_id ] ? - 1 : 1 ;
598596 } ) ;
599597
600598 const payloadInserts = preparedInserts
601599 . map ( ( { payloadInsert } ) => payloadInsert )
602600 . filter ( Boolean )
603601 // batch inserts in clickhouse are more performant if the items
604602 // are pre-sorted by the primary key
605- // Array indices: [0]=run_id
606603 . sort ( ( a , b ) => {
607- return a [ 0 ] < b [ 0 ] ? - 1 : 1 ; // run_id
604+ return a [ PAYLOAD_INDEX . run_id ] < b [ PAYLOAD_INDEX . run_id ] ? - 1 : 1 ;
608605 } ) ;
609606
610607 span . setAttribute ( "task_run_inserts" , taskRunInserts . length ) ;
@@ -769,7 +766,7 @@ export class RunsReplicationService {
769766 } ;
770767 }
771768
772- async #insertTaskRunInserts( taskRunInserts : any [ ] [ ] , attempt : number ) {
769+ async #insertTaskRunInserts( taskRunInserts : TaskRunInsertArray [ ] , attempt : number ) {
773770 return await startSpan ( this . _tracer , "insertTaskRunsInserts" , async ( span ) => {
774771 const [ insertError , insertResult ] =
775772 await this . options . clickhouse . taskRuns . insertCompactArrays ( taskRunInserts , {
@@ -792,7 +789,7 @@ export class RunsReplicationService {
792789 } ) ;
793790 }
794791
795- async #insertPayloadInserts( payloadInserts : any [ ] [ ] , attempt : number ) {
792+ async #insertPayloadInserts( payloadInserts : PayloadInsertArray [ ] , attempt : number ) {
796793 return await startSpan ( this . _tracer , "insertPayloadInserts" , async ( span ) => {
797794 const [ insertError , insertResult ] =
798795 await this . options . clickhouse . taskRuns . insertPayloadsCompactArrays ( payloadInserts , {
@@ -817,7 +814,7 @@ export class RunsReplicationService {
817814
818815 async #prepareRunInserts(
819816 batchedRun : TaskRunInsert
820- ) : Promise < { taskRunInsert ?: any [ ] ; payloadInsert ?: any [ ] } > {
817+ ) : Promise < { taskRunInsert ?: TaskRunInsertArray ; payloadInsert ?: PayloadInsertArray } > {
821818 this . logger . debug ( "Preparing run" , {
822819 batchedRun,
823820 } ) ;
@@ -854,7 +851,7 @@ export class RunsReplicationService {
854851 environmentType : string ,
855852 event : "insert" | "update" | "delete" ,
856853 _version : bigint
857- ) : Promise < any [ ] > {
854+ ) : Promise < TaskRunInsertArray > {
858855 const output = await this . #prepareJson( run . output , run . outputType ) ;
859856
860857 // Return array matching TASK_RUN_COLUMNS order
@@ -907,7 +904,7 @@ export class RunsReplicationService {
907904 ] ;
908905 }
909906
910- async #preparePayloadInsert( run : TaskRun , _version : bigint ) : Promise < any [ ] > {
907+ async #preparePayloadInsert( run : TaskRun , _version : bigint ) : Promise < PayloadInsertArray > {
911908 const payload = await this . #prepareJson( run . payload , run . payloadType ) ;
912909
913910 // Return array matching PAYLOAD_COLUMNS order
0 commit comments