Browse Source

支持test mode

jiantaoli 4 years ago
parent
commit
7751e11953
4 changed files with 64 additions and 11 deletions
  1. 1 0
      adslib/config.go
  2. 17 0
      adslib/device/device.go
  3. 16 11
      adslib/graylog/graylog.go
  4. 30 0
      adslib/redis_data/redis_data.go

+ 1 - 0
adslib/config.go

@@ -26,6 +26,7 @@ type SvrConf struct {
26
 	LogPath            string                                `toml:"log_path"`
26
 	LogPath            string                                `toml:"log_path"`
27
 	RedisHost          string                                `toml:"redis_host"`
27
 	RedisHost          string                                `toml:"redis_host"`
28
 	RedisPassword      string                                `toml:"redis_password"`
28
 	RedisPassword      string                                `toml:"redis_password"`
29
+	TestMode           bool                                  `toml:"test_mode"`
29
 }
30
 }
30
 type FlogControlItem struct {
31
 type FlogControlItem struct {
31
 	Percent  int `toml:"percent"`
32
 	Percent  int `toml:"percent"`

+ 17 - 0
adslib/device/device.go

@@ -6,6 +6,7 @@ import (
6
 	"github.com/gomodule/redigo/redis"
6
 	"github.com/gomodule/redigo/redis"
7
 	log "github.com/sirupsen/logrus"
7
 	log "github.com/sirupsen/logrus"
8
 	"math/rand"
8
 	"math/rand"
9
+	"miads/adslib"
9
 	"miads/adslib/ads_redis"
10
 	"miads/adslib/ads_redis"
10
 	"strconv"
11
 	"strconv"
11
 	"strings"
12
 	"strings"
@@ -24,6 +25,10 @@ func SetAdsTagLog(adv string, reqSource string, key string, cityCode int) {
24
 }
25
 }
25
 
26
 
26
 func setTagLog(key string) {
27
 func setTagLog(key string) {
28
+	if adslib.GetConf().TestMode {
29
+		return
30
+	}
31
+
27
 	conn := ads_redis.RedisConn.Get()
32
 	conn := ads_redis.RedisConn.Get()
28
 	defer conn.Close()
33
 	defer conn.Close()
29
 
34
 
@@ -35,6 +40,10 @@ func setTagLog(key string) {
35
 
40
 
36
 // 存储ip上一次请求时间
41
 // 存储ip上一次请求时间
37
 func SetIpReqTime(ip string, reqTime int64) {
42
 func SetIpReqTime(ip string, reqTime int64) {
43
+	if adslib.GetConf().TestMode {
44
+		return
45
+	}
46
+
38
 	conn := ads_redis.RedisConn.Get()
47
 	conn := ads_redis.RedisConn.Get()
39
 	defer conn.Close()
48
 	defer conn.Close()
40
 
49
 
@@ -62,6 +71,10 @@ func GetIpReqTime(ip string) (int64, error) {
62
 }
71
 }
63
 
72
 
64
 func SetMiDeviceConf(key string, d DeviceConf) error {
73
 func SetMiDeviceConf(key string, d DeviceConf) error {
74
+	if adslib.GetConf().TestMode {
75
+		return
76
+	}
77
+
65
 	conn := ads_redis.RedisConn.Get()
78
 	conn := ads_redis.RedisConn.Get()
66
 	defer conn.Close()
79
 	defer conn.Close()
67
 
80
 
@@ -108,6 +121,10 @@ func GetMiDeviceConf(ip string, randNum int, specialDeviceNum int) (*DeviceConf,
108
 
121
 
109
 // 设置每天的city_code的值
122
 // 设置每天的city_code的值
110
 func SetDailyFakeDeviceConfByCityCode(cityCode int, d DeviceConf, dateInt int) error {
123
 func SetDailyFakeDeviceConfByCityCode(cityCode int, d DeviceConf, dateInt int) error {
124
+	if adslib.GetConf().TestMode {
125
+		return nil
126
+	}
127
+
111
 	conn := ads_redis.RedisConn.Get()
128
 	conn := ads_redis.RedisConn.Get()
112
 	defer conn.Close()
129
 	defer conn.Close()
113
 
130
 

+ 16 - 11
adslib/graylog/graylog.go

@@ -9,7 +9,8 @@ import (
9
 	"net/url"
9
 	"net/url"
10
 	"time"
10
 	"time"
11
 )
11
 )
12
-var grayLog  *gelf.Gelf
12
+
13
+var grayLog *gelf.Gelf
13
 
14
 
14
 func init() {
15
 func init() {
15
 	grayLog = gelf.New(gelf.Config{
16
 	grayLog = gelf.New(gelf.Config{
@@ -25,18 +26,22 @@ func Log(log interface{}) {
25
 	logStr, _ := json.Marshal(log)
26
 	logStr, _ := json.Marshal(log)
26
 	fmt.Printf("graylog: %s\n", logStr)
27
 	fmt.Printf("graylog: %s\n", logStr)
27
 	grayLog.Log(string(logStr))
28
 	grayLog.Log(string(logStr))
29
+
30
+	if adslib.GetConf().TestMode {
31
+		return
32
+	}
28
 }
33
 }
29
 
34
 
30
 func LogApi(shortMsg string, desc string, extraDesc string, reqSource string) {
35
 func LogApi(shortMsg string, desc string, extraDesc string, reqSource string) {
31
 	grayLogData := struct {
36
 	grayLogData := struct {
32
-		ShortMessage string `json:"short_message"`
33
-		ReqSource string `json:"req_source"`
34
-		AdsDetail string `json:"ads_detail"`
37
+		ShortMessage   string `json:"short_message"`
38
+		ReqSource      string `json:"req_source"`
39
+		AdsDetail      string `json:"ads_detail"`
35
 		AdsExtraDetail string `json:"ads_extra_detail"`
40
 		AdsExtraDetail string `json:"ads_extra_detail"`
36
 	}{
41
 	}{
37
-		ShortMessage: shortMsg,
38
-		ReqSource:    reqSource,
39
-		AdsDetail: desc,
42
+		ShortMessage:   shortMsg,
43
+		ReqSource:      reqSource,
44
+		AdsDetail:      desc,
40
 		AdsExtraDetail: extraDesc,
45
 		AdsExtraDetail: extraDesc,
41
 	}
46
 	}
42
 	Log(grayLogData)
47
 	Log(grayLogData)
@@ -50,9 +55,9 @@ func ReportGrayLog(req adslib.Request, dsp utils.DspParam, uid int, jsonFlag int
50
 	gelfData["_device_id"] = req.DeviceId
55
 	gelfData["_device_id"] = req.DeviceId
51
 	gelfData["_ip"] = dsp.Ip
56
 	gelfData["_ip"] = dsp.Ip
52
 	gelfData["_uri"] = req.QueryString
57
 	gelfData["_uri"] = req.QueryString
53
-	gelfData["_province"]=dsp.Province
54
-	gelfData["_city"]=dsp.City
55
-	gelfData["_city_code"]=dsp.DspCityCode
58
+	gelfData["_province"] = dsp.Province
59
+	gelfData["_city"] = dsp.City
60
+	gelfData["_city_code"] = dsp.DspCityCode
56
 
61
 
57
 	var reqData url.Values
62
 	var reqData url.Values
58
 	if jsonFlag == 0 {
63
 	if jsonFlag == 0 {
@@ -67,7 +72,7 @@ func ReportGrayLog(req adslib.Request, dsp utils.DspParam, uid int, jsonFlag int
67
 		if k == "ip" {
72
 		if k == "ip" {
68
 			continue
73
 			continue
69
 		}
74
 		}
70
-		gelfData["_" + k] = v
75
+		gelfData["_"+k] = v
71
 	}
76
 	}
72
 
77
 
73
 	gelfData["_request"] = reqData.Encode()
78
 	gelfData["_request"] = reqData.Encode()

+ 30 - 0
adslib/redis_data/redis_data.go

@@ -158,6 +158,10 @@ func GetFlowPercentDuration(advertiser string, action string) *adslib.FlogContro
158
 
158
 
159
 // 设置回调回来的广告动作的次数
159
 // 设置回调回来的广告动作的次数
160
 func SetDistributeActionNum(advertiser string, action string, bannerid int) {
160
 func SetDistributeActionNum(advertiser string, action string, bannerid int) {
161
+	if adslib.GetConf().TestMode {
162
+		return
163
+	}
164
+
161
 	if advertiser == "" {
165
 	if advertiser == "" {
162
 		return
166
 		return
163
 	}
167
 	}
@@ -196,6 +200,9 @@ func GetDeviceIpReport(deviceId string, ip string) (bool, error) {
196
 
200
 
197
 // 设置设备+ip上报的时间点
201
 // 设置设备+ip上报的时间点
198
 func SetDeviceIpReport(deviceid string, ip string) {
202
 func SetDeviceIpReport(deviceid string, ip string) {
203
+	if adslib.GetConf().TestMode {
204
+		return
205
+	}
199
 	conn := ads_redis.RedisConn.Get()
206
 	conn := ads_redis.RedisConn.Get()
200
 	defer conn.Close()
207
 	defer conn.Close()
201
 
208
 
@@ -205,6 +212,9 @@ func SetDeviceIpReport(deviceid string, ip string) {
205
 
212
 
206
 // 设置总请求到广告的次数
213
 // 设置总请求到广告的次数
207
 func SetAdsRequestNum(advertiser string, bannerId int) {
214
 func SetAdsRequestNum(advertiser string, bannerId int) {
215
+	if adslib.GetConf().TestMode {
216
+		return
217
+	}
208
 	if advertiser == "" {
218
 	if advertiser == "" {
209
 		return
219
 		return
210
 	}
220
 	}
@@ -220,6 +230,9 @@ func SetAdsRequestNum(advertiser string, bannerId int) {
220
 }
230
 }
221
 
231
 
222
 func SetAdsRealRequestNum(advertiser string) {
232
 func SetAdsRealRequestNum(advertiser string) {
233
+	if adslib.GetConf().TestMode {
234
+		return
235
+	}
223
 	if advertiser == "" {
236
 	if advertiser == "" {
224
 		return
237
 		return
225
 	}
238
 	}
@@ -260,6 +273,9 @@ func GetMinScriptOrderByAdv(advertiser string) (int64, error) {
260
 
273
 
261
 // 设置来源的曝光次数
274
 // 设置来源的曝光次数
262
 func SetReqSourceView(adv string, reqSource string, source string) {
275
 func SetReqSourceView(adv string, reqSource string, source string) {
276
+	if adslib.GetConf().TestMode {
277
+		return
278
+	}
263
 	conn := ads_redis.RedisConn.Get()
279
 	conn := ads_redis.RedisConn.Get()
264
 	defer conn.Close()
280
 	defer conn.Close()
265
 
281
 
@@ -309,6 +325,10 @@ func GetFinishedDispatchCount(orderId int64, dispatchType string, curTime time.T
309
 // 增加已经投放的量
325
 // 增加已经投放的量
310
 // dispatchType: "click", "show"
326
 // dispatchType: "click", "show"
311
 func IncrFinishedDispatchCount(orderId int64, dispatchType string, incrCnt int, curTime time.Time) (int, error) {
327
 func IncrFinishedDispatchCount(orderId int64, dispatchType string, incrCnt int, curTime time.Time) (int, error) {
328
+	if adslib.GetConf().TestMode {
329
+		return 0, nil
330
+	}
331
+
312
 	conn := ads_redis.RedisConn.Get()
332
 	conn := ads_redis.RedisConn.Get()
313
 	defer conn.Close()
333
 	defer conn.Close()
314
 
334
 
@@ -347,6 +367,10 @@ func GetPreMinuteFinishedDispatchCount(orderId int64, dispatchType string, curTi
347
 // 增加当前分钟已经投放的量
367
 // 增加当前分钟已经投放的量
348
 // dispatchType: "click", "show"
368
 // dispatchType: "click", "show"
349
 func IncrPreMinuteFinishedDispatchCount(orderId int64, dispatchType string, incrCnt int, curTime time.Time) (int, error) {
369
 func IncrPreMinuteFinishedDispatchCount(orderId int64, dispatchType string, incrCnt int, curTime time.Time) (int, error) {
370
+	if adslib.GetConf().TestMode {
371
+		return 0, nil
372
+	}
373
+
350
 	conn := ads_redis.RedisConn.Get()
374
 	conn := ads_redis.RedisConn.Get()
351
 	defer conn.Close()
375
 	defer conn.Close()
352
 
376
 
@@ -363,6 +387,9 @@ func IncrPreMinuteFinishedDispatchCount(orderId int64, dispatchType string, incr
363
 // 设置总计划投放数量
387
 // 设置总计划投放数量
364
 // dispatchType: "click", "show"
388
 // dispatchType: "click", "show"
365
 func SetPlanDispatchCount(orderId int64, dispatchType string, cnt int, curTime time.Time) {
389
 func SetPlanDispatchCount(orderId int64, dispatchType string, cnt int, curTime time.Time) {
390
+	if adslib.GetConf().TestMode {
391
+		return
392
+	}
366
 	conn := ads_redis.RedisConn.Get()
393
 	conn := ads_redis.RedisConn.Get()
367
 	defer conn.Close()
394
 	defer conn.Close()
368
 
395
 
@@ -373,6 +400,9 @@ func SetPlanDispatchCount(orderId int64, dispatchType string, cnt int, curTime t
373
 // 设置订单计划投放数量
400
 // 设置订单计划投放数量
374
 // dispatchType: "click", "show"
401
 // dispatchType: "click", "show"
375
 func SetOrderPlanDispatchCount(orderId int64, dispatchType string, cnt int, curTime time.Time) {
402
 func SetOrderPlanDispatchCount(orderId int64, dispatchType string, cnt int, curTime time.Time) {
403
+	if adslib.GetConf().TestMode {
404
+		return
405
+	}
376
 	conn := ads_redis.RedisConn.Get()
406
 	conn := ads_redis.RedisConn.Get()
377
 	defer conn.Close()
407
 	defer conn.Close()
378
 
408