@@ -22,6 +22,7 @@ import { Cache } from "../../../utils/cache/cache";
22
22
import { CmabClient } from "./cmab_client" ;
23
23
import { v4 as uuidV4 } from 'uuid' ;
24
24
import murmurhash from "murmurhash" ;
25
+ import { a } from "vitest/dist/chunks/suite.CcK46U-P" ;
25
26
26
27
export type CmabDecision = {
27
28
variationId : string ,
@@ -32,14 +33,14 @@ export interface CmabService {
32
33
/**
33
34
* Get variation id for the user
34
35
* @param {OptimizelyUserContext } userContext
35
- * @param {string } experimentId
36
+ * @param {string } ruleId
36
37
* @param {OptimizelyDecideOption[] } options
37
38
* @return {Promise<CmabDecision> }
38
39
*/
39
40
getDecision (
40
41
projectConfig : ProjectConfig ,
41
42
userContext : OptimizelyUserContext ,
42
- experimentId : string ,
43
+ ruleId : string ,
43
44
options : OptimizelyDecideOption [ ]
44
45
) : Promise < CmabDecision >
45
46
}
@@ -76,7 +77,7 @@ export class DefaultCmabService implements CmabService {
76
77
const filteredAttributes = this . filterAttributes ( projectConfig , userContext , ruleId ) ;
77
78
78
79
if ( options . includes ( OptimizelyDecideOption . IGNORE_CMAB_CACHE ) ) {
79
- return this . fetchVariation ( ruleId , userContext . getUserId ( ) , filteredAttributes ) ;
80
+ return this . fetchDecision ( ruleId , userContext . getUserId ( ) , filteredAttributes ) ;
80
81
}
81
82
82
83
if ( options . includes ( OptimizelyDecideOption . RESET_CMAB_CACHE ) ) {
@@ -90,7 +91,9 @@ export class DefaultCmabService implements CmabService {
90
91
}
91
92
92
93
const cachedValue = await this . cmabCache . get ( cacheKey ) ;
93
- const attributesHash = String ( murmurhash . v3 ( JSON . stringify ( filteredAttributes ) ) ) ;
94
+
95
+ const attributesJson = JSON . stringify ( filteredAttributes , Object . keys ( filteredAttributes ) . sort ( ) ) ;
96
+ const attributesHash = String ( murmurhash . v3 ( attributesJson ) ) ;
94
97
95
98
if ( cachedValue ) {
96
99
if ( cachedValue . attributesHash === attributesHash ) {
@@ -100,7 +103,7 @@ export class DefaultCmabService implements CmabService {
100
103
}
101
104
}
102
105
103
- const variation = await this . fetchVariation ( ruleId , userContext . getUserId ( ) , filteredAttributes ) ;
106
+ const variation = await this . fetchDecision ( ruleId , userContext . getUserId ( ) , filteredAttributes ) ;
104
107
this . cmabCache . set ( cacheKey , {
105
108
attributesHash,
106
109
variationId : variation . variationId ,
@@ -110,7 +113,7 @@ export class DefaultCmabService implements CmabService {
110
113
return variation ;
111
114
}
112
115
113
- private async fetchVariation (
116
+ private async fetchDecision (
114
117
ruleId : string ,
115
118
userId : string ,
116
119
attributes : UserAttributes ,
@@ -126,19 +129,20 @@ export class DefaultCmabService implements CmabService {
126
129
ruleId : string
127
130
) : UserAttributes {
128
131
const filteredAttributes : UserAttributes = { } ;
129
- const attributes = userContext . getAttributes ( ) ;
132
+ const userAttributes = userContext . getAttributes ( ) ;
130
133
131
134
const experiment = projectConfig . experimentIdMap [ ruleId ] ;
132
135
if ( ! experiment || ! experiment . cmab ) {
133
136
return filteredAttributes ;
134
137
}
135
138
136
139
const cmabAttributeIds = experiment . cmab . attributeIds ;
137
-
138
- Object . keys ( attributes ) . forEach ( ( key ) => {
139
- const attributeId = projectConfig . attributeKeyMap [ key ] . id ;
140
- if ( cmabAttributeIds . includes ( attributeId ) ) {
141
- filteredAttributes [ key ] = attributes [ key ] ;
140
+
141
+ cmabAttributeIds . forEach ( ( aid ) => {
142
+ const attribute = projectConfig . attributeIdMap [ aid ] ;
143
+
144
+ if ( userAttributes . hasOwnProperty ( attribute . key ) ) {
145
+ filteredAttributes [ attribute . key ] = userAttributes [ attribute . key ] ;
142
146
}
143
147
} ) ;
144
148
0 commit comments