2023-07-08 08:43:30 -06:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2023-08-24 13:28:57 -06:00
|
|
|
"context"
|
2023-07-08 08:43:30 -06:00
|
|
|
"embed"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg"
|
2023-08-24 13:28:57 -06:00
|
|
|
"github.com/fastenhealth/fastenhealth-onprem/backend/pkg/database"
|
2023-07-08 08:43:30 -06:00
|
|
|
"github.com/gin-gonic/gin"
|
2023-08-24 13:28:57 -06:00
|
|
|
"github.com/google/go-github/v54/github"
|
2023-07-08 08:43:30 -06:00
|
|
|
"github.com/sirupsen/logrus"
|
2023-08-24 13:28:57 -06:00
|
|
|
"golang.org/x/exp/slices"
|
2023-07-08 08:43:30 -06:00
|
|
|
"io/fs"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
//go:embed dashboard/*.json
|
|
|
|
var dashboardFS embed.FS
|
|
|
|
|
|
|
|
func GetDashboard(c *gin.Context) {
|
|
|
|
logger := c.MustGet(pkg.ContextKeyTypeLogger).(*logrus.Entry)
|
2023-08-24 13:28:57 -06:00
|
|
|
//appConfig := c.MustGet(pkg.ContextKeyTypeConfig).(config.Interface)
|
|
|
|
databaseRepo := c.MustGet(pkg.ContextKeyTypeDatabase).(database.DatabaseRepository)
|
2023-07-08 08:43:30 -06:00
|
|
|
|
|
|
|
var dirEntries []fs.DirEntry
|
|
|
|
var err error
|
|
|
|
|
2023-08-24 13:28:57 -06:00
|
|
|
//load settings from dashboard
|
|
|
|
logger.Infof("Loading User Settings..")
|
|
|
|
userSettings, err := databaseRepo.LoadUserSettings(c)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logger.Debugf("User Settings: %v", userSettings)
|
2023-07-08 08:43:30 -06:00
|
|
|
|
2023-08-24 13:28:57 -06:00
|
|
|
//get current user
|
|
|
|
currentUser, err := databaseRepo.GetCurrentUser(c)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("Error getting current user: %v", err)
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
2023-07-08 08:43:30 -06:00
|
|
|
|
2023-08-24 13:28:57 -06:00
|
|
|
var dashboards []map[string]interface{}
|
2023-07-08 08:43:30 -06:00
|
|
|
|
2023-08-24 13:28:57 -06:00
|
|
|
if userSettings.DashboardLocations != nil && len(userSettings.DashboardLocations) > 0 {
|
|
|
|
logger.Infof("Loading dashboard(s) from %v", userSettings.DashboardLocations)
|
2023-07-08 08:43:30 -06:00
|
|
|
|
2023-08-24 13:28:57 -06:00
|
|
|
// initialize the cache directory
|
|
|
|
cacheDir, err := getCacheDir(currentUser.ID.String())
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("Error creating cache directory: %v", err)
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
2023-07-08 08:43:30 -06:00
|
|
|
|
2023-08-24 13:28:57 -06:00
|
|
|
dirEntries, err = os.ReadDir(cacheDir)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
2023-07-08 08:43:30 -06:00
|
|
|
|
2023-08-24 18:07:06 -06:00
|
|
|
dashboards, err = getDashboardFromDir(cacheDir, dirEntries, os.ReadFile)
|
2023-08-24 13:28:57 -06:00
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": err.Error()})
|
|
|
|
return
|
2023-07-08 08:43:30 -06:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dirEntries, err = dashboardFS.ReadDir("dashboard")
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-24 18:07:06 -06:00
|
|
|
dashboards, err = getDashboardFromDir("dashboard", dirEntries, dashboardFS.ReadFile)
|
2023-07-08 08:43:30 -06:00
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": dashboards})
|
|
|
|
}
|
|
|
|
|
2023-08-24 18:07:06 -06:00
|
|
|
func AddDashboardLocation(c *gin.Context) {
|
2023-08-24 13:28:57 -06:00
|
|
|
logger := c.MustGet(pkg.ContextKeyTypeLogger).(*logrus.Entry)
|
|
|
|
//appConfig := c.MustGet(pkg.ContextKeyTypeConfig).(config.Interface)
|
|
|
|
databaseRepo := c.MustGet(pkg.ContextKeyTypeDatabase).(database.DatabaseRepository)
|
|
|
|
|
2023-08-24 18:07:06 -06:00
|
|
|
var dashboardLocation map[string]string
|
|
|
|
if err := c.ShouldBindJSON(&dashboardLocation); err != nil {
|
|
|
|
logger.Errorln("An error occurred while parsing new dashboard location", err)
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"success": false})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-24 13:28:57 -06:00
|
|
|
//load settings from database
|
|
|
|
logger.Infof("Loading User Settings..")
|
|
|
|
userSettings, err := databaseRepo.LoadUserSettings(c)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
2023-08-24 18:07:06 -06:00
|
|
|
|
2023-08-24 13:28:57 -06:00
|
|
|
//override locations with new locations
|
2023-08-24 18:07:06 -06:00
|
|
|
userSettings.DashboardLocations = append(userSettings.DashboardLocations, dashboardLocation["location"])
|
2023-08-24 13:28:57 -06:00
|
|
|
|
|
|
|
logger.Debugf("User Settings: %v", userSettings)
|
|
|
|
|
|
|
|
//get current user
|
|
|
|
currentUser, err := databaseRepo.GetCurrentUser(c)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("Error getting current user: %v", err)
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cacheDir, err := getCacheDir(currentUser.ID.String())
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("Error creating cache directory: %v", err)
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialize the github client (Anonymous access)
|
|
|
|
githubClient := github.NewClient(nil)
|
|
|
|
|
|
|
|
cacheErrors := map[string]error{}
|
|
|
|
cacheDashboards := []string{}
|
|
|
|
for _, remoteDashboardLocation := range userSettings.DashboardLocations {
|
|
|
|
cacheDashboardLocation, err := cacheCustomDashboard(logger, githubClient, cacheDir, remoteDashboardLocation)
|
|
|
|
if err != nil {
|
|
|
|
cacheErrors[remoteDashboardLocation] = err
|
|
|
|
}
|
|
|
|
cacheDashboards = append(cacheDashboards, filepath.Base(cacheDashboardLocation))
|
|
|
|
}
|
2023-08-24 18:07:06 -06:00
|
|
|
if len(cacheErrors) > 0 {
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": fmt.Errorf("error caching dashboards: %v", cacheErrors)})
|
|
|
|
return
|
|
|
|
}
|
2023-08-24 13:28:57 -06:00
|
|
|
//cleanup any files in the cache that are no longer in the dashboard locations
|
2023-08-24 18:07:06 -06:00
|
|
|
logger.Infof("Cleaning cache dir: %v", cacheDir)
|
2023-08-24 13:28:57 -06:00
|
|
|
dirEntries, err := os.ReadDir(cacheDir)
|
|
|
|
if err != nil {
|
2023-08-24 18:07:06 -06:00
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": fmt.Errorf("error before cleaning cache dir: %v", err)})
|
2023-08-24 13:28:57 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, dirEntry := range dirEntries {
|
|
|
|
if !slices.Contains(cacheDashboards, dirEntry.Name()) {
|
|
|
|
logger.Debugf("Removing %v from cache", dirEntry.Name())
|
|
|
|
err = os.RemoveAll(filepath.Join(cacheDir, dirEntry.Name()))
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("Error removing %v from cache: %v", dirEntry.Name(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-24 18:07:06 -06:00
|
|
|
err = databaseRepo.SaveUserSettings(c, userSettings)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": fmt.Errorf("error saving user settings: %v", err)})
|
2023-08-24 13:28:57 -06:00
|
|
|
return
|
|
|
|
}
|
2023-08-24 18:07:06 -06:00
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true})
|
|
|
|
return
|
2023-08-24 13:28:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
//private functions
|
|
|
|
func getCacheDir(currentUserId string) (string, error) {
|
|
|
|
// initialize the cache directory
|
|
|
|
cacheDir := filepath.Join("cache", currentUserId, "dashboard")
|
|
|
|
err := os.MkdirAll(cacheDir, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error creating cache directory: %v", err)
|
|
|
|
}
|
|
|
|
return cacheDir, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func cacheCustomDashboard(logger *logrus.Entry, githubClient *github.Client, cacheDir string, remoteDashboardLocation string) (string, error) {
|
|
|
|
//- validate that the url is to a github gist, no other locations are supported
|
|
|
|
//- download the gist metadata
|
|
|
|
//- if more than 1 file found, look for a dashboard.json
|
|
|
|
//- check if the file sha exists on the file system (content-addressible file system)
|
|
|
|
//- if its not present, download it
|
|
|
|
//- if its not json, throw an error
|
|
|
|
//- if it doesnt match the dashboard config schema, throw an error.
|
|
|
|
|
|
|
|
if !strings.HasPrefix(remoteDashboardLocation, "https://gist.github.com") {
|
|
|
|
return "", fmt.Errorf("remote dashboard location is not a github gist: %v", remoteDashboardLocation)
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Infof("Processing custom dashboard from %v", remoteDashboardLocation)
|
|
|
|
|
2023-08-24 18:07:06 -06:00
|
|
|
gistSlug := strings.TrimPrefix(remoteDashboardLocation, "https://gist.github.com/")
|
|
|
|
gistSlugParts := strings.Split(gistSlug, "/")
|
|
|
|
if len(gistSlugParts) != 2 {
|
|
|
|
return "", fmt.Errorf("invalid gist slug: %v", gistSlug)
|
|
|
|
}
|
|
|
|
|
|
|
|
gist, _, err := githubClient.Gists.Get(context.Background(), gistSlugParts[1])
|
2023-08-24 13:28:57 -06:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error retrieving remote gist: %v", err)
|
|
|
|
}
|
2023-08-24 18:07:06 -06:00
|
|
|
logger.Debugf("Got Gist %v, files: %d", gist.GetID(), len(gist.GetFiles()))
|
2023-08-24 13:28:57 -06:00
|
|
|
|
|
|
|
//check if gist has more than 1 file
|
|
|
|
var dashboardJsonFile github.GistFile
|
|
|
|
if len(gist.Files) == 0 {
|
|
|
|
return "", fmt.Errorf("gist has no files: %v", remoteDashboardLocation)
|
|
|
|
}
|
|
|
|
if len(gist.Files) > 1 {
|
|
|
|
//find the dashboard.json file
|
|
|
|
if gistFile, ok := gist.Files["dashboard.json"]; ok {
|
|
|
|
dashboardJsonFile = gistFile
|
|
|
|
} else {
|
|
|
|
return "", fmt.Errorf("dashboard location gist has more than 1 file and no dashboard.json: %v", remoteDashboardLocation)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//only 1 file, use it
|
|
|
|
for _, gistFile := range gist.Files {
|
|
|
|
dashboardJsonFile = gistFile
|
|
|
|
|
|
|
|
if contentType := gistFile.GetType(); contentType != "application/json" {
|
|
|
|
logger.Warnf("ContentType is not detected as JSON: %v", remoteDashboardLocation)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//ensure that the file is valid json
|
2023-08-24 18:07:06 -06:00
|
|
|
logger.Debugf("validating dashboard gist json")
|
2023-08-24 13:28:57 -06:00
|
|
|
var dashboardJson map[string]interface{}
|
|
|
|
err = json.Unmarshal([]byte(dashboardJsonFile.GetContent()), &dashboardJson)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error unmarshalling dashboard configuration (invalid JSON?): %v", err)
|
|
|
|
}
|
|
|
|
|
2023-08-24 18:07:06 -06:00
|
|
|
dashboardJson["source"] = remoteDashboardLocation
|
|
|
|
dashboardJson["id"] = gist.GetID()
|
|
|
|
|
2023-08-24 13:28:57 -06:00
|
|
|
//TODO: validate against DashboardConfigSchema
|
|
|
|
|
2023-08-24 18:07:06 -06:00
|
|
|
absCacheFileLocation := filepath.Join(cacheDir, fmt.Sprintf("%s.json", gist.GetID()))
|
2023-08-24 13:28:57 -06:00
|
|
|
//write it to filesystem
|
|
|
|
logger.Infof("Writing new dashboard configuration to filesystem: %v", remoteDashboardLocation)
|
|
|
|
|
|
|
|
//write file to cache
|
2023-08-24 18:07:06 -06:00
|
|
|
dashboardJsonBytes, err := json.MarshalIndent(dashboardJson, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error marshalling dashboard configuration: %v", err)
|
|
|
|
}
|
|
|
|
err = os.WriteFile(absCacheFileLocation, dashboardJsonBytes, 0644)
|
2023-08-24 13:28:57 -06:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error writing dashboard configuration to cache: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return absCacheFileLocation, nil
|
|
|
|
}
|
|
|
|
|
2023-08-24 18:07:06 -06:00
|
|
|
func getDashboardFromDir(parentDir string, dirEntries []fs.DirEntry, fsReadFile func(name string) ([]byte, error)) ([]map[string]interface{}, error) {
|
2023-07-08 08:43:30 -06:00
|
|
|
dashboards := []map[string]interface{}{}
|
|
|
|
|
|
|
|
for _, file := range dirEntries {
|
|
|
|
if file.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
//unmarshal file into map
|
2023-08-24 18:07:06 -06:00
|
|
|
embeddedFile, err := fsReadFile(filepath.Join(parentDir, file.Name()))
|
2023-07-08 08:43:30 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var dashboardJson map[string]interface{}
|
|
|
|
err = json.Unmarshal(embeddedFile, &dashboardJson)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dashboards = append(dashboards, dashboardJson)
|
|
|
|
}
|
|
|
|
return dashboards, nil
|
|
|
|
}
|