@datatester/node-sdk
TypeScript icon, indicating that this package has built-in type declarations

1.1.4 • Public • Published

Datatester分流Node.js SDK

使用及说明

import AbClient from '@datatester/node-sdk'
// const AbClient = require('@datatester/node-sdk').default

(async () => {
    // 在rangers应用管理中查询token信息
    const abClient = new AbClient('token')

    // 因为返回的是promise,业务使用上需要自己处理reject行为
    try {
        const conf = await abClient.activate('key', 'decisionId', 'trackId', new Map(), '')
        // 获取key版本配置
        const config = conf.val
    } catch(e) {
        console.error(e)
    }
})()

初始化参数

名词 类型 说明 备注
token string app级别的token, 可以在rangers控制台应用列表中查询
metaHost string 获取meta数据服务host, 默认为AbClient.metaHost.cn
trackHost string 上报实验曝光数据, 默认为AbClient.trackHost.cn
interval number 元数据更新间隔时间,单位秒,默认60s
metaType string 元数据类型, 可选值:'cdn', 'server', 'file' 对文件读取一次,不会按照interval去更新数据
metaFilePath string 元数据文件本地路径,使用前确保应用对文件有可读权限 metaType==='file'时候才生效
enableFile boolean 开启日志文件保存
dir string 日志写入的文件路径, 默认为当前应用启动的根路径, 确保应用对路径有写路径 enableFile=true情况下日志写入路径,
maxSize string 文件最大值 https://github.com/iccicci/rotating-file-stream#size
level string 需要记录的日志等级 可选值:'console','info', 'warn', 'error'
formatter function 对日志做自定义格式化输出 参数请参照types/plugins/logger/TLog
trackMaxNum number 一次上报的事件量个数,可选值1-50,默认每10秒会自动上报一次,一次上报50条

接口及参数

接口类型参照types/type.d.ts

参数说明

  • 入参
参数 类型 说明
interval number 元数据获取时间间隔
experimentId string 实验id
decisionId string 决策ID, 分流口径, 和系统中设置的白名单数据行为一致
featureId string feature flag id
trackId string 上报数据唯一标识
attributes Map<string, any> 分流的受众数据,和系统中设置的目标受众做比对
  • 返回值(TConfig)
参数 类型 说明
val string 元数据获取时间间隔
vid string 实验id
entity_id string 决策ID, 分流口径, 和系统中设置的白名单数据行为一致

接口说明

接口 返回值 说明
getExperimentVariantName(experimentId: string, decisionId: string, attributes: Map<string, any>, options?: TOptions) Promise<string> 命中分流结果名称
getExperimentVariantNameWithInpression(experimentId: string, decisionId: string, trackId: string, attributes: Map<string, any>, options?: TOptions) Promise<string> 命中分流结果名称并曝光实验信息
getExperimentConfigs(experimentId: string, decisionId: string, attributes: Map<string, any>, options?: TOptions) Promise<TConfig> 命中分流结果配置信息
getExperimentConfigsWithInpression(experimentId: string, decisionId: string, trackId: string, attributes: Map<string, any>, options?: TOptions) Promise<TConfig> 命中分流结果配置信息并曝光信息
getAllExperimentConfigs(decisionId: string, attributes: Map<string, any>, options?: TOptions) Promise<TConfig> 获取app下所有实验命中分流结果配置
verifyFeatureEnabled(featureId: string, decisionId: string, attributes: Map<string, any>, options?: TOptions) Promise<boolean> 检验feature flag是否命中
getFeatureConfigs(featureId: string, decisionId: string, attributes: Map<string, any>, options?: TOptions) Promise<TConfig> feature flag 分流结果配置
getFeatureConfigsWithInpression(featureId: string, decisionId: string, trackId: string, attributes: Map<string, any>, options?: TOptions) Promise<TConfig> feature flag 分流结果配置并曝光信息
getAllFeatureConfigs(decisionId: string, attributes: Map<string, any>, options?: TOptions) Promise<TConfig> 命中的所有feature flag版本信息
getEnabledFeatureIds(decisionId: string, attributes: Map<string, any>, options?: TOptions) Promise<string[]> 获取所有命中的feature flag id
activate(variantKey: string, decisionId: string, trackId: string, attributes: Map<string, any>, defaultValue: unkown, options?: TOptions) Promise<{val: any}> 根据key做分流,并上报实验信息; 返回逻辑:命中的实验信息 -> 命中的feature flag信息 -> 传入的默认值
close() void 主动关闭所有元数据服务的schedule job
setInterval(interval: number) void 动态调整获取元数据的间隔时间

不同场景使用

自定义获取元数据和实验信息曝光地址

  • SaaS服务不同区域的配置
import AbClient from '@datatester/node-sdk'

// metaHost
// cn: AbClient.metaHost.cn 
// sg: AbClient.metaHost.sg
// va: AbClient.metaHost.va 

// trackHost
// cn: AbClient.trackHost.cn 
// sg: AbClient.trackHost.sg
// va: AbClient.trackHost.va 

const abSdk = new AbClient('token', {
    trackHost: AbClient.trackHost.sg
    metaHost: AbClient.metaHost.sg 
})
  • 私有化服务
import AbClient from '@datatester/node-sdk'
const abSdk = new AbClient('token', {
    trackHost: '' // 可访问到的上报http接口的host
    metaHost: ''// 可访问到的获取元数据http接口的host
})
  • 监听内部日志

sdk对外暴露了一个EventEmitter的实现,可以对sdk内部一些日志进行监听

// meta获取成功
abSdk.on('meta.success', data => {})
// meta获取失败
abSdk.on('meta.failure', error => {})
// ...

Package Sidebar

Install

npm i @datatester/node-sdk

Weekly Downloads

0

Version

1.1.4

License

ISC

Unpacked Size

41.2 kB

Total Files

16

Last publish

Collaborators

  • shenchen
  • tuobaye0711
  • bytednpm
  • carroll
  • winger
  • studzw
  • liuluying