Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions internal/api/docs/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,15 @@ components:
$ref: '#/components/schemas/ErrorResponse'
description: Precondition Failed
schemas:
AIModel:
properties:
description:
type: string
id:
type: string
name:
type: string
type: object
AIModelItem:
properties:
brick_ids:
Expand Down Expand Up @@ -1336,6 +1345,10 @@ components:
type: string
category:
type: string
compatible_models:
items:
$ref: '#/components/schemas/AIModel'
type: array
config_variables:
items:
$ref: '#/components/schemas/BrickConfigVariable'
Expand Down
24 changes: 16 additions & 8 deletions internal/e2e/client/client.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ var (
Value: f.Ptr("/models/ootb/ei/mobilenet-v2-224px.eim"),
},
}

expectedModelInfo = []client.AIModel{
{
Id: f.Ptr("mobilenet-image-classification"),
Name: f.Ptr("General purpose image classification"),
Description: f.Ptr("General purpose image classification model based on MobileNetV2. This model is trained on the ImageNet dataset and can classify images into 1000 categories."),
},
{
Id: f.Ptr("person-classification"),
Name: f.Ptr("Person classification"),
Description: f.Ptr("Person classification model based on WakeVision dataset. This model is trained to classify images into two categories: person and not-person."),
}}
)

func setupTestApp(t *testing.T) (*client.CreateAppResp, *client.ClientWithResponses) {
Expand Down Expand Up @@ -78,7 +90,6 @@ func setupTestApp(t *testing.T) (*client.CreateAppResp, *client.ClientWithRespon
)
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode())

return createResp, httpClient
}

Expand Down Expand Up @@ -135,6 +146,20 @@ func TestGetAppBrickInstanceById(t *testing.T) {
require.NotEmpty(t, brickInstance.JSON200)
require.Equal(t, ImageClassifactionBrickID, *brickInstance.JSON200.Id)
require.Equal(t, expectedConfigVariables, (*brickInstance.JSON200.ConfigVariables))
require.NotNil(t, brickInstance.JSON200.CompatibleModels)
require.Equal(t, expectedModelInfo, *(brickInstance.JSON200.CompatibleModels))
})
t.Run("GetAppBrickInstanceByBrickIDWithCompatibleModels_Success", func(t *testing.T) {
brickInstance, err := httpClient.GetAppBrickInstanceByBrickIDWithResponse(
t.Context(),
*createResp.JSON201.Id,
ImageClassifactionBrickID,
func(ctx context.Context, req *http.Request) error { return nil })
require.NoError(t, err)
require.NotEmpty(t, brickInstance.JSON200)
require.Equal(t, ImageClassifactionBrickID, *brickInstance.JSON200.Id)
require.NotNil(t, brickInstance.JSON200.CompatibleModels)
require.Equal(t, expectedModelInfo, *(brickInstance.JSON200.CompatibleModels))
})

t.Run("GetAppBrickInstanceByBrickID_InvalidAppID_Fails", func(t *testing.T) {
Expand Down
7 changes: 7 additions & 0 deletions internal/orchestrator/bricks/bricks.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ func (s *Service) AppBrickInstanceDetails(a *app.ArduinoApp, brickID string) (Br
Variables: variables,
ConfigVariables: configVariables,
ModelID: modelID,
CompatibleModels: f.Map(s.modelsIndex.GetModelsByBrick(brick.ID), func(m modelsindex.AIModel) AIModel {
return AIModel{
ID: m.ID,
Name: m.Name,
Description: m.ModuleDescription,
}
}),
}, nil
}

Expand Down
153 changes: 153 additions & 0 deletions internal/orchestrator/bricks/bricks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/arduino/arduino-app-cli/internal/orchestrator/app"
"github.com/arduino/arduino-app-cli/internal/orchestrator/bricksindex"
"github.com/arduino/arduino-app-cli/internal/orchestrator/modelsindex"
)

