diff --git a/bin/date.txt b/bin/date.txt
index c44844a..23f69a2 100644
--- a/bin/date.txt
+++ b/bin/date.txt
@@ -1 +1 @@
-2024-11-14 14:40:02.659
\ No newline at end of file
+2024-11-15 08:27:48.861
\ No newline at end of file
diff --git a/bin/subversion.txt b/bin/subversion.txt
index 3e3908a..813d714 100644
--- a/bin/subversion.txt
+++ b/bin/subversion.txt
@@ -1 +1 @@
-00183
\ No newline at end of file
+00184
\ No newline at end of file
diff --git a/internal/logic/loki/loki.go b/internal/logic/loki/loki.go
index 2d4ad34..b34f9b4 100644
--- a/internal/logic/loki/loki.go
+++ b/internal/logic/loki/loki.go
@@ -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 := `` + ServiceName + ""
+ URL := FindURLLoki(Message1.ServiceName, DateFrom, DateTo)
+ //TextServiceName := `` + Message1.ServiceName + ""
//отправим ошибки в 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 на которую можно кликнуть в телеграмме
diff --git a/internal/logic/sql/sql.go b/internal/logic/sql/sql.go
index f4db4e2..b069eee 100644
--- a/internal/logic/sql/sql.go
+++ b/internal/logic/sql/sql.go
@@ -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
}
diff --git a/internal/loki_http/loki_http.go b/internal/loki_http/loki_http.go
index 4600d21..2a11a0e 100644
--- a/internal/loki_http/loki_http.go
+++ b/internal/loki_http/loki_http.go
@@ -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 {
diff --git a/internal/telegram/telegram.go b/internal/telegram/telegram.go
index 5aea7b4..6ee5f10 100644
--- a/internal/telegram/telegram.go
+++ b/internal/telegram/telegram.go
@@ -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 = `` + Message1.ServiceName + ""
+ }
+ 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 {
diff --git a/internal/types/types.go b/internal/types/types.go
index b5284fb..7b394c9 100644
--- a/internal/types/types.go
+++ b/internal/types/types.go
@@ -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
+}
diff --git a/pkg/version/version.txt b/pkg/version/version.txt
index 93d2b01..839a320 100644
--- a/pkg/version/version.txt
+++ b/pkg/version/version.txt
@@ -1 +1 @@
-359b624 2024-11-14 15:23:17 +0300
+c8c6027 2024-11-15 09:40:07 +0300