add pageviews to graph

This commit is contained in:
Danny van Kooten 2016-11-23 20:30:09 +01:00
parent a7af934f23
commit 11ad2d5da4
4 changed files with 70 additions and 14 deletions

2
ana.go
View File

@ -30,7 +30,9 @@ func main() {
r.Handle("/api/visits/count/day", api.Authorize(api.GetVisitsDayCountHandler)).Methods("GET") r.Handle("/api/visits/count/day", api.Authorize(api.GetVisitsDayCountHandler)).Methods("GET")
r.Handle("/api/visits/count/realtime", api.Authorize(api.GetVisitsRealtimeCount)).Methods("GET") r.Handle("/api/visits/count/realtime", api.Authorize(api.GetVisitsRealtimeCount)).Methods("GET")
r.Handle("/api/visits", api.Authorize(api.GetVisitsHandler)).Methods("GET") r.Handle("/api/visits", api.Authorize(api.GetVisitsHandler)).Methods("GET")
r.Handle("/api/pageviews/count/day", api.Authorize(api.GetPageviewsDayCountHandler)).Methods("GET")
r.Handle("/api/pageviews", api.Authorize(api.GetPageviewsHandler)).Methods("GET") r.Handle("/api/pageviews", api.Authorize(api.GetPageviewsHandler)).Methods("GET")
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static/")))) r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
r.Handle("/", http.FileServer(http.Dir("./views/"))) r.Handle("/", http.FileServer(http.Dir("./views/")))

View File

@ -36,3 +36,37 @@ var GetPageviewsHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.R
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(results) json.NewEncoder(w).Encode(results)
}) })
// URL: /api/pageviews/count/day
var GetPageviewsDayCountHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
stmt, err := core.DB.Prepare(`SELECT
COUNT(*) AS count, DATE_FORMAT(timestamp, '%Y-%m-%d') AS date_group
FROM visits
GROUP BY date_group`)
checkError(err)
defer stmt.Close()
rows, err := stmt.Query()
checkError(err)
type Datapoint struct {
Count int
Label string
}
results := make([]Datapoint, 0)
defer rows.Close()
for rows.Next() {
v := Datapoint{}
err = rows.Scan(&v.Count, &v.Label);
checkError(err)
results = append(results, v)
}
err = rows.Err();
checkError(err)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(results)
})

View File

@ -63,7 +63,7 @@ var GetVisitsRealtimeCount = http.HandlerFunc(func(w http.ResponseWriter, r *htt
// URL: /api/visits/count/day // URL: /api/visits/count/day
var GetVisitsDayCountHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { var GetVisitsDayCountHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
stmt, err := core.DB.Prepare(`SELECT stmt, err := core.DB.Prepare(`SELECT
COUNT(*) AS count, DATE_FORMAT(timestamp, '%Y-%m-%d') AS date_group COUNT(DISTINCT(ip_address)) AS count, DATE_FORMAT(timestamp, '%Y-%m-%d') AS date_group
FROM visits FROM visits
GROUP BY date_group`) GROUP BY date_group`)
checkError(err) checkError(err)

View File

@ -3,28 +3,39 @@
import { h, render, Component } from 'preact'; import { h, render, Component } from 'preact';
import Chart from 'chart.js' import Chart from 'chart.js'
Chart.defaults.global.tooltips.xPadding = 10;
Chart.defaults.global.tooltips.yPadding = 10;
class Graph extends Component { class Graph extends Component {
constructor(props) { constructor(props) {
super(props) super(props)
this.state = { this.state = {
data: [] visitorData: [],
pageviewData: []
} }
this.fetchData = this.fetchData.bind(this); this.fetchData = this.fetchData.bind(this);
this.fetchData(); this.fetchData();
} }
initChart() { refreshChart() {
new Chart(this.ctx, { this.chart = new Chart(this.ctx, {
type: 'line', type: 'line',
data: { data: {
labels: this.state.data.map((d) => d.Label), labels: this.state.visitorData.map((d) => d.Label),
datasets: [{ datasets: [
{
label: '# of Visitors', label: '# of Visitors',
data: this.state.data.map((d) => d.Count), data: this.state.visitorData.map((d) => d.Count),
backgroundColor: 'rgba(0, 155, 255, .2)' backgroundColor: 'rgba(0, 0, 255, .2)'
}] },
{
label: '# of Pageviews',
data: this.state.pageviewData.map((d) => d.Count),
backgroundColor: 'rgba(0, 0, 125, .2)'
}
]
}, },
options: { options: {
scale: { scale: {
@ -37,13 +48,22 @@ class Graph extends Component {
} }
fetchData() { fetchData() {
return fetch('/api/visits/count/day', { // fetch visitor data
fetch('/api/visits/count/day', {
credentials: 'include' credentials: 'include'
}) }).then((r) => r.json())
.then((r) => r.json())
.then((data) => { .then((data) => {
this.setState({ data: data}) this.setState({ visitorData: data})
this.initChart() this.refreshChart();
});
// fetch pageview data
fetch('/api/pageviews/count/day', {
credentials: 'include'
}).then((r) => r.json())
.then((data) => {
this.setState({ pageviewData: data})
this.refreshChart();
}); });
} }