package notifyrender import ( "bytes" "strings" "time" "github.com/olekukonko/tablewriter" tablewriterTw "github.com/olekukonko/tablewriter/tw" "rocketgit.ru/rsmon/worker/app/models" "rocketgit.ru/rsmon/worker/internal/util" ) // RenderEventsTable provides functionality. func RenderEventsTable(message *models.Message, tn time.Time) (string, string) { if message.Kind == stateTest { return "Тестовое сообщение от rsmon.ru", "Тестовое сообщение от rsmon.ru" } cols := []string{"Монитор", "Проверка", "Время начала", "Время окончания", "Продолжительность", "Статус", "Ошибка"} asciiBuf := bytes.NewBuffer([]byte{}) markdownBuf := bytes.NewBuffer([]byte{}) table := tablewriter.NewTable(asciiBuf, tablewriter.WithHeader(cols), tablewriter.WithBorders(tablewriterTw.Border{Left: tablewriterTw.On, Top: tablewriterTw.Off, Right: tablewriterTw.On, Bottom: tablewriterTw.Off}), //nolint:lll,staticcheck // deprecated API, pending migration ) markdownBuf.WriteString("|") for _, col := range cols { markdownBuf.WriteString(" " + col + " |") } markdownBuf.WriteString("\n") markdownBuf.WriteString("|") for range cols { markdownBuf.WriteString(" --- |") } markdownBuf.WriteString("\n") for _, event := range message.Events { //nolint:gocritic // range copy is acceptable here checksDown := []string{} for _, check := range event.Checks { //nolint:gocritic // range copy is acceptable here msg := check.Kind + ":" if check.Name != nil { msg = msg + " " + *check.Name } if check.URL != nil { msg = msg + " " + *check.URL + "" } // msg = msg + "\n" if check.Error != nil { // msg = msg + "Ошибка:" + *check.Error + "" msg = msg + " :warning: Ошибка:" + *check.Error } // msg = msg + "\n" checksDown = append(checksDown, msg) } startTime := "" if event.StartTime != nil { startTime = event.StartTime.Format("02.01.2006 15:04:05") } endTime := "" if event.EndTime != nil { endTime = event.EndTime.Format("02.01.2006 15:04:05") } row := []string{ event.Monitor.GetLabel(), strings.Join(checksDown, " ; "), startTime, endTime, util.FormatDuration(event.GetDuration(tn)), event.State, event.Reason, } _ = table.Append(row) markdownBuf.WriteString("| ") for _, col := range row { markdownBuf.WriteString(" " + col + " |") } markdownBuf.WriteString("\n") } // markdownBuf.WriteString("\n") _ = table.Render() return asciiBuf.String(), markdownBuf.String() }