From 8242cdcbfb3fe1f433e8ca65133ee7da674c4cfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torjus=20H=C3=A5kestad?= Date: Sat, 10 Apr 2021 08:26:03 +0200 Subject: [PATCH] Add total stats for postgres --- honeypot/store/postgres.go | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/honeypot/store/postgres.go b/honeypot/store/postgres.go index 2a3e053..e0e7e46 100644 --- a/honeypot/store/postgres.go +++ b/honeypot/store/postgres.go @@ -127,5 +127,37 @@ func (s *PostgresStore) Stats(statType LoginStats, limit int) ([]StatsResult, er } 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 }