Add total stats for postgres

This commit is contained in:
Torjus Håkestad 2021-04-10 08:26:03 +02:00
parent 5ec1d11ea7
commit 8242cdcbfb

View File

@ -127,5 +127,37 @@ func (s *PostgresStore) Stats(statType LoginStats, limit int) ([]StatsResult, er
} }
func (s *PostgresStore) statsTotal(limit int) ([]StatsResult, error) { func (s *PostgresStore) statsTotal(limit int) ([]StatsResult, error) {
return nil, nil uniquePasswordStmt := `select count(*) from (select distinct password from login_attempts) as temp`
uniqueUsernameStmt := `select count(*) from (select distinct username from login_attempts) as temp`
uniqueIPStmt := `select count(*) from (select distinct remote_ip from login_attempts) as temp`
uniqueCountryStmt := `select count(*) from (select distinct country from login_attempts) as temp`
attemptsCountStmt := `select count(1) from login_attempts`
var uniquePasswordsCount int
if err := s.db.QueryRow(uniquePasswordStmt).Scan(&uniquePasswordsCount); err != nil {
return nil, err
}
var uniqueUsernameCount int
if err := s.db.QueryRow(uniqueUsernameStmt).Scan(&uniqueUsernameCount); err != nil {
return nil, err
}
var uniqueIPCount int
if err := s.db.QueryRow(uniqueIPStmt).Scan(&uniqueIPCount); err != nil {
return nil, err
}
var uniqueCountryCount int
if err := s.db.QueryRow(uniqueCountryStmt).Scan(&uniqueCountryCount); err != nil {
return nil, err
}
var attemptsCount int
if err := s.db.QueryRow(attemptsCountStmt).Scan(&attemptsCount); err != nil {
return nil, err
}
return []StatsResult{
{Name: "UniquePasswords", Count: uniquePasswordsCount},
{Name: "UniqueUsernames", Count: uniqueUsernameCount},
{Name: "UniqueIPs", Count: uniqueIPCount},
{Name: "UniqueCountries", Count: uniqueCountryCount},
{Name: "TotalLoginAttempts", Count: attemptsCount},
}, nil
} }