Skip to content
This repository has been archived by the owner on Apr 8, 2024. It is now read-only.

Commit

Permalink
fix(server): print warning when there is asset paths not matched to m…
Browse files Browse the repository at this point in the history
…axlod file path sdkapi
  • Loading branch information
rot1024 committed Dec 7, 2023
1 parent 4a91c6c commit c9b6506
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 33 deletions.
1 change: 1 addition & 0 deletions server/cmsintegration/cmsintegrationv3/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.csv
21 changes: 13 additions & 8 deletions server/cmsintegration/cmsintegrationv3/service_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import (
)

type SetupCityItemsInput struct {
ProjectID string `json:"projectId"`
DataURL string `json:"dataUrl"`
Force bool `json:"force"`
Offset int `json:"offset"`
Limit int `json:"limit"`
ProjectID string `json:"projectId"`
DataURL string `json:"dataUrl"`
DataBody io.Reader `json:"-"`
Force bool `json:"force"`
Offset int `json:"offset"`
Limit int `json:"limit"`
}

type SetupCSVItem struct {
Expand All @@ -33,7 +34,7 @@ func SetupCityItems(ctx context.Context, s *Services, inp SetupCityItemsInput, o
return fmt.Errorf("modelId is required")
}

if inp.DataURL == "" {
if inp.DataURL == "" && inp.DataBody == nil {
return fmt.Errorf("dataUrl is required")
}

Expand Down Expand Up @@ -70,7 +71,7 @@ func SetupCityItems(ctx context.Context, s *Services, inp SetupCityItemsInput, o
}

// parse data
setupItems, features, err := getAndParseSetupCSV(ctx, s, inp.DataURL)
setupItems, features, err := getAndParseSetupCSV(ctx, s, inp.DataURL, inp.DataBody)
if err != nil {
return fmt.Errorf("failed to get and parse data: %w", err)
}
Expand Down Expand Up @@ -151,7 +152,11 @@ func SetupCityItems(ctx context.Context, s *Services, inp SetupCityItemsInput, o
return nil
}

func getAndParseSetupCSV(ctx context.Context, s *Services, url string) ([]SetupCSVItem, []string, error) {
func getAndParseSetupCSV(ctx context.Context, s *Services, url string, body io.Reader) ([]SetupCSVItem, []string, error) {
if body != nil {
return parseSetupCSV(ctx, body)
}

r, err := s.GET(ctx, url)
if err != nil {
return nil, nil, err
Expand Down
16 changes: 14 additions & 2 deletions server/sdkapi/cms.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"fmt"
"net/url"
"path"
"strings"

cms "github.com/reearth/reearth-cms-api/go"
"github.com/reearth/reearthx/log"
"github.com/reearth/reearthx/rerror"
"github.com/reearth/reearthx/util"
)
Expand Down Expand Up @@ -71,7 +73,12 @@ func (c *CMS) FilesWithPublicAPI(ctx context.Context, model, id string) (FilesRe
return nil, rerror.ErrInternalBy(err)
}

return MaxLODFiles(maxlod, asset.Files, nil), nil
res, warning := MaxLODFiles(maxlod, asset.Files, nil)
if len(warning) > 0 {
log.Warnfc(ctx, "sdkapi: warning: %s", strings.Join(warning, "\n"))
}

return res, nil
}

func (c *CMS) DatasetsWithIntegrationAPI(ctx context.Context, model string) (*DatasetResponse, error) {
Expand Down Expand Up @@ -115,5 +122,10 @@ func (c *CMS) FilesWithIntegrationAPI(ctx context.Context, model, id string) (Fi
return nil, rerror.ErrInternalBy(err)
}

return MaxLODFiles(maxlod, asset.File.Paths(), assetBase), nil
res, warning := MaxLODFiles(maxlod, asset.File.Paths(), assetBase)
if len(warning) > 0 {
log.Warnfc(ctx, "sdkapi: warning: %s", strings.Join(warning, "\n"))
}

return res, nil
}
7 changes: 4 additions & 3 deletions server/sdkapi/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ func (mc MaxLODColumns) Map() MaxLODMap {
return m
}

func (mm MaxLODMap) Files(urls []*url.URL) (r FilesResponse) {
func (mm MaxLODMap) Files(urls []*url.URL) (r FilesResponse, warning []string) {
r = FilesResponse{}
for ty, m := range mm {
if _, ok := r[ty]; !ok {
Expand All @@ -343,9 +343,10 @@ func (mm MaxLODMap) Files(urls []*url.URL) (r FilesResponse) {
URL: u.String(),
MaxLOD: item.MaxLOD,
})
} else {
warning = append(warning, fmt.Sprintf("unmatched:type=%s,code=%s,path=%s", ty, code, f))
}
}

}
slices.SortFunc(r[ty], func(i, j File) int {
return strings.Compare(i.Code, j.Code)
Expand Down Expand Up @@ -513,7 +514,7 @@ func ReadMaxLODCSV(b io.Reader) (MaxLODColumns, error) {
return results, nil
}

func MaxLODFiles(maxLOD MaxLODColumns, assetPaths []string, assetBase *url.URL) FilesResponse {
func MaxLODFiles(maxLOD MaxLODColumns, assetPaths []string, assetBase *url.URL) (FilesResponse, []string) {
files := lo.FilterMap(assetPaths, func(u string, _ int) (*url.URL, bool) {
if path.Ext(u) != ".gml" {
return nil, false
Expand Down
43 changes: 23 additions & 20 deletions server/sdkapi/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,7 @@ func TestMaxLODColumns_Map(t *testing.T) {
}

func TestMaxLODMap_Files(t *testing.T) {
assert.Equal(t, FilesResponse{
"bldg": []File{
{Code: "1", URL: "https://example.com/1_bldg_xxx.gml", MaxLOD: 1},
{Code: "2", URL: "https://example.com/2_bldg_yyy.gml", MaxLOD: 1},
},
"veg": []File{
{Code: "1", URL: "https://example.com/1_veg_zzz.gml", MaxLOD: 2},
},
"frn": nil,
"fld": []File{
{Code: "3", URL: "https://example.com/aaa.gml", MaxLOD: 1},
},
"dem": []File{
{Code: "1111", URL: "https://example.com/00000_dem_1111_00_op.gml", MaxLOD: 1},
{Code: "1111", URL: "https://example.com/00000_dem_1111_05_op.gml", MaxLOD: 1},
{Code: "1111", URL: "https://example.com/00000_dem_1111_50_op.gml", MaxLOD: 1},
{Code: "1111", URL: "https://example.com/00000_dem_1111_55_op.gml", MaxLOD: 1},
},
}, MaxLODMap{
res, warning := MaxLODMap{
"bldg": map[string]MaxLODMapItem{
"2": {MaxLOD: 1, Files: []string{""}},
"1": {MaxLOD: 1, Files: []string{""}},
Expand Down Expand Up @@ -131,7 +113,28 @@ func TestMaxLODMap_Files(t *testing.T) {
lo.Must(url.Parse("https://example.com/00000_dem_1111_05_op.gml")),
lo.Must(url.Parse("https://example.com/00000_dem_1111_50_op.gml")),
lo.Must(url.Parse("https://example.com/00000_dem_1111_55_op.gml")),
}))
})

assert.Equal(t, warning, []string{"unmatched:type=frn,code=2,path="})
assert.Equal(t, FilesResponse{
"bldg": []File{
{Code: "1", URL: "https://example.com/1_bldg_xxx.gml", MaxLOD: 1},
{Code: "2", URL: "https://example.com/2_bldg_yyy.gml", MaxLOD: 1},
},
"veg": []File{
{Code: "1", URL: "https://example.com/1_veg_zzz.gml", MaxLOD: 2},
},
"frn": nil,
"fld": []File{
{Code: "3", URL: "https://example.com/aaa.gml", MaxLOD: 1},
},
"dem": []File{
{Code: "1111", URL: "https://example.com/00000_dem_1111_00_op.gml", MaxLOD: 1},
{Code: "1111", URL: "https://example.com/00000_dem_1111_05_op.gml", MaxLOD: 1},
{Code: "1111", URL: "https://example.com/00000_dem_1111_50_op.gml", MaxLOD: 1},
{Code: "1111", URL: "https://example.com/00000_dem_1111_55_op.gml", MaxLOD: 1},
},
}, res)
}

func TestItemsFromIntegration(t *testing.T) {
Expand Down

0 comments on commit c9b6506

Please sign in to comment.