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 26
 	LogPath            string                                `toml:"log_path"`
27 27
 	RedisHost          string                                `toml:"redis_host"`
28 28
 	RedisPassword      string                                `toml:"redis_password"`
29
+	TestMode           bool                                  `toml:"test_mode"`
29 30
 }
30 31
 type FlogControlItem struct {
31 32
 	Percent  int `toml:"percent"`

+ 17 - 0
adslib/device/device.go

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

+ 16 - 11
adslib/graylog/graylog.go

@@ -9,7 +9,8 @@ import (
9 9
 	"net/url"
10 10
 	"time"
11 11
 )
12
-var grayLog  *gelf.Gelf
12
+
13
+var grayLog *gelf.Gelf
13 14
 
14 15
 func init() {
15 16
 	grayLog = gelf.New(gelf.Config{
@@ -25,18 +26,22 @@ func Log(log interface{}) {
25 26
 	logStr, _ := json.Marshal(log)
26 27
 	fmt.Printf("graylog: %s\n", logStr)
27 28
 	grayLog.Log(string(logStr))
29
+
30
+	if adslib.GetConf().TestMode {
31
+		return
32
+	}
28 33
 }
29 34
 
30 35
 func LogApi(shortMsg string, desc string, extraDesc string, reqSource string) {
31 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 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 45
 		AdsExtraDetail: extraDesc,
41 46
 	}
42 47
 	Log(grayLogData)
@@ -50,9 +55,9 @@ func ReportGrayLog(req adslib.Request, dsp utils.DspParam, uid int, jsonFlag int
50 55
 	gelfData["_device_id"] = req.DeviceId
51 56
 	gelfData["_ip"] = dsp.Ip
52 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 62
 	var reqData url.Values
58 63
 	if jsonFlag == 0 {
@@ -67,7 +72,7 @@ func ReportGrayLog(req adslib.Request, dsp utils.DspParam, uid int, jsonFlag int
67 72
 		if k == "ip" {
68 73
 			continue
69 74
 		}
70
-		gelfData["_" + k] = v
75
+		gelfData["_"+k] = v
71 76
 	}
72 77
 
73 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 160
 func SetDistributeActionNum(advertiser string, action string, bannerid int) {
161
+	if adslib.GetConf().TestMode {
162
+		return
163
+	}
164
+
161 165
 	if advertiser == "" {
162 166
 		return
163 167
 	}
@@ -196,6 +200,9 @@ func GetDeviceIpReport(deviceId string, ip string) (bool, error) {
196 200
 
197 201
 // 设置设备+ip上报的时间点
198 202
 func SetDeviceIpReport(deviceid string, ip string) {
203
+	if adslib.GetConf().TestMode {
204
+		return
205
+	}
199 206
 	conn := ads_redis.RedisConn.Get()
200 207
 	defer conn.Close()
201 208
 
@@ -205,6 +212,9 @@ func SetDeviceIpReport(deviceid string, ip string) {
205 212
 
206 213
 // 设置总请求到广告的次数
207 214
 func SetAdsRequestNum(advertiser string, bannerId int) {
215
+	if adslib.GetConf().TestMode {
216
+		return
217
+	}
208 218
 	if advertiser == "" {
209 219
 		return
210 220
 	}
@@ -220,6 +230,9 @@ func SetAdsRequestNum(advertiser string, bannerId int) {
220 230
 }
221 231
 
222 232
 func SetAdsRealRequestNum(advertiser string) {
233
+	if adslib.GetConf().TestMode {
234
+		return
235
+	}
223 236
 	if advertiser == "" {
224 237
 		return
225 238
 	}
@@ -260,6 +273,9 @@ func GetMinScriptOrderByAdv(advertiser string) (int64, error) {
260 273
 
261 274
 // 设置来源的曝光次数
262 275
 func SetReqSourceView(adv string, reqSource string, source string) {
276
+	if adslib.GetConf().TestMode {
277
+		return
278
+	}
263 279
 	conn := ads_redis.RedisConn.Get()
264 280
 	defer conn.Close()
265 281
 
@@ -309,6 +325,10 @@ func GetFinishedDispatchCount(orderId int64, dispatchType string, curTime time.T
309 325
 // 增加已经投放的量
310 326
 // dispatchType: "click", "show"
311 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 332
 	conn := ads_redis.RedisConn.Get()
313 333
 	defer conn.Close()
314 334
 
@@ -347,6 +367,10 @@ func GetPreMinuteFinishedDispatchCount(orderId int64, dispatchType string, curTi
347 367
 // 增加当前分钟已经投放的量
348 368
 // dispatchType: "click", "show"
349 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 374
 	conn := ads_redis.RedisConn.Get()
351 375
 	defer conn.Close()
352 376
 
@@ -363,6 +387,9 @@ func IncrPreMinuteFinishedDispatchCount(orderId int64, dispatchType string, incr
363 387
 // 设置总计划投放数量
364 388
 // dispatchType: "click", "show"
365 389
 func SetPlanDispatchCount(orderId int64, dispatchType string, cnt int, curTime time.Time) {
390
+	if adslib.GetConf().TestMode {
391
+		return
392
+	}
366 393
 	conn := ads_redis.RedisConn.Get()
367 394
 	defer conn.Close()
368 395
 
@@ -373,6 +400,9 @@ func SetPlanDispatchCount(orderId int64, dispatchType string, cnt int, curTime t
373 400
 // 设置订单计划投放数量
374 401
 // dispatchType: "click", "show"
375 402
 func SetOrderPlanDispatchCount(orderId int64, dispatchType string, cnt int, curTime time.Time) {
403
+	if adslib.GetConf().TestMode {
404
+		return
405
+	}
376 406
 	conn := ads_redis.RedisConn.Get()
377 407
 	defer conn.Close()
378 408