forked from shen100/wemall
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
刘珅
committed
Apr 8, 2017
1 parent
a7ad803
commit 4cc5d60
Showing
5 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
{ | ||
"plugins": [ | ||
"transform-runtime", | ||
"recharts", | ||
["import", { libraryName: "antd", style: "css" }] | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters