@@ -7,7 +7,7 @@ import Knock from "../../knock";
77
88import {
99 DEFAULT_GROUP_KEY ,
10- SelectionResult ,
10+ // SelectionResult,
1111 byKey ,
1212 checkStateIfThrottled ,
1313 findDefaultGroup ,
@@ -46,7 +46,9 @@ import {
4646 SelectFilterParams ,
4747 SelectGuideOpts ,
4848 SelectGuidesOpts ,
49- SelectQueryParams ,
49+ SelectQueryLimit ,
50+ SelectionResult ,
51+ // SelectQueryParams,
5052 StepMessageState ,
5153 StoreState ,
5254 TargetParams ,
@@ -151,7 +153,16 @@ const safeJsonParseDebugParams = (value: string): DebugState => {
151153 }
152154} ;
153155
154- const select = ( state : StoreState , filters : SelectFilterParams = { } ) => {
156+ type SelectQueryMetadata = {
157+ limit : SelectQueryLimit ;
158+ opts : SelectGuideOpts ;
159+ } ;
160+
161+ const select = (
162+ state : StoreState ,
163+ filters : SelectFilterParams ,
164+ metadata : SelectQueryMetadata ,
165+ ) => {
155166 // A map of selected guides as values, with its order index as keys.
156167 const result = new SelectionResult ( ) ;
157168
@@ -176,7 +187,8 @@ const select = (state: StoreState, filters: SelectFilterParams = {}) => {
176187 result . set ( index , guide ) ;
177188 }
178189
179- result . metadata = { guideGroup : defaultGroup , filters } ;
190+ result . metadata = { guideGroup : defaultGroup , filters, ...metadata } ;
191+
180192 return result ;
181193} ;
182194
@@ -618,29 +630,35 @@ export class KnockGuideClient {
618630 `[Guide] .selectGuides (filters: ${ formatFilters ( filters ) } ; state: ${ formatState ( state ) } )` ,
619631 ) ;
620632
633+ // 1. First, call selectGuide() using the same filters to ensure we have a
634+ // group stage open and respect throttling. This isn't the real query, but
635+ // rather it's a shortcut ahead of handling the actual query result below.
621636 const selectedGuide = this . selectGuide ( state , filters , {
622637 ...opts ,
623- // Do not record this selectGuide() query, since this is a short to check
624- // throttling, and we should be recording the actual selectGuides query
625- // (as needed).
638+ // Don't record this result, not the actual query result we need.
626639 recordSelectQuery : false ,
627640 } ) ;
628641
629- // Record AFTER the selectGuide() query to ensure we have a group stage open
630- // to be able to record.
642+ // 2. Now make the actual select query with the provided filters and opts,
643+ // and record the result (as needed). By default, we only record the result
644+ // while in debugging.
631645 const { recordSelectQuery = ! ! state . debug ?. debugging } = opts ;
632- this . maybeRecordSelectQuery (
633- { ...filters , limit : "one" } ,
634- { ...opts , recordSelectQuery } ,
635- ) ;
646+ const metadata : SelectQueryMetadata = {
647+ limit : "all" ,
648+ opts : { ...opts , recordSelectQuery } ,
649+ } ;
650+ const result = select ( state , filters , metadata ) ;
651+ this . maybeRecordSelectResult ( result ) ;
636652
653+ // 3. Stop if there is not at least one guide to return.
637654 if ( ! selectedGuide ) {
638655 return [ ] ;
639656 }
640657
641658 // There should be at least one guide to return here now.
642- const guides = [ ...select ( state , filters ) . values ( ) ] ;
659+ const guides = [ ...result . values ( ) ] ;
643660
661+ // 4. If throttled, filter out any throttled guides.
644662 if ( ! opts . includeThrottled && checkStateIfThrottled ( state ) ) {
645663 const unthrottledGuides = guides . filter (
646664 ( g ) => g . bypass_global_group_limit ,
@@ -706,15 +724,15 @@ export class KnockGuideClient {
706724 this . stage = this . openGroupStage ( ) ; // Assign here to make tsc happy
707725 }
708726
709- // Must come AFTER we ensure a group stage exists, so we can record select
710- // queries.
727+ // Must come AFTER we ensure a group stage exists above , so we can record
728+ // select queries. By default, we only record the result while in debugging .
711729 const { recordSelectQuery = ! ! state . debug ?. debugging } = opts ;
712- this . maybeRecordSelectQuery (
713- { ... filters , limit : "one" } ,
714- { ...opts , recordSelectQuery } ,
715- ) ;
716-
717- const result = select ( state , filters ) ;
730+ const metadata : SelectQueryMetadata = {
731+ limit : "one" ,
732+ opts : { ...opts , recordSelectQuery } ,
733+ } ;
734+ const result = select ( state , filters , metadata ) ;
735+ this . maybeRecordSelectResult ( result ) ;
718736
719737 if ( result . size === 0 ) {
720738 this . knock . log ( "[Guide] Selection found zero result" ) ;
@@ -735,10 +753,12 @@ export class KnockGuideClient {
735753
736754 // Check if inside the throttle window (i.e. throttled) and if so stop and
737755 // return undefined unless explicitly given the option to include throttled.
738- if ( ! opts . includeThrottled && checkStateIfThrottled ( state ) ) {
739- this . knock . log ( `[Guide] Throttling the selected guide: ${ guide . key } ` ) ;
740- return undefined ;
741- }
756+ const throttled = ! opts . includeThrottled && checkStateIfThrottled ( state ) ;
757+
758+ // if (!opts.includeThrottled && checkStateIfThrottled(state)) {
759+ // this.knock.log(`[Guide] Throttling the selected guide: ${guide.key}`);
760+ // return undefined;
761+ // }
742762
743763 switch ( this . stage . status ) {
744764 case "open" : {
@@ -751,6 +771,11 @@ export class KnockGuideClient {
751771 this . knock . log ( `[Guide] Patching the group stage: ${ guide . key } ` ) ;
752772 this . stage . ordered [ index ] = guide . key ;
753773
774+ if ( throttled ) {
775+ this . knock . log ( `[Guide] Throttling the selected guide: ${ guide . key } ` ) ;
776+ return undefined ;
777+ }
778+
754779 const ret = this . stage . resolved === guide . key ? guide : undefined ;
755780 this . knock . log (
756781 `[Guide] Returning \`${ ret ?. key } \` (stage: ${ formatGroupStage ( this . stage ) } )` ,
@@ -759,6 +784,11 @@ export class KnockGuideClient {
759784 }
760785
761786 case "closed" : {
787+ if ( throttled ) {
788+ this . knock . log ( `[Guide] Throttling the selected guide: ${ guide . key } ` ) ;
789+ return undefined ;
790+ }
791+
762792 const ret = this . stage . resolved === guide . key ? guide : undefined ;
763793 this . knock . log (
764794 `[Guide] Returning \`${ ret ?. key } \` (stage: ${ formatGroupStage ( this . stage ) } )` ,
@@ -768,36 +798,66 @@ export class KnockGuideClient {
768798 }
769799 }
770800
771- private maybeRecordSelectQuery (
772- params : SelectQueryParams ,
773- opts : SelectGuideOpts ,
774- ) {
801+ private maybeRecordSelectResult ( result : SelectionResult ) {
802+ if ( ! result . metadata ) return ;
803+
804+ const { opts , filters , limit } = result . metadata ;
775805 if ( ! opts . recordSelectQuery ) return ;
806+ if ( ! filters . key && ! filters . type ) return ;
776807 if ( ! this . stage || this . stage . status === "closed" ) return ;
777- if ( ! params . key && ! params . type ) return ;
778-
779- // Deep merge into the query logs:
780- const queriesByKey = this . stage . queries . key || { } ;
781- if ( params . key ) {
782- queriesByKey [ params . key ] = {
783- ...( queriesByKey [ params . key ] || { } ) ,
784- ...{ [ params . limit ] : opts } ,
808+
809+ // Deep merge to accumulate the results.
810+ const queriedByKey = this . stage . results . key || { } ;
811+ if ( filters . key ) {
812+ queriedByKey [ filters . key ] = {
813+ ...( queriedByKey [ filters . key ] || { } ) ,
814+ ...{ [ limit ] : result } ,
785815 } ;
786816 }
787- const queriesByType = this . stage . queries . type || { } ;
788- if ( params . type ) {
789- queriesByType [ params . type ] = {
790- ...( queriesByType [ params . type ] || { } ) ,
791- ...{ [ params . limit ] : opts } ,
817+ const queriedByType = this . stage . results . type || { } ;
818+ if ( filters . type ) {
819+ queriedByType [ filters . type ] = {
820+ ...( queriedByType [ filters . type ] || { } ) ,
821+ ...{ [ limit ] : result } ,
792822 } ;
793823 }
794824
795825 this . stage = {
796826 ...this . stage ,
797- queries : { key : queriesByKey , type : queriesByType } ,
827+ results : { key : queriedByKey , type : queriedByType } ,
798828 } ;
799829 }
800830
831+ // private maybeRecordSelectQuery(
832+ // params: SelectQueryParams,
833+ // opts: SelectGuideOpts,
834+ // ) {
835+ // if (!opts.recordSelectQuery) return;
836+ // if (!this.stage || this.stage.status === "closed") return;
837+ // if (!params.key && !params.type) return;
838+ //
839+ // // Deep merge into the query logs:
840+ // const queriesByKey = this.stage.queries.key || {};
841+ // if (params.key) {
842+ // queriesByKey[params.key] = {
843+ // ...(queriesByKey[params.key] || {}),
844+ // ...{ [params.limit]: opts },
845+ // };
846+ // }
847+ // const queriesByType = this.stage.queries.type || {};
848+ // if (params.type) {
849+ // queriesByType[params.type] = {
850+ // ...(queriesByType[params.type] || {}),
851+ // ...{ [params.limit]: opts },
852+ // };
853+ // }
854+ //
855+ // this.stage = {
856+ // ...this.stage,
857+ // queries: { key: queriesByKey, type: queriesByType },
858+ // };
859+ // }
860+
801861 getStage ( ) {
802862 return this . stage ;
803863 }
@@ -817,7 +877,7 @@ export class KnockGuideClient {
817877 this . stage = {
818878 status : "open" ,
819879 ordered : [ ] ,
820- queries : { } ,
880+ results : { } ,
821881 timeoutId,
822882 } ;
823883
0 commit comments