-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Nicola Murino <[email protected]>
- Loading branch information
Showing
15 changed files
with
200 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package db | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/rs/xid" | ||
"gorm.io/gorm" | ||
|
||
"github.com/sftpgo/sftpgo-plugin-eventstore/logger" | ||
) | ||
|
||
// LogEvent defines a log event | ||
type LogEvent struct { | ||
ID string `json:"id" gorm:"primaryKey"` | ||
Timestamp int64 `json:"timestamp"` | ||
Event int `json:"event"` | ||
Protocol string `json:"protocol,omitempty"` | ||
Username string `json:"username,omitempty"` | ||
IP string `json:"ip,omitempty"` | ||
Message string `json:"message,omitempty"` | ||
Role string `json:"role,omitempty"` | ||
InstanceID string `json:"instance_id,omitempty"` | ||
} | ||
|
||
// TableName defines the database table name | ||
func (ev *LogEvent) TableName() string { | ||
return "eventstore_log_events" | ||
} | ||
|
||
// BeforeCreate implements gorm hook | ||
func (ev *LogEvent) BeforeCreate(_ *gorm.DB) (err error) { | ||
ev.ID = xid.New().String() | ||
return | ||
} | ||
|
||
// Create persists the object | ||
func (ev *LogEvent) Create(tx *gorm.DB) error { | ||
return tx.Create(ev).Error | ||
} | ||
|
||
func cleanupLogEvents(timestamp time.Time) error { | ||
sess, cancel := getSessionWithTimeout(20 * time.Minute) | ||
defer cancel() | ||
|
||
logger.AppLogger.Debug("removing log events", "timestamp", timestamp) | ||
sess = sess.Where("timestamp < ?", timestamp.UnixNano()).Delete(&LogEvent{}) | ||
err := sess.Error | ||
if err == nil { | ||
logger.AppLogger.Debug("log events deleted", "num", sess.RowsAffected) | ||
} | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package migration | ||
|
||
import ( | ||
"github.com/go-gormigrate/gormigrate/v2" | ||
"gorm.io/gorm" | ||
) | ||
|
||
const ( | ||
mignationV6ID = "6" | ||
) | ||
|
||
type logEventV1 struct { | ||
ID string `gorm:"primaryKey;size:36"` | ||
Timestamp int64 `gorm:"size:64;not null;index:idx_log_events_timestamp"` | ||
Event int `gorm:"size:32;not null;index:idx_log_events_event"` | ||
Protocol string `gorm:"size:30;index:idx_log_events_protocol"` | ||
Username string `gorm:"size:255;index:idx_log_events_username"` | ||
IP string `gorm:"size:50;index:idx_log_events_ip"` | ||
Message string | ||
Role string `gorm:"size:255;index:idx_log_events_role"` | ||
InstanceID string `gorm:"size:60;index:idx_log_events_instance_id"` | ||
} | ||
|
||
func (ev *logEventV1) TableName() string { | ||
return "eventstore_log_events" | ||
} | ||
|
||
func v6Up(tx *gorm.DB) error { | ||
modelsToMigrate := []interface{}{ | ||
&logEventV1{}, | ||
} | ||
return tx.AutoMigrate(modelsToMigrate...) | ||
} | ||
|
||
func v6Down(tx *gorm.DB) error { | ||
modelsToMigrate := []interface{}{ | ||
&logEventV1{}, | ||
} | ||
return tx.Migrator().DropTable(modelsToMigrate...) | ||
} | ||
|
||
func getV6Migration() *gormigrate.Migration { | ||
return &gormigrate.Migration{ | ||
ID: mignationV6ID, | ||
Migrate: func(tx *gorm.DB) error { | ||
return v6Up(tx) | ||
}, | ||
Rollback: func(tx *gorm.DB) error { | ||
return v6Down(tx) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters