From 4cc5d600d55f383134a6f12d6c4fb33d48aca494 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=8F=85?= Date: Sat, 8 Apr 2017 16:32:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8E=E5=8F=B0=E8=AE=BF=E9=97=AE=E8=AE=B0?= =?UTF-8?q?=E5=BD=95=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go/controller/visit/visit.go | 27 +++++++++++++++ go/model/uservisit.go | 66 ++++++++++++++++++++++++++++++++++++ main.go | 3 ++ nodejs/.babelrc | 1 + nodejs/package.json | 2 ++ 5 files changed, 99 insertions(+) create mode 100644 go/controller/visit/visit.go create mode 100644 go/model/uservisit.go diff --git a/go/controller/visit/visit.go b/go/controller/visit/visit.go new file mode 100644 index 00000000..9e25a163 --- /dev/null +++ b/go/controller/visit/visit.go @@ -0,0 +1,27 @@ +package visit + +import ( + "../../model" + "gopkg.in/kataras/iris.v6" +) + +// Latest30Day 近30天,每天的PV +func Latest30Day(ctx *iris.Context) { + var userVisit model.UserVisit; + result := userVisit.Latest30DayPV() + var data iris.Map; + if result == nil { + data = iris.Map{ + "list": [0]int{}, + } + } else { + data = iris.Map{ + "list": result, + } + } + ctx.JSON(iris.StatusOK, iris.Map{ + "errNo" : model.ErrorCode.SUCCESS, + "msg" : "success", + "data" : data, + }) +} diff --git a/go/model/uservisit.go b/go/model/uservisit.go new file mode 100644 index 00000000..52278645 --- /dev/null +++ b/go/model/uservisit.go @@ -0,0 +1,66 @@ +package model + +import ( + "time" + "../config" + "strings" + "github.com/jinzhu/gorm" +) + +// UserVisit 访客记录 +type UserVisit struct { + ID uint `gorm:"primary_key" json:"id"` + URL string `json:"url"` + Referrer string `json:"referrer"` + ClientID string `json:"clientID"` + UserID uint `json:"userID"` + VisitTime time.Time `json:"visitTime"` + IP string `json:"ip"` + DeviceWidth uint `json:"deviceWidth"` + DeviceHeight uint `json:"deviceHeight"` + BrowserName string `json:"browserName"` + BrowserVersion string `json:"browserVersion"` + DeviceModel string `json:"deviceModel"` + Country string `json:"country"` + Language string `json:"language"` + OSName string `json:"osName"` + OSVersion string `json:"osVersion"` +} + +// PVPerDay 每天的pv +type PVPerDay []struct { + PV int `json:"pv"` + Date string `gorm:"column:date" json:"date"` +} + +// Latest30DayPV 近30天的PV +func (userVisit UserVisit) Latest30DayPV() (PVPerDay) { + now := time.Now() + today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local) + todaySec := today.Unix() //秒 + before29 := todaySec - 29 * 24 * 60 * 60; + before29Date := time.Unix(before29, 0) + + sqlData := before29Date.Format("2006-01-02") + sqlArr := []string{ + "SELECT count(*) as pv, DATE_FORMAT(visit_time,'%Y-%m-%d') as date", + "FROM user_visit", + "WHERE visit_time >= ?", + "GROUP BY DATE_FORMAT(visit_time,'%Y-%m-%d');", + }; + + sql := strings.Join(sqlArr, " ") + db, err := gorm.Open(config.DBConfig.Dialect, config.DBConfig.URL) + if err != nil { + return nil + } + + defer db.Close() + + var result PVPerDay + err = db.Raw(sql, sqlData).Scan(&result).Error + if err != nil { + return nil + } + return result +} \ No newline at end of file diff --git a/main.go b/main.go index beb7af4b..2a90d609 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "./go/controller/product" "./go/controller/order" "./go/controller/user" + "./go/controller/visit" _ "github.com/jinzhu/gorm/dialects/mysql" "gopkg.in/kataras/iris.v6" "gopkg.in/kataras/iris.v6/adaptors/httprouter" @@ -49,6 +50,8 @@ func main() { adminRouter.Get("/user/yesterday", user.YesterdayRegisterUser) adminRouter.Get("/user/latest/30", user.Latest30Day) adminRouter.Get("/user/analyze", user.Analyze) + + adminRouter.Get("/visit/pv/latest/30", visit.Latest30Day) } app.OnError(iris.StatusNotFound, func(ctx *iris.Context) { diff --git a/nodejs/.babelrc b/nodejs/.babelrc index 6c03feae..d839278e 100644 --- a/nodejs/.babelrc +++ b/nodejs/.babelrc @@ -1,6 +1,7 @@ { "plugins": [ "transform-runtime", + "recharts", ["import", { libraryName: "antd", style: "css" }] ] } diff --git a/nodejs/package.json b/nodejs/package.json index 6061f418..52cbef8e 100755 --- a/nodejs/package.json +++ b/nodejs/package.json @@ -22,6 +22,7 @@ "babel-core": "6.24.0", "babel-loader": "6.4.1", "babel-plugin-import": "1.1.1", + "babel-plugin-recharts": "1.1.0", "babel-plugin-transform-runtime": "6.23.0", "babel-polyfill": "6.23.0", "babel-preset-es2015": "6.24.0", @@ -38,6 +39,7 @@ "react-redux": "5.0.3", "react-router": "3.0.2", "react-router-redux": "4.0.8", + "recharts": "0.22.0", "redux": "3.6.0", "redux-devtools": "3.3.2", "redux-thunk": "2.2.0",