-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSampleVariable.class.php
More file actions
120 lines (90 loc) · 3.18 KB
/
SampleVariable.class.php
File metadata and controls
120 lines (90 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/**
* Created by taopeng@meilishuo.com.
* User: 2014
* Date: 14-6-17
* Time: 下午10:14
*/
require_once("UserSplit.class.php");
class SampleVariable {
# 全局唯一的配置文件路径
private static $yamlFile = "sample_variable.json";
# 配置内容
private static $conf;
# singleton
private static $instance;
function __construct() {
# yaml related: http://www.php.net/manual/en/yaml.examples.php
# $yaml = yaml_parse(file_get_contents(self::$yamlFile));
# 线上php没有yaml扩展,先用json做配置
$yaml = json_decode(file_get_contents(self::$yamlFile), true);
self::$conf = self::mapYaml2Kv($yaml);
}
/**
* 将yaml的规范json转化为相对简单的kv json
* res['name1'] = {'default': 0, 'layer': 'A', 'condition':{'A1': 10, 'A2':100}}
* TODO: 进行严格的正确性检查。segment命名不冲突,切分区间顺序不重叠。
*/
private static function mapYaml2Kv($yaml) {
$res = array();
foreach ($yaml as $name => $vars ) {
$default = $vars['default'];
$layer = $vars['layer'];
$condition = $vars['condition'];
$seg2val = array();
foreach ($condition as $pair) {
$seg2val[$pair['segment']] = $pair['value'];
}
$res[$name] = array(
'default' => $default,
'layer' => $layer,
'condition' => $seg2val);
}
return $res;
}
public static function instance() {
is_null(self::$instance) && self::$instance = new self();
return self::$instance;
}
public function getSVInWeb($name) {
$sv = self::$conf[$name];
$layer = $sv['layer'];
$split = UserSplit::instance();
$segment = $split->splitInWeb($layer);
return $this->getSV($name, $segment);
}
public function getSVInMob($name) {
$sv = self::$conf[$name];
$layer = $sv['layer'];
$split = UserSplit::instance();
$segment = $split->splitInMob($layer);
return $this->getSV($name, $segment);
}
/**
* 高级接口,根据指定的$userInfo用户定义切分。
*/
public function getSVWithUserInfo($name, $userInfo) {
$sv = self::$conf[$name];
$layer = $sv['layer'];
$split = UserSplit::instance();
$segment = $split->splitWithUserInfo($userInfo, $layer);
return $this->getSV($name, $segment);
}
/**
* 根据指定的$split_string: A1_B2, 确认抽样变量$name的值。
* 无condition命中则返回default。
* NOTE: 在这里写AB实验的日志。
*/
public function getSV($name, $splitString) {
$segNames = explode('_', trim($splitString));
$sv = self::$conf[$name];
$seg2val = $sv['condition'];
foreach ($segNames as $s) {
if (isset($seg2val[$s])) {
ABtestLog::write($s);
return $seg2val[$s];
}
}
return $sv['default'];
}
}