Skip to content

Commit

Permalink
feat: Introduce demo mode, suitable for screenshot
Browse files Browse the repository at this point in the history
Let's also update the screenshot, to include today's changes.

The mocked storage was conceived because of the auto update GUI. At
least it works, but it was a pain to discover after carefully trying to
mock the data with Firefox' dev tools.
  • Loading branch information
chelmertz committed Sep 25, 2024
1 parent dcb9e12 commit 913748f
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 12 deletions.
Binary file modified gui.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type IndexHtmlData struct {
//go:embed index.html
var index embed.FS

func ServeWeb(url, username string, goldenTestingEnabled bool, store *storage.Storage, refreshingChannel chan types.RefreshAction, timeoutMinutes int, version string, logger *slog.Logger) {
func ServeWeb(url, username string, goldenTestingEnabled bool, store storage.Storage, refreshingChannel chan types.RefreshAction, timeoutMinutes int, version string, logger *slog.Logger) {
temp, err := template.ParseFS(index, "index.html")
check(err)

Expand Down
26 changes: 18 additions & 8 deletions internal/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,21 @@ func check(err error) {
}
}

type Storage struct {
type Storage interface {
Prs() StoredState
StoreRepoPrs(orderedPrs []types.ViewPr) error
Bury(prUrl string) error
Unbury(prUrl string) error
GetPr(prUrl string) (Pr, error)
}

type DbStorage struct {
db *Queries
logger *slog.Logger
}

var _ Storage = (*DbStorage)(nil)

type StoredState struct {
Prs []types.ViewPr
LastFetched time.Time
Expand All @@ -35,7 +45,7 @@ type StoredState struct {
//go:embed schema.sql
var ddl string

func NewStorage(logger *slog.Logger) *Storage {
func NewStorage(logger *slog.Logger) *DbStorage {
dirname, err := os.UserCacheDir()
check(err)
ourCacheDir := filepath.Join(dirname, "elly")
Expand All @@ -56,13 +66,13 @@ func NewStorage(logger *slog.Logger) *Storage {
check(err)
}

return &Storage{
return &DbStorage{
db: New(db),
logger: logger,
}
}

func (s *Storage) Prs() StoredState {
func (s *DbStorage) Prs() StoredState {
dbPrs, err := s.db.ListPrs(context.Background())
check(err)
prs := make([]types.ViewPr, 0)
Expand Down Expand Up @@ -101,7 +111,7 @@ func (s *Storage) Prs() StoredState {
return state
}

func (s *Storage) StoreRepoPrs(orderedPrs []types.ViewPr) error {
func (s *DbStorage) StoreRepoPrs(orderedPrs []types.ViewPr) error {
s.logger.Info("storing prs", slog.Int("prs", len(orderedPrs)))

buriedPrs, err := s.db.BuriedPrs(context.Background())
Expand Down Expand Up @@ -154,14 +164,14 @@ func (s *Storage) StoreRepoPrs(orderedPrs []types.ViewPr) error {
return nil
}

func (s *Storage) Bury(prUrl string) error {
func (s *DbStorage) Bury(prUrl string) error {
return s.db.Bury(context.Background(), prUrl)
}

func (s *Storage) Unbury(prUrl string) error {
func (s *DbStorage) Unbury(prUrl string) error {
return s.db.Unbury(context.Background(), prUrl)
}

func (s *Storage) GetPr(prUrl string) (Pr, error) {
func (s *DbStorage) GetPr(prUrl string) (Pr, error) {
return s.db.GetPr(context.Background(), prUrl)
}
104 changes: 104 additions & 0 deletions internal/storage/storage_demo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package storage

import (
_ "embed"
"time"

"github.com/chelmertz/elly/internal/types"
_ "github.com/mattn/go-sqlite3"
)

type StorageDemo struct{}

var _ Storage = (*StorageDemo)(nil)

func NewStorageDemo() *StorageDemo {
return &StorageDemo{}
}

func (s *StorageDemo) Prs() StoredState {
prs := make([]types.ViewPr, 0)
lastUpdated := time.Now().UTC()

pr1 := types.ViewPr{
Url: "1", // points is calculated based on PR URL, must be unique
ReviewStatus: "",
Title: "feat: Scaffolding script for a new service",
Author: "chelmertz",
RepoName: "api",
RepoOwner: "chelmertz",
RepoUrl: "",
IsDraft: false,
LastUpdated: lastUpdated,
LastPrCommenter: "",
ThreadsActionable: 3,
ThreadsWaiting: 2,
Additions: 32,
Deletions: 15,
ReviewRequestedFromUsers: []string{},
Buried: false,
}

pr2 := types.ViewPr{
Url: "2",
ReviewStatus: "",
Title: "chore: update license",
Author: "channy2011",
RepoName: "infrastructure",
RepoOwner: "chelmertz",
RepoUrl: "",
IsDraft: true,
LastUpdated: lastUpdated,
LastPrCommenter: "",
ThreadsActionable: 0,
ThreadsWaiting: 0,
Additions: 32,
Deletions: 15,
ReviewRequestedFromUsers: []string{},
Buried: false,
}

pr3 := types.ViewPr{
Url: "3",
ReviewStatus: "APPROVED",
Title: "feature: add settings for maximum minutes of idling",
Author: "bierden22",
RepoName: "web",
RepoOwner: "chelmertz",
RepoUrl: "",
IsDraft: true,
LastUpdated: lastUpdated,
LastPrCommenter: "",
ThreadsActionable: 0,
ThreadsWaiting: 0,
Additions: 32,
Deletions: 15,
ReviewRequestedFromUsers: []string{},
Buried: false,
}

prs = append(prs, pr1, pr2, pr3)

state := StoredState{
Prs: prs,
LastFetched: time.Now().UTC(),
}

return state
}

func (s *StorageDemo) StoreRepoPrs(orderedPrs []types.ViewPr) error {
return nil
}

func (s *StorageDemo) Bury(prUrl string) error {
return nil
}

func (s *StorageDemo) Unbury(prUrl string) error {
return nil
}

func (s *StorageDemo) GetPr(prUrl string) (Pr, error) {
return Pr{}, nil
}
11 changes: 8 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
var timeoutMinutes = flag.Int("timeout", 5, "refresh PRs every N minutes")
var url = flag.String("url", "localhost:9876", "URL for web GUI")
var golden = flag.Bool("golden", false, "provide a button for turning a PR into a test. do NOT use outside of development")
var demo = flag.Bool("demo", false, "mock the PRs so you can take a proper screenshot of the GUI")
var versionFlag = flag.Bool("version", false, "show version")
var logger = slog.New(slog.NewTextHandler(os.Stdout, nil))

Expand Down Expand Up @@ -46,22 +47,26 @@ func main() {
}
os.Unsetenv("GITHUB_PAT")

store := storage.NewStorage(logger)
var store storage.Storage = storage.NewStorage(logger)
username, err := github.UsernameFromPat(token, logger)
if err != nil {
logger.Error("could not get username from PAT", "error", err)
os.Exit(1)
}

logger.Info("starting elly", "version", version, "timeout_minutes", *timeoutMinutes, "github_user", username, "golden_testing_enabled", *golden)
logger.Info("starting elly", "version", version, "timeout_minutes", *timeoutMinutes, "github_user", username, "golden_testing_enabled", *golden, "demo", *demo)

if *demo {
store = storage.NewStorageDemo()
}

refreshChannel := make(chan types.RefreshAction, 1)
go startRefreshLoop(token, username, store, refreshChannel)
refreshChannel <- types.RefreshUpstart
server.ServeWeb(*url, username, *golden, store, refreshChannel, *timeoutMinutes, version, logger)
}

func startRefreshLoop(token, username string, store *storage.Storage, refresh chan types.RefreshAction) {
func startRefreshLoop(token, username string, store storage.Storage, refresh chan types.RefreshAction) {
refreshTimer := time.NewTicker(time.Duration(*timeoutMinutes) * time.Minute)
retriesLeft := 5

Expand Down

0 comments on commit 913748f

Please sign in to comment.