Skip to content

Commit

Permalink
feat(notifier): Add dump summary to Healthchecks logs
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed May 1, 2024
1 parent c0097d2 commit f039fb1
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 40 deletions.
2 changes: 2 additions & 0 deletions cmd/cmd.go
Expand Up @@ -105,6 +105,8 @@ func preRun(cmd *cobra.Command, _ []string) error {
log.Err(err).Msg("Notifications ping finished failed")
}
})

cmd.SetContext(notifier.NewContext(cmd.Context(), handler))
}
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -15,6 +15,7 @@ require (
github.com/gabe565/go-spinners v1.0.1
github.com/klauspost/pgzip v1.2.6
github.com/mattn/go-isatty v0.0.20
github.com/muesli/termenv v0.15.2
github.com/rs/zerolog v1.32.0
github.com/schollz/progressbar/v3 v3.14.2
github.com/spf13/cobra v1.8.0
Expand Down Expand Up @@ -103,7 +104,6 @@ require (
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
github.com/pelletier/go-toml/v2 v2.2.0 // indirect
Expand Down
40 changes: 28 additions & 12 deletions internal/actions/dump/dump.go
Expand Up @@ -17,11 +17,13 @@ import (
"github.com/clevyr/kubedb/internal/database/sqlformat"
"github.com/clevyr/kubedb/internal/github"
"github.com/clevyr/kubedb/internal/kubernetes"
"github.com/clevyr/kubedb/internal/notifier"
"github.com/clevyr/kubedb/internal/progressbar"
"github.com/clevyr/kubedb/internal/storage"
"github.com/clevyr/kubedb/internal/tui"
"github.com/clevyr/kubedb/internal/util"
gzip "github.com/klauspost/pgzip"
"github.com/muesli/termenv"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -178,6 +180,12 @@ func (action Dump) Run(ctx context.Context) error {
Stringer("took", time.Since(startTime).Truncate(10*time.Millisecond)).
Stringer("size", sizeW).
Msg("dump complete")

if handler, ok := notifier.FromContext(ctx); ok {
if logger, ok := handler.(notifier.Logs); ok {
logger.SetLog(action.summary(nil, time.Since(startTime).Truncate(10*time.Millisecond), sizeW, true))
}
}
return nil
}

Expand All @@ -202,13 +210,15 @@ func (action Dump) buildCommand() (*command.Builder, error) {
return cmd, nil
}

func (action Dump) printSummary(err error, took time.Duration, size *util.SizeWriter) {
out := os.Stdout
if action.Filename == "-" {
out = os.Stderr
func (action Dump) summary(err error, took time.Duration, size *util.SizeWriter, plain bool) string {
var r *lipgloss.Renderer
if plain {
r = lipgloss.NewRenderer(os.Stdout, termenv.WithTTY(false))
r.SetColorProfile(termenv.Ascii)
r.SetHasDarkBackground(lipgloss.HasDarkBackground())
}

t := tui.MinimalTable().
t := tui.MinimalTable(r).
Row("Context", action.Context).
Row("Namespace", action.Namespace).
Row("Pod", action.DBPod.Name)
Expand All @@ -218,18 +228,24 @@ func (action Dump) printSummary(err error, took time.Duration, size *util.SizeWr
if action.Database != "" {
t.Row("Database", action.Database)
}
t.Row("File", tui.OutPath(action.Filename)).
t.Row("File", tui.OutPath(action.Filename, r)).
Row("Took", took.String())
if err != nil {
t.Row("Error", lipgloss.NewStyle().Foreground(lipgloss.Color("1")).Render(err.Error()))
t.Row("Error", lipgloss.NewStyle().Renderer(r).Foreground(lipgloss.Color("1")).Render(err.Error()))
} else if size != nil {
t.Row("Size", size.String())
}

_, _ = io.WriteString(out,
lipgloss.JoinVertical(lipgloss.Center,
tui.HeaderStyle().PaddingTop(1).Render("Dump Summary"),
t.Render(),
)+"\n",
return lipgloss.JoinVertical(lipgloss.Center,
tui.HeaderStyle(r).Render("Dump Summary"),
t.Render(),
)
}

func (action Dump) printSummary(err error, took time.Duration, size *util.SizeWriter) {
out := os.Stdout
if action.Filename == "-" {
out = os.Stderr
}
_, _ = io.WriteString(out, "\n"+action.summary(err, took, size, false)+"\n")
}
10 changes: 5 additions & 5 deletions internal/actions/portforward/portforward.go
Expand Up @@ -79,12 +79,12 @@ func (a PortForward) printTable() {
Uint16("remote", a.Port).
Msg("port forward is ready")

info := tui.MinimalTable().
info := tui.MinimalTable(nil).
Row("Context", a.Context).
Row("Namespace", a.Namespace).
Row("Pod", a.DBPod.Name)

params := tui.MinimalTable().
params := tui.MinimalTable(nil).
Row("Type", a.Dialect.PrettyName()).
Row("Namespace", a.Namespace).
Row("Hostname", "localhost").
Expand Down Expand Up @@ -115,8 +115,8 @@ func (a PortForward) printTable() {
}
}

headerStyle := tui.HeaderStyle()
italicStyle := tui.BorderStyle().Italic(true)
headerStyle := tui.HeaderStyle(nil)
italicStyle := tui.BorderStyle(nil).Italic(true)
data := lipgloss.JoinVertical(lipgloss.Left,
lipgloss.JoinVertical(lipgloss.Center,
headerStyle.Render("Database Instance"),
Expand All @@ -129,7 +129,7 @@ func (a PortForward) printTable() {
),
"",
headerStyle.Render("Tip:")+
tui.BorderStyle().Render(" If you're connecting from a Docker container, set the hostname to ")+
tui.BorderStyle(nil).Render(" If you're connecting from a Docker container, set the hostname to ")+
italicStyle.Render("host.docker.internal"),
)

Expand Down
6 changes: 3 additions & 3 deletions internal/actions/restore/restore.go
Expand Up @@ -227,7 +227,7 @@ func (action Restore) runInDatabasePod(ctx context.Context, r *io.PipeReader, st
}

func (action Restore) Table() *table.Table {
t := tui.MinimalTable().
t := tui.MinimalTable(nil).
Row("Context", action.Context).
Row("Namespace", action.Namespace).
Row("Pod", action.DBPod.Name)
Expand All @@ -237,7 +237,7 @@ func (action Restore) Table() *table.Table {
if action.Database != "" {
t.Row("Database", action.Database)
}
t.Row("File", tui.InPath(action.Filename))
t.Row("File", tui.InPath(action.Filename, nil))
return t
}

Expand All @@ -263,7 +263,7 @@ func (action Restore) printSummary(err error, took time.Duration, size *util.Siz

fmt.Println( //nolint:forbidigo
lipgloss.JoinVertical(lipgloss.Center,
tui.HeaderStyle().PaddingTop(1).Render("Restore Summary"),
tui.HeaderStyle(nil).PaddingTop(1).Render("Restore Summary"),
t.Render(),
),
)
Expand Down
26 changes: 18 additions & 8 deletions internal/notifier/healthchecks.go
Expand Up @@ -11,21 +11,22 @@ import (
"time"
)

var _ Logs = &Healthchecks{}

func NewHealthchecks(url string) (Notifier, error) {
if url == "" {
return nil, fmt.Errorf("healthchecks %w", ErrEmptyURL)
}

return &Healthchecks{
url: url,
}, nil
return &Healthchecks{url: url}, nil
}

type Healthchecks struct {
url string
log string
}

func (h Healthchecks) SendStatus(status Status, log string) error {
func (h *Healthchecks) SendStatus(status Status, log string) error {
var statusStr string
switch status {
case StatusStart:
Expand Down Expand Up @@ -65,13 +66,22 @@ func (h Healthchecks) SendStatus(status Status, log string) error {
return nil
}

func (h Healthchecks) Started() error {
func (h *Healthchecks) Started() error {
return h.SendStatus(StatusStart, "")
}

func (h Healthchecks) Finished(err error) error {
func (h *Healthchecks) SetLog(log string) {
h.log = log
}

func (h *Healthchecks) Finished(err error) error {
if err == nil {
return h.SendStatus(StatusSuccess, "")
return h.SendStatus(StatusSuccess, h.log)
}

msg := "Error: " + err.Error()
if h.log != "" {
msg += "\n\n" + h.log
}
return h.SendStatus(StatusFailure, "Error: "+err.Error())
return h.SendStatus(StatusFailure, msg)
}
4 changes: 4 additions & 0 deletions internal/notifier/notifier.go
Expand Up @@ -25,6 +25,10 @@ type Notifier interface {
Finished(err error) error
}

type Logs interface {
SetLog(log string)
}

func New(handler, url string) (Notifier, error) {
switch strings.ToLower(handler) {
case "healthchecks":
Expand Down
8 changes: 4 additions & 4 deletions internal/tui/filepath.go
Expand Up @@ -7,16 +7,16 @@ import (
"github.com/charmbracelet/lipgloss"
)

func InPath(path string) string {
style := lipgloss.NewStyle().Italic(true)
func InPath(path string, r *lipgloss.Renderer) string {
style := lipgloss.NewStyle().Renderer(r).Italic(true)
if path == "-" {
return "stdin"
}
return style.Render(CleanPath(path))
}

func OutPath(path string) string {
style := lipgloss.NewStyle().Italic(true)
func OutPath(path string, r *lipgloss.Renderer) string {
style := lipgloss.NewStyle().Renderer(r).Italic(true)
if path == "-" {
return "stdout"
}
Expand Down
6 changes: 4 additions & 2 deletions internal/tui/styles.go
Expand Up @@ -2,6 +2,8 @@ package tui

import "github.com/charmbracelet/lipgloss"

func HeaderStyle() lipgloss.Style {
return lipgloss.NewStyle().Bold(true).Foreground(lipgloss.AdaptiveColor{Light: "#5A56E0", Dark: "#7571F9"})
func HeaderStyle(r *lipgloss.Renderer) lipgloss.Style {
return lipgloss.NewStyle().Renderer(r).
Bold(true).
Foreground(lipgloss.AdaptiveColor{Light: "#5A56E0", Dark: "#7571F9"})
}
11 changes: 6 additions & 5 deletions internal/tui/table.go
Expand Up @@ -5,16 +5,17 @@ import (
"github.com/charmbracelet/lipgloss/table"
)

func BorderStyle() lipgloss.Style {
return lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "", Dark: "243"})
func BorderStyle(r *lipgloss.Renderer) lipgloss.Style {
return lipgloss.NewStyle().Renderer(r).
Foreground(lipgloss.AdaptiveColor{Light: "", Dark: "243"})
}

func MinimalTable() *table.Table {
colStyle := BorderStyle().Padding(0, 1)
func MinimalTable(r *lipgloss.Renderer) *table.Table {
colStyle := BorderStyle(r).Padding(0, 1)
firstColStyle := colStyle.Copy().Align(lipgloss.Right).Bold(true)

return table.New().
BorderStyle(BorderStyle()).
BorderStyle(BorderStyle(r)).
StyleFunc(func(_, col int) lipgloss.Style {
if col == 0 {
return firstColStyle
Expand Down

0 comments on commit f039fb1

Please sign in to comment.