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 <bob@vibioh.fr>
  • 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.