Skip to content
This repository was archived by the owner on Feb 15, 2022. It is now read-only.

Commit

Permalink
adds stats page
Browse files Browse the repository at this point in the history
close #6
  • Loading branch information
maxhille committed Feb 1, 2017
1 parent 02460b2 commit fe019d5
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 14 deletions.
32 changes: 30 additions & 2 deletions backend/app.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,59 @@
package backend

import (
"encoding/json"
"net/http"
"time"

"google.golang.org/appengine"
"google.golang.org/appengine/log"
)

func init() {
http.HandleFunc("/poll", pollFunc)
http.HandleFunc("/update-stats", updateStatsFunc)
http.HandleFunc("/stats", statsFunc)
}

func statsFunc(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)

db := &Datastore{ctx}
logger := &AELogger{ctx}

be := &Backend{db, nil, logger, nil}

s, err := be.latestStats()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}

b, err := json.Marshal(s)
if err != nil {
log.Errorf(ctx, "cannot marshal %v", s)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Write(b)
}

func updateStatsFunc(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)

db := &Datastore{ctx}
provider := &Blockcypher{ctx}
logger := &AELogger{ctx}
reactor := &AEReactor{ctx}

b := &Backend{db, provider, logger, reactor}

err := b.stats(time.Now())
err := b.updateStats(time.Now())
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("err " + err.Error()))
w.Write([]byte(err.Error()))
}
}

Expand Down
2 changes: 1 addition & 1 deletion backend/app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ handlers:
secure: always
login: admin

- url: /stats
- url: /update-stats
script: _go_app
secure: always
login: admin
Expand Down
8 changes: 6 additions & 2 deletions backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ type Backend struct {

var day = time.Hour * 24

func (be *Backend) stats(ts time.Time) error {
func (be *Backend) latestStats() (Stats, error) {
return be.LatestStats()
}

func (be *Backend) updateStats(ts time.Time) error {
s := Stats{Timestamp: ts, Votes: make(map[string]Vote)}

d30ts := ts.Add(-day * 30)
Expand Down Expand Up @@ -122,7 +126,7 @@ func makeVote(cs []int, total []int) Vote {
func (be *Backend) poll() error {
// determine next block height
var h int
lb, err := be.Latest()
lb, err := be.LatestBlock()
switch err {
case ErrNoBlocks:
// height from config
Expand Down
8 changes: 6 additions & 2 deletions backend/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ func (db *TestDB) GetConfig() (Config, error) {
return Config{}, nil
}

func (db *TestDB) Latest() (Block, error) {
func (db *TestDB) LatestBlock() (Block, error) {
return Block{}, nil
}

func (db *TestDB) LatestStats() (Stats, error) {
return Stats{}, nil
}

func (db *TestDB) SaveBlock(Block) error {
return nil
}
Expand Down Expand Up @@ -89,7 +93,7 @@ func Test_Stats(t *testing.T) {
db := &TestDB{blocks: []Block{b1, b2, b3, b4}}

be := &Backend{db, nil, &TestLogger{}, &TestReactor{}}
err := be.stats(ts)
err := be.updateStats(ts)
if err != nil {
t.Fatalf("stats failed: %v", err)
}
Expand Down
47 changes: 43 additions & 4 deletions backend/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
// Errors
var (
ErrNoBlocks = errors.New("no blocks available")
ErrNoStats = errors.New("no stats available")
)

// Datastore implements DB via Google Cloud Datastore
Expand All @@ -27,8 +28,11 @@ type DB interface {
// GetConfig gets the current config from the database
GetConfig() (Config, error)

// Latest returns the latest block (by height) in the database
Latest() (Block, error)
// LatestBlock returns the latest block (by height) in the database
LatestBlock() (Block, error)

// LatestStats returns the latest stats in the database
LatestStats() (Stats, error)

// SaveStats saves the given stats into the db
SaveStats(Stats) error
Expand Down Expand Up @@ -85,8 +89,8 @@ func (ds *Datastore) GetConfig() (c Config, err error) {
return c, err
}

// Latest returns the latest block (by height) in the database
func (ds *Datastore) Latest() (b Block, err error) {
// LatestBlock returns the latest block (by height) in the database
func (ds *Datastore) LatestBlock() (b Block, err error) {
// run query
bs := []Block{}
_, err = datastore.NewQuery("Block").
Expand All @@ -106,6 +110,41 @@ func (ds *Datastore) Latest() (b Block, err error) {
return
}

// LatestStats returns the latest stats in the database
func (ds *Datastore) LatestStats() (Stats, error) {
ss := []Stats{}

ks, err := datastore.NewQuery("Stats").
Limit(1).
Order("-Timestamp").
GetAll(ds.ctx, &ss)
if err != nil {
return Stats{}, err
}

if len(ss) == 0 {
return Stats{}, ErrNoStats
}

s := ss[0]
k := ks[0]

var vs []Vote
vks, err := datastore.NewQuery("Vote").
Ancestor(k).
GetAll(ds.ctx, &vs)
if err != nil {
return Stats{}, err
}

s.Votes = make(map[string]Vote)
for i, v := range vs {
s.Votes[vks[i].StringID()] = v
}

return s, nil
}

// ForEachFrom calls the given method for each saves Block starting from the
// given Timestamp
func (ds *Datastore) ForEachFrom(ts time.Time, f func(Block)) error {
Expand Down
4 changes: 2 additions & 2 deletions backend/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func Test_Latest_OK(t *testing.T) {
db.SaveBlock(b1)
db.SaveBlock(b2)

b, err := db.Latest()
b, err := db.LatestBlock()

if err != nil {
t.Fatal(err)
Expand All @@ -107,7 +107,7 @@ func Test_Latest_ErrNoBlocks(t *testing.T) {

db := Datastore{ctx}

_, err := db.Latest()
_, err := db.LatestBlock()

if err != ErrNoBlocks {
t.Fatal("should have no blocks error")
Expand Down
2 changes: 1 addition & 1 deletion backend/reactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type AEReactor struct {

// TriggerUpdateStats triggers a stats update
func (r *AEReactor) TriggerUpdateStats() error {
t := taskqueue.NewPOSTTask("/stats", nil)
t := taskqueue.NewPOSTTask("/update-stats", nil)
_, err := taskqueue.Add(r.ctx, t, "")
return err
}

0 comments on commit fe019d5

Please sign in to comment.