Skip to content

πŸ“š The open-source, offline-first Notion, Obsidian & Confluence alternative. Advanced Markdown, multi-tenant teams, OIDC/SSO, and local S3 backups. Deploy in one command.

Improve performances (async & tests)

Smaug#673975fa26f62529
diff --git a/backend/controllers/authentication.controller.go b/backend/controllers/authentication.controller.go
index 573166ce..d215e21b 100644
--- a/backend/controllers/authentication.controller.go
+++ b/backend/controllers/authentication.controller.go
@@ -90,15 +90,17 @@ func (dc *AuthControllerImpl) Login(c *gin.Context) (int, any) {
c.SetCookie("RefreshToken", session.RefreshToken, int(time.Duration(dc.app.Config.Auth.RefreshTokenExpiry).Seconds()), "/", "localhost", false, true)
user.Password = ""
- dc.log_service.CreateConnectionLog(&models.Log{
- Id: dc.app.Snowflake.Generate(),
- UserId: user.Id,
- IpAddr: c.ClientIP(),
- Timestamp: time.Now().UnixMilli(),
- Type: "login",
- Location: dc.log_service.GetLocationFromIp(c.ClientIP()),
- })
-
+ go func() {
+ dc.log_service.CreateConnectionLog(&models.Log{
+ Id: dc.app.Snowflake.Generate(),
+ UserId: user.Id,
+ IpAddr: c.ClientIP(),
+ Timestamp: time.Now().UnixMilli(),
+ Type: "login",
+ Location: dc.log_service.GetLocationFromIp(c.ClientIP()),
+ UserAgent: c.Request.UserAgent(),
+ })
+ }()
return http.StatusOK, user
}
diff --git a/backend/models/log.model.go b/backend/models/log.model.go
index 4ce3402f..c423143e 100644
--- a/backend/models/log.model.go
+++ b/backend/models/log.model.go
@@ -1,11 +1,11 @@
package models
type Log struct {
- Id int64 `json:"id"`
- UserId int64 `json:"user_id"`
- IpAddr string `json:"ip_address"`
- UserAgent *string `json:"user_agent"`
- Location string `json:"location"`
- Type string `json:"type"`
- Timestamp int64 `json:"timestamp"`
+ Id int64 `json:"id"`
+ UserId int64 `json:"user_id"`
+ IpAddr string `json:"ip_address"`
+ UserAgent string `json:"user_agent"`
+ Location string `json:"location"`
+ Type string `json:"type"`
+ Timestamp int64 `json:"timestamp"`
}
diff --git a/backend/services/log.service.go b/backend/services/log.service.go
index 65e73123..e2e796bd 100644
--- a/backend/services/log.service.go
+++ b/backend/services/log.service.go
@@ -4,9 +4,11 @@ import (
"Smaug6739/Alexandrie/models"
"database/sql"
"errors"
+ "fmt"
"math/big"
"net"
"strings"
+ "time"
)
type LogService interface {
@@ -52,27 +54,14 @@ func (s *Service) GetLastConnection(userId int64) (*models.Log, error) {
}
func (s *Service) CreateConnectionLog(log *models.Log) (*models.Log, error) {
- _, err := s.db.Exec("INSERT INTO connections_logs (ip_addr, location, timestamp, type, user_agent, user_id) VALUES (?, ?, ?, ?, ?, ?)",
- log.IpAddr, log.Location, log.Timestamp, log.Type, log.UserAgent, log.UserId)
+ _, err := s.db.Exec("INSERT INTO connections_logs (id, user_id, ip_adress, user_agent, location, type, timestamp) VALUES (?, ?, ?, ?, ?, ?, ?)",
+ log.Id, log.UserId, log.IpAddr, log.UserAgent, log.Location, log.Type, log.Timestamp)
if err != nil {
return nil, err
}
return log, nil
}
-func (s *Service) getLocationIdFromIp(ip string) (int64, error) {
- ipInt, err := ipToDecimal(ip)
- if err != nil {
- return 0, err
- }
- var locationId int64
- err = s.db.QueryRow("SELECT geoname_id FROM City_IPv4_complete WHERE ? BETWEEN network_start_integer AND network_last_integer LIMIT 1", ipInt).Scan(&locationId)
- if err != nil {
- return 0, err
- }
- return locationId, nil
-}
-
func (s *Service) GetLocationFromIp(ip string) string {
geonameId, err := s.getLocationIdFromIp(ip)
if err != nil {
@@ -92,6 +81,23 @@ func (s *Service) GetLocationFromIp(ip string) string {
return strings.Join(parts, ", ")
}
+func (s *Service) getLocationIdFromIp(ip string) (int64, error) {
+ ipInt, err := ipToDecimal(ip)
+ if err != nil {
+ return 0, err
+ }
+ var locationId int64
+ // Performance test:
+ t := time.Now()
+ err = s.db.QueryRow("SELECT geoname_id FROM City_IPv4_complete WHERE ? BETWEEN network_start_integer AND network_last_integer LIMIT 1", ipInt).Scan(&locationId)
+ t2 := time.Now()
+ fmt.Println("Time taken to get location ID:", t2.Sub(t))
+ if err != nil {
+ return 0, err
+ }
+ return locationId, nil
+}
+
func ipToDecimal(ipStr string) (string, error) {
ip := net.ParseIP(ipStr)
if ip == nil {