2024-01-20 01:27:10 +02:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2024-01-21 18:25:23 +02:00
|
|
|
"strconv"
|
2024-01-20 18:35:50 +02:00
|
|
|
"time"
|
2024-01-20 01:27:10 +02:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"tea.chunkbyte.com/kato/drive-health/lib/hardware"
|
2024-01-20 01:40:55 +02:00
|
|
|
"tea.chunkbyte.com/kato/drive-health/lib/svc"
|
2024-01-20 01:27:10 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func setupApi(r *gin.Engine) {
|
2024-01-20 18:35:50 +02:00
|
|
|
api := r.Group("/api/v1")
|
2024-01-20 01:27:10 +02:00
|
|
|
|
2024-01-21 18:25:23 +02:00
|
|
|
api.GET("/disks/:diskid/chart", func(ctx *gin.Context) {
|
|
|
|
diskIDString := ctx.Param("diskid")
|
|
|
|
diskId, err := strconv.Atoi(diskIDString)
|
|
|
|
if err != nil {
|
|
|
|
ctx.AbortWithStatusJSON(400, gin.H{
|
|
|
|
"error": err.Error(),
|
|
|
|
"message": "Invalid Disk ID",
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
graphData, err := svc.GetDiskGraphImage(diskId, nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
ctx.AbortWithStatusJSON(500, gin.H{
|
|
|
|
"error": err.Error(),
|
|
|
|
"message": "Graph generation issue",
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the content type header
|
|
|
|
ctx.Writer.Header().Set("Content-Type", "image/png")
|
|
|
|
|
|
|
|
// Write the image data to the response
|
|
|
|
ctx.Writer.WriteHeader(http.StatusOK)
|
|
|
|
_, err = graphData.WriteTo(ctx.Writer)
|
|
|
|
if err != nil {
|
|
|
|
ctx.AbortWithStatusJSON(500, gin.H{
|
|
|
|
"error": err.Error(),
|
|
|
|
"message": "Write error",
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-01-20 01:27:10 +02:00
|
|
|
api.GET("/disks", func(ctx *gin.Context) {
|
2024-01-20 18:35:50 +02:00
|
|
|
|
|
|
|
olderThan := time.Now().Add(time.Minute * time.Duration(10) * -1)
|
|
|
|
newerThan := time.Now()
|
|
|
|
|
2024-01-20 01:27:10 +02:00
|
|
|
// Fetch the disk list
|
2024-01-20 18:35:50 +02:00
|
|
|
disks, err := hardware.GetSystemHardDrives(svc.GetDatabaseRef(), &olderThan, &newerThan)
|
2024-01-20 01:27:10 +02:00
|
|
|
if err != nil {
|
|
|
|
ctx.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.Request.URL.Query().Get("temp") != "" {
|
|
|
|
for _, d := range disks {
|
2024-01-20 18:35:50 +02:00
|
|
|
d.GetTemperature()
|
2024-01-20 01:27:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, gin.H{
|
|
|
|
"message": "Disk List",
|
|
|
|
"disks": disks,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|