Skip to content

Commit

Permalink
feat(server): add CMS URL to admin in datacatalogv3 (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 authored Oct 23, 2024
1 parent a8a510a commit 680bcf0
Show file tree
Hide file tree
Showing 20 changed files with 232 additions and 134 deletions.
1 change: 1 addition & 0 deletions server/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
indexer/testdata
/cache
/govpolygondata
/repo_*.json
99 changes: 63 additions & 36 deletions server/datacatalog/datacatalogv3/cms.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,38 @@ type CMS struct {
cacheDir string
}

func NewCMS(cms cms.Interface, pcms plateaucms.SpecStore, year int, plateau bool, project string, cache bool) *CMS {
type CMSOpts struct {
CMS cms.Interface
PCMS plateaucms.SpecStore
Year int
Plateau bool
Project string
Cache bool
}

func NewCMS(opts CMSOpts) *CMS {
return &CMS{
cms: cms,
pcms: pcms,
project: project,
year: year,
plateau: plateau,
cache: cache,
cacheDir: filepath.Join(cacheDir, cachePrefix+project),
cms: opts.CMS,
pcms: opts.PCMS,
project: opts.Project,
year: opts.Year,
plateau: opts.Plateau,
cache: opts.Cache,
cacheDir: filepath.Join(cacheDir, cachePrefix+opts.Project),
}
}

func (c *CMS) GetAll(ctx context.Context) (*AllData, error) {
cmsinfo, err := c.GetCMSInfo(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get CMS info: %w", err)
}
all := AllData{
Name: c.project,
Year: c.year,
Name: c.project,
Year: c.year,
CMSInfo: cmsinfo,
}

// TODO: get CMSInfo

specs, err := getPlateauSpecs(ctx, c.pcms, c.year)
if err != nil {
return nil, fmt.Errorf("failed to get plateau specs: %w", err)
Expand Down Expand Up @@ -325,30 +337,45 @@ func (c *CMS) GetGeospatialjpDataItems(ctx context.Context, project string) ([]*
return items, err
}

// func (c *CMS) GetGeospatialjpDataItemsWithMaxLODContent(ctx context.Context, project string) ([]*GeospatialjpDataItem, error) {
// items, err := c.GetGeospatialjpDataItems(ctx, project)
// if err != nil {
// return nil, err
// }

// urls := lo.Map(items, func(i *GeospatialjpDataItem, _ int) string {
// return i.MaxLOD
// })

// maxlods, err := fetchMaxLODContents(ctx, urls)
// if err != nil {
// return nil, err
// }

// for i, m := range maxlods {
// if m == nil {
// continue
// }
// items[i].MaxLODContent = m
// }

// return items, nil
// }
func (c *CMS) GetCMSInfo(ctx context.Context) (CMSInfo, error) {
metadata := plateaucms.GetAllCMSMetadataFromContext(ctx)
if len(metadata) == 0 {
return CMSInfo{}, fmt.Errorf("metadata not found")
}

md, ok := metadata.FindMetadata(c.project, true, false)
if !ok {
return CMSInfo{}, fmt.Errorf("metadata not found")
}

modelIDs, err := c.GetModelIDs(ctx)
if err != nil {
return CMSInfo{}, fmt.Errorf("failed to get model IDs: %w", err)
}

return CMSInfo{
CMSURL: md.CMSURL,
WorkspaceID: md.WorkspaceID,
ProjectID: md.ProjectID,
ModelIDMap: modelIDs,
}, nil
}

func (c *CMS) GetModelIDs(ctx context.Context) (ModelIDMap, error) {
models, err := c.cms.GetModels(ctx, c.project)
if err != nil {
return nil, fmt.Errorf("failed to get models: %w", err)
}

res := make(ModelIDMap, len(models.Models))
for _, model := range models.Models {
if strings.HasPrefix(model.Key, modelPrefix) {
res[strings.TrimPrefix(model.Key, modelPrefix)] = model.ID
}
}

return res, nil
}

func getItemsAndConv[T any](cms cms.Interface, ctx context.Context, project, model string, conv func(cms.Item) *T) ([]*T, error) {
items, err := cms.GetItemsByKeyInParallel(ctx, project, model, true, 100)
Expand Down
9 changes: 8 additions & 1 deletion server/datacatalog/datacatalogv3/cms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ func TestExtractDataFromCMS(t *testing.T) {
})
assert.NoError(t, err)

c2 := NewCMS(c, pcms, 2023, true, prj, false)
c2 := NewCMS(CMSOpts{
CMS: c,
PCMS: pcms,
Year: 2023,
Plateau: true,
Project: prj,
Cache: false,
})
all, err := c2.GetAll(ctx)
assert.NoError(t, err)

Expand Down
15 changes: 4 additions & 11 deletions server/datacatalog/datacatalogv3/conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ func (all *AllData) Into() (res *plateauapi.InMemoryRepoContext, warning []strin
res.DatasetTypes = all.FeatureTypes.ToDatasetTypes(res.PlateauSpecs)

ic := newInternalContext()
ic.plateauCMSURL = all.CMSInfo.PlateauItemBaseURL()
ic.relatedCMSURL = all.CMSInfo.RelatedItemBaseURL()
ic.genericCMSURL = all.CMSInfo.GenericItemBaseURL()
ic.cmsinfo = all.CMSInfo
ic.regYear = all.Year

// layer names
Expand Down Expand Up @@ -211,22 +209,17 @@ func convertPlateauRaw(
continue
}

cmsurl := ""
if ic.plateauCMSURL != nil {
cmsurl = ic.plateauCMSURL[ftcode]
}

opts := ToPlateauDatasetsOptions{
ID: ds.ID,
CreatedAt: ds.CreatedAt,
UpdatedAt: ds.UpdatedAt,
CMSURL: cmsurl,
Area: area,
Spec: spec,
DatasetType: pdt,
LayerNames: layerNames,
FeatureType: ft,
Year: ic.regYear,
CMSInfo: ic.cmsinfo,
}
ds, w := ds.toDatasets(opts)
warning = append(warning, w...)
Expand All @@ -246,7 +239,7 @@ func convertRelated(items []*RelatedItem, datasetTypes []plateauapi.DatasetType,
continue
}

ds, w := ds.toDatasets(area, datasetTypes, ic.regYear, ic.relatedCMSURL)
ds, w := ds.toDatasets(area, datasetTypes, ic.regYear, ic.cmsinfo)
warning = append(warning, w...)
if ds != nil {
res = append(res, ds...)
Expand All @@ -264,7 +257,7 @@ func convertGeneric(items []*GenericItem, datasetTypes []plateauapi.DatasetType,
continue
}

ds, w := ds.toDatasets(area, datasetTypes, ic.regYear, ic.genericCMSURL)
ds, w := ds.toDatasets(area, datasetTypes, ic.regYear, ic.cmsinfo)
warning = append(warning, w...)
if ds != nil {
res = append(res, ds...)
Expand Down
3 changes: 1 addition & 2 deletions server/datacatalog/datacatalogv3/conv_citygml.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
)

func toCityGMLs(all *AllData, regYear int) (map[plateauapi.ID]*plateauapi.CityGMLDataset, []string) {
cmsurl := all.CMSInfo.CMSURL
res := map[plateauapi.ID]*plateauapi.CityGMLDataset{}
resCity := map[string]*plateauapi.CityGMLDataset{}
dataMap := make(map[string]*GeospatialjpDataItem)
Expand Down Expand Up @@ -41,7 +40,7 @@ func toCityGMLs(all *AllData, regYear int) (map[plateauapi.ID]*plateauapi.CityGM
Admin: adminFrom(Admin{
ItemID: city.ID,
Stage: city.SDKStage(),
CMSURL: cmsurl,
CMSURL: all.CMSInfo.ItemBaseURL(cityModel),
MaxLODURLs: []string{data.MaxLOD},
CityGMLURLs: []string{data.CityGML},
}),
Expand Down
4 changes: 2 additions & 2 deletions server/datacatalog/datacatalogv3/conv_dataset_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/samber/lo"
)

func (i *GenericItem) toDatasets(area *areaContext, dts []plateauapi.DatasetType, year int, cmsurl string) (_ []plateauapi.Dataset, warning []string) {
func (i *GenericItem) toDatasets(area *areaContext, dts []plateauapi.DatasetType, year int, cmsinfo CMSInfo) (_ []plateauapi.Dataset, warning []string) {
if area == nil {
area = &areaContext{}
}
Expand Down Expand Up @@ -71,7 +71,7 @@ func (i *GenericItem) toDatasets(area *areaContext, dts []plateauapi.DatasetType
Admin: adminFrom(Admin{
ItemID: i.ID,
Stage: i.Stage(),
CMSURL: cmsurl,
CMSURL: cmsinfo.ItemBaseURL(genericModel),
}),
Items: items,
}
Expand Down
9 changes: 7 additions & 2 deletions server/datacatalog/datacatalogv3/conv_dataset_generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func Test_GenericItem_ToDatasets(t *testing.T) {
TypeID: plateauapi.NewID("usecase", plateauapi.TypeDatasetType),
TypeCode: "usecase",
Admin: &plateauapi.Admin{
CMSURL: "https://example.com/id",
CMSURL: "https://example.com/workspace/ws/project/prj/content/gen/details/id",
CMSItemID: "id",
Stage: string(stageAlpha),
},
Expand Down Expand Up @@ -101,7 +101,12 @@ func Test_GenericItem_ToDatasets(t *testing.T) {
},
}

res, warning := item.toDatasets(area, dts, 2023, "https://example.com/")
res, warning := item.toDatasets(area, dts, 2023, CMSInfo{
CMSURL: "https://example.com",
WorkspaceID: "ws",
ProjectID: "prj",
ModelIDMap: ModelIDMap{"generic": "gen"},
})
assert.Equal(t, []string{"generic id[2]: invalid url: "}, warning)
assert.Equal(t, expected, res)
}
2 changes: 1 addition & 1 deletion server/datacatalog/datacatalogv3/conv_dataset_plateau.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ func (i *PlateauFeatureItem) toWards(pref *plateauapi.Prefecture, city *plateaua

type ToPlateauDatasetsOptions struct {
ID string
CMSURL string
CreatedAt time.Time
UpdatedAt time.Time
Area *areaContext
Expand All @@ -57,6 +56,7 @@ type ToPlateauDatasetsOptions struct {
LayerNames LayerNames
FeatureType *FeatureType
Year int
CMSInfo CMSInfo
}

func (i *PlateauFeatureItem) toDatasets(opts ToPlateauDatasetsOptions) (res []*plateauapi.PlateauDataset, warning []string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func plateauDatasetSeedsFrom(i *PlateauFeatureItem, opts ToPlateauDatasetsOption
res[i].Admin = adminFrom(Admin{
ItemID: opts.ID,
Stage: opts.Area.CityItem.PlateauStage(opts.DatasetType.Code),
CMSURL: opts.CMSURL,
CMSURL: opts.CMSInfo.ItemBaseURL(opts.DatasetType.Code),
CreatedAt: opts.CreatedAt,
UpdatedAt: opts.UpdatedAt,
SubAreaCode: opts.Area.CityItem.SubCityCode,
Expand Down
13 changes: 10 additions & 3 deletions server/datacatalog/datacatalogv3/conv_dataset_plateau_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func TestPlateauDataset_ToDatasets_Bldg(t *testing.T) {
PlateauSpecMinorID: plateauapi.NewID("3.2", plateauapi.TypePlateauSpec),
Admin: &plateauapi.Admin{
CMSItemID: "cityid",
CMSURL: "https://example.com/cityid",
CMSURL: "https://example.com/workspace/ws/project/prj/content/BLDG/details/cityid",
Stage: string(stageAlpha),
},
Items: []*plateauapi.PlateauDatasetItem{
Expand Down Expand Up @@ -154,7 +154,7 @@ func TestPlateauDataset_ToDatasets_Bldg(t *testing.T) {
PlateauSpecMinorID: plateauapi.NewID("3.2", plateauapi.TypePlateauSpec),
Admin: &plateauapi.Admin{
CMSItemID: "cityid",
CMSURL: "https://example.com/cityid",
CMSURL: "https://example.com/workspace/ws/project/prj/content/BLDG/details/cityid",
Stage: string(stageAlpha),
},
Items: []*plateauapi.PlateauDatasetItem{
Expand Down Expand Up @@ -221,13 +221,20 @@ func TestPlateauDataset_ToDatasets_Bldg(t *testing.T) {

opts := ToPlateauDatasetsOptions{
ID: "cityid",
CMSURL: "https://example.com/",
Area: area,
Spec: spec,
DatasetType: dts,
LayerNames: layerNames,
FeatureType: &FeatureType{},
Year: 2024,
CMSInfo: CMSInfo{
CMSURL: "https://example.com",
WorkspaceID: "ws",
ProjectID: "prj",
ModelIDMap: ModelIDMap{
"bldg": "BLDG",
},
},
}
res, warning := item.toDatasets(opts)
assert.Nil(t, warning)
Expand Down
4 changes: 2 additions & 2 deletions server/datacatalog/datacatalogv3/conv_dataset_related.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

var convIgnored = []string{"border"}

func (i *RelatedItem) toDatasets(area *areaContext, dts []plateauapi.DatasetType, year int, cmsurl string) (res []plateauapi.Dataset, warning []string) {
func (i *RelatedItem) toDatasets(area *areaContext, dts []plateauapi.DatasetType, year int, cmsinfo CMSInfo) (res []plateauapi.Dataset, warning []string) {
if !area.IsValid() {
warning = append(warning, fmt.Sprintf("related %s: invalid area", i.ID))
return
Expand All @@ -34,7 +34,7 @@ func (i *RelatedItem) toDatasets(area *areaContext, dts []plateauapi.DatasetType
admin := adminFrom(Admin{
ItemID: i.ID,
Stage: relatedStage(i, area.CityItem),
CMSURL: cmsurl,
CMSURL: cmsinfo.ItemBaseURL(relatedModel),
})

for _, seed := range seeds {
Expand Down
19 changes: 14 additions & 5 deletions server/datacatalog/datacatalogv3/conv_dataset_related_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestRelatedItem_ToDatasets(t *testing.T) {
TypeCode: "landmark",
Admin: &plateauapi.Admin{
CMSItemID: "id",
CMSURL: "https://example.com/id",
CMSURL: "https://example.com/workspace/ws/project/prj/content/RELATED/details/id",
Stage: string(stageAlpha),
},
Items: []*plateauapi.RelatedDatasetItem{
Expand Down Expand Up @@ -84,7 +84,7 @@ func TestRelatedItem_ToDatasets(t *testing.T) {
TypeCode: "landmark",
Admin: &plateauapi.Admin{
CMSItemID: "id",
CMSURL: "https://example.com/id",
CMSURL: "https://example.com/workspace/ws/project/prj/content/RELATED/details/id",
Stage: string(stageAlpha),
},
Items: []*plateauapi.RelatedDatasetItem{
Expand Down Expand Up @@ -114,7 +114,7 @@ func TestRelatedItem_ToDatasets(t *testing.T) {
TypeCode: "border",
Admin: &plateauapi.Admin{
CMSItemID: "id",
CMSURL: "https://example.com/id",
CMSURL: "https://example.com/workspace/ws/project/prj/content/RELATED/details/id",
Stage: string(stageAlpha),
},
Items: []*plateauapi.RelatedDatasetItem{
Expand Down Expand Up @@ -144,7 +144,7 @@ func TestRelatedItem_ToDatasets(t *testing.T) {
TypeCode: "emergency_route",
Admin: &plateauapi.Admin{
CMSItemID: "id",
CMSURL: "https://example.com/id",
CMSURL: "https://example.com/workspace/ws/project/prj/content/RELATED/details/id",
Stage: string(stageAlpha),
},
Items: []*plateauapi.RelatedDatasetItem{
Expand Down Expand Up @@ -207,7 +207,16 @@ func TestRelatedItem_ToDatasets(t *testing.T) {
},
}

res, warnings := item.toDatasets(area, dts, 2023, "https://example.com/")
cmsinfo := CMSInfo{
CMSURL: "https://example.com",
WorkspaceID: "ws",
ProjectID: "prj",
ModelIDMap: ModelIDMap{
"related": "RELATED",
},
}

res, warnings := item.toDatasets(area, dts, 2023, cmsinfo)
assert.Nil(t, warnings)
assert.Equal(t, expected, res)
}
Loading

0 comments on commit 680bcf0

Please sign in to comment.