logentry.go

46 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 43 44 45 46
package models

import (
	"fmt"
	"time"

	"congo.gg/pkg/database"
)

// LogEntry is a structured activity log from the twin or the system.
type LogEntry struct {
	database.Model
	Type    string // work, morning, alert, system, deploy
	Summary string
	Detail  string
	Author  string // twin, connor, system
}

// Ago returns a human-readable relative time string.
func (e *LogEntry) Ago() string {
	d := time.Since(e.CreatedAt)
	switch {
	case d < 5*time.Second:
		return "now"
	case d < time.Minute:
		return fmt.Sprintf("%ds", int(d.Seconds()))
	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)
	}
}