Compare commits
3 Commits
6be0c5fa37
...
d962d77a00
| Author | SHA1 | Date |
|---|---|---|
|
|
d962d77a00 | |
|
|
a6eb6c6ca0 | |
|
|
95ce47a406 |
|
|
@ -48,7 +48,13 @@ func (r *inventoryIssueLineRepository) GetById(ctx context.Context, tx *gorm.DB,
|
||||||
|
|
||||||
func (r *inventoryIssueLineRepository) GetAllByIssueId(ctx context.Context, issueId string) ([]entities.TInventoryIssueLineEntity, error) {
|
func (r *inventoryIssueLineRepository) GetAllByIssueId(ctx context.Context, issueId string) ([]entities.TInventoryIssueLineEntity, error) {
|
||||||
var lines []entities.TInventoryIssueLineEntity
|
var lines []entities.TInventoryIssueLineEntity
|
||||||
if err := r.db.WithContext(ctx).Where("issue_id = ?", issueId).Preload("Product").Preload("Warehouse").Preload("Issue").Preload("Client").Find(&lines).Error; err != nil {
|
if err := r.db.WithContext(ctx).
|
||||||
|
Where("inv_issue_id = ?", issueId).
|
||||||
|
Preload("Product").
|
||||||
|
Preload("Warehouse").
|
||||||
|
Preload("InvIssue").
|
||||||
|
Preload("Client").
|
||||||
|
Find(&lines).Error; err != nil {
|
||||||
return lines, err
|
return lines, err
|
||||||
}
|
}
|
||||||
return lines, nil
|
return lines, nil
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,11 @@ func (r *inventoryReturnLineRepository) DeleteByReturnId(ctx context.Context, tx
|
||||||
// GetAllByReturnId implements InventoryReturnLineRepository.
|
// GetAllByReturnId implements InventoryReturnLineRepository.
|
||||||
func (r *inventoryReturnLineRepository) GetAllByReturnId(ctx context.Context, returnId string) ([]entities.TInventoryReturnLineEntity, error) {
|
func (r *inventoryReturnLineRepository) GetAllByReturnId(ctx context.Context, returnId string) ([]entities.TInventoryReturnLineEntity, error) {
|
||||||
var lines []entities.TInventoryReturnLineEntity
|
var lines []entities.TInventoryReturnLineEntity
|
||||||
if err := r.db.WithContext(ctx).Where("inv_return_id = ?", returnId).Find(&lines).Error; err != nil {
|
if err := r.db.WithContext(ctx).Where("inv_return_id = ?", returnId).
|
||||||
|
Preload("Product").
|
||||||
|
Preload("InvReturn").
|
||||||
|
Preload("Client").
|
||||||
|
Find(&lines).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return lines, nil
|
return lines, nil
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
@ -11,10 +12,124 @@ import (
|
||||||
type MonitoringController interface {
|
type MonitoringController interface {
|
||||||
HealthCheck(ctx *gin.Context)
|
HealthCheck(ctx *gin.Context)
|
||||||
ReadErrorLog(ctx *gin.Context)
|
ReadErrorLog(ctx *gin.Context)
|
||||||
|
ReadQueryLog(ctx *gin.Context)
|
||||||
|
ReadAppLog(ctx *gin.Context)
|
||||||
}
|
}
|
||||||
|
|
||||||
type monitoringController struct{}
|
type monitoringController struct{}
|
||||||
|
|
||||||
|
// ReadAppLog implements MonitoringController.
|
||||||
|
func (c *monitoringController) ReadAppLog(ctx *gin.Context) {
|
||||||
|
// Ambil query param 'month', default ke bulan sekarang jika tidak ada
|
||||||
|
month := ctx.Query("month")
|
||||||
|
if month == "" {
|
||||||
|
month = time.Now().Format("January") // contoh: "December"
|
||||||
|
} else {
|
||||||
|
// Pastikan format bulan sesuai dengan nama bulan bahasa Inggris
|
||||||
|
// Misal: "december" -> "December"
|
||||||
|
month = strings.Title(strings.ToLower(month))
|
||||||
|
}
|
||||||
|
|
||||||
|
logFile := strings.ToLower(month) + "_app.log" // contoh: "december_app.log"
|
||||||
|
|
||||||
|
// Cari file log di beberapa lokasi
|
||||||
|
locations := []string{
|
||||||
|
"config/logs/query_log/" + logFile, // lokasi log sesuai permintaan
|
||||||
|
"./logs/" + logFile,
|
||||||
|
"/tmp/" + logFile,
|
||||||
|
"/var/log/" + logFile,
|
||||||
|
"./" + logFile,
|
||||||
|
}
|
||||||
|
|
||||||
|
var content string
|
||||||
|
var found bool
|
||||||
|
|
||||||
|
for _, location := range locations {
|
||||||
|
if data, err := os.ReadFile(location); err == nil {
|
||||||
|
content = string(data)
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !found {
|
||||||
|
ctx.JSON(http.StatusNotFound, gin.H{
|
||||||
|
"error": "App log file not found",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return sebagai plain text atau JSON
|
||||||
|
if strings.Contains(ctx.GetHeader("Accept"), "application/json") {
|
||||||
|
lines := strings.Split(strings.ReplaceAll(content, "\r\n", "\n"), "\n")
|
||||||
|
if len(lines) > 0 && lines[len(lines)-1] == "" {
|
||||||
|
lines = lines[:len(lines)-1]
|
||||||
|
}
|
||||||
|
ctx.JSON(http.StatusOK, gin.H{
|
||||||
|
"log_entries": lines,
|
||||||
|
"total_lines": len(lines),
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
ctx.Data(http.StatusOK, "text/plain; charset=utf-8", []byte(content))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReadQueryLog implements MonitoringController.
|
||||||
|
func (c *monitoringController) ReadQueryLog(ctx *gin.Context) {
|
||||||
|
// Ambil query param 'month', default ke bulan sekarang jika tidak ada
|
||||||
|
month := ctx.Query("month")
|
||||||
|
if month == "" {
|
||||||
|
month = time.Now().Format("January") // contoh: "December"
|
||||||
|
} else {
|
||||||
|
// Pastikan format bulan sesuai dengan nama bulan bahasa Inggris
|
||||||
|
// Misal: "december" -> "December"
|
||||||
|
month = strings.Title(strings.ToLower(month))
|
||||||
|
}
|
||||||
|
|
||||||
|
logFile := strings.ToLower(month) + "_query.log" // contoh: "december_query.log"
|
||||||
|
|
||||||
|
// Lokasi file log
|
||||||
|
locations := []string{
|
||||||
|
"config/logs/query_log/" + logFile,
|
||||||
|
"./logs/" + logFile,
|
||||||
|
"/tmp/" + logFile,
|
||||||
|
"/var/log/" + logFile,
|
||||||
|
"./" + logFile,
|
||||||
|
}
|
||||||
|
|
||||||
|
var content string
|
||||||
|
var found bool
|
||||||
|
|
||||||
|
for _, location := range locations {
|
||||||
|
if data, err := os.ReadFile(location); err == nil {
|
||||||
|
content = string(data)
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !found {
|
||||||
|
ctx.JSON(http.StatusNotFound, gin.H{
|
||||||
|
"error": "Query log file not found",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return sebagai plain text atau JSON
|
||||||
|
if strings.Contains(ctx.GetHeader("Accept"), "application/json") {
|
||||||
|
lines := strings.Split(strings.ReplaceAll(content, "\r\n", "\n"), "\n")
|
||||||
|
if len(lines) > 0 && lines[len(lines)-1] == "" {
|
||||||
|
lines = lines[:len(lines)-1]
|
||||||
|
}
|
||||||
|
ctx.JSON(http.StatusOK, gin.H{
|
||||||
|
"log_entries": lines,
|
||||||
|
"total_lines": len(lines),
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
ctx.Data(http.StatusOK, "text/plain; charset=utf-8", []byte(content))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func NewMonitoringController() MonitoringController {
|
func NewMonitoringController() MonitoringController {
|
||||||
return &monitoringController{}
|
return &monitoringController{}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,5 +13,8 @@ func RegisterRoutes(server *gin.Engine) {
|
||||||
{
|
{
|
||||||
monitoring.GET("/health", monitoringController.HealthCheck)
|
monitoring.GET("/health", monitoringController.HealthCheck)
|
||||||
monitoring.GET("/errors", monitoringController.ReadErrorLog)
|
monitoring.GET("/errors", monitoringController.ReadErrorLog)
|
||||||
|
// get log query_log and app_log
|
||||||
|
monitoring.GET("/logs/query", monitoringController.ReadQueryLog)
|
||||||
|
monitoring.GET("/logs/app", monitoringController.ReadAppLog)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue