This repository was archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatastore.go
More file actions
145 lines (132 loc) · 4.15 KB
/
Copy pathdatastore.go
File metadata and controls
145 lines (132 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package swgohapi
import (
"encoding/json"
"fmt"
"time"
"golang.org/x/net/context"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/log"
"google.golang.org/appengine/memcache"
)
// PlayerDataKind is the Datastore Kind used to save the profile cache.
const PlayerDataKind = "ProfileCache"
// PlayerData is a datastore structure that caches the player
// data. LastUpdate will match the last time at which website
// parsed the data (fetched from the profile), and is indexed
// for use in the periodic reload.
type PlayerData struct {
Key *datastore.Key `datastore:"-"`
LastUpdate time.Time
Data []byte
}
// Decode returns the Profile from the PlayerData.Data attribute.
// Returns an error if player data is an invalid JSON.
func (p *PlayerData) Decode() (*Profile, error) {
if p == nil {
return nil, fmt.Errorf("swgohapi: nil player data")
}
var profile Profile
err := json.Unmarshal(p.Data, &profile)
if err != nil {
return nil, err
}
return &profile, nil
}
// Encode updates the PlayerData.Data attribute with a valid content.
func (p *PlayerData) Encode(profile *Profile) error {
b, err := json.Marshal(profile)
if err != nil {
return err
}
p.Data = b
p.LastUpdate = profile.LastUpdate
return nil
}
// Expired returns true if the profile has expired.
// Profile is assumed to be expired if the time since
// the last update is larger than 24 hours.
func (p *PlayerData) Expired() bool {
return time.Since(p.LastUpdate) >= 24*time.Hour
}
// GetPlayerData fetches a PlayerData structure from the Cloud Datastore.
// Return both nil data and nil error if no value is cached.
func GetPlayerData(c context.Context, player string) (playerData *PlayerData, err error) {
playerData = &PlayerData{}
// Let's try from memcache first
_, err = memcache.JSON.Get(c, player, playerData)
// If an error, fetch from datastore. Otherwise it is filled in playerData.
if err != nil {
log.Debugf(c, "Not found on memcache, fetching from datastore (err=%v)", err)
key := datastore.NewKey(c, PlayerDataKind, player, 0, nil)
err = datastore.Get(c, key, playerData)
if err == nil {
if err := memcache.JSON.Set(c, &memcache.Item{Key: player, Object: playerData}); err != nil {
log.Errorf(c, "Unable to save to cache: %v", err)
}
}
}
return playerData, err
}
// SavePlayerData updates the player data into Datastore.
func SavePlayerData(c context.Context, player string, playerData *PlayerData) (err error) {
if playerData == nil {
return fmt.Errorf("swgohapi: error saving: nil player data")
}
key := datastore.NewKey(c, PlayerDataKind, player, 0, nil)
_, err = datastore.Put(c, key, playerData)
if err == nil {
if err := memcache.JSON.Set(c, &memcache.Item{Key: player, Object: playerData}); err != nil {
log.Errorf(c, "Unable to save to cache: %v", err)
}
}
return err
}
// PlayerStats represents system-wide player profile statistics
type PlayerStats struct {
PlayerCount int
StalePlayerCount int
OldestPlayerSync time.Time `datastore:"LastUpdate"`
}
// GetPlayerStats returns basic statistics about player profiles in the system.
func GetPlayerStats(c context.Context) (s PlayerStats, err error) {
s = PlayerStats{}
// Query how many player profiles we have in database.
s.PlayerCount, err = datastore.NewQuery(PlayerDataKind).
Count(c)
if err != nil {
return
}
// Query how many stale profiles we have currently.
s.StalePlayerCount, err = datastore.NewQuery(PlayerDataKind).
Filter("LastUpdate <= ", time.Now().AddDate(0, 0, -1)).
Count(c)
if err != nil {
return
}
// Query the oldest data we have.
_, err = datastore.NewQuery(PlayerDataKind).
Order("LastUpdate").
Project("LastUpdate").
Limit(1).
Run(c).
Next(&s)
if err == datastore.Done {
err = nil
}
return
}
// ListStalePlayers returns a list of the oldest 100 profiles that needs to be sync'ed
func ListStalePlayers(c context.Context) (profiles []PlayerData, err error) {
q := datastore.NewQuery(PlayerDataKind).
Filter("LastUpdate <= ", time.Now().AddDate(0, 0, -1)).
Order("LastUpdate").
Limit(100)
keys, err := q.GetAll(c, &profiles)
if err != nil {
return nil, err
}
for i := range keys {
profiles[i].Key = keys[i]
}
return
}