add last flush to /info command

new-sql-view v0.5.0
William Perron 11 months ago
parent 5cbaff0820
commit c1d5e472f2
Signed by: wperron
GPG Key ID: BFDB4EF72D73C5F2

@ -81,10 +81,10 @@ func (s *Store) Audit(ev *AuditableEvent) {
} }
type AuditEvent struct { type AuditEvent struct {
id int Id int
userId string UserId string
eventType EventType EventType EventType
timestamp time.Time Timestamp time.Time
} }
func (s *Store) LastOf(ctx context.Context, t EventType) (AuditEvent, error) { func (s *Store) LastOf(ctx context.Context, t EventType) (AuditEvent, error) {
@ -98,15 +98,15 @@ func (s *Store) LastOf(ctx context.Context, t EventType) (AuditEvent, error) {
ev := AuditEvent{} ev := AuditEvent{}
var rawEventType string var rawEventType string
err = row.Scan(&ev.id, &ev.userId, &rawEventType, &ev.timestamp) err = row.Scan(&ev.Id, &ev.UserId, &rawEventType, &ev.Timestamp)
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
return AuditEvent{}, errors.New("no rows found") return AuditEvent{}, ErrNever
} }
if err != nil { if err != nil {
return AuditEvent{}, fmt.Errorf("failed to scan row: %w", err) return AuditEvent{}, fmt.Errorf("failed to scan row: %w", err)
} }
ev.eventType, err = EventTypeFromString(rawEventType) ev.EventType, err = EventTypeFromString(rawEventType)
if err != nil { if err != nil {
return AuditEvent{}, fmt.Errorf("failed to parse event type %s: %w", rawEventType, err) return AuditEvent{}, fmt.Errorf("failed to parse event type %s: %w", rawEventType, err)
} }

@ -231,10 +231,30 @@ func main() {
} }
} }
ev, err := store.LastOf(ctx, themis.EventFlush)
var lastFlush string
if err != nil {
if err != themis.ErrNever {
log.Error().Err(err).Msg("failed get last flush event")
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Oops, something went wrong! :(",
},
})
if err != nil {
log.Error().Err(err).Msg("failed to respond to interaction")
}
}
lastFlush = "never"
} else {
lastFlush = ev.Timestamp.Format(time.DateTime)
}
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource, Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{ Data: &discordgo.InteractionResponseData{
Content: fmt.Sprintf("Server has been up for %s, has %d claims from %d unique players", uptime, claimCount, uniquePlayers), Content: fmt.Sprintf("Server has been up for %s, has %d claims from %d unique players.\nThe last time claims were flushed was: %s.", uptime, claimCount, uniquePlayers, lastFlush),
}, },
}) })
if err != nil { if err != nil {

@ -6,6 +6,7 @@ import (
) )
var ErrNoSuchClaim = errors.New("no such claim") var ErrNoSuchClaim = errors.New("no such claim")
var ErrNever = errors.New("event never occured")
type ErrConflict struct { type ErrConflict struct {
Conflicts []Conflict Conflicts []Conflict

@ -99,8 +99,8 @@ func TestStore_Claim(t *testing.T) {
ae, err := store.LastOf(context.TODO(), EventClaim) ae, err := store.LastOf(context.TODO(), EventClaim)
require.NoError(t, err) require.NoError(t, err)
assert.Greater(t, ae.id, lastAudit) assert.Greater(t, ae.Id, lastAudit)
lastAudit = ae.id lastAudit = ae.Id
}) })
} }
} }
@ -173,7 +173,7 @@ func TestDeleteClaim(t *testing.T) {
ae, err := store.LastOf(context.TODO(), EventUnclaim) ae, err := store.LastOf(context.TODO(), EventUnclaim)
require.NoError(t, err) require.NoError(t, err)
last := ae.id last := ae.Id
err = store.DeleteClaim(context.TODO(), barId, "000000000000000001") err = store.DeleteClaim(context.TODO(), barId, "000000000000000001")
assert.Error(t, err) assert.Error(t, err)
@ -181,7 +181,7 @@ func TestDeleteClaim(t *testing.T) {
ae, err = store.LastOf(context.TODO(), EventUnclaim) ae, err = store.LastOf(context.TODO(), EventUnclaim)
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, last, ae.id) // no new audit log was added assert.Equal(t, last, ae.Id) // no new audit log was added
} }
func TestDescribeClaim(t *testing.T) { func TestDescribeClaim(t *testing.T) {

Loading…
Cancel
Save