recconf.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. package recconf
  2. import (
  3. "encoding/json"
  4. "reflect"
  5. "gitlab.alibaba-inc.com/pai_biz_arch/pairec/config"
  6. )
  7. var (
  8. Config *RecommendConfig
  9. adapterName = "json"
  10. )
  11. const (
  12. DaoConf_Adapter_Mysql = "mysql"
  13. DaoConf_Adapter_Redis = "redis"
  14. DaoConf_Adapter_TableStore = "tablestore"
  15. DaoConf_Adapter_HBase = "hbase"
  16. DaoConf_Adapter_Hologres = "hologres"
  17. DataSource_Type_Kafka = "kafka"
  18. )
  19. func init() {
  20. Config = newRecommendConfig()
  21. }
  22. type RecommendConfig struct {
  23. RunMode string // Run Mode: daily | product
  24. Region string
  25. ListenConf ListenConfig
  26. FeatureConfs map[string]SceneFeatureConfig
  27. SortNames map[string][]string
  28. FilterNames map[string][]string
  29. AlgoConfs []AlgoConfig
  30. RecallConfs []RecallConfig
  31. FilterConfs []FilterConfig
  32. RedisConfs map[string]RedisConfig
  33. MysqlConfs map[string]MysqlConfig
  34. HologresConfs map[string]HologresConfig
  35. KafkaConfs map[string]KafkaConfig
  36. DatahubConfs map[string]DatahubConfig
  37. HBaseConfs map[string]HBaseConfig
  38. TableStoreConfs map[string]TableStoreConfig
  39. SceneConfs map[string]map[string]CategoryConfig
  40. RankConf map[string]RankConfig
  41. LogConf LogConfig
  42. ABTestConf ABTestConfig
  43. CallBackConfs map[string]CallBackConfig
  44. GeneralRankConfs map[string]GeneralRankConfig
  45. ColdStartRankConfs map[string]ColdStartRankConfig
  46. }
  47. type ListenConfig struct {
  48. HttpAddr string
  49. HttpPort int
  50. }
  51. type DaoConfig struct {
  52. Adapter string
  53. AdapterType string
  54. RedisName string
  55. RedisPrefix string
  56. RedisDefaultKey string
  57. MysqlName string
  58. MysqlTable string
  59. Config string
  60. TableStoreName string
  61. TableStoreTableName string
  62. HBasePrefix string
  63. HBaseName string
  64. HBaseTable string
  65. ColumnFamily string
  66. Qualifier string
  67. // hologres
  68. HologresName string
  69. HologresTableName string
  70. }
  71. type SceneFeatureConfig struct {
  72. FeatureLoadConfs []FeatureLoadConfig
  73. AsynLoadFeature bool
  74. }
  75. type FeatureLoadConfig struct {
  76. FeatureDaoConf FeatureDaoConfig
  77. Features []FeatureConfig
  78. }
  79. type FeatureDaoConfig struct {
  80. DaoConfig
  81. FeatureKey string
  82. FeatureStore string // user or item
  83. UserFeatureKeyName string
  84. ItemFeatureKeyName string
  85. UserSelectFields string
  86. ItemSelectFields string
  87. }
  88. type FeatureConfig struct {
  89. FeatureType string
  90. FeatureName string
  91. FeatureSource string
  92. FeatureStore string // user or item
  93. RemoveFeatureSource bool // delete feature source
  94. Normalizer string
  95. }
  96. type AlgoConfig struct {
  97. Name string
  98. Type string
  99. EasConf EasConfig
  100. VectorConf VectorConfig
  101. BanditConf BanditConfig
  102. LookupConf LookupConfig
  103. }
  104. type LookupConfig struct {
  105. FieldName string
  106. }
  107. type BanditConfig struct {
  108. Alpha float64
  109. FeatureName string
  110. SharedFeatureName string
  111. ArmIdKey string
  112. Hologres string
  113. ModelTable string
  114. FeatureTable string
  115. CacheExpire int
  116. CacheMaxSize int
  117. GlobalModelParamId string
  118. RequestArmNumPerTime int
  119. RequestArmInterval int
  120. Parallelism int
  121. }
  122. type EasConfig struct {
  123. Processor string
  124. Url string
  125. Auth string
  126. SignatureName string
  127. Timeout int
  128. RetryTimes int
  129. ResponseFuncName string
  130. }
  131. type VectorConfig struct {
  132. ServerAddress string
  133. Timeout int64
  134. }
  135. type RecallConfig struct {
  136. Name string
  137. RecallType string
  138. RecallCount int
  139. RecallAlgo string
  140. ItemType string
  141. CacheAdapter string
  142. CacheConfig string
  143. CachePrefix string
  144. CacheTime int // cache time by seconds
  145. Triggers []TriggerConfig
  146. HologresVectorConf HologresVectorConfig
  147. UserCollaborativeDaoConf UserCollaborativeDaoConfig
  148. ItemCollaborativeDaoConf ItemCollaborativeDaoConfig
  149. User2ItemDaoConf User2ItemDaoConfig
  150. UserTopicDaoConf UserTopicDaoConfig
  151. DaoConf DaoConfig
  152. VectorDaoConf VectorDaoConfig
  153. ColdStartDaoConf ColdStartDaoConfig
  154. }
  155. type ColdStartDaoConfig struct {
  156. SqlDaoConfig
  157. TimeInterval int // second
  158. }
  159. type SqlDaoConfig struct {
  160. DaoConfig
  161. WhereClause string
  162. PrimaryKey string
  163. SelectFields string
  164. }
  165. type HologresVectorConfig struct {
  166. VectorTable string
  167. VectorKeyField string
  168. VectorEmbeddingField string
  169. }
  170. type UserCollaborativeDaoConfig struct {
  171. DaoConfig
  172. User2ItemTable string
  173. Item2ItemTable string
  174. }
  175. type ItemCollaborativeDaoConfig struct {
  176. DaoConfig
  177. Item2ItemTable string
  178. }
  179. type User2ItemDaoConfig struct {
  180. DaoConfig
  181. User2ItemTable string
  182. Item2ItemTable string
  183. }
  184. type UserTopicDaoConfig struct {
  185. DaoConfig
  186. UserTopicTable string
  187. TopicItemTable string
  188. }
  189. type VectorDaoConfig struct {
  190. DaoConfig
  191. EmbeddingField string
  192. KeyField string
  193. }
  194. type RedisConfig struct {
  195. Host string
  196. Port int
  197. Password string
  198. DbNum int
  199. MaxIdle int
  200. ConnectTimeout int
  201. ReadTimeout int
  202. WriteTimeout int
  203. }
  204. type MysqlConfig struct {
  205. DSN string
  206. }
  207. type HologresConfig struct {
  208. DSN string
  209. }
  210. type KafkaConfig struct {
  211. BootstrapServers string
  212. Topic string
  213. }
  214. type DatahubConfig struct {
  215. AccessId string
  216. AccessKey string
  217. Endpoint string
  218. ProjectName string
  219. TopicName string
  220. }
  221. type HBaseConfig struct {
  222. ZKQuorum string
  223. }
  224. type TableStoreConfig struct {
  225. EndPoint string
  226. InstanceName string
  227. AccessKeyId string
  228. AccessKeySecret string
  229. RoleArn string
  230. }
  231. type SceneConfig struct {
  232. Categories []string
  233. }
  234. type CategoryConfig struct {
  235. RecallNames []string
  236. }
  237. type RankConfig struct {
  238. RankAlgoList []string
  239. RankScore string
  240. Processor string
  241. BatchCount int
  242. }
  243. type OperatorValueConfig struct {
  244. Type string // "property", "function"
  245. Name string
  246. From string // item or user
  247. }
  248. type LogConfig struct {
  249. RetensionDays int
  250. DiskSize int // unit : G, if value = 20, the true size is 20G
  251. LogLevel string // valid value is DEBUG, INFO , ERROR , FATAL
  252. Output string // valid value is file, console
  253. }
  254. type ABTestConfig struct {
  255. Host string
  256. Token string
  257. }
  258. type FilterConfig struct {
  259. Name string
  260. FilterType string
  261. DaoConf DaoConfig
  262. MaxItems int
  263. TimeInterval int // second
  264. RetainNum int
  265. WriteLog bool
  266. WriteLogExcludeScenes []string
  267. GenerateItemDataFuncName string
  268. AdjustCountConfs []AdjustCountConfig
  269. ItemStateDaoConf ItemStateDaoConfig
  270. FilterEvaluableExpression string
  271. FilterParams []FilterParamConfig
  272. }
  273. type FilterParamConfig struct {
  274. Name string
  275. Operator string
  276. Type string // string, int, int64
  277. Value interface{}
  278. }
  279. type ItemStateDaoConfig struct {
  280. DaoConfig
  281. ItemFieldName string
  282. WhereClause string
  283. SelectFields string
  284. }
  285. type AdjustCountConfig struct {
  286. RecallName string
  287. Count int
  288. Type string
  289. }
  290. type CallBackConfig struct {
  291. DataSource DataSourceConfig
  292. }
  293. type GeneralRankConfig struct {
  294. FeatureLoadConfs []FeatureLoadConfig
  295. RankConf RankConfig
  296. RetainNum int
  297. BatchCount int
  298. }
  299. type ColdStartRankConfig struct {
  300. RecallName string
  301. AlgoName string
  302. }
  303. type DataSourceConfig struct {
  304. Name string
  305. Type string
  306. }
  307. type TriggerConfig struct {
  308. TriggerKey string
  309. DefaultValue string
  310. Boundaries []int
  311. }
  312. func newRecommendConfig() *RecommendConfig {
  313. conf := RecommendConfig{
  314. RunMode: "daily",
  315. ListenConf: ListenConfig{
  316. HttpAddr: "",
  317. HttpPort: 80,
  318. },
  319. // SortNames: []string{"item_score"},
  320. }
  321. return &conf
  322. }
  323. func CopyConfig(src, dst *RecommendConfig, filters ...func(string) bool) {
  324. srcVal := reflect.ValueOf(src).Elem()
  325. srcType := reflect.TypeOf(src).Elem()
  326. dstVal := reflect.ValueOf(dst).Elem()
  327. numOfFields := srcVal.NumField()
  328. for i := 0; i < numOfFields; i++ {
  329. fieldType := srcType.Field(i)
  330. flag := true
  331. for _, filter := range filters {
  332. flag = filter(fieldType.Name)
  333. if flag == false {
  334. break
  335. }
  336. }
  337. if flag == false {
  338. continue
  339. }
  340. elemField := dstVal.FieldByName(fieldType.Name)
  341. if elemField.CanSet() {
  342. fieldVal := srcVal.Field(i)
  343. elemField.Set(fieldVal)
  344. }
  345. }
  346. }
  347. // parammeter:
  348. // filePath: config file path
  349. func Load(filePath string) error {
  350. configer, err := config.NewConfig(adapterName, filePath)
  351. if err != nil {
  352. return err
  353. }
  354. rawdata := configer.RawData()
  355. err = json.Unmarshal(rawdata, Config)
  356. if err != nil {
  357. return err
  358. }
  359. return nil
  360. }
  361. /**
  362. func loadConfig(filePath string) error {
  363. content, err := ioutil.ReadFile(filePath)
  364. if err != nil {
  365. return err
  366. }
  367. if len(content) == 0 {
  368. return errors.New("config content empty")
  369. }
  370. conf := RecommendConfig{}
  371. err = json.Unmarshal(content, &conf)
  372. if err != nil {
  373. return err
  374. }
  375. Config = &conf
  376. log.Info("load config success")
  377. return nil
  378. }
  379. **/