2018-06-04 00:05:44 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
func domainStatistics(domain string) ([]int64, error) {
|
|
|
|
statement := `
|
|
|
|
SELECT COUNT(views.viewDate)
|
|
|
|
FROM (
|
|
|
|
SELECT to_char(date_trunc('day', (current_date - offs)), 'YYYY-MM-DD') AS date
|
|
|
|
FROM generate_series(0, 30, 1) AS offs
|
|
|
|
) gen LEFT OUTER JOIN views
|
|
|
|
ON gen.date = to_char(date_trunc('day', views.viewDate), 'YYYY-MM-DD') AND
|
|
|
|
views.domain=$1
|
|
|
|
GROUP BY gen.date
|
|
|
|
ORDER BY gen.date;
|
|
|
|
`
|
|
|
|
rows, err := db.Query(statement, domain)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("cannot get daily views: %v", err)
|
|
|
|
return []int64{}, errorInternal
|
|
|
|
}
|
|
|
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
last30Days := []int64{}
|
|
|
|
for rows.Next() {
|
|
|
|
var count int64
|
|
|
|
if err = rows.Scan(&count); err != nil {
|
|
|
|
logger.Errorf("cannot get daily views for the last month: %v", err)
|
|
|
|
return []int64{}, errorInternal
|
|
|
|
}
|
|
|
|
last30Days = append(last30Days, count)
|
|
|
|
}
|
|
|
|
|
|
|
|
return last30Days, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func domainStatisticsHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
type request struct {
|
2018-06-20 11:29:55 +08:00
|
|
|
OwnerToken *string `json:"ownerToken"`
|
2018-06-20 11:50:11 +08:00
|
|
|
Domain *string `json:"domain"`
|
2018-06-04 00:05:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var x request
|
2018-07-24 14:58:43 +08:00
|
|
|
if err := bodyUnmarshal(r, &x); err != nil {
|
|
|
|
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
2018-06-04 00:05:44 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-20 11:29:55 +08:00
|
|
|
o, err := ownerGetByOwnerToken(*x.OwnerToken)
|
2018-06-04 00:05:44 +08:00
|
|
|
if err != nil {
|
2018-07-24 14:58:43 +08:00
|
|
|
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
2018-06-04 00:05:44 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
domain := stripDomain(*x.Domain)
|
|
|
|
isOwner, err := domainOwnershipVerify(o.OwnerHex, domain)
|
|
|
|
if err != nil {
|
2018-07-24 14:58:43 +08:00
|
|
|
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
2018-06-04 00:05:44 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isOwner {
|
2018-07-24 14:58:43 +08:00
|
|
|
bodyMarshal(w, response{"success": false, "message": errorNotAuthorised.Error()})
|
2018-06-04 00:05:44 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
viewsLast30Days, err := domainStatistics(domain)
|
|
|
|
if err != nil {
|
2018-07-24 14:58:43 +08:00
|
|
|
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
2018-06-04 00:05:44 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
commentsLast30Days, err := commentStatistics(domain)
|
|
|
|
if err != nil {
|
2018-07-24 14:58:43 +08:00
|
|
|
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
2018-06-04 00:05:44 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-24 14:58:43 +08:00
|
|
|
bodyMarshal(w, response{"success": true, "viewsLast30Days": viewsLast30Days, "commentsLast30Days": commentsLast30Days})
|
2018-06-04 00:05:44 +08:00
|
|
|
}
|