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

feat(server): SDK APi v3 #492

Closed
wants to merge 27 commits into from
Closed
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
393 changes: 1 addition & 392 deletions go.work.sum

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion server/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"os"

"github.com/eukarya-inc/reearth-plateauview/server/cmsintegration"
Expand Down Expand Up @@ -154,11 +155,17 @@ func (c *Config) SearchIndex() searchindex.Config {

func (c *Config) SDKAPI() sdkapi.Config {
return sdkapi.Config{
// common
Token: c.SDK_Token,

// v3
GQLBaseURL: fmt.Sprintf("http://[::]:%d/datacatalog/graphql", c.Port),

// v2
CMSBaseURL: c.CMS_BaseURL,
CMSToken: c.CMS_Token,
Project: c.CMS_PlateauProject,
// Model: c.CMS_SDKModel,
Token: c.SDK_Token,
DisableCache: c.SDKAPI_DisableCache,
CacheTTL: c.SDKAPI_CacheTTL,
}
Expand Down
22 changes: 19 additions & 3 deletions server/datacatalog/datacatalogv2/datacatalogv2adapter/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,20 @@ func fetchAndCreateCache(ctx context.Context, project string, fetcher datacatalo
return nil, fmt.Errorf("failed to update datacatalog cache: %w", err)
}

return plateauapi.NewInMemoryRepo(newCache(r)), nil
all := r.All()

if err := fetchMaxLOD(ctx, all); err != nil {
return nil, fmt.Errorf("failed to fetch max lod: %w", err)
}

return plateauapi.NewInMemoryRepo(newCache(all)), nil
}

func newCache(r datacatalogv2.ResponseAll) *plateauapi.InMemoryRepoContext {
func newCache(items []datacatalogv2.DataCatalogItem) *plateauapi.InMemoryRepoContext {
cache := &plateauapi.InMemoryRepoContext{
PlateauSpecs: plateauSpecs,
}

items := r.All()
areas := make(map[plateauapi.AreaCode]struct{})

for _, d := range items {
Expand Down Expand Up @@ -137,6 +142,17 @@ func newCache(r datacatalogv2.ResponseAll) *plateauapi.InMemoryRepoContext {
if !slices.Contains(cache.Years, d.Year) {
cache.Years = append(cache.Years, d.Year)
}

if citygml := citygmlFrom(d); citygml != nil {
if cache.CityGML == nil {
cache.CityGML = map[plateauapi.ID]*plateauapi.CityGMLDataset{}
}

cg := cache.CityGML[citygml.ID]
if cg == nil || cg.Year < citygml.Year {
cache.CityGML[citygml.ID] = citygml
}
}
}

slices.SortStableFunc(cache.Years, func(a, b int) int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,20 @@ func TestNewCache(t *testing.T) {
},
},
},
MaxLOD: &cms.PublicAsset{
Asset: cms.Asset{
URL: "maxlod",
},
},
},
},
}

all := r.All()
all[0].MaxLODContent = [][]string{
{"000000", "bldg", "1", "test.gml"},
}

expectedCache := &plateauapi.InMemoryRepoContext{
Areas: plateauapi.Areas{
plateauapi.AreaTypePrefecture: []plateauapi.Area{
Expand Down Expand Up @@ -100,12 +110,37 @@ func TestNewCache(t *testing.T) {
},
},
},
CityGML: map[plateauapi.ID]*plateauapi.CityGMLDataset{
"cg_13101": {
ID: "cg_13101",
Year: 2022,
RegistrationYear: 2022,
PrefectureID: "p_13",
PrefectureCode: "13",
CityID: "c_13101",
CityCode: "13101",
PlateauSpecMinorID: "ps_2.3",
URL: "https://example.com/13101_tokyo23ku_2022_citygml_op.zip",
FeatureTypes: []string{"bldg"},
Items: []*plateauapi.CityGMLDatasetItem{
{
URL: "https://example.com/13101_tokyo23ku_2022_citygml_op/udx/bldg/test.gml",
MeshCode: "000000",
TypeCode: "bldg",
MaxLod: 1,
},
},
Admin: map[string]any{
"maxlod": "maxlod",
},
},
},
Years: []int{
2022,
},
PlateauSpecs: plateauSpecs,
}

cache := newCache(r)
cache := newCache(all)
assert.Equal(t, expectedCache, cache)
}
78 changes: 78 additions & 0 deletions server/datacatalog/datacatalogv2/datacatalogv2adapter/maxlod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package datacatalogv2adapter

