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