func TestBrickCreate(t *testing.T) {
Expand Down Expand Up @@ -190,3 +191,155 @@ func TestGetBrickInstanceVariableDetails(t *testing.T) {
})
}
}

func TestAppBrickInstanceModelsDetails(t *testing.T) {

bIndex := &bricksindex.BricksIndex{
Bricks: []bricksindex.Brick{
{
ID: "arduino:object_detection",
Name: "Object Detection",
Category: "video",
ModelName: "yolox-object-detection", // Default model
Variables: []bricksindex.BrickVariable{
{Name: "EI_OBJ_DETECTION_MODEL", DefaultValue: "default_path", Description: "path to the model file"},
{Name: "CUSTOM_MODEL_PATH", DefaultValue: "/home/arduino/.arduino-bricks/ei-models", Description: "path to the custom model directory"},
},
},
{
ID: "arduino:weather_forecast",
Name: "Weather Forecast",
Category: "miscellaneous",
ModelName: "",
},
},
}

mIndex := &modelsindex.ModelsIndex{
Models: []modelsindex.AIModel{

{
ID: "yolox-object-detection",
Name: "General purpose object detection - YoloX",
ModuleDescription: "General purpose object detection...",
Bricks: []string{"arduino:object_detection", "arduino:video_object_detection"},
},
{
ID: "face-detection",
Name: "Lightweight-Face-Detection",
Bricks: []string{"arduino:object_detection", "arduino:video_object_detection"},
},
}}

svc := &Service{
bricksIndex: bIndex,
modelsIndex: mIndex,
}

tests := []struct {
name string
app *app.ArduinoApp
brickID string
expectedError string
validate func(*testing.T, BrickInstance)
}{
{
name: "Brick not found in global Index",
brickID: "arduino:non_existent_brick",
app: &app.ArduinoApp{
Descriptor: app.AppDescriptor{Bricks: []app.Brick{}},
},
expectedError: "brick not found",
},
{
name: "Brick found in Index but not added to App",
brickID: "arduino:object_detection",
app: &app.ArduinoApp{
Descriptor: app.AppDescriptor{
Bricks: []app.Brick{
{ID: "arduino:weather_forecast"},
},
},
},
expectedError: "brick arduino:object_detection not added in the app",
},
{
name: "Success - Standard Brick without Model",
brickID: "arduino:weather_forecast",
app: &app.ArduinoApp{
Descriptor: app.AppDescriptor{
Bricks: []app.Brick{
{ID: "arduino:weather_forecast"},
},
},
},
validate: func(t *testing.T, res BrickInstance) {
require.Equal(t, "arduino:weather_forecast", res.ID)
require.Equal(t, "Weather Forecast", res.Name)
require.Equal(t, "installed", res.Status)
require.Empty(t, res.ModelID)
require.Empty(t, res.CompatibleModels)
},
},
{
name: "Success - Brick with Default Model",
brickID: "arduino:object_detection",
app: &app.ArduinoApp{
Descriptor: app.AppDescriptor{
Bricks: []app.Brick{
{
ID: "arduino:object_detection",
},
},
},
},
validate: func(t *testing.T, res BrickInstance) {
require.Equal(t, "arduino:object_detection", res.ID)
require.Equal(t, "yolox-object-detection", res.ModelID)
require.Len(t, res.CompatibleModels, 2)
require.Equal(t, "yolox-object-detection", res.CompatibleModels[0].ID)
require.Equal(t, "face-detection", res.CompatibleModels[1].ID)
},
},
{
name: "Success - Brick with Overridden Model in App",
brickID: "arduino:object_detection",
app: &app.ArduinoApp{
Descriptor: app.AppDescriptor{
Bricks: []app.Brick{
{
ID: "arduino:object_detection",
Model: "face-detection",
},
},
},
},
validate: func(t *testing.T, res BrickInstance) {
require.Equal(t, "arduino:object_detection", res.ID)
require.Equal(t, "face-detection", res.ModelID)
require.Len(t, res.CompatibleModels, 2)
require.Equal(t, "yolox-object-detection", res.CompatibleModels[0].ID)
require.Equal(t, "face-detection", res.CompatibleModels[1].ID)
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := svc.AppBrickInstanceDetails(tt.app, tt.brickID)

if tt.expectedError != "" {
require.Error(t, err)
if err != nil {
require.Contains(t, err.Error(), tt.expectedError)
}
return
}

require.NoError(t, err)
if tt.validate != nil {
tt.validate(t, result)
}
})
}
}
22 changes: 14 additions & 8 deletions internal/orchestrator/bricks/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,22 @@ type AppBrickInstancesResult struct {
}

type BrickInstance struct {
ID string `json:"id"`
Name string `json:"name"`
Author string `json:"author"`
Category string `json:"category"`
Status string `json:"status"`
Variables map[string]string `json:"variables,omitempty" description:"Deprecated: use config_variables instead. This field is kept for backward compatibility."`
ConfigVariables []BrickConfigVariable `json:"config_variables,omitempty"`
ModelID string `json:"model,omitempty"`
ID string `json:"id"`
Name string `json:"name"`
Author string `json:"author"`
Category string `json:"category"`
Status string `json:"status"`
Variables map[string]string `json:"variables,omitempty" description:"Deprecated: use config_variables instead. This field is kept for backward compatibility."`
ConfigVariables []BrickConfigVariable `json:"config_variables,omitempty"`
ModelID string `json:"model,omitempty"`
CompatibleModels []AIModel `json:"compatible_models,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the result of omitempty with a list ?
I expect that the JSON response is `"compatible_models: [] ". We can add a test for it

}

type AIModel struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
}
type BrickConfigVariable struct {
Name string `json:"name"`
Value string `json:"value"`
Expand Down
25 changes: 16 additions & 9 deletions internal/orchestrator/modelsindex/models_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,33 @@ type AIModel struct {
}

type ModelsIndex struct {
models []AIModel
Models []AIModel
}

// NewModelsIndex creates a new index instance (Helper for tests or manual creation)
func NewModelsIndex(models []AIModel) *ModelsIndex {
return &ModelsIndex{
Models: models,
}
}

func (m *ModelsIndex) GetModels() []AIModel {
return m.models
return m.Models
}

func (m *ModelsIndex) GetModelByID(id string) (*AIModel, bool) {
idx := slices.IndexFunc(m.models, func(v AIModel) bool { return v.ID == id })
idx := slices.IndexFunc(m.Models, func(v AIModel) bool { return v.ID == id })
if idx == -1 {
return nil, false
}
return &m.models[idx], true
return &m.Models[idx], true
}

func (m *ModelsIndex) GetModelsByBrick(brick string) []AIModel {
var matches []AIModel
for i := range m.models {
if len(m.models[i].Bricks) > 0 && slices.Contains(m.models[i].Bricks, brick) {
matches = append(matches, m.models[i])
for i := range m.Models {
if len(m.Models[i].Bricks) > 0 && slices.Contains(m.Models[i].Bricks, brick) {
matches = append(matches, m.Models[i])
}
}
if len(matches) == 0 {
Expand All @@ -84,7 +91,7 @@ func (m *ModelsIndex) GetModelsByBrick(brick string) []AIModel {

func (m *ModelsIndex) GetModelsByBricks(bricks []string) []AIModel {
var matchingModels []AIModel
for _, model := range m.models {
for _, model := range m.Models {
for _, modelBrick := range model.Bricks {
if slices.Contains(bricks, modelBrick) {
matchingModels = append(matchingModels, model)
Expand Down Expand Up @@ -113,5 +120,5 @@ func GenerateModelsIndexFromFile(dir *paths.Path) (*ModelsIndex, error) {
models[i] = model
}
}
return &ModelsIndex{models: models}, nil
return &ModelsIndex{Models: models}, nil
}