Skip to content

Commit

Permalink
feat(notifier): Adding user count in reported metrics
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Jun 10, 2021
1 parent b0d043c commit a5ea76a
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/ketchup/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func main() {
publicRendererApp, err := renderer.New(rendererConfig, content, ketchup.FuncMap)
logger.Fatal(err)

notifierApp := notifier.New(notifierConfig, repositoryServiceApp, ketchupServiceApp, mailerApp, helmApp)
notifierApp := notifier.New(notifierConfig, repositoryServiceApp, ketchupServiceApp, userServiceApp, mailerApp, helmApp)
schedulerApp := scheduler.New(schedulerConfig, notifierApp, redisApp)
ketchupApp := ketchup.New(publicRendererApp, ketchupServiceApp, userServiceApp, repositoryServiceApp, redisApp)

Expand Down
5 changes: 4 additions & 1 deletion cmd/notifier/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ import (
"github.com/ViBiOh/ketchup/pkg/provider/pypi"
ketchupService "github.com/ViBiOh/ketchup/pkg/service/ketchup"
repositoryService "github.com/ViBiOh/ketchup/pkg/service/repository"
userService "github.com/ViBiOh/ketchup/pkg/service/user"
ketchupStore "github.com/ViBiOh/ketchup/pkg/store/ketchup"
repositoryStore "github.com/ViBiOh/ketchup/pkg/store/repository"
userStore "github.com/ViBiOh/ketchup/pkg/store/user"
mailer "github.com/ViBiOh/mailer/pkg/client"
)

Expand Down Expand Up @@ -53,8 +55,9 @@ func main() {
pypiApp := pypi.New()
repositoryServiceApp := repositoryService.New(repositoryStore.New(ketchupDb), github.New(githubConfig, nil), helmApp, docker.New(dockerConfig), npmApp, pypiApp)
ketchupServiceApp := ketchupService.New(ketchupStore.New(ketchupDb), repositoryServiceApp)
userServiceApp := userService.New(userStore.New(ketchupDb), nil)

notifierApp := notifier.New(notifierConfig, repositoryServiceApp, ketchupServiceApp, mailerApp, helmApp)
notifierApp := notifier.New(notifierConfig, repositoryServiceApp, ketchupServiceApp, userServiceApp, mailerApp, helmApp)

logger.Info("Starting notifier...")

Expand Down
12 changes: 11 additions & 1 deletion pkg/notifier/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/ViBiOh/ketchup/pkg/semver"
"github.com/ViBiOh/ketchup/pkg/service/ketchup"
"github.com/ViBiOh/ketchup/pkg/service/repository"
"github.com/ViBiOh/ketchup/pkg/service/user"
mailer "github.com/ViBiOh/mailer/pkg/client"
mailerModel "github.com/ViBiOh/mailer/pkg/model"
"github.com/prometheus/client_golang/prometheus/push"
Expand All @@ -39,6 +40,7 @@ type Config struct {
type app struct {
repositoryService repository.App
ketchupService ketchup.App
userService user.App
mailerApp mailer.App
helmApp helm.App

Expand All @@ -57,13 +59,14 @@ func Flags(fs *flag.FlagSet, prefix string) Config {
}

// New creates new App from Config
func New(config Config, repositoryService repository.App, ketchupService ketchup.App, mailerApp mailer.App, helmApp helm.App) App {
func New(config Config, repositoryService repository.App, ketchupService ketchup.App, userService user.App, mailerApp mailer.App, helmApp helm.App) App {
return app{
loginID: uint64(*config.loginID),
pushURL: strings.TrimSpace(*config.pushURL),

repositoryService: repositoryService,
ketchupService: ketchupService,
userService: userService,
mailerApp: mailerApp,
helmApp: helmApp,
}
Expand Down Expand Up @@ -98,6 +101,13 @@ func (a app) Notify(ctx context.Context) error {
if len(a.pushURL) != 0 {
registry, metrics := configurePrometheus()

userCount, err := a.userService.Count(ctx)
if err != nil {
logger.Error("unable to get users count: %s", err)
} else {
metrics.WithLabelValues("users").Set(float64(userCount))
}

metrics.WithLabelValues("repositories").Set(float64(repoCount))
metrics.WithLabelValues("releases").Set(float64(len(newReleases)))
metrics.WithLabelValues("notifications").Set(float64(len(ketchupsToNotify)))
Expand Down
5 changes: 5 additions & 0 deletions pkg/service/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
type App interface {
Create(ctx context.Context, o model.User) (model.User, error)
StoreInContext(ctx context.Context) context.Context
Count(ctx context.Context) (uint64, error)
}

type app struct {
Expand Down Expand Up @@ -103,3 +104,7 @@ func (a app) check(ctx context.Context, _, new model.User) error {

return httpModel.ConcatError(output)
}

func (a app) Count(ctx context.Context) (uint64, error) {
return a.Count(ctx)
}
4 changes: 4 additions & 0 deletions pkg/service/user/usertest/usertest.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ func (a app) Create(_ context.Context, _ model.User) (model.User, error) {
func (a app) StoreInContext(ctx context.Context) context.Context {
return model.StoreUser(ctx, model.NewUser(0, "nobody@localhost", authModel.NewUser(0, "")))
}

func (a app) Count(ctx context.Context) (uint64, error) {
return 2, nil
}
16 changes: 16 additions & 0 deletions pkg/store/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type App interface {
GetByEmail(ctx context.Context, email string) (model.User, error)
GetByLoginID(ctx context.Context, loginID uint64) (model.User, error)
Create(ctx context.Context, o model.User) (uint64, error)
Count(ctx context.Context) (uint64, error)
}

type app struct {
Expand Down Expand Up @@ -103,3 +104,18 @@ INSERT INTO
func (a app) Create(ctx context.Context, o model.User) (uint64, error) {
return db.Create(ctx, insertQuery, strings.ToLower(o.Email), o.Login.ID)
}

const countQuery = `
SELECT
COUNT(1)
FROM
ketchup.user
`

func (a app) Count(ctx context.Context) (uint64, error) {
var count uint64

return count, db.Get(ctx, a.db, func(row *sql.Row) error {
return row.Scan(&count)
}, countQuery)
}
16 changes: 16 additions & 0 deletions pkg/store/user/usertest/usertest.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ type App struct {

createID uint64
createErr error

count uint64
countErr error
}

// New creates raw mock
Expand Down Expand Up @@ -59,6 +62,14 @@ func (a *App) SetCreate(id uint64, err error) *App {
return a
}

// SetCount mocks
func (a *App) SetCount(count uint64, err error) *App {
a.count = count
a.countErr = err

return a
}

// DoAtomic mocks
func (a *App) DoAtomic(ctx context.Context, action func(context.Context) error) error {
if ctx == context.TODO() {
Expand All @@ -81,3 +92,8 @@ func (a *App) GetByLoginID(ctx context.Context, loginID uint64) (model.User, err
func (a *App) Create(ctx context.Context, o model.User) (uint64, error) {
return a.createID, a.createErr
}

// Count mocks
func (a *App) Count(ctx context.Context) (uint64, error) {
return a.count, a.countErr
}

0 comments on commit a5ea76a

Please sign in to comment.