Skip to content

Commit

Permalink
meld all parent dbc files if any
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesReate committed Sep 19, 2024
1 parent 5703b07 commit 1454291
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 42 deletions.
29 changes: 18 additions & 11 deletions internal/controllers/device_config_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/binary"
"encoding/json"
"fmt"
"github.com/volatiletech/sqlboiler/v4/queries/qm"
"io"
"strings"
"time"
Expand Down Expand Up @@ -248,7 +249,7 @@ func (d *DeviceConfigController) GetDeviceSettingsByName(c *fiber.Ctx) error {
}

// GetDBCFileByTemplateName godoc
// @Description Fetches the DBC file from the dbc_files table given a template name
// @Description Fetches the DBC file from the dbc_files table given a template name. Will get all the parent dbc files as well and meld them together
// @Tags device-config
// @Produce plain
// @Success 200 {string} string "Successfully retrieved DBC file"
Expand All @@ -259,22 +260,28 @@ func (d *DeviceConfigController) GetDBCFileByTemplateName(c *fiber.Ctx) error {
templateNameWithVersion := c.Params("templateName")
templateName, _ := parseOutTemplateAndVersion(templateNameWithVersion)
// ignore version since not really using right now

dbResult, err := models.DBCFiles(
models.DBCFileWhere.TemplateName.EQ(templateName)).One(c.Context(), d.dbs().Reader)

// Error handling
templateNames, _, err := queries.GetAllParentTemplates(c.Context(), d.dbs, templateName)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return c.Status(fiber.StatusNotFound).SendString(fmt.Sprintf("No DBC file found for template name: %s", templateName))
}
return errors.Wrap(err, "Failed to retrieve DBC File")
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}

dbcFiles, err := models.DBCFiles(
qm.WhereIn("template_name IN ?", queries.ToAnySlice(templateNames)...),
).All(c.Context(), d.dbs().Reader)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
// string append each dbc file
var dbcFileStrings []string

Check failure on line 275 in internal/controllers/device_config_controller.go

View workflow job for this annotation

GitHub Actions / lint

Consider pre-allocating `dbcFileStrings` (prealloc)
for _, dbcFile := range dbcFiles {
dbcFileStrings = append(dbcFileStrings, dbcFile.DBCFile)
}
dbResult := strings.Join(dbcFileStrings, "\n")

// Return the DBC file itself
if c.Accepts("text/plain") == "text/plain" {
c.Status(fiber.StatusOK).Set("Content-Type", "text/plain")
return c.SendString(dbResult.DBCFile)
return c.SendString(dbResult)
}
return c.Status(fiber.StatusNotAcceptable).SendString("Not Acceptable")
}
Expand Down
76 changes: 45 additions & 31 deletions internal/core/queries/get_pid_config_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,10 @@ type GetPidsQueryRequest struct {

// GetPidsByTemplate gets all pids in a template and their children from inherited parent templates
func GetPidsByTemplate(ctx context.Context, dbs func() *db.ReaderWriter, request *GetPidsQueryRequest) (models.PidConfigSlice, *models.Template, error) {
template, err := models.FindTemplate(ctx, dbs().Reader, request.TemplateName)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, nil, fmt.Errorf("no template with name: %s found", request.TemplateName)
}
return nil, nil, errors.Wrapf(err, "failed to retrieve Template %s", request.TemplateName)
}

var templates []models.Template
currentTemplate := template
for {
templates = append(templates, *currentTemplate)

if currentTemplate.ParentTemplateName.Valid {
currentTemplate, err = models.FindTemplate(ctx, dbs().Reader, currentTemplate.ParentTemplateName.String)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
break
}
return nil, nil, errors.Wrapf(err, "failed to retrieve parent Template %s", currentTemplate.ParentTemplateName.String)
}
} else {
break
}
}

templateNames := make([]interface{}, len(templates))
for i, tmpl := range templates {
templateNames[i] = tmpl.TemplateName
}
templateNames, template, err := GetAllParentTemplates(ctx, dbs, request.TemplateName)

Check failure on line 21 in internal/core/queries/get_pid_config_all.go

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to err (ineffassign)

pidConfigs, err := models.PidConfigs(
qm.WhereIn("template_name IN ?", templateNames...),
qm.WhereIn("template_name IN ?", ToAnySlice(templateNames)...),
).All(ctx, dbs().Reader)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return nil, nil, errors.Wrap(err, "Failed to retrieve PID Configs")
Expand Down Expand Up @@ -81,3 +52,46 @@ func GetPidsByTemplate(ctx context.Context, dbs func() *db.ReaderWriter, request

return result, template, nil
}

// GetAllParentTemplates gets all the parent templates for a template, if none returns empty just the current template name
func GetAllParentTemplates(ctx context.Context, dbs func() *db.ReaderWriter, templateName string) ([]string, *models.Template, error) {
template, err := models.FindTemplate(ctx, dbs().Reader, templateName)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, nil, fmt.Errorf("no template with name: %s found", templateName)
}
return nil, nil, errors.Wrapf(err, "failed to retrieve Template %s", templateName)
}

var templates []models.Template
currentTemplate := template
for {
templates = append(templates, *currentTemplate)

if currentTemplate.ParentTemplateName.Valid {
currentTemplate, err = models.FindTemplate(ctx, dbs().Reader, currentTemplate.ParentTemplateName.String)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
break
}
return nil, nil, errors.Wrapf(err, "failed to retrieve parent Template %s", currentTemplate.ParentTemplateName.String)
}
} else {
break
}
}

templateNames := make([]string, len(templates))
for i, tmpl := range templates {
templateNames[i] = tmpl.TemplateName
}
return templateNames, template, nil
}

func ToAnySlice(slice []string) []any {
anySlice := make([]any, len(slice))
for i, v := range slice {
anySlice[i] = v
}
return anySlice
}

0 comments on commit 1454291

Please sign in to comment.