Moved version file into webapp source tree. Replacing fmt with logger calls. moved Jobber config file into /scrutiny/jobber directory. Added scsi json file. Moved API rooute handlers into their own files in a module. Added not yet implemnented tooltips.

This commit is contained in:
Jason Kulatunga
2020-08-21 18:06:00 -07:00
parent c14f313fa1
commit d636709aa1
23 changed files with 267 additions and 200 deletions
-8
View File
@@ -1,8 +0,0 @@
package web
// the following cronjobs need to be defined here:
// - get storage information for all approved disks
// - run short test against approved disks
// - run long test against approved disks
// - get S.M.A.R.T. metrics from approved disks
// - clean up / resolution for time-series data in sqlite database.
@@ -0,0 +1,27 @@
package handler
import (
"github.com/analogj/scrutiny/webapp/backend/pkg/metadata"
dbModels "github.com/analogj/scrutiny/webapp/backend/pkg/models/db"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"net/http"
)
func GetDeviceDetails(c *gin.Context) {
db := c.MustGet("DB").(*gorm.DB)
device := dbModels.Device{}
db.Debug().
Preload("SmartResults", func(db *gorm.DB) *gorm.DB {
return db.Order("smarts.created_at DESC").Limit(40)
}).
Preload("SmartResults.SmartAttributes").
Where("wwn = ?", c.Param("wwn")).
First(&device)
device.SquashHistory()
device.ApplyMetadataRules()
c.JSON(http.StatusOK, gin.H{"success": true, "data": device, "lookup": metadata.AtaSmartAttributes})
}
@@ -0,0 +1,26 @@
package handler
import (
dbModels "github.com/analogj/scrutiny/webapp/backend/pkg/models/db"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"net/http"
)
func GetDevicesSummary(c *gin.Context) {
db := c.MustGet("DB").(*gorm.DB)
devices := []dbModels.Device{}
//We need the last x (for now all) Smart objects for each Device, so that we can graph Temperature
//We also need the last
db.Debug().
Preload("SmartResults", func(db *gorm.DB) *gorm.DB {
return db.Order("smarts.created_at DESC") //OLD: .Limit(devicesCount)
}).
Find(&devices)
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": devices,
})
}
@@ -0,0 +1,39 @@
package handler
import (
dbModels "github.com/analogj/scrutiny/webapp/backend/pkg/models/db"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
log "github.com/sirupsen/logrus"
"net/http"
)
// filter devices that are detected by various collectors.
func RegisterDevices(c *gin.Context) {
db := c.MustGet("DB").(*gorm.DB)
var collectorDeviceWrapper dbModels.DeviceWrapper
err := c.BindJSON(&collectorDeviceWrapper)
if err != nil {
log.Error("Cannot parse detected devices")
c.JSON(http.StatusOK, gin.H{"success": false})
}
//TODO: filter devices here (remove excludes, force includes)
for _, dev := range collectorDeviceWrapper.Data {
//insert devices into DB if not already there.
db.Where(dbModels.Device{WWN: dev.WWN}).FirstOrCreate(&dev)
}
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
})
} else {
c.JSON(http.StatusOK, dbModels.DeviceWrapper{
Success: true,
Data: collectorDeviceWrapper.Data,
})
}
}
@@ -0,0 +1,40 @@
package handler
import (
"github.com/analogj/scrutiny/webapp/backend/pkg/models/collector"
dbModels "github.com/analogj/scrutiny/webapp/backend/pkg/models/db"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
log "github.com/sirupsen/logrus"
"net/http"
)
func UploadDeviceMetrics(c *gin.Context) {
db := c.MustGet("DB").(*gorm.DB)
var collectorSmartData collector.SmartInfo
err := c.BindJSON(&collectorSmartData)
if err != nil {
//TODO: cannot parse smart data
log.Error("Cannot parse SMART data")
c.JSON(http.StatusOK, gin.H{"success": false})
}
//update the device information if necessary
var device dbModels.Device
db.Where("wwn = ?", c.Param("wwn")).First(&device)
device.UpdateFromCollectorSmartInfo(collectorSmartData)
db.Model(&device).Updates(device)
// insert smart info
deviceSmartData := dbModels.Smart{}
err = deviceSmartData.FromCollectorSmartInfo(c.Param("wwn"), collectorSmartData)
if err != nil {
c.JSON(http.StatusOK, gin.H{"success": false})
return
}
db.Create(&deviceSmartData)
c.JSON(http.StatusOK, gin.H{"success": true})
}
@@ -0,0 +1,7 @@
package handler
import "github.com/gin-gonic/gin"
func UploadDeviceSelfTests(c *gin.Context) {
}
+7 -131
View File
@@ -4,12 +4,8 @@ import (
"fmt"
"github.com/analogj/scrutiny/webapp/backend/pkg/config"
"github.com/analogj/scrutiny/webapp/backend/pkg/database"
"github.com/analogj/scrutiny/webapp/backend/pkg/metadata"
"github.com/analogj/scrutiny/webapp/backend/pkg/models/collector"
dbModels "github.com/analogj/scrutiny/webapp/backend/pkg/models/db"
"github.com/analogj/scrutiny/webapp/backend/pkg/web/handler"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
log "github.com/sirupsen/logrus"
"net/http"
)
@@ -30,13 +26,12 @@ func (ae *AppEngine) Start() error {
})
})
//TODO: notifications
api.POST("/devices/register", RegisterDevices)
api.GET("/summary", GetDevicesSummary)
api.POST("/device/:wwn/smart", UploadDeviceSmartData)
api.POST("/device/:wwn/selftest", UploadDeviceSelfTestData)
api.POST("/devices/register", handler.RegisterDevices)
api.GET("/summary", handler.GetDevicesSummary)
api.POST("/device/:wwn/smart", handler.UploadDeviceMetrics)
api.POST("/device/:wwn/selftest", handler.UploadDeviceSelfTests)
api.GET("/device/:wwn/details", GetDeviceDetails)
api.GET("/device/:wwn/details", handler.GetDeviceDetails)
}
//Static request routing
@@ -52,124 +47,5 @@ func (ae *AppEngine) Start() error {
c.File(fmt.Sprintf("%s/index.html", ae.Config.GetString("web.src.frontend.path")))
})
return r.Run(fmt.Sprintf("%s:%s", ae.Config.GetString("web.listen.host"), ae.Config.GetString("web.listen.port"))) // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
// filter devices that are detected by various collectors.
func RegisterDevices(c *gin.Context) {
db := c.MustGet("DB").(*gorm.DB)
var collectorDeviceWrapper dbModels.DeviceWrapper
err := c.BindJSON(&collectorDeviceWrapper)
if err != nil {
log.Error("Cannot parse detected devices")
c.JSON(http.StatusOK, gin.H{"success": false})
}
//TODO: filter devices here (remove excludes, force includes)
for _, dev := range collectorDeviceWrapper.Data {
//insert devices into DB if not already there.
db.Where(dbModels.Device{WWN: dev.WWN}).FirstOrCreate(&dev)
}
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
})
} else {
c.JSON(http.StatusOK, dbModels.DeviceWrapper{
Success: true,
Data: collectorDeviceWrapper.Data,
})
}
}
func UploadDeviceSmartData(c *gin.Context) {
db := c.MustGet("DB").(*gorm.DB)
var collectorSmartData collector.SmartInfo
err := c.BindJSON(&collectorSmartData)
if err != nil {
//TODO: cannot parse smart data
log.Error("Cannot parse SMART data")
c.JSON(http.StatusOK, gin.H{"success": false})
}
//update the device information if necessary
var device dbModels.Device
db.Where("wwn = ?", c.Param("wwn")).First(&device)
device.UpdateFromCollectorSmartInfo(collectorSmartData)
db.Model(&device).Updates(device)
// insert smart info
deviceSmartData := dbModels.Smart{}
err = deviceSmartData.FromCollectorSmartInfo(c.Param("wwn"), collectorSmartData)
if err != nil {
c.JSON(http.StatusOK, gin.H{"success": false})
return
}
db.Create(&deviceSmartData)
c.JSON(http.StatusOK, gin.H{"success": true})
}
func UploadDeviceSelfTestData(c *gin.Context) {
}
func GetDeviceDetails(c *gin.Context) {
db := c.MustGet("DB").(*gorm.DB)
device := dbModels.Device{}
db.Debug().
Preload("SmartResults", func(db *gorm.DB) *gorm.DB {
return db.Order("smarts.created_at DESC").Limit(40)
}).
Preload("SmartResults.SmartAttributes").
Where("wwn = ?", c.Param("wwn")).
First(&device)
device.SquashHistory()
device.ApplyMetadataRules()
c.JSON(http.StatusOK, gin.H{"success": true, "data": device, "lookup": metadata.AtaSmartAttributes})
}
func GetDevicesSummary(c *gin.Context) {
db := c.MustGet("DB").(*gorm.DB)
devices := []dbModels.Device{}
//OLD: cant seem to figure out how to get the latest SmartResults for each Device, so instead
// we're going to assume that results were retrieved at the same time, so we'll just get the last x number of results
//var devicesCount int
//db.Table("devices").Count(&devicesCount)
//We need the last x (for now all) Smart objects for each Device, so that we can graph Temperature
//We also need the last
db.Debug().
Preload("SmartResults", func(db *gorm.DB) *gorm.DB {
return db.Order("smarts.created_at DESC") //OLD: .Limit(devicesCount)
}).
//Preload("SmartResults").
// Preload("SmartResults.SmartAttributes").
Find(&devices)
//for _, dev := range devices {
// log.Printf("===== device: %s\n", dev.WWN)
// log.Print(len(dev.SmartResults))
//}
//a, _ := json.Marshal(devices) //get json byte array
//n := len(a) //Find the length of the byte array
//s := string(a[:n]) //convert to string
//log.Print(s) //write to response
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": devices,
})
//c.Data(http.StatusOK, "application/json", a)
return r.Run(fmt.Sprintf("%s:%s", ae.Config.GetString("web.listen.host"), ae.Config.GetString("web.listen.port")))
}