import (
"context"
"encoding/csv"
"fmt"
"net/http"

"github.com/eukarya-inc/reearth-plateauview/server/datacatalog/datacatalogv2"
"github.com/samber/lo"
"github.com/spkg/bom"
"golang.org/x/sync/errgroup"
)

func fetchMaxLOD(ctx context.Context, all []datacatalogv2.DataCatalogItem) error {
urls := lo.Map(all, func(item datacatalogv2.DataCatalogItem, _ int) string {
if !item.SDKPublic {
return ""
}
return item.MaxLODURL
})

maxlod, err := fetchMaxLODContents(ctx, urls)
if err != nil {
return fmt.Errorf("failed to fetch max lod: %w", err)
}

for i, m := range maxlod {
all[i].MaxLODContent = m
}
return nil
}

func fetchMaxLODContents(ctx context.Context, urls []string) ([][][]string, error) {
res := make([][][]string, len(urls))
eg, ctx := errgroup.WithContext(ctx)
eg.SetLimit(10)

for i := 0; i < len(urls); i++ {
i := i
url := urls[i]
if url == "" {
continue
}

eg.Go(func() error {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return fmt.Errorf("items[%d]: failed to create request: %w", i, err)
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("items[%d]: failed to get max LOD content: %w", i, err)
}

defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("items[%d]: failed to get max LOD content: status code %d", i, resp.StatusCode)
}

c := csv.NewReader(bom.NewReader(resp.Body))
records, err := c.ReadAll()
if err != nil {
return fmt.Errorf("items[%d]: failed to read max LOD content: %w", i, err)
}

res[i] = records
return nil
})
}

if err := eg.Wait(); err != nil {
return nil, err
}

return res, nil
}
47 changes: 47 additions & 0 deletions server/datacatalog/datacatalogv2/datacatalogv2adapter/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package datacatalogv2adapter

import (
"fmt"
"net/url"
"path"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -494,6 +496,51 @@ func cityFrom(d datacatalogv2.DataCatalogItem) *plateauapi.City {
}
}

func citygmlFrom(d datacatalogv2.DataCatalogItem) *plateauapi.CityGMLDataset {
id, code := cityIDFrom(d), cityCodeFrom(d)
if id == nil || code == nil || d.CityGMLURL == "" || d.Spec == "" || len(d.CityGMLFeatureTypes) == 0 {
return nil
}

b := strings.TrimSuffix(d.CityGMLURL, path.Ext(d.CityGMLURL))
items := make([]*plateauapi.CityGMLDatasetItem, 0, len(d.MaxLODContent))
for _, item := range d.MaxLODContent {
if len(item) < 4 {
continue
}

u, _ := url.JoinPath(b, "udx", item[1], item[3])
if u == "" {
continue
}

maxlod, _ := strconv.Atoi(item[2])
items = append(items, &plateauapi.CityGMLDatasetItem{
MeshCode: item[0],
TypeCode: item[1],
MaxLod: maxlod,
URL: u,
})
}

return &plateauapi.CityGMLDataset{
ID: plateauapi.CityGMLDatasetIDFrom(plateauapi.AreaCode(d.CityCode)),
Year: d.Year,
RegistrationYear: registrationYear,
PrefectureID: *prefectureIDFrom(d),
PrefectureCode: *prefectureCodeFrom(d),
CityID: *id,
CityCode: *code,
PlateauSpecMinorID: plateauSpecIDFrom(d.Spec),
URL: d.CityGMLURL,
FeatureTypes: d.CityGMLFeatureTypes,
Admin: map[string]any{
"maxlod": d.MaxLODURL,
},
Items: items,
}
}

func wardFrom(d datacatalogv2.DataCatalogItem) *plateauapi.Ward {
id, code := wardIDFrom(d), wardCodeFrom(d)
if id == nil || code == nil {
Expand Down
11 changes: 8 additions & 3 deletions server/datacatalog/datacatalogv2/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,14 @@ type DataCatalogItem struct {
// alias of type that is used as a folder name
Category string `json:"category,omitempty"`
// internal
Spec string `json:"-"`
Family string `json:"-"`
Edition string `json:"-"`
Spec string `json:"-"`
Family string `json:"-"`
Edition string `json:"-"`
CityGMLURL string `json:"-"`
CityGMLFeatureTypes []string `json:"-"`
MaxLODURL string `json:"-"`
MaxLODContent [][]string `json:"-"`
SDKPublic bool `json:"-"`
}

func (i DataCatalogItem) MainConfigItem() *datacatalogutil.DataCatalogItemConfigItem {
Expand Down
Loading
Loading