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

Commit

Permalink
fix(server): datacatalog v3 bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Jan 9, 2024
1 parent a04b2b0 commit 15c6342
Show file tree
Hide file tree
Showing 17 changed files with 170 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ func New(cmsbase, project string) (*plateauapi.RepoWrapper, error) {
}

func From(fetcher datacatalogv2.Fetchable, project string) *plateauapi.RepoWrapper {
return plateauapi.NewRepoWrapper(nil, func(ctx context.Context, repo *plateauapi.Repo) error {
r := plateauapi.NewRepoWrapper(nil, func(ctx context.Context, repo *plateauapi.Repo) error {
r, err := fetchAndCreateCache(ctx, project, fetcher, datacatalogv2.FetcherDoOptions{})
if err != nil {
return err
}
*repo = r
return nil
})
r.SetName(fmt.Sprintf("%s(v2)", project))
return r
}

func fetchAndCreateCache(ctx context.Context, project string, fetcher datacatalogv2.Fetchable, opts datacatalogv2.FetcherDoOptions) (*plateauapi.InMemoryRepo, error) {
Expand Down
4 changes: 3 additions & 1 deletion server/datacatalog/datacatalogv3/cms.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ func NewCMS(cms cms.Interface) *CMS {
}

func (c *CMS) GetAll(ctx context.Context, project string) (*AllData, error) {
all := AllData{}
all := AllData{
Name: project,
}

// TODO: get CMSInfo

Expand Down
1 change: 1 addition & 0 deletions server/datacatalog/datacatalogv3/conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

func (all *AllData) Into() (res *plateauapi.InMemoryRepoContext, warning []string) {
res = &plateauapi.InMemoryRepoContext{
Name: all.Name,
Areas: plateauapi.Areas{},
Datasets: plateauapi.Datasets{},
}
Expand Down
20 changes: 15 additions & 5 deletions server/datacatalog/datacatalogv3/conv_dataset_plateau_seed.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,22 @@ func plateauDatasetSeedsFrom(i *PlateauFeatureItem, dt *plateauapi.PlateauDatase
return
}

if dt.Code == "bldg" && len(i.Items) == 0 {
items := i.Items
if len(i.Data) > 0 {
items = append(items, lo.Map(i.Data, func(url string, _ int) PlateauFeatureItemDatum {
return PlateauFeatureItemDatum{
Data: []string{url},
Desc: i.Desc,
}
})...)
}

if dt.Code == "bldg" {
seeds, w := plateauDatasetSeedsFromBldg(i, dt, cityCode, area.Wards)
warning = append(warning, w...)
res = append(res, seeds...)
} else {
for _, item := range i.Items {
for _, item := range items {
seeds, w := plateauDatasetSeedsFromItem(i, item, dt, dic, cityCode)
warning = append(warning, w...)
res = append(res, seeds)
Expand All @@ -70,13 +80,13 @@ func plateauDatasetSeedsFrom(i *PlateauFeatureItem, dt *plateauapi.PlateauDatase
}

func plateauDatasetSeedsFromItem(i *PlateauFeatureItem, item PlateauFeatureItemDatum, dt *plateauapi.PlateauDatasetType, dic Dic, cityCode string) (res plateauDatasetSeed, warning []string) {
assets := lo.Map(item.Data, func(url string, _ int) *AssetName {
assets := lo.FilterMap(item.Data, func(url string, _ int) (*AssetName, bool) {
n := nameWithoutExt(nameFromURL(url))
an := ParseAssetName(n)
if an == nil || !an.Ex.IsValid() {
warning = append(warning, fmt.Sprintf("plateau %s %s: invalid asset name: %s", cityCode, dt.Code, n))
}
return an
return an, an != nil
})
if len(assets) == 0 {
// warning = append(warning, fmt.Sprintf("plateau %s %s: no assets", cityCode, dt.Code))
Expand All @@ -86,7 +96,7 @@ func plateauDatasetSeedsFromItem(i *PlateauFeatureItem, item PlateauFeatureItemD
assetName := assets[0]
key, dickey := assetName.Ex.ItemKey(), assetName.Ex.DicKey()
if key == "" || dickey == "" {
warning = append(warning, fmt.Sprintf("plateau %s %s: invalid asset name key: %s", cityCode, dt.Code, assets[0].Ex.Ex))
warning = append(warning, fmt.Sprintf("plateau %s %s: invalid asset name key: %s", cityCode, dt.Code, assetName.Ex.Ex))
return
}

Expand Down
1 change: 1 addition & 0 deletions server/datacatalog/datacatalogv3/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package datacatalogv3
import "github.com/eukarya-inc/reearth-plateauview/server/datacatalog/plateauapi"

type AllData struct {
Name string
PlateauSpecs []plateauapi.PlateauSpecSimple
FeatureTypes FeatureTypes
City []*CityItem
Expand Down
6 changes: 4 additions & 2 deletions server/datacatalog/datacatalogv3/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (r *Repos) Update(ctx context.Context, project string, rawcms cms.Interface
r.cms[project] = cms
}

log.Infofc(ctx, "datacatalogv3: updating project %s", project)
log.Infofc(ctx, "datacatalogv3: updating repo %s", project)
data, err := cms.GetAll(ctx, project)
if err != nil {
return err
Expand All @@ -97,6 +97,7 @@ func (r *Repos) Update(ctx context.Context, project string, rawcms cms.Interface
adminRepoWrapper := r.adminRepos[project]
if adminRepoWrapper == nil {
adminRepoWrapper = plateauapi.NewRepoWrapper(adminRepo, nil)
adminRepoWrapper.SetName(fmt.Sprintf("%s(admin)", project))
r.adminRepos[project] = adminRepoWrapper
} else {
adminRepoWrapper.SetRepo(adminRepo)
Expand All @@ -105,6 +106,7 @@ func (r *Repos) Update(ctx context.Context, project string, rawcms cms.Interface
repoWrapper := r.repos[project]
if repoWrapper == nil {
repoWrapper = plateauapi.NewRepoWrapper(repo, nil)
repoWrapper.SetName(project)
r.repos[project] = repoWrapper
} else {
repoWrapper.SetRepo(repo)
Expand All @@ -116,7 +118,7 @@ func (r *Repos) Update(ctx context.Context, project string, rawcms cms.Interface
r.updatedAt[project] = time.Now()
}

log.Infofc(ctx, "datacatalogv3: updated project %s", project)
log.Infofc(ctx, "datacatalogv3: updated repo %s", project)
return nil
}

Expand Down
49 changes: 43 additions & 6 deletions server/datacatalog/plateauapi/generated.go

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

2 changes: 1 addition & 1 deletion server/datacatalog/plateauapi/gqlgen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ models:
resolver: true
type:
resolver: true
plateauSpec:
plateauSpecMinor:
resolver: true
PlateauDatasetItem:
fields:
Expand Down
9 changes: 9 additions & 0 deletions server/datacatalog/plateauapi/inmemory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package plateauapi

import (
"context"
"fmt"
"slices"

"github.com/reearth/reearthx/util"
Expand All @@ -10,6 +11,7 @@ import (
)

type InMemoryRepoContext struct {
Name string
Areas Areas
DatasetTypes DatasetTypes
Datasets Datasets
Expand All @@ -34,6 +36,13 @@ func NewInMemoryRepo(ctx *InMemoryRepoContext) *InMemoryRepo {
return r
}

func (c *InMemoryRepo) Name() string {
if c.ctx == nil || c.ctx.Name == "" {
return "inmemory"
}
return fmt.Sprintf("inmemory(%s)", c.ctx.Name)
}

func (c *InMemoryRepo) SetContext(ctx *InMemoryRepoContext) {
c.ctx = ctx
c.areasForDataTypes = areasForDatasetTypes(ctx.Datasets.All())
Expand Down
15 changes: 15 additions & 0 deletions server/datacatalog/plateauapi/inmemory_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type RepoUpdater func(ctx context.Context, repo *Repo) error
// RepoWrapper is a thread-safe wrapper of Repo.
type RepoWrapper struct {
repo Repo
name string
lock sync.RWMutex
updater RepoUpdater
}
Expand All @@ -36,6 +37,10 @@ func (a *RepoWrapper) SetRepo(repo Repo) {
a.repo = repo
}

func (a *RepoWrapper) SetName(name string) {
a.name = name
}

func (a *RepoWrapper) Update(ctx context.Context) error {
if a.updater == nil {
return nil
Expand All @@ -59,6 +64,16 @@ func (a *RepoWrapper) use(f func(r Repo) error) error {

var _ Repo = (*RepoWrapper)(nil)

func (a *RepoWrapper) Name() string {
if a.name != "" {
return a.name
}
if a.repo == nil {
return "wrapper"
}
return a.repo.Name()
}

func (a *RepoWrapper) Node(ctx context.Context, id ID) (res Node, err error) {
err = a.use(func(r Repo) (err error) {
res, err = r.Node(ctx, id)
Expand Down
26 changes: 25 additions & 1 deletion server/datacatalog/plateauapi/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package plateauapi

import (
"context"
"fmt"
"sort"

"github.com/reearth/reearthx/util"
Expand All @@ -19,6 +20,15 @@ func NewMerger(repos ...Repo) *Merger {

var _ Repo = (*Merger)(nil)

func (m *Merger) Name() string {
return fmt.Sprintf("merger(%s)", lo.Map(m.repos, func(r Repo, _ int) string {
if r == nil {
return "nil"
}
return r.Name()
}))
}

func (m *Merger) Node(ctx context.Context, id ID) (Node, error) {
nodes, err := getRepoResults(m.repos, func(r Repo) (Node, error) {
return r.Node(ctx, id)
Expand Down Expand Up @@ -118,7 +128,11 @@ func getRepoResults[T any](repos []Repo, f func(r Repo) (T, error)) ([]T, error)
if r == nil {
return
}
return f(r)
res, err := f(r)
if err != nil {
return res, fmt.Errorf("repo %s: %w", r.Name(), err)
}
return res, nil
})
}

Expand Down Expand Up @@ -152,6 +166,9 @@ func sortNodes[T IDNode](nodes []T) {
}

func getLatestYearNode[T any](results []T) T {
results = lo.Filter(results, func(a T, _ int) bool {
return isNodePresent(a)
})
return lo.MaxBy(results, func(a, b T) bool {
return getYear(a) > getYear(b)
})
Expand Down Expand Up @@ -181,6 +198,13 @@ type YearNode interface {
GetYear() int
}

func isNodePresent(n any) bool {
if n, ok := n.(Node); ok {
return n != nil
}
return false
}

func getYear(n any) int {
if yn, ok := n.(YearNode); ok {
return yn.GetYear()
Expand Down
1 change: 1 addition & 0 deletions server/datacatalog/plateauapi/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var ErrDatacatalogUnavailable = errors.New("datacatalog is currently unavailable

type Repo interface {
QueryResolver
Name() string
}

type Resolver struct {
Expand Down
18 changes: 5 additions & 13 deletions server/datacatalog/plateauapi/schema.resolvers.go

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

Loading

0 comments on commit 15c6342

Please sign in to comment.