From 357297e6e9ef298df5df1f3e9921fe58262d439b Mon Sep 17 00:00:00 2001 From: David Rauh Date: Wed, 3 May 2023 14:51:15 +0200 Subject: [PATCH] #21 Add basic contents page --- Dockerfile-debug | 3 +- api/schemas/FilterOptions.yaml | 16 ++-- cmd/mb3server/.openapi-generator/FILES | 3 +- cmd/mb3server/.openapi-generator/VERSION | 2 +- cmd/mb3server/api/openapi.yaml | 86 +++++++++++-------- cmd/mb3server/main.go | 4 +- cmd/mb3server/src/api-impl.go | 81 +++++++++++++++++ cmd/mb3server/src/api_default_service.go | 22 +++-- cmd/mb3server/src/model_browse_options.go | 40 +++++---- cmd/mb3server/src/routers.go | 2 +- docker/docker-compose.yaml | 4 +- pkg/database/db_interface.go | 2 + pkg/database/mongodb.go | 7 ++ pkg/database/postgres.go | 4 + .../src/lib/components/FilterBox.svelte | 19 ++++ web-frontend/src/routes/+layout.server.js | 6 ++ web-frontend/src/routes/contents/+page.svelte | 62 ++++++++++++- 17 files changed, 282 insertions(+), 81 deletions(-) create mode 100644 cmd/mb3server/src/api-impl.go create mode 100644 web-frontend/src/lib/components/FilterBox.svelte create mode 100644 web-frontend/src/routes/+layout.server.js diff --git a/Dockerfile-debug b/Dockerfile-debug index 5227016..a4d4ea3 100644 --- a/Dockerfile-debug +++ b/Dockerfile-debug @@ -2,6 +2,7 @@ FROM docker.io/golang:1.20 AS build WORKDIR /go/src COPY cmd/mb3server/src ./cmd/mb3server/src COPY cmd/mb3server/main.go ./cmd/mb3server +COPY pkg ./pkg COPY go.mod . ENV CGO_ENABLED=0 @@ -16,5 +17,5 @@ VOLUME /var/log COPY --from=build /go/src/mb3server ./ COPY --from=build /go/bin/dlv / EXPOSE 8080/tcp 40000 -CMD ["/dlv", "--log", "--log-dest", "/var/log/delve.log", "--log-output", "rpc","--listen=:40000", "--headless=true", "--api-version=2", "--accept-multiclient", "exec", "./mb3server"] +CMD ["/dlv", "--log", "--log-dest", "/var/log/delve.log", "--log-output", "debugger","--listen=:40000", "--headless=true", "--api-version=2", "--accept-multiclient", "exec", "./mb3server"] diff --git a/api/schemas/FilterOptions.yaml b/api/schemas/FilterOptions.yaml index 8991969..91e16a9 100644 --- a/api/schemas/FilterOptions.yaml +++ b/api/schemas/FilterOptions.yaml @@ -1,15 +1,15 @@ components: schemas: StringCount: - type: object - properties: - values: - type: array - items: + type: array + items: + type: object + properties: + value: type: string - count: - type: integer - minimum: 0 + count: + type: integer + minimum: 0 FilterOptions: type: object properties: diff --git a/cmd/mb3server/.openapi-generator/FILES b/cmd/mb3server/.openapi-generator/FILES index fb4f32f..8812271 100644 --- a/cmd/mb3server/.openapi-generator/FILES +++ b/cmd/mb3server/.openapi-generator/FILES @@ -3,7 +3,6 @@ api/openapi.yaml main.go src/api.go src/api_default.go -src/api_default_service.go src/error.go src/helpers.go src/impl.go @@ -38,5 +37,5 @@ src/model_ms_focused_ion_inner.go src/model_search_result.go src/model_search_result_data_inner.go src/model_search_result_data_inner_spectra_inner.go -src/model_string_count.go +src/model_string_count_inner.go src/routers.go diff --git a/cmd/mb3server/.openapi-generator/VERSION b/cmd/mb3server/.openapi-generator/VERSION index e7e42a4..4be2c72 100644 --- a/cmd/mb3server/.openapi-generator/VERSION +++ b/cmd/mb3server/.openapi-generator/VERSION @@ -1 +1 @@ -6.3.0 \ No newline at end of file +6.5.0 \ No newline at end of file diff --git a/cmd/mb3server/api/openapi.yaml b/cmd/mb3server/api/openapi.yaml index 36e05b3..9c643e4 100644 --- a/cmd/mb3server/api/openapi.yaml +++ b/cmd/mb3server/api/openapi.yaml @@ -594,59 +594,58 @@ components: result_count: 0 timestamp: timestamp compound_start: - values: - - values - - values - count: 0 + - count: 0 + value: value + - count: 0 + value: value contributor: - values: - - values - - values - count: 0 + - count: 0 + value: value + - count: 0 + value: value ms_type: - values: - - values - - values - count: 0 + - count: 0 + value: value + - count: 0 + value: value ion_mode: - values: - - values - - values - count: 0 + - count: 0 + value: value + - count: 0 + value: value instrument_type: - values: - - values - - values - count: 0 + - count: 0 + value: value + - count: 0 + value: value properties: metadata: $ref: '#/components/schemas/Metadata' contributor: - $ref: '#/components/schemas/StringCount' + items: + $ref: '#/components/schemas/StringCount_inner' + type: array instrument_type: - $ref: '#/components/schemas/StringCount' + items: + $ref: '#/components/schemas/StringCount_inner' + type: array ms_type: - $ref: '#/components/schemas/StringCount' + items: + $ref: '#/components/schemas/StringCount_inner' + type: array ion_mode: - $ref: '#/components/schemas/StringCount' + items: + $ref: '#/components/schemas/StringCount_inner' + type: array compound_start: - $ref: '#/components/schemas/StringCount' - type: object - StringCount: - example: - values: - - values - - values - count: 0 - properties: - values: items: - type: string + $ref: '#/components/schemas/StringCount_inner' type: array - count: - minimum: 0 - type: integer type: object + StringCount: + items: + $ref: '#/components/schemas/StringCount_inner' + type: array Authors: items: $ref: '#/components/schemas/Authors_inner' @@ -1154,6 +1153,17 @@ components: default: 100 type: integer type: object + StringCount_inner: + example: + count: 0 + value: value + properties: + value: + type: string + count: + minimum: 0 + type: integer + type: object Authors_inner: description: Name of the author or affiliation example: diff --git a/cmd/mb3server/main.go b/cmd/mb3server/main.go index 81cadd7..ac94ebd 100644 --- a/cmd/mb3server/main.go +++ b/cmd/mb3server/main.go @@ -12,13 +12,15 @@ package main import ( "log" "net/http" + "time" mb3server "github.com/MassBank/MassBank3/cmd/mb3server/src" ) func main() { log.Printf("Server started") - + time.Sleep(5 * time.Second) + log.Printf("Server started 5") DefaultApiService := mb3server.NewDefaultApiService() DefaultApiController := mb3server.NewDefaultApiController(DefaultApiService) diff --git a/cmd/mb3server/src/api-impl.go b/cmd/mb3server/src/api-impl.go new file mode 100644 index 0000000..419c9d8 --- /dev/null +++ b/cmd/mb3server/src/api-impl.go @@ -0,0 +1,81 @@ +package mb3server + +import ( + "github.com/MassBank/MassBank3/pkg/database" + "log" + "os" +) + +var db database.MB3Database = nil + +func initDB() error { + if db == nil { + var mongo_uri = os.Getenv("MONGO_URI") + var mongo_name = os.Getenv("MONGO_DB_NAME") + log.Println("MongoDB URI: ", mongo_uri) + log.Println("Database_Name", mongo_name) + var err error = nil + var config = database.DBConfig{ + Database: database.MongoDB, + DbUser: "", + DbPwd: "", + DbHost: "", + DbName: os.Getenv("MONGO_DB_NAME"), + DbPort: 0, + DbConnStr: os.Getenv("MONGO_URI"), + } + + db, err = database.NewMongoDB(config) + if err != nil { + return err + } + err = db.Connect() + if err != nil { + return err + } + } + return db.Ping() + +} + +func GetBrowseOptions() (*BrowseOptions, error) { + if err := initDB(); err != nil { + return nil, err + } + vals, err := db.GetUniqueValues(database.Filters{}) + if err != nil { + return nil, err + } + var result = BrowseOptions{} + result.Metadata = Metadata{ + Version: "", + Timestamp: "", + GitCommit: "", + SpectraCount: 0, + CompoundCount: 0, + IsomerCount: 0, + ResultCount: 0, + Page: 0, + Limit: 0, + } + for _, val := range vals.IonMode { + result.IonMode = append(result.IonMode, StringCountInner{ + Value: val.Val, + Count: int32(val.Count), + }) + } + for _, val := range vals.MSType { + result.MsType = append(result.MsType, StringCountInner{ + Value: val.Val, + Count: int32(val.Count), + }) + } + for _, val := range vals.InstrumentType { + result.InstrumentType = append(result.InstrumentType, StringCountInner{ + Value: val.Val, + Count: int32(val.Count), + }) + } + + return &result, nil +} diff --git a/cmd/mb3server/src/api_default_service.go b/cmd/mb3server/src/api_default_service.go index 812f7c0..34c4946 100644 --- a/cmd/mb3server/src/api_default_service.go +++ b/cmd/mb3server/src/api_default_service.go @@ -28,24 +28,22 @@ func NewDefaultApiService() DefaultApiServicer { // GetBrowseOptions - get browse options func (s *DefaultApiService) GetBrowseOptions(ctx context.Context) (ImplResponse, error) { - // TODO - update GetBrowseOptions with the required logic for this service method. - // Add api_default_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation. - - //TODO: Uncomment the next line to return response Response(200, BrowseOptions{}) or use other options such as http.Ok ... - //return Response(200, BrowseOptions{}), nil - - return Response(http.StatusNotImplemented, nil), errors.New("GetBrowseOptions method not implemented") + opt, err := GetBrowseOptions() + if err != nil { + return Response(http.StatusInternalServerError, nil), errors.New("Could not get results") + } + return Response(http.StatusOK, opt), nil } // GetFilterOptions - get filter options func (s *DefaultApiService) GetFilterOptions(ctx context.Context) (ImplResponse, error) { - // TODO - update GetFilterOptions with the required logic for this service method. - // Add api_default_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation. - //TODO: Uncomment the next line to return response Response(200, FilterOptions{}) or use other options such as http.Ok ... - //return Response(200, FilterOptions{}), nil + opt, err := GetBrowseOptions() + if err != nil { + return Response(http.StatusInternalServerError, nil), errors.New("Could not get results") + } + return Response(http.StatusOK, opt), nil - return Response(http.StatusNotImplemented, nil), errors.New("GetFilterOptions method not implemented") } // GetMetadata - get massbank metadata diff --git a/cmd/mb3server/src/model_browse_options.go b/cmd/mb3server/src/model_browse_options.go index 967ac16..cab8e10 100644 --- a/cmd/mb3server/src/model_browse_options.go +++ b/cmd/mb3server/src/model_browse_options.go @@ -12,15 +12,15 @@ package mb3server type BrowseOptions struct { Metadata Metadata `json:"metadata,omitempty"` - Contributor StringCount `json:"contributor,omitempty"` + Contributor []StringCountInner `json:"contributor,omitempty"` - InstrumentType StringCount `json:"instrument_type,omitempty"` + InstrumentType []StringCountInner `json:"instrument_type,omitempty"` - MsType StringCount `json:"ms_type,omitempty"` + MsType []StringCountInner `json:"ms_type,omitempty"` - IonMode StringCount `json:"ion_mode,omitempty"` + IonMode []StringCountInner `json:"ion_mode,omitempty"` - CompoundStart StringCount `json:"compound_start,omitempty"` + CompoundStart []StringCountInner `json:"compound_start,omitempty"` } // AssertBrowseOptionsRequired checks if the required fields are not zero-ed @@ -28,20 +28,30 @@ func AssertBrowseOptionsRequired(obj BrowseOptions) error { if err := AssertMetadataRequired(obj.Metadata); err != nil { return err } - if err := AssertStringCountRequired(obj.Contributor); err != nil { - return err + for _, el := range obj.Contributor { + if err := AssertStringCountInnerRequired(el); err != nil { + return err + } } - if err := AssertStringCountRequired(obj.InstrumentType); err != nil { - return err + for _, el := range obj.InstrumentType { + if err := AssertStringCountInnerRequired(el); err != nil { + return err + } } - if err := AssertStringCountRequired(obj.MsType); err != nil { - return err + for _, el := range obj.MsType { + if err := AssertStringCountInnerRequired(el); err != nil { + return err + } } - if err := AssertStringCountRequired(obj.IonMode); err != nil { - return err + for _, el := range obj.IonMode { + if err := AssertStringCountInnerRequired(el); err != nil { + return err + } } - if err := AssertStringCountRequired(obj.CompoundStart); err != nil { - return err + for _, el := range obj.CompoundStart { + if err := AssertStringCountInnerRequired(el); err != nil { + return err + } } return nil } diff --git a/cmd/mb3server/src/routers.go b/cmd/mb3server/src/routers.go index 6f2af3c..bbbf4d0 100644 --- a/cmd/mb3server/src/routers.go +++ b/cmd/mb3server/src/routers.go @@ -192,7 +192,7 @@ func parseBoolParameter(param string, required bool) (bool, error) { return false, err } - return val, nil + return bool(val), nil } // parseInt64ArrayParameter parses a string parameter containing array of values to []int64. diff --git a/docker/docker-compose.yaml b/docker/docker-compose.yaml index 19b7d90..85a4a88 100644 --- a/docker/docker-compose.yaml +++ b/docker/docker-compose.yaml @@ -34,7 +34,7 @@ services: ports: - "${MB3_API_PORT}:8080" environment: - MONGO_URI: "mongodb://${MONGODB_USER}:${MONGODB_PWD}@mongo:27017" + MONGO_URI: "mongodb://${MONGODB_USER}:${MONGODB_PWD}@mongodb:27017" MONGO_DB_NAME: "massbank3" depends_on: - mongodb @@ -42,3 +42,5 @@ services: mb3frontend: restart: always + environment: + VITE_SERVER_API_URL: "http://localhost:8081" diff --git a/pkg/database/db_interface.go b/pkg/database/db_interface.go index 403f970..506d64c 100644 --- a/pkg/database/db_interface.go +++ b/pkg/database/db_interface.go @@ -112,6 +112,8 @@ type MB3Database interface { // Disconnect from the database. Disconnect() error + Ping() error + // Count MassBank records in the database. Count() (int64, error) diff --git a/pkg/database/mongodb.go b/pkg/database/mongodb.go index 44bcc84..e802380 100644 --- a/pkg/database/mongodb.go +++ b/pkg/database/mongodb.go @@ -283,6 +283,13 @@ func (db *Mb3MongoDB) Connect() error { return db.init() } +func (db *Mb3MongoDB) Ping() error { + timeout, _ := time.ParseDuration(MongoConnectTimeout) + ctx, cancel := context.WithTimeout(context.Background(), timeout) + defer cancel() + return db.database.Client().Ping(ctx, nil) +} + // Disconnect see [MB3Database.Disconnect] func (db *Mb3MongoDB) Disconnect() error { if db.database == nil { diff --git a/pkg/database/postgres.go b/pkg/database/postgres.go index 56498e0..28bae82 100644 --- a/pkg/database/postgres.go +++ b/pkg/database/postgres.go @@ -76,6 +76,10 @@ func (p *PostgresSQLDB) Connect() error { return p.init() } +func (p *PostgresSQLDB) Ping() error { + return p.database.Ping() +} + // Disconnect see [MB3Database.Disconnect] func (p *PostgresSQLDB) Disconnect() error { if p.database == nil { diff --git a/web-frontend/src/lib/components/FilterBox.svelte b/web-frontend/src/lib/components/FilterBox.svelte new file mode 100644 index 0000000..562346d --- /dev/null +++ b/web-frontend/src/lib/components/FilterBox.svelte @@ -0,0 +1,19 @@ + + +
+ +
+ + \ No newline at end of file diff --git a/web-frontend/src/routes/+layout.server.js b/web-frontend/src/routes/+layout.server.js new file mode 100644 index 0000000..79e1a91 --- /dev/null +++ b/web-frontend/src/routes/+layout.server.js @@ -0,0 +1,6 @@ +/** @type {import('./$types').LayoutServerLoad} */ +export async function load() { + return { + baseurl: import.meta.env.VITE_SERVER_API_URL, + }; +} \ No newline at end of file diff --git a/web-frontend/src/routes/contents/+page.svelte b/web-frontend/src/routes/contents/+page.svelte index e7cc1b6..8928afd 100644 --- a/web-frontend/src/routes/contents/+page.svelte +++ b/web-frontend/src/routes/contents/+page.svelte @@ -1,4 +1,64 @@ -
Record Index
\ No newline at end of file + + +
Record Index
+ +
+
+ {#await getFilters(base)} +

loading...

+ {:then filters} +
+

Filters

+ +

Contributor

+

Instrument Type

+ {#each filters.instrument_type as t} + {t.value} ({t.count}) + {/each} +

MS Type

+ {#each filters.ms_type as t} + {t.value} ({t.count}) + {/each} +

Ion Mode

+ {#each filters.ion_mode as t} + {t.value} ({t.count}) + {/each} +
+ + {/await} +
+
+

Results

+
+
+ + + \ No newline at end of file