Basic timezone support.

Digests are generated for day boundaries in the given timezone, and timestamps
that are displayed are in that timezone too. No UI for actually specifying a
timezone.
This commit is contained in:
Mihai Parparita 2014-08-03 23:00:48 -07:00
parent b76c164d45
commit fe11e2a97a
4 changed files with 46 additions and 20 deletions

View file

@ -3,6 +3,7 @@ package githop
import ( import (
"bytes" "bytes"
"encoding/gob" "encoding/gob"
"time"
"appengine" "appengine"
"appengine/datastore" "appengine/datastore"
@ -15,7 +16,9 @@ type Account struct {
// The datastore API doesn't store maps, and the token contains one. We // The datastore API doesn't store maps, and the token contains one. We
// thefore store a gob-serialized version instead. // thefore store a gob-serialized version instead.
OAuthTokenSerialized []byte OAuthTokenSerialized []byte
OAuthToken oauth.Token `datastore:"-,"` OAuthToken oauth.Token `datastore:"-,"`
TimezoneName string `datastore:",noindex"`
TimezoneLocation *time.Location `datastore:"-,"`
} }
func getAccount(c appengine.Context, gitHubUserId int) (*Account, error) { func getAccount(c appengine.Context, gitHubUserId int) (*Account, error) {
@ -25,9 +28,22 @@ func getAccount(c appengine.Context, gitHubUserId int) (*Account, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
r := bytes.NewBuffer(account.OAuthTokenSerialized) r := bytes.NewBuffer(account.OAuthTokenSerialized)
err = gob.NewDecoder(r).Decode(&account.OAuthToken) err = gob.NewDecoder(r).Decode(&account.OAuthToken)
return account, err if err != nil {
return nil, err
}
if len(account.TimezoneName) == 0 {
account.TimezoneName = "America/Los_Angeles"
}
account.TimezoneLocation, err = time.LoadLocation(account.TimezoneName)
if err != nil {
return nil, err
}
return account, nil
} }
func getAllAccounts(c appengine.Context, accounts *[]Account) error { func getAllAccounts(c appengine.Context, accounts *[]Account) error {

View file

@ -14,11 +14,11 @@ type DigestCommit struct {
URL string URL string
Title string Title string
Message string Message string
Date *time.Time Date time.Time
RepositoryCommit *github.RepositoryCommit RepositoryCommit *github.RepositoryCommit
} }
func newDigestCommit(commit *github.RepositoryCommit, repo *github.Repository) DigestCommit { func newDigestCommit(commit *github.RepositoryCommit, repo *github.Repository, location *time.Location) DigestCommit {
messagePieces := strings.SplitN(*commit.Commit.Message, "\n", 2) messagePieces := strings.SplitN(*commit.Commit.Message, "\n", 2)
title := messagePieces[0] title := messagePieces[0]
message := "" message := ""
@ -30,11 +30,15 @@ func newDigestCommit(commit *github.RepositoryCommit, repo *github.Repository) D
URL: fmt.Sprintf("https://github.com/%s/commit/%s", *repo.FullName, *commit.SHA), URL: fmt.Sprintf("https://github.com/%s/commit/%s", *repo.FullName, *commit.SHA),
Title: title, Title: title,
Message: message, Message: message,
Date: commit.Commit.Author.Date, Date: commit.Commit.Author.Date.In(location),
RepositoryCommit: commit, RepositoryCommit: commit,
} }
} }
func (commit DigestCommit) DisplayDate() string {
return commit.Date.Format("3:04pm")
}
type RepoDigest struct { type RepoDigest struct {
Repo *github.Repository Repo *github.Repository
Commits []DigestCommit Commits []DigestCommit
@ -48,13 +52,14 @@ func (a ByRepoFullName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByRepoFullName) Less(i, j int) bool { return *a[i].Repo.FullName < *a[j].Repo.FullName } func (a ByRepoFullName) Less(i, j int) bool { return *a[i].Repo.FullName < *a[j].Repo.FullName }
type Digest struct { type Digest struct {
User *github.User User *github.User
StartTime time.Time StartTime time.Time
EndTime time.Time EndTime time.Time
RepoDigests []*RepoDigest TimezoneLocation *time.Location
RepoDigests []*RepoDigest
} }
func newDigest(githubClient *github.Client) (*Digest, error) { func newDigest(githubClient *github.Client, account *Account) (*Digest, error) {
user, _, err := githubClient.Users.Get("") user, _, err := githubClient.Users.Get("")
if err != nil { if err != nil {
return nil, err return nil, err
@ -82,7 +87,7 @@ func newDigest(githubClient *github.Client) (*Digest, error) {
repos = newRepos repos = newRepos
} }
now := time.Now() now := time.Now().In(account.TimezoneLocation)
digestStartTime := time.Date(now.Year()-1, now.Month(), now.Day(), 0, 0, 0, 0, now.Location()) digestStartTime := time.Date(now.Year()-1, now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
digestEndTime := digestStartTime.AddDate(0, 0, 1) digestEndTime := digestStartTime.AddDate(0, 0, 1)
@ -95,10 +100,11 @@ func newDigest(githubClient *github.Client) (*Digest, error) {
} }
repos = digestRepos repos = digestRepos
digest := &Digest{ digest := &Digest{
User: user, User: user,
RepoDigests: make([]*RepoDigest, 0, len(repos)), RepoDigests: make([]*RepoDigest, 0, len(repos)),
StartTime: digestStartTime, StartTime: digestStartTime,
EndTime: digestEndTime, EndTime: digestEndTime,
TimezoneLocation: account.TimezoneLocation,
} }
err = digest.fetch(repos, githubClient) err = digest.fetch(repos, githubClient)
return digest, err return digest, err
@ -125,7 +131,7 @@ func (digest *Digest) fetch(repos []github.Repository, githubClient *github.Clie
} else { } else {
digestCommits := make([]DigestCommit, 0, len(commits)) digestCommits := make([]DigestCommit, 0, len(commits))
for i, _ := range commits { for i, _ := range commits {
digestCommits = append(digestCommits, newDigestCommit(&commits[i], &repo)) digestCommits = append(digestCommits, newDigestCommit(&commits[i], &repo, digest.TimezoneLocation))
} }
ch <- &RepoDigestResponse{&RepoDigest{&repo, digestCommits}, nil} ch <- &RepoDigestResponse{&RepoDigest{&repo, digestCommits}, nil}
} }
@ -145,3 +151,7 @@ func (digest *Digest) fetch(repos []github.Repository, githubClient *github.Clie
sort.Sort(ByRepoFullName(digest.RepoDigests)) sort.Sort(ByRepoFullName(digest.RepoDigests))
return nil return nil
} }
func (digest *Digest) DisplayDate() string {
return digest.StartTime.Format("January 2, 2006 was a Monday")
}

View file

@ -102,7 +102,7 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
oauthTransport.Token = &account.OAuthToken oauthTransport.Token = &account.OAuthToken
githubClient := github.NewClient(oauthTransport.Client()) githubClient := github.NewClient(oauthTransport.Client())
digest, err := newDigest(githubClient) digest, err := newDigest(githubClient, account)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
} }
@ -159,7 +159,7 @@ func sendDigestForAccount(account *Account, c appengine.Context) error {
oauthTransport.Token = &account.OAuthToken oauthTransport.Token = &account.OAuthToken
githubClient := github.NewClient(oauthTransport.Client()) githubClient := github.NewClient(oauthTransport.Client())
digest, err := newDigest(githubClient) digest, err := newDigest(githubClient, account)
if err != nil { if err != nil {
return err return err
} }

View file

@ -2,7 +2,7 @@
<dl> <dl>
<p>Activity a year ago for <a href="https://github.com/{{.User.Login}}" title={{.User.Name}}><img src={{.User.AvatarURL}} width="20" height="20" border="0">{{.User.Login}}</a>.</p> <p>{{.DisplayDate}}. Here's the GitHub activity on that day for <a href="https://github.com/{{.User.Login}}" title={{.User.Name}}><img src={{.User.AvatarURL}} width="20" height="20" border="0">{{.User.Login}}</a>.</p>
{{range $index, $repoDigest := .RepoDigests}} {{range $index, $repoDigest := .RepoDigests}}
<h2> <h2>
@ -20,7 +20,7 @@
{{end}} {{end}}
<div> <div>
<a href="{{.URL}}">{{.DisplaySHA}}</a> <a href="{{.URL}}">{{.DisplaySHA}}</a>
<i>{{.Date}}</i> <i>{{.DisplayDate}}</i>
</div> </div>
</div> </div>
{{end}} {{end}}