Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-protocol/src/experiments/configcat.ts
2500 views
1
/**
2
* Copyright (c) 2022 Gitpod GmbH. All rights reserved.
3
* Licensed under the GNU Affero General Public License (AGPL).
4
* See License.AGPL.txt in the project root for license information.
5
*/
6
7
import { Attributes, Client } from "./types";
8
import { User as ConfigCatUser } from "configcat-common/lib/RolloutEvaluator";
9
import { IConfigCatClient } from "configcat-common/lib/ConfigCatClient";
10
11
export const USER_ID_ATTRIBUTE = "user_id";
12
export const PROJECT_ID_ATTRIBUTE = "project_id";
13
export const TEAM_ID_ATTRIBUTE = "team_id";
14
export const TEAM_NAME_ATTRIBUTE = "team_name";
15
export const BILLING_TIER_ATTRIBUTE = "billing_tier";
16
export const GITPOD_HOST = "gitpod_host";
17
18
export class ConfigCatClient implements Client {
19
constructor(private readonly client: IConfigCatClient, private readonly gitpodHost?: string) {}
20
21
getValueAsync<T>(experimentName: string, defaultValue: T, attributes: Attributes): Promise<T> {
22
return this.client.getValueAsync(
23
experimentName,
24
defaultValue,
25
attributesToUser({
26
gitpodHost: this.gitpodHost,
27
...attributes,
28
}),
29
);
30
}
31
32
dispose(): void {
33
return this.client.dispose();
34
}
35
}
36
37
export function attributesToUser(attributes: Attributes): ConfigCatUser {
38
const userId = attributes.user?.id || "";
39
const email = attributes.user?.email || "";
40
41
const custom: { [key: string]: string } = {};
42
if (userId) {
43
custom[USER_ID_ATTRIBUTE] = userId;
44
}
45
if (attributes.projectId) {
46
custom[PROJECT_ID_ATTRIBUTE] = attributes.projectId;
47
}
48
if (attributes.teamId) {
49
custom[TEAM_ID_ATTRIBUTE] = attributes.teamId;
50
}
51
if (attributes.teamName) {
52
custom[TEAM_NAME_ATTRIBUTE] = attributes.teamName;
53
}
54
if (attributes.billingTier) {
55
custom[BILLING_TIER_ATTRIBUTE] = attributes.billingTier;
56
}
57
if (attributes.gitpodHost) {
58
custom[GITPOD_HOST] = attributes.gitpodHost;
59
}
60
61
return new ConfigCatUser(userId, email, "", custom);
62
}
63
64