mirror of
https://github.com/ManyakRus/telegram_loki.git
synced 2024-12-12 09:33:50 +02:00
сделал URL
This commit is contained in:
parent
c8c6027ab6
commit
c4b3f610af
@ -1 +1 @@
|
||||
2024-11-14 14:40:02.659
|
||||
2024-11-15 08:27:48.861
|
@ -1 +1 @@
|
||||
00183
|
||||
00184
|
@ -6,13 +6,11 @@ import (
|
||||
"github.com/ManyakRus/starter/log"
|
||||
"github.com/ManyakRus/starter/micro"
|
||||
"github.com/ManyakRus/telegram_loki/internal/config"
|
||||
"github.com/ManyakRus/telegram_loki/internal/constants"
|
||||
"github.com/ManyakRus/telegram_loki/internal/loki_http"
|
||||
"github.com/ManyakRus/telegram_loki/internal/telegram"
|
||||
"github.com/ManyakRus/telegram_loki/internal/types"
|
||||
"github.com/golang-module/carbon/v2"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -77,11 +75,14 @@ loop_for:
|
||||
}
|
||||
|
||||
//запуск проверки логов одного сервиса
|
||||
Text, err1 := Start_period1(ServiceName, DeveloperName, Date1, Date2)
|
||||
Message1 := types.Message{}
|
||||
Message1.ServiceName = ServiceName
|
||||
Message1.DeveloperName = DeveloperName
|
||||
err1 := Start_period1(&Message1, Date1, Date2)
|
||||
if err1 != nil {
|
||||
micro.Pause_ctx(ctxMain, 1000) //error: 502 Bad Gateway
|
||||
} else {
|
||||
if Text != "" {
|
||||
if Message1.Text != "" {
|
||||
CountText = CountText + 1
|
||||
}
|
||||
IsOnlyErrors = false
|
||||
@ -100,7 +101,9 @@ loop_for:
|
||||
if IsOnlyErrors == true {
|
||||
TextError := fmt.Sprint("Search errors: only errors. Last error: ", err1)
|
||||
log.Debug(TextError)
|
||||
err = telegram.SendMessage(config.Settings.TELEGRAM_CHAT_NAME, TextError)
|
||||
Message1 := types.Message{}
|
||||
Message1.Text = TextError
|
||||
err = telegram.SendMessage(Message1)
|
||||
if err != nil {
|
||||
log.Error("telegram.SendMessage() error: ", err)
|
||||
}
|
||||
@ -110,17 +113,18 @@ loop_for:
|
||||
|
||||
// Start_period1 - запускает чтение логов одного сервиса за период
|
||||
// возвращает Текст отправленного сообщения, и ошибку
|
||||
func Start_period1(ServiceName, DeveloperName0 string, DateFrom, DateTo time.Time) (string, error) {
|
||||
func Start_period1(Message1 *types.Message, DateFrom, DateTo time.Time) error {
|
||||
var err error
|
||||
Text := ""
|
||||
|
||||
ctxMain := contextmain.GetContext()
|
||||
DeveloperName0 := Message1.DeveloperName
|
||||
|
||||
LokiMessage, err := loki_http.DownloadJSON(ServiceName, DateFrom, DateTo)
|
||||
LokiMessage, err := loki_http.DownloadJSON(Message1.ServiceName, DateFrom, DateTo)
|
||||
if err != nil {
|
||||
//log.Error("DownloadJSON() error: ", err)
|
||||
micro.Pause_ctx(ctxMain, 1000)
|
||||
return Text, err
|
||||
return err
|
||||
}
|
||||
|
||||
for _, Result1 := range LokiMessage.Data.Result {
|
||||
@ -136,9 +140,8 @@ func Start_period1(ServiceName, DeveloperName0 string, DateFrom, DateTo time.Tim
|
||||
}
|
||||
|
||||
//
|
||||
URL := FindURLLoki(ServiceName, DateFrom, DateTo)
|
||||
//TextServiceName := ServiceName + "[inline URL](" + URL + ")"
|
||||
TextServiceName := `<a href="` + URL + `">` + ServiceName + "</a>"
|
||||
URL := FindURLLoki(Message1.ServiceName, DateFrom, DateTo)
|
||||
//TextServiceName := `<a href="` + URL + `">` + Message1.ServiceName + "</a>"
|
||||
|
||||
//отправим ошибки в Telegram
|
||||
for _, MassValues1 := range Result1.Values {
|
||||
@ -161,30 +164,35 @@ func Start_period1(ServiceName, DeveloperName0 string, DateFrom, DateTo time.Tim
|
||||
continue
|
||||
}
|
||||
Date := time.Unix(0, iDate)
|
||||
TextDate := Date.Format(constants.Layout)
|
||||
TextDate2 := micro.SubstringLeft(TextDate, 10)
|
||||
if strings.Contains(TextLog, TextDate2) == true {
|
||||
TextDate = ""
|
||||
} else {
|
||||
TextDate = TextDate + " "
|
||||
}
|
||||
//TextDate := Date.Format(constants.Layout)
|
||||
//TextDate2 := micro.SubstringLeft(TextDate, 10)
|
||||
//if strings.Contains(TextLog, TextDate2) == true {
|
||||
// TextDate = ""
|
||||
//} else {
|
||||
// TextDate = TextDate + " "
|
||||
//}
|
||||
DeveloperName := DeveloperName0
|
||||
|
||||
//если такая же ошибка то не пишем имя разработчика
|
||||
TextLogWithoutTime := TextLogWithoutTime(TextLog)
|
||||
LastError, IsFind2 := MapLastErrors[ServiceName]
|
||||
LastError, IsFind2 := MapLastErrors[Message1.ServiceName]
|
||||
if IsFind2 == true && LastError == TextLogWithoutTime {
|
||||
DeveloperName = ""
|
||||
}
|
||||
|
||||
//запомним последнюю ошибку
|
||||
MapLastErrors[ServiceName] = TextLogWithoutTime
|
||||
MapLastErrors[Message1.ServiceName] = TextLogWithoutTime
|
||||
|
||||
//
|
||||
Text = TextServiceName + " " + TextDate + DeveloperName + "\n" + TextLog
|
||||
//Text = TextServiceName + " " + TextDate + DeveloperName + "\n" + TextLog
|
||||
Text = TextLog
|
||||
Message1.Text = Text
|
||||
Message1.DeveloperName = DeveloperName
|
||||
Message1.Date = Date
|
||||
Message1.LokiURL = URL
|
||||
|
||||
//
|
||||
err = telegram.SendMessage(DeveloperName, Text)
|
||||
err = telegram.SendMessage(*Message1)
|
||||
if err != nil {
|
||||
log.Error("SendMessage() error: ", err)
|
||||
continue
|
||||
@ -192,7 +200,7 @@ func Start_period1(ServiceName, DeveloperName0 string, DateFrom, DateTo time.Tim
|
||||
}
|
||||
}
|
||||
|
||||
return Text, err
|
||||
return err
|
||||
}
|
||||
|
||||
// FindURLLoki - находит URL ссылку в LOKI на которую можно кликнуть в телеграмме
|
||||
|
@ -25,7 +25,7 @@ var MapLastErrors = make(map[string]string)
|
||||
|
||||
// Start - старт работы чтения логов LOKI
|
||||
func Start() {
|
||||
RunSQL_and_Send()
|
||||
RunSQL_all_and_Send()
|
||||
|
||||
Ticker = time.NewTicker(time.Duration(config.Settings.DATABASE_CHECKER_INTERVAL_MINUTES) * time.Minute)
|
||||
|
||||
@ -39,19 +39,19 @@ func ReadTicker() {
|
||||
case <-contextmain.GetContext().Done():
|
||||
return
|
||||
case <-Ticker.C:
|
||||
RunSQL_and_Send()
|
||||
RunSQL_all_and_Send()
|
||||
}
|
||||
}
|
||||
|
||||
Ticker.Stop()
|
||||
}
|
||||
|
||||
// RunSQL_and_Send - запускает все скрипты .sql, и отправляет ошибки в Телеграм
|
||||
func RunSQL_and_Send() {
|
||||
DeveloperName, err := RunSQL()
|
||||
if err != nil {
|
||||
// RunSQL_all_and_Send - запускает все скрипты .sql, и отправляет ошибки в Телеграм
|
||||
func RunSQL_all_and_Send() {
|
||||
Message1, err := RunSQL_all()
|
||||
if Message1.Text != "" {
|
||||
//отправим в Telegram
|
||||
err = telegram.SendMessage(DeveloperName, err.Error())
|
||||
err = telegram.SendMessage(Message1)
|
||||
if err != nil {
|
||||
log.Error("SendMessage() error: ", err)
|
||||
}
|
||||
@ -59,10 +59,10 @@ func RunSQL_and_Send() {
|
||||
|
||||
}
|
||||
|
||||
// RunSQL - запускает все скрипты .sql
|
||||
// RunSQL_all - запускает все скрипты .sql
|
||||
// возвращает DeveloperName, error
|
||||
func RunSQL() (string, error) {
|
||||
DeveloperName := ""
|
||||
func RunSQL_all() (types.Message, error) {
|
||||
Otvet := types.Message{}
|
||||
var err error
|
||||
|
||||
log.Debug("Start run .sql scripts")
|
||||
@ -73,7 +73,8 @@ func RunSQL() (string, error) {
|
||||
if err != nil {
|
||||
err = fmt.Errorf("os.ReadDir() error: %w", err)
|
||||
log.Error(err)
|
||||
return DeveloperName, err
|
||||
Otvet.Text = err.Error()
|
||||
return Otvet, err
|
||||
}
|
||||
|
||||
//пройдемся по всем файлам
|
||||
@ -92,22 +93,27 @@ func RunSQL() (string, error) {
|
||||
continue
|
||||
}
|
||||
|
||||
Otvet = types.Message{}
|
||||
//Otvet.ServiceName = FilenameShort
|
||||
|
||||
//запускаем скрипт
|
||||
err = RunSQL1(Filename)
|
||||
if err != nil {
|
||||
DeveloperName, _ = types.MapSQLDeveloper[FilenameShort]
|
||||
DeveloperNameTrim := FindDeveloperName_if_err(FilenameShort, err)
|
||||
err2 := fmt.Errorf("%w\n%s", err, DeveloperNameTrim)
|
||||
log.Warn(err2)
|
||||
Text := RunSQL1(Filename)
|
||||
if Text != "" {
|
||||
DeveloperName, _ := types.MapSQLDeveloper[FilenameShort]
|
||||
Otvet.DeveloperName = DeveloperName
|
||||
Otvet.Text = Text
|
||||
//DeveloperNameTrim := FindDeveloperName_if_err(FilenameShort, err)
|
||||
//err2 := fmt.Errorf("%w\n%s", err, DeveloperNameTrim)
|
||||
//log.Warn(err2)
|
||||
|
||||
//запомним ошибку
|
||||
MapLastErrors[FilenameShort] = err.Error()
|
||||
MapLastErrors[FilenameShort] = Text
|
||||
|
||||
return DeveloperName, err2
|
||||
return Otvet, nil
|
||||
}
|
||||
}
|
||||
|
||||
return "", err
|
||||
return Otvet, err
|
||||
}
|
||||
|
||||
// FindDeveloperName_if_err - возвращает имя разработчика, если ошибка другая
|
||||
@ -133,7 +139,8 @@ func FindDeveloperName_if_err(FilenemeShort string, err error) string {
|
||||
}
|
||||
|
||||
// RunSQL1 - запускает 1 скрипт
|
||||
func RunSQL1(Filename string) error {
|
||||
func RunSQL1(Filename string) string {
|
||||
Otvet := ""
|
||||
var err error
|
||||
|
||||
//подключимся к БД
|
||||
@ -146,10 +153,10 @@ func RunSQL1(Filename string) error {
|
||||
//прочитаем файл
|
||||
bytes, err := os.ReadFile(Filename)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("ReadFile() Filename: %s error: %w", Filename, err)
|
||||
log.Error(err)
|
||||
Otvet = fmt.Sprintf("ReadFile() Filename: %s error: %v", Filename, err)
|
||||
log.Error(Otvet)
|
||||
|
||||
return err
|
||||
return Otvet
|
||||
}
|
||||
TextSQL := string(bytes)
|
||||
|
||||
@ -163,20 +170,19 @@ func RunSQL1(Filename string) error {
|
||||
|
||||
//нет строк - это хорошо
|
||||
if err == pgx.ErrNoRows {
|
||||
err = nil
|
||||
return err
|
||||
return Otvet
|
||||
}
|
||||
|
||||
//ошибки не должно быть
|
||||
if err != nil {
|
||||
err = fmt.Errorf("db.Exec() Filename: %s, error: %w", Filename, err)
|
||||
log.Error(err)
|
||||
Otvet = fmt.Sprintf("db.Exec() Filename: %s, error: %v", Filename, err)
|
||||
log.Error(Otvet)
|
||||
|
||||
return err
|
||||
return Otvet
|
||||
}
|
||||
|
||||
//запрос вернул число(строку)
|
||||
err = fmt.Errorf(`скрипт "%s" вернул значение: %s`, FilenameShort, ResultSQL)
|
||||
Otvet = fmt.Sprintf(`скрипт "%s" вернул значение: %s`, FilenameShort, ResultSQL)
|
||||
|
||||
return err
|
||||
return Otvet
|
||||
}
|
||||
|
@ -40,8 +40,8 @@ func QueryApp(ServiceName string, DateFrom, DateTo time.Time, Filter string) str
|
||||
return Otvet
|
||||
}
|
||||
|
||||
func DownloadJSON(ServiceName string, DateFrom, DateTo time.Time) (types.Message, error) {
|
||||
Otvet := types.Message{}
|
||||
func DownloadJSON(ServiceName string, DateFrom, DateTo time.Time) (types.MessageLoki, error) {
|
||||
Otvet := types.MessageLoki{}
|
||||
var err error
|
||||
|
||||
if Client == nil {
|
||||
|
@ -3,9 +3,11 @@ package telegram
|
||||
import (
|
||||
"github.com/ManyakRus/starter/contextmain"
|
||||
"github.com/ManyakRus/starter/log"
|
||||
"github.com/ManyakRus/starter/micro"
|
||||
"github.com/ManyakRus/starter/stopapp"
|
||||
"github.com/ManyakRus/starter/telegram_bot"
|
||||
"github.com/ManyakRus/telegram_loki/internal/config"
|
||||
"github.com/ManyakRus/telegram_loki/internal/constants"
|
||||
"github.com/ManyakRus/telegram_loki/internal/load_json"
|
||||
"github.com/ManyakRus/telegram_loki/internal/types"
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
@ -14,7 +16,7 @@ import (
|
||||
|
||||
// SendMessage - отправляет сообщения в Telegram, в общий чат, многим разрабочикам
|
||||
// ChatName - в формате "@Имя1,@Имя2"
|
||||
func SendMessage(ChatName string, Text string) error {
|
||||
func SendMessage(Message1 types.Message) error {
|
||||
var err error
|
||||
|
||||
//проверка отмены контекста
|
||||
@ -24,7 +26,26 @@ func SendMessage(ChatName string, Text string) error {
|
||||
}
|
||||
|
||||
//
|
||||
ChatName = strings.TrimSpace(ChatName)
|
||||
ChatName := strings.TrimSpace(Message1.DeveloperName)
|
||||
//TextDate := Message1.Date.String()
|
||||
TextDate := Message1.Date.Format(constants.Layout)
|
||||
TextDate2 := micro.SubstringLeft(TextDate, 10)
|
||||
if strings.Contains(Message1.Text, TextDate2) == true {
|
||||
TextDate = ""
|
||||
} else {
|
||||
TextDate = "\n" + TextDate
|
||||
}
|
||||
if Message1.Date.IsZero() == true {
|
||||
TextDate = ""
|
||||
}
|
||||
DeveloperName := Message1.DeveloperName
|
||||
Text := Message1.Text
|
||||
|
||||
TextServiceName := Message1.ServiceName
|
||||
if Message1.LokiURL != "" {
|
||||
TextServiceName = `<a href="` + Message1.LokiURL + `">` + Message1.ServiceName + "</a>"
|
||||
}
|
||||
Text = TextServiceName + " " + DeveloperName + " " + TextDate + "\n" + Text
|
||||
|
||||
//отправка в общую группу
|
||||
if config.Settings.TELEGRAM_CHAT_NAME != "" {
|
||||
@ -43,6 +64,11 @@ func SendMessage(ChatName string, Text string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
Text = Message1.Text
|
||||
if TextServiceName != "" {
|
||||
Text = TextServiceName + "\n" + Text
|
||||
}
|
||||
|
||||
//
|
||||
MassChatNames := strings.Split(ChatName, ",")
|
||||
for _, ChatName1 := range MassChatNames {
|
||||
|
@ -1,6 +1,10 @@
|
||||
package types
|
||||
|
||||
type Message struct {
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type MessageLoki struct {
|
||||
Status string `json:"status"`
|
||||
Data struct {
|
||||
ResultType string `json:"resultType"`
|
||||
@ -76,3 +80,12 @@ var MapSQLDeveloper = make(map[string]string)
|
||||
|
||||
// MapTelegramUsers - карта имя в телеграм - id чата в телеграм
|
||||
var MapTelegramUsers = make(map[string]int64)
|
||||
|
||||
// Message - структура сообщения для отправки в Телеграм
|
||||
type Message struct {
|
||||
ServiceName string
|
||||
DeveloperName string
|
||||
LokiURL string
|
||||
Date time.Time
|
||||
Text string
|
||||
}
|
||||
|
@ -1 +1 @@
|
||||
359b624 2024-11-14 15:23:17 +0300
|
||||
c8c6027 2024-11-15 09:40:07 +0300
|
||||
|
Loading…
Reference in New Issue
Block a user