activity.go

42 lines
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
package models

import (
	"fmt"
	"time"

	"congo.gg/pkg/database"
)

// Activity represents an audit log entry.
type Activity struct {
	database.Model
	Action string // clone, deploy, restart, agent, etc.
	Detail string
}

// Ago returns a human-readable relative time string.
func (a *Activity) Ago() string {
	d := time.Since(a.CreatedAt)
	switch {
	case d < time.Minute:
		return "just now"
	case d < time.Hour:
		m := int(d.Minutes())
		if m == 1 {
			return "1 min ago"
		}
		return fmt.Sprintf("%d mins ago", m)
	case d < 24*time.Hour:
		h := int(d.Hours())
		if h == 1 {
			return "1 hour ago"
		}
		return fmt.Sprintf("%d hours ago", h)
	default:
		days := int(d.Hours() / 24)
		if days == 1 {
			return "1 day ago"
		}
		return fmt.Sprintf("%d days ago", days)
	}
}