张金坤 8 years ago
parent
commit
090ce2fbe5

+ 15 - 0
src/jfw/modules/behaviorcollect/README.md

@@ -0,0 +1,15 @@
+ajx调用,model,s_url属性必填
+var content='{"model":"accesslog","s_url":"/jylab/entsearch/","s_describe":"剑鱼实验室企业查询"}'
+
+
+$.post('/accessinfo/byajax',{json:content},function(r){
+	
+});
+
+//跨域请求
+$.ajax({  
+    url:"http://192.168.100.60:8084/accessinfo/byajax",  
+    dataType:'jsonp',  
+    data:{json:content},  
+    jsonp:'callback' 
+});  

+ 4 - 1
src/jfw/modules/behaviorcollect/src/config.json

@@ -4,7 +4,10 @@
     "insertthreads": 5,
     "cachesize": 50000,
     "influxdb": "https://jianyu:Topnet@20150501@wxlmjy.qmx.top:443",
-    "log_model": {
+    "model_url": {
         "剑鱼实验室": "/jylab/.*"
+    },
+    "model_invalid": {
+        "accesslog": "30d"
     }
 }

+ 10 - 8
src/jfw/modules/behaviorcollect/src/main.go

@@ -19,7 +19,8 @@ type config struct {
 	RpcAddr       string                 `json:"rpcaddr"`
 	Cachesize     int                    `json:"cachesize"`
 	WebAddr       string                 `json:"webaddr"`
-	Log_model     map[string]interface{} `json:"log_model"`
+	Modelurl      map[string]interface{} `json:"model_url"`     //模块
+	Modelinvalid  map[string]string      `json:"model_invalid"` //模块数据时效
 }
 
 //
@@ -47,7 +48,7 @@ func init() {
 	list = map[string]parse.Face{
 		"accesslog": parse.AccessLogParse{},
 	}
-	parse.AccessLogInit(c.Log_model)
+	parse.InitModelUrl(c.Modelurl)
 }
 
 //
@@ -55,11 +56,11 @@ func processrequest(jsonstr string, req *http.Request) {
 	data := map[string]interface{}{}
 	json.Unmarshal([]byte(jsonstr), &data)
 	model := data["model"].(string)
-	if v, ok := list[model]; ok {
+	invalid := c.Modelinvalid[model]
+	if v, ok := list[model]; ok && invalid != "" {
 		switch model {
 		case "accesslog":
-			writer.Write("jy_logs", "30d", v.Parse(data, req))
-
+			writer.Write("jy_logs", invalid, v.Parse(data, req))
 		}
 	}
 }
@@ -67,10 +68,11 @@ func processrequest(jsonstr string, req *http.Request) {
 //
 func main() {
 	//TODO 开通RPC ajax服务
-	http.HandleFunc("/ajax", func(w http.ResponseWriter, req *http.Request) {
-		//
+	http.HandleFunc("/accessinfo/byajax", func(w http.ResponseWriter, req *http.Request) {
 		jsontxt := req.FormValue("json") //post方式传过来
-		processrequest(jsontxt, req)
+		if jsontxt != "" {
+			processrequest(jsontxt, req)
+		}
 	})
 	http.ListenAndServe(c.WebAddr, nil)
 	//开通json rpc服务

+ 8 - 91
src/jfw/modules/behaviorcollect/src/parse/accesslog.go

@@ -2,11 +2,10 @@ package parse
 
 import (
 	"fmt"
-	"net"
 	"net/http"
 	"regexp"
-	"strings"
 	"time"
+	"util"
 
 	"github.com/influxdata/influxdb-client"
 )
@@ -21,7 +20,7 @@ var tagtitle = map[string]interface{}{
 	"s_browse": true,
 	"s_model":  true,
 }
-var log_model = map[string]*regexp.Regexp{}
+var model_url = map[string]*regexp.Regexp{}
 
 //
 func (alp AccessLogParse) Parse(data map[string]interface{}, req *http.Request) *influxdb.Point {
@@ -45,108 +44,26 @@ func (alp AccessLogParse) Parse(data map[string]interface{}, req *http.Request)
 		Time:   time.Now(),
 	}
 	return ponit
-
 }
 
 //
-func AccessLogInit(model map[string]interface{}) {
+func InitModelUrl(model map[string]interface{}) {
 	//TODO 加载IP转换,url模块对应
 	for k, v := range model {
 		reg, _ := regexp.Compile(fmt.Sprint(v))
-		log_model[k] = reg
+		model_url[k] = reg
 	}
 }
 
 //补充基本数据字段
 func AddBasicData(data map[string]interface{}, req *http.Request) map[string]interface{} {
 	agent := req.Header.Get("user-agent")
-	data["s_os"] = GetOS(agent)
-	data["s_ip"] = GetIp(req)
-	data["s_browse"] = GetBrowse(agent)
+	data["s_os"] = util.GetOS(agent)
+	data["s_ip"] = util.GetIp(req)
+	data["s_browse"] = util.GetBrowse(agent)
 	data["s_refer"] = req.Referer()
 	data["s_client"] = agent
-	data["s_model"] = GetModel(fmt.Sprint(data["s_url"]))
+	data["s_model"] = util.GetModel(fmt.Sprint(data["s_url"]), model_url)
 	delete(data, "model")
 	return data
 }
-
-//根据url获取所属模块
-func GetModel(url string) string {
-	s_model := "其他"
-	for k, v := range log_model {
-		b := v.MatchString(url)
-		if b {
-			s_model = k
-			break
-		}
-	}
-	return s_model
-}
-
-//获取平台类型
-func GetOS(useros string) string {
-	osVersion := "其他"
-	if strings.Contains(useros, "NT 6.0") {
-		osVersion = "Windows Vista/Server 2008"
-	} else if strings.Contains(useros, "NT 5.2") {
-		osVersion = "Windows Server 2003"
-	} else if strings.Contains(useros, "NT 5.1") {
-		osVersion = "Windows XP"
-	} else if strings.Contains(useros, "NT 5") {
-		osVersion = "Windows 2000"
-	} else if strings.Contains(useros, "Mac") {
-		osVersion = "Mac"
-	} else if strings.Contains(useros, "Unix") {
-		osVersion = "UNIX"
-	} else if strings.Contains(useros, "Linux") {
-		osVersion = "Linux"
-	} else if strings.Contains(useros, "SunOS") {
-		osVersion = "SunOS"
-	} else if strings.Contains(useros, "NT 6.3") {
-		osVersion = "Window8"
-	} else if strings.Contains(useros, "NT 6.1") {
-		osVersion = "Window7"
-	} else if strings.Contains(useros, "NT 10.0") {
-		osVersion = "Window10"
-	}
-	return osVersion
-}
-
-//获取浏览器类型
-func GetBrowse(userbrowser string) string {
-	browserVersion := "其他"
-	if strings.Contains(userbrowser, "MSIE") {
-		browserVersion = "IE"
-	} else if strings.Contains(userbrowser, "Firefox") {
-		browserVersion = "Firefox"
-	} else if strings.Contains(userbrowser, "Chrome") {
-		browserVersion = "Chrome"
-	} else if strings.Contains(userbrowser, "Safari") {
-		browserVersion = "Safari"
-	} else if strings.Contains(userbrowser, "rv:11.0") {
-		browserVersion = "IE11"
-	}
-	return browserVersion
-}
-
-//获取ip
-func GetIp(req *http.Request) string {
-	if req == nil {
-		return ""
-	}
-	ip_for := req.Header.Get("x-forwarded-for")
-	ip_client := req.Header.Get("http_client_ip")
-	ip_addr := req.Header.Get("Remote_addr")
-	un := "unknown"
-	if (ip_for != un) && (len(strings.TrimSpace(ip_for)) > 0) {
-		return ip_for
-	}
-	if (ip_client != un) && (len(strings.TrimSpace(ip_client)) > 0) {
-		return ip_client
-	}
-	if (ip_addr != un) && (len(strings.TrimSpace(ip_addr)) > 0) {
-		return ip_addr
-	}
-	ip, _, _ := net.SplitHostPort(req.RemoteAddr)
-	return ip
-}

+ 90 - 0
src/jfw/modules/behaviorcollect/src/util/util.go

@@ -0,0 +1,90 @@
+// util
+package util
+
+import (
+	"net"
+	"net/http"
+	"regexp"
+	"strings"
+)
+
+//根据url获取所属模块
+func GetModel(url string, model_url map[string]*regexp.Regexp) string {
+	s_model := "其他"
+	for k, v := range model_url {
+		b := v.MatchString(url)
+		if b {
+			s_model = k
+			break
+		}
+	}
+	return s_model
+}
+
+//获取平台类型
+func GetOS(useros string) string {
+	osVersion := "其他"
+	if strings.Contains(useros, "NT 6.0") {
+		osVersion = "Windows Vista/Server 2008"
+	} else if strings.Contains(useros, "NT 5.2") {
+		osVersion = "Windows Server 2003"
+	} else if strings.Contains(useros, "NT 5.1") {
+		osVersion = "Windows XP"
+	} else if strings.Contains(useros, "NT 5") {
+		osVersion = "Windows 2000"
+	} else if strings.Contains(useros, "Mac") {
+		osVersion = "Mac"
+	} else if strings.Contains(useros, "Unix") {
+		osVersion = "UNIX"
+	} else if strings.Contains(useros, "Linux") {
+		osVersion = "Linux"
+	} else if strings.Contains(useros, "SunOS") {
+		osVersion = "SunOS"
+	} else if strings.Contains(useros, "NT 6.3") {
+		osVersion = "Window8"
+	} else if strings.Contains(useros, "NT 6.1") {
+		osVersion = "Window7"
+	} else if strings.Contains(useros, "NT 10.0") {
+		osVersion = "Window10"
+	}
+	return osVersion
+}
+
+//获取浏览器类型
+func GetBrowse(userbrowser string) string {
+	browserVersion := "其他"
+	if strings.Contains(userbrowser, "MSIE") {
+		browserVersion = "IE"
+	} else if strings.Contains(userbrowser, "Firefox") {
+		browserVersion = "Firefox"
+	} else if strings.Contains(userbrowser, "Chrome") {
+		browserVersion = "Chrome"
+	} else if strings.Contains(userbrowser, "Safari") {
+		browserVersion = "Safari"
+	} else if strings.Contains(userbrowser, "rv:11.0") {
+		browserVersion = "IE11"
+	}
+	return browserVersion
+}
+
+//获取ip
+func GetIp(req *http.Request) string {
+	if req == nil {
+		return ""
+	}
+	ip_for := req.Header.Get("x-forwarded-for")
+	ip_client := req.Header.Get("http_client_ip")
+	ip_addr := req.Header.Get("Remote_addr")
+	un := "unknown"
+	if (ip_for != un) && (len(strings.TrimSpace(ip_for)) > 0) {
+		return ip_for
+	}
+	if (ip_client != un) && (len(strings.TrimSpace(ip_client)) > 0) {
+		return ip_client
+	}
+	if (ip_addr != un) && (len(strings.TrimSpace(ip_addr)) > 0) {
+		return ip_addr
+	}
+	ip, _, _ := net.SplitHostPort(req.RemoteAddr)
+	return ip
+}