2019-06-20 12:52:32 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package consoleserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-06-24 16:15:31 +01:00
|
|
|
"encoding/json"
|
2019-06-20 12:52:32 +01:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
2019-06-24 16:15:31 +01:00
|
|
|
"time"
|
2019-06-20 12:52:32 +01:00
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"golang.org/x/sync/errgroup"
|
2019-07-17 12:42:00 +01:00
|
|
|
"gopkg.in/spacemonkeygo/monkit.v2"
|
2019-06-20 12:52:32 +01:00
|
|
|
|
2019-07-17 12:42:00 +01:00
|
|
|
"storj.io/storj/internal/date"
|
2019-06-24 16:15:31 +01:00
|
|
|
"storj.io/storj/internal/version"
|
|
|
|
"storj.io/storj/pkg/storj"
|
2019-06-20 12:52:32 +01:00
|
|
|
"storj.io/storj/storagenode/console"
|
2019-07-17 12:42:00 +01:00
|
|
|
"storj.io/storj/storagenode/nodestats"
|
2019-06-20 12:52:32 +01:00
|
|
|
)
|
|
|
|
|
2019-06-24 16:15:31 +01:00
|
|
|
const (
|
|
|
|
contentType = "Content-Type"
|
|
|
|
|
|
|
|
applicationJSON = "application/json"
|
|
|
|
)
|
|
|
|
|
2019-06-20 12:52:32 +01:00
|
|
|
// Error is storagenode console web error type
|
2019-06-24 16:15:31 +01:00
|
|
|
var (
|
|
|
|
mon = monkit.Package()
|
|
|
|
Error = errs.Class("storagenode console web error")
|
|
|
|
)
|
2019-06-20 12:52:32 +01:00
|
|
|
|
|
|
|
// Config contains configuration for storagenode console web server
|
|
|
|
type Config struct {
|
|
|
|
Address string `help:"server address of the api gateway and frontend app" default:"127.0.0.1:14002"`
|
|
|
|
StaticDir string `help:"path to static resources" default:""`
|
|
|
|
}
|
|
|
|
|
2019-07-17 12:42:00 +01:00
|
|
|
// DashboardResponse stores data and error message
|
|
|
|
type DashboardResponse struct {
|
|
|
|
Data DashboardData `json:"data"`
|
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// DashboardData stores all needed information about storagenode
|
|
|
|
type DashboardData struct {
|
|
|
|
Bandwidth console.BandwidthInfo `json:"bandwidth"`
|
|
|
|
DiskSpace console.DiskSpaceInfo `json:"diskSpace"`
|
|
|
|
WalletAddress string `json:"walletAddress"`
|
|
|
|
VersionInfo version.Info `json:"versionInfo"`
|
|
|
|
IsLastVersion bool `json:"isLastVersion"`
|
|
|
|
Uptime time.Duration `json:"uptime"`
|
|
|
|
NodeID string `json:"nodeId"`
|
|
|
|
Satellites storj.NodeIDList `json:"satellites"`
|
|
|
|
UptimeCheck nodestats.ReputationStats `json:"uptimeCheck"`
|
|
|
|
AuditCheck nodestats.ReputationStats `json:"auditCheck"`
|
|
|
|
BandwidthChartData []console.BandwidthUsed `json:"bandwidthChartData"`
|
|
|
|
DiskSpaceChartData []nodestats.SpaceUsageStamp `json:"diskSpaceChartData"`
|
|
|
|
}
|
|
|
|
|
2019-06-20 12:52:32 +01:00
|
|
|
// Server represents storagenode console web server
|
|
|
|
type Server struct {
|
|
|
|
log *zap.Logger
|
|
|
|
|
|
|
|
config Config
|
|
|
|
service *console.Service
|
|
|
|
listener net.Listener
|
|
|
|
|
|
|
|
server http.Server
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer creates new instance of storagenode console web server
|
|
|
|
func NewServer(logger *zap.Logger, config Config, service *console.Service, listener net.Listener) *Server {
|
|
|
|
server := Server{
|
|
|
|
log: logger,
|
|
|
|
service: service,
|
|
|
|
config: config,
|
|
|
|
listener: listener,
|
|
|
|
}
|
|
|
|
|
|
|
|
var fs http.Handler
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
|
|
|
|
// handle static pages
|
|
|
|
if config.StaticDir != "" {
|
|
|
|
fs = http.FileServer(http.Dir(server.config.StaticDir))
|
|
|
|
|
|
|
|
mux.Handle("/static/", http.StripPrefix("/static", fs))
|
|
|
|
mux.Handle("/", http.HandlerFunc(server.appHandler))
|
2019-06-24 16:15:31 +01:00
|
|
|
mux.Handle("/api/dashboard/", http.HandlerFunc(server.dashboardHandler))
|
2019-06-20 12:52:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
server.server = http.Server{
|
|
|
|
Handler: mux,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &server
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run starts the server that host webapp and api endpoints
|
2019-07-17 12:42:00 +01:00
|
|
|
func (server *Server) Run(ctx context.Context) (err error) {
|
2019-06-24 16:15:31 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-06-20 12:52:32 +01:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
var group errgroup.Group
|
|
|
|
group.Go(func() error {
|
|
|
|
<-ctx.Done()
|
2019-07-17 12:42:00 +01:00
|
|
|
return server.server.Shutdown(nil)
|
2019-06-20 12:52:32 +01:00
|
|
|
})
|
|
|
|
group.Go(func() error {
|
|
|
|
defer cancel()
|
2019-07-17 12:42:00 +01:00
|
|
|
return server.server.Serve(server.listener)
|
2019-06-20 12:52:32 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
return group.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes server and underlying listener
|
2019-07-17 12:42:00 +01:00
|
|
|
func (server *Server) Close() error {
|
|
|
|
return server.server.Close()
|
2019-06-20 12:52:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// appHandler is web app http handler function
|
2019-07-17 12:42:00 +01:00
|
|
|
func (server *Server) appHandler(writer http.ResponseWriter, request *http.Request) {
|
|
|
|
http.ServeFile(writer, request, filepath.Join(server.config.StaticDir, "dist", "index.html"))
|
2019-06-20 12:52:32 +01:00
|
|
|
}
|
2019-06-24 16:15:31 +01:00
|
|
|
|
|
|
|
// appHandler is web app http handler function
|
2019-07-17 12:42:00 +01:00
|
|
|
func (server *Server) dashboardHandler(writer http.ResponseWriter, request *http.Request) {
|
|
|
|
ctx := request.Context()
|
2019-06-24 16:15:31 +01:00
|
|
|
defer mon.Task()(&ctx)(nil)
|
2019-07-17 12:42:00 +01:00
|
|
|
writer.Header().Set(contentType, applicationJSON)
|
|
|
|
|
|
|
|
var response = DashboardResponse{}
|
2019-06-24 16:15:31 +01:00
|
|
|
|
|
|
|
defer func() {
|
2019-07-17 12:42:00 +01:00
|
|
|
err := json.NewEncoder(writer).Encode(&response)
|
2019-06-24 16:15:31 +01:00
|
|
|
if err != nil {
|
2019-07-17 12:42:00 +01:00
|
|
|
server.log.Error(err.Error())
|
2019-06-24 16:15:31 +01:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-07-17 12:42:00 +01:00
|
|
|
if request.Method != http.MethodGet {
|
|
|
|
writer.WriteHeader(http.StatusNotFound)
|
2019-06-26 14:36:47 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-17 12:42:00 +01:00
|
|
|
satelliteIDParam := request.URL.Query().Get("satelliteId")
|
|
|
|
satelliteID, err := server.parseSatelliteIDParam(satelliteIDParam)
|
2019-06-24 16:15:31 +01:00
|
|
|
if err != nil {
|
2019-07-17 12:42:00 +01:00
|
|
|
server.log.Error("satellite id is not valid", zap.Error(err))
|
2019-06-24 16:15:31 +01:00
|
|
|
response.Error = "satellite id is not valid"
|
2019-07-17 12:42:00 +01:00
|
|
|
writer.WriteHeader(http.StatusBadRequest)
|
2019-06-24 16:15:31 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-17 12:42:00 +01:00
|
|
|
data, err := server.getDashboardData(ctx, satelliteID)
|
2019-06-24 16:15:31 +01:00
|
|
|
if err != nil {
|
2019-07-17 12:42:00 +01:00
|
|
|
server.log.Error("can not get dashboard data", zap.Error(err))
|
|
|
|
response.Error = err.Error()
|
|
|
|
writer.WriteHeader(http.StatusBadRequest)
|
2019-06-24 16:15:31 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-17 12:42:00 +01:00
|
|
|
response.Data = data
|
|
|
|
|
|
|
|
writer.WriteHeader(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (server *Server) getDashboardData(ctx context.Context, satelliteID *storj.NodeID) (DashboardData, error) {
|
|
|
|
var response = DashboardData{}
|
|
|
|
|
|
|
|
satellites, err := server.service.GetSatellites(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return response, err
|
|
|
|
}
|
|
|
|
|
2019-06-26 14:36:47 +01:00
|
|
|
// checks if current satellite id is related to current storage node
|
2019-06-24 16:15:31 +01:00
|
|
|
if satelliteID != nil {
|
2019-07-17 12:42:00 +01:00
|
|
|
if err = server.checkSatelliteID(satellites, *satelliteID); err != nil {
|
|
|
|
return response, err
|
2019-06-24 16:15:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-17 12:42:00 +01:00
|
|
|
space, err := server.getStorage(ctx, satelliteID)
|
2019-06-24 16:15:31 +01:00
|
|
|
if err != nil {
|
2019-07-17 12:42:00 +01:00
|
|
|
return response, err
|
2019-06-24 16:15:31 +01:00
|
|
|
}
|
|
|
|
|
2019-07-17 12:42:00 +01:00
|
|
|
usage, err := server.getBandwidth(ctx, satelliteID)
|
2019-06-24 16:15:31 +01:00
|
|
|
if err != nil {
|
2019-07-17 12:42:00 +01:00
|
|
|
return response, err
|
2019-06-24 16:15:31 +01:00
|
|
|
}
|
|
|
|
|
2019-07-17 12:42:00 +01:00
|
|
|
walletAddress := server.service.GetWalletAddress(ctx)
|
2019-06-24 16:15:31 +01:00
|
|
|
|
2019-07-17 12:42:00 +01:00
|
|
|
versionInfo := server.service.GetVersion(ctx)
|
2019-06-24 16:15:31 +01:00
|
|
|
|
2019-07-17 12:42:00 +01:00
|
|
|
err = server.service.CheckVersion(ctx)
|
2019-06-24 16:15:31 +01:00
|
|
|
if err != nil {
|
2019-07-17 12:42:00 +01:00
|
|
|
return response, err
|
2019-06-24 16:15:31 +01:00
|
|
|
}
|
|
|
|
|
2019-07-17 12:42:00 +01:00
|
|
|
bandwidthChartData, err := server.getBandwidthChartData(ctx, satelliteID)
|
|
|
|
if err != nil {
|
|
|
|
return response, err
|
|
|
|
}
|
2019-06-24 16:15:31 +01:00
|
|
|
|
2019-07-17 12:42:00 +01:00
|
|
|
// TODO: uncomment in future, when caching will be implemented
|
|
|
|
// diskSpaceChartData, err := server.getDiskSpaceChartData(ctx, satelliteID, satellites)
|
|
|
|
// if err != nil {
|
|
|
|
// return response, err
|
|
|
|
// }
|
|
|
|
|
|
|
|
uptime := server.service.GetUptime(ctx)
|
|
|
|
nodeID := server.service.GetNodeID(ctx)
|
|
|
|
|
|
|
|
// TODO: uncomment in future, when caching will be implemented
|
|
|
|
// if satelliteID != nil {
|
|
|
|
// satelliteStats, err := server.service.GetStatsFromSatellite(ctx, *satelliteID)
|
|
|
|
// if err != nil {
|
|
|
|
// return response, err
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// response.UptimeCheck = satelliteStats.UptimeCheck
|
|
|
|
// response.AuditCheck = satelliteStats.AuditCheck
|
|
|
|
// }
|
|
|
|
|
|
|
|
response.DiskSpace = *space
|
|
|
|
response.Bandwidth = *usage
|
|
|
|
response.WalletAddress = walletAddress
|
|
|
|
response.VersionInfo = versionInfo
|
|
|
|
response.IsLastVersion = true
|
|
|
|
response.Uptime = uptime
|
|
|
|
response.NodeID = nodeID.String()
|
|
|
|
response.Satellites = satellites
|
|
|
|
response.BandwidthChartData = bandwidthChartData
|
|
|
|
//response.DiskSpaceChartData = diskSpaceChartData
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|
2019-06-26 14:36:47 +01:00
|
|
|
|
2019-07-17 12:42:00 +01:00
|
|
|
func (server *Server) getBandwidth(ctx context.Context, satelliteID *storj.NodeID) (_ *console.BandwidthInfo, err error) {
|
|
|
|
if satelliteID != nil {
|
|
|
|
return server.service.GetBandwidthBySatellite(ctx, *satelliteID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return server.service.GetUsedBandwidthTotal(ctx)
|
2019-06-24 16:15:31 +01:00
|
|
|
}
|
|
|
|
|
2019-07-17 12:42:00 +01:00
|
|
|
func (server *Server) getBandwidthChartData(ctx context.Context, satelliteID *storj.NodeID) (_ []console.BandwidthUsed, err error) {
|
|
|
|
from, to := date.MonthBoundary()
|
|
|
|
|
2019-06-24 16:15:31 +01:00
|
|
|
if satelliteID != nil {
|
2019-07-17 12:42:00 +01:00
|
|
|
return server.service.GetDailyBandwidthUsed(ctx, *satelliteID, from, to)
|
2019-06-24 16:15:31 +01:00
|
|
|
}
|
|
|
|
|
2019-07-17 12:42:00 +01:00
|
|
|
return server.service.GetDailyTotalBandwidthUsed(ctx, from, to)
|
2019-06-24 16:15:31 +01:00
|
|
|
}
|
|
|
|
|
2019-07-17 12:42:00 +01:00
|
|
|
func (server *Server) getStorage(ctx context.Context, satelliteID *storj.NodeID) (_ *console.DiskSpaceInfo, err error) {
|
2019-06-24 16:15:31 +01:00
|
|
|
if satelliteID != nil {
|
2019-07-17 12:42:00 +01:00
|
|
|
return server.service.GetUsedStorageBySatellite(ctx, *satelliteID)
|
2019-06-24 16:15:31 +01:00
|
|
|
}
|
|
|
|
|
2019-07-17 12:42:00 +01:00
|
|
|
return server.service.GetUsedStorageTotal(ctx)
|
2019-06-24 16:15:31 +01:00
|
|
|
}
|
|
|
|
|
2019-07-17 12:42:00 +01:00
|
|
|
func (server *Server) checkSatelliteID(satelliteIDs storj.NodeIDList, satelliteID storj.NodeID) error {
|
2019-06-24 16:15:31 +01:00
|
|
|
for _, id := range satelliteIDs {
|
|
|
|
if satelliteID == id {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-17 12:42:00 +01:00
|
|
|
return errs.New("satellite id is not found in the available satellite list")
|
2019-06-24 16:15:31 +01:00
|
|
|
}
|
|
|
|
|
2019-07-17 12:42:00 +01:00
|
|
|
func (server *Server) parseSatelliteIDParam(satelliteID string) (*storj.NodeID, error) {
|
2019-06-24 16:15:31 +01:00
|
|
|
if satelliteID != "" {
|
|
|
|
id, err := storj.NodeIDFromString(satelliteID)
|
|
|
|
return &id, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|