fix fetching of watch items

This commit is contained in:
Aaron Bieber 2022-12-06 20:03:32 -07:00
parent 706275cc71
commit cb1dd3f69c
No known key found for this signature in database
3 changed files with 38 additions and 23 deletions

View File

@ -53,6 +53,16 @@ type PullRequestIgnore struct {
Repo string `json:"repo"` Repo string `json:"repo"`
} }
type Todo struct {
OwnerID int64 `json:"owner_id"`
LinkID int64 `json:"link_id"`
CreatedAt time.Time `json:"created_at"`
CompletedAt sql.NullTime `json:"completed_at"`
Completed bool `json:"completed"`
Title string `json:"title"`
Body string `json:"body"`
}
type WatchItem struct { type WatchItem struct {
ID int64 `json:"id"` ID int64 `json:"id"`
OwnerID int64 `json:"owner_id"` OwnerID int64 `json:"owner_id"`

13
main.go
View File

@ -57,7 +57,6 @@ func main() {
if err != nil { if err != nil {
log.Fatal("can't get ts local client: ", err) log.Fatal("can't get ts local client: ", err)
} }
app.watches = &WatchResults{}
if *dbFile == ":memory:" { if *dbFile == ":memory:" {
tmpDBPopulate(db) tmpDBPopulate(db)
@ -138,11 +137,15 @@ func main() {
} }
go func() { go func() {
err := app.watches.Update(ghToken) for {
if err != nil { var err error
log.Fatal("can't update watches: ", err) app.watches, err = UpdateWatches(ghToken)
if err != nil {
log.Fatal("can't update watches: ", err)
}
time.Sleep(5 * time.Minute)
} }
time.Sleep(5 * time.Minute)
}() }()
go func() { go func() {

38
page.go
View File

@ -100,9 +100,9 @@ type Page struct {
PullRequests []data.PullRequest PullRequests []data.PullRequest
Links []data.Link Links []data.Link
Node tailcfg.Node Node tailcfg.Node
Watches WatchResults
CurrentLimits *RateLimit CurrentLimits *RateLimit
Ignores []data.PullRequestIgnore Ignores []data.PullRequestIgnore
Watches *WatchResults
} }
func (p *Page) Sort() { func (p *Page) Sort() {
@ -112,27 +112,21 @@ func (p *Page) Sort() {
sort.Slice(p.PullRequests, func(i, j int) bool { sort.Slice(p.PullRequests, func(i, j int) bool {
return p.PullRequests[i].Number > p.PullRequests[j].Number return p.PullRequests[i].Number > p.PullRequests[j].Number
}) })
sort.Slice(p.Watches, func(i, j int) bool {
return p.Watches[i].Name < p.Watches[j].Name
})
for _, w := range p.Watches {
sort.Slice(w.Data.Search.Edges, func(i, j int) bool {
return w.Data.Search.Edges[i].Node.CreatedAt.After(w.Data.Search.Edges[j].Node.CreatedAt)
})
}
} }
type WatchResults []WatchResult type WatchResults []WatchResult
func (w *WatchResults) forID(ownerID int64) WatchResults { func (w *WatchResults) forID(ownerID int64) *WatchResults {
newResults := WatchResults{} newResults := WatchResults{}
for _, r := range *w { for _, r := range *w {
if r.OwnerID == ownerID { if r.OwnerID == ownerID {
newResults = append(newResults, r) newResults = append(newResults, r)
} }
} }
sort.Slice(newResults, func(i, j int) bool {
return newResults return newResults[i].Name < newResults[j].Name
})
return &newResults
} }
func (w WatchResults) GetLimits() *RateLimit { func (w WatchResults) GetLimits() *RateLimit {
@ -146,28 +140,36 @@ func (w WatchResults) GetLimits() *RateLimit {
return rl return rl
} }
func (w *WatchResults) Update(ghToken string) error { func UpdateWatches(ghToken string) (*WatchResults, error) {
ctx := context.Background() ctx := context.Background()
w := WatchResults{}
watches, err := app.queries.GetAllWatchItems(ctx) watches, err := app.queries.GetAllWatchItems(ctx)
if err != nil { if err != nil {
return err return nil, err
} }
for _, watch := range watches { for _, watch := range watches {
qd := GQLQuery{Query: fmt.Sprintf(graphQuery, watch.Repo, watch.Name)} qd := GQLQuery{Query: fmt.Sprintf(graphQuery, watch.Repo, watch.Name)}
wr, err := getData(qd, ghToken) wr, err := getData(qd, ghToken)
if err != nil { if err != nil {
return err return nil, err
} }
// TODO: cross ref the list of ignores and prune the wr accordingly
wr.OwnerID = watch.OwnerID wr.OwnerID = watch.OwnerID
wr.Name = watch.Name wr.Name = watch.Name
wr.Repo = watch.Repo wr.Repo = watch.Repo
*w = append(*w, *wr) sort.Slice(wr.Data.Search.Edges, func(i, j int) bool {
return wr.Data.Search.Edges[i].Node.CreatedAt.After(wr.Data.Search.Edges[j].Node.CreatedAt)
})
w = append(w, *wr)
} }
return nil sort.Slice(w, func(i, j int) bool {
return w[i].Name < w[j].Name
})
return &w, nil
} }
type WatchResult struct { type WatchResult struct {