云眼配置
About 3 min
云眼配置
本主题介绍如何使用 EyeofcloudConfig 访问数据文件中的项目配置数据。
概述
云眼灰度发布(特性标帜)AB实验 SDK 打开一组定义良好的公共 API,隐藏所有实现详细信息。但是,某些客户端可能需要访问数据文件中的项目配置数据。
在本文档中,我们扩展了公共 API 以定义数据模型和访问方法,客户端可以使用这些模型和方法访问项目配置数据。
EyeofcloudConfig API
公共配置数据模型(EyeofcloudConfig)在下面定义为静态Eyeofcloud项目数据的结构化格式。
获取云眼配置
EyeofcloudConfig可以通过以下公共API调用从EyeofcloudClient(顶级)访问:
TypeScript
const eocConfig = eyeofcloudClient.getEyeofcloudConfig();
getEyeofcloudConfig
返回一个EyeofcloudConfig
实例,其中包括
- 环境键
- 开发工具包密钥
- 数据文件修订号
- 按其键值映射的所有实验
- 所有属性
- 所有受众
- 所有活动
- 按其键值映射的灰度发布(特性标帜)
📘 注意
当 SDK 数据文件更新时(客户端可以为
EYEOFCLOUD_CONFIG_UPDATE
添加通知侦听器以获取通知),客户端应调用该方法以获取更新的 EyeofcloudConfig 数据。请参阅以下示例。
获取数据文件
若要确保多个 SDK 实例都从同一配置实例化(例如,在客户端/服务器方案中),可以在实例之间传递配置(数据文件)的 JSON 字符串表示形式。若要获取数据文件,请使用对象的方法。有关更多信息,请参阅与多个 SDK 实现共享数据文件。EyeofcloudConfig``getDatafile
对象模型
下面显示了 EyeofcloudConfig 的对象模型。
TypeScript - 对象模型
export interface EyeofcloudConfig {
environmentKey: string;
sdkKey: string;
revision: string;
/**
* This experimentsMap is for experiments of legacy projects only.
* For flag projects, experiment keys are not guaranteed to be unique
* across multiple flags, so this map may not include all experiments
* when keys conflict.
*/
experimentsMap: EyeofcloudExperimentsMap;
featuresMap: EyeofcloudFeaturesMap;
attributes: EyeofcloudAttribute[];
audiences: EyeofcloudAudience[];
events: EyeofcloudEvent[];
getDatafile(): string;
}
export type EyeofcloudExperimentsMap = {
[experimentKey: string]: EyeofcloudExperiment;
}
export interface EyeofcloudExperiment {
id: string;
key: string;
audiences: string;
variationsMap: {
[variationKey: string]: EyeofcloudVariation;
};
}
export interface EyeofcloudVariation {
id: string;
key: string;
featureEnabled?: boolean;
variablesMap: EyeofcloudVariablesMap;
}
export type EyeofcloudVariablesMap = {
[variableKey: string]: EyeofcloudVariable;
}
export interface EyeofcloudVariable {
id: string;
key: string;
type: string;
value: string;
}
export type EyeofcloudFeaturesMap = {
[featureKey: string]: EyeofcloudFeature;
}
export interface EyeofcloudFeature {
id: string;
key: string;
experimentRules: EyeofcloudExperiment[];
deliveryRules: EyeofcloudExperiment[];
variablesMap: EyeofcloudVariablesMap;
/**
* @deprecated Use experimentRules and deliveryRules instead
*/
experimentsMap: EyeofcloudExperimentsMap;
}
export type EyeofcloudAttribute = {
id: string;
key: string;
};
export type EyeofcloudAudience = {
id: string;
name: string;
conditions: string;
};
export type EyeofcloudEvent = {
id: string;
key: string;
experimentsIds: string[];
};
例子
可以从 EyeofcloudClient(顶级)访问 EyeofcloudConfig,如下所示:
React
import { enums } from '@eyeofcloud/react-sdk';
const config = eyeofcloudClient.getEyeofcloudConfig();
console.log(`[EyeofcloudConfig] revision = ${config.revision}`);
console.log(`[EyeofcloudConfig] sdkKey = ${config.sdkKey}`);
console.log(`[EyeofcloudConfig] environmentKey = ${config.environmentKey}`);
console.log(`[EyeofcloudConfig] attributes:`);
config.attributes.forEach((attribute) => {
console.log(`[EyeofcloudAttribute] -- (id, key) = (${attribute.id}, ${attribute.key})`);
});
console.log(`[EyeofcloudConfig] audiences:`);
config.audiences.forEach((audience) => {
console.log(
`[EyeofcloudAudience] -- (id, name, conditions) = = (${audience.id}, ${audience.name}, ${audience.conditions})`
);
});
console.log(`[EyeofcloudConfig] events:`);
config.events.forEach((event) => {
console.log(
`[EyeofcloudEvent] -- (id, key, experimentIds) = (${event.id}, ${event.key}, ${event.experimentsIds})`
);
});
// all flags
const flags = [];
const flagKeys = [];
for (var key in config.featuresMap) {
flags.push(config.featuresMap[key]);
flagKeys.push(key);
}
flagKeys.forEach((flagKey) => {
const flag = config.featuresMap[flagKey];
const experimentRules = flag.experimentRules;
const deliveryRules = flag.deliveryRules;
// use experiment rules and delivery rules and other flag data here...
experimentRules.forEach((experiment) => {
console.log(`[EyeofcloudExperiment] - experiment rule-key = ${experiment.key}`);
console.log(`[EyeofcloudExperiment] - experiment audiences = ${experiment.audiences}`);
const variationsMap = experiment.variationsMap;
const variationKeys = [];
for (var key in variationsMap) {
variationKeys.push(key);
}
variationKeys.forEach((varKey) => {
const variation = variationsMap[varKey];
console.log(
`[EyeofcloudVariation] -- variation = { key: ${varKey}, id: ${variation.id}, featureEnabled: ${variation.featureEnabled} })`
);
const variablesMap = variationsMap[varKey].variablesMap;
const variableKeys = [];
for (var key in variablesMap) {
variableKeys.push(key);
}
variableKeys.forEach((variableKey) => {
const variable = variablesMap[variableKey];
console.log(`[EyeofcloudVariable] --- variable: ${variableKey}, ${variable}`);
});
});
});
deliveryRules.forEach((delivery) => {
console.log(`[EyeofcloudExperiment] - delivery rule-key = ${delivery.key}`);
console.log(`[EyeofcloudExperiment] - delivery audiences = ${delivery.audiences}`);
// use delivery rule data here...
});
});
// listen to EYEOFCLOUD_CONFIG_UPDATE to get updated data
eyeofcloudClient.notificationCenter.addNotificationListener(
enums.NOTIFICATION_TYPES.EYEOFCLOUD_CONFIG_UPDATE,
function () {
const newConfig = eyeofcloudClient.getEyeofcloudConfig();
console.log(`[EyeofcloudConfig] revision = ${newConfig.revision}`);
}
);