Skip to content

Commit

Permalink
#21 Add frontend filters
Browse files Browse the repository at this point in the history
  • Loading branch information
David Rauh committed May 10, 2023
1 parent 0434d4d commit d72e23f
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 57 deletions.
54 changes: 43 additions & 11 deletions cmd/mb3server/src/api-impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mb3server

import (
"github.com/MassBank/MassBank3/pkg/database"
"github.com/MassBank/MassBank3/pkg/massbank"
"log"
"os"
)
Expand Down Expand Up @@ -34,7 +35,11 @@ func initDB() error {
return err
}
}
return db.Ping()
err := db.Ping()
if err != nil {
db = nil
}
return err

}

Expand Down Expand Up @@ -82,40 +87,67 @@ func GetBrowseOptions() (*BrowseOptions, error) {
Count: int32(val.Count),
})
}
for _, val := range vals.CompoundStart {
result.CompoundStart = append(result.CompoundStart, StringCountInner{
Value: val.Val,
Count: int32(val.Count),
})
}

return &result, nil
}

func GetRecords(limit int32, page int32) (*SearchResult, error) {
func GetRecords(limit int32, page int32, contributor string, instrumentType []string, msType []string, ionMode string) (*SearchResult, error) {
if limit <= 0 {
limit = 20
}
if page <= 0 {
page = 1
}

msTypeLokal := &[]massbank.MsType{}
for _, t := range msType {
switch t {
case "MS":
*msTypeLokal = append(*msTypeLokal, massbank.MS)
case "MS2":
*msTypeLokal = append(*msTypeLokal, massbank.MS2)
case "MS3":
*msTypeLokal = append(*msTypeLokal, massbank.MS3)
case "MS4":
*msTypeLokal = append(*msTypeLokal, massbank.MS4)
}
}

if len(*msTypeLokal) == 0 {
msTypeLokal = nil
}

var ionModeLokal massbank.IonMode = massbank.ANY
switch ionMode {
case "POSITIVE":
ionModeLokal = massbank.POSITIVE
case "NEGATIVE":
ionModeLokal = massbank.NEGATIVE
}
log.Println(ionModeLokal)
it := &instrumentType
if len(*it) == 0 {
it = nil
}

var offset = (page - 1) * limit
if err := initDB(); err != nil {
return nil, err
}

var filters = database.Filters{
InstrumentType: nil,
Splash: "",
MsType: nil,
IonMode: "",
MsType: msTypeLokal,
IonMode: ionModeLokal,
CompoundName: "",
Mass: nil,
MassEpsilon: nil,
Formula: "",
Peaks: nil,
PeakDifferences: nil,
InchiKey: "",
Contributor: "",
Contributor: contributor,
IntensityCutoff: nil,
Limit: int64(limit),
Offset: int64(offset),
Expand Down
2 changes: 1 addition & 1 deletion cmd/mb3server/src/api_default_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (s *DefaultApiService) GetRecord(ctx context.Context, accession string) (Im

// GetRecords - Get a list of records
func (s *DefaultApiService) GetRecords(ctx context.Context, instrumentType []string, splash string, msType []string, ionMode string, compoundName string, exactMass string, massTolerance float64, formula string, peaks []string, intensity int32, peakDifferences []string, peakList []string, limit int32, page int32, intensityCutoff int32, inchiKey string, contributor string) (ImplResponse, error) {
result, err := GetRecords(limit, page)
result, err := GetRecords(limit, page, contributor, instrumentType, msType, ionMode)
if err != nil {
return Response(http.StatusInternalServerError, nil), errors.New("Could not get results")
}
Expand Down
27 changes: 7 additions & 20 deletions pkg/database/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,6 @@ func (db *Mb3MongoDB) GetUniqueValues(filters Filters) (MB3Values, error) {
var query = `
[
{ "$facet": {
"CompoundStart": [
{ "$project": { "compound.names": 1}},
{ "$unwind": "$compound.names" },
{"$group": {
"_id": {"$substr": [ {"$trim": { "input": {"$toUpper":"$compound.names"}, "chars": "()+-[]{}/? ,"}},0,1]},
"Count": {"$sum": 1}
}},
{
"$project": {
"_id": 0,
"Val": "$_id",
"Count": 1
}
},
{
"$sort": {
"Val": 1
}
}
],
"Contributor": [
{
"$project": { "_id": 0, "cont": {"$arrayElemAt":[ {"$split": [ "$accession","-"]}, 1]} }
Expand Down Expand Up @@ -359,6 +339,13 @@ func (db *Mb3MongoDB) Ping() error {
timeout, _ := time.ParseDuration(MongoConnectTimeout)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
if db.database == nil {
return errors.New("Database not connected")
}
client := db.database.Client()
if client == nil {
return errors.New("Database not connected")
}
return db.database.Client().Ping(ctx, nil)
}

Expand Down
13 changes: 7 additions & 6 deletions web-frontend/src/lib/components/FilterBox.svelte
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
<script>
export let group;
export let val;
let values= []
export let values;
export let result= [];
</script>

{#each values as v}
<div class="filteroption">
<label>
<input type=checkbox bind:group={values} name={group} value={val}>
<slot />
<input type=checkbox bind:group={result} name={group} value={v.value}>
{v.value} ({v.count}}
</label>
</div>

{/each}
<style>
.filteroption {
font-size: 0.8em;
padding: 0 0.5em;
}
</style>
</style>
3 changes: 3 additions & 0 deletions web-frontend/src/lib/components/Pagination.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
<slot></slot>

<style>
.pagination {
padding: 1em;
}
.current-page {
font-size: 1.2em;
font-weight: bold;
Expand Down
35 changes: 16 additions & 19 deletions web-frontend/src/routes/contents/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
async function getResults(page) {
let url = new URL("/v1/records",base)
url.searchParams.append('page',page.toString())
contribuors.forEach(element => url.searchParams.append('contributor',element))
msType.forEach(element => url.searchParams.append('ms_type',element))
instrumentType.forEach(element => url.searchParams.append('instrument_type',element))
ionMode.forEach(element => url.searchParams.append('ion-mode',element))
console.log(url)
let resp = await fetch(url)
let jsonData = await resp.json();
Expand All @@ -37,10 +42,14 @@
}
}
$: base = data.baseurl;
let contribuors = [];
let msType = [];
let instrumentType = [];
let ionMode = [];
$: base = data.baseurl
</script>

{page}
{base}
{contribuors}
<div class="pure-g">
<div class="pure-u-1-5">
{#await getFilters(base)}
Expand All @@ -49,25 +58,13 @@
<div class="card">
<h2>Filters</h2>
<h3>Contributor</h3>
{#each filters.contributor as t}
<FilterButton group="cont" val={t.value}>{t.value} ({t.count})</FilterButton>
{/each}
<FilterButton bind:result={contribuors} group="cont" values={filters.contributor}></FilterButton>
<h3>Instrument Type</h3>
{#each filters.instrument_type as t}
<FilterButton group="insttype" val={t.value}>{t.value} ({t.count})</FilterButton>
{/each}
<FilterButton bind:result={instrumentType} group="itype" values={filters.instrument_type}></FilterButton>
<h3>MS Type</h3>
{#each filters.ms_type as t}
<FilterButton group="mstype" val={t.value}>{t.value} ({t.count})</FilterButton>
{/each}
<FilterButton bind:result={msType} group="mstype" values={filters.ms_type}></FilterButton>
<h3>Ion Mode</h3>
{#each filters.ion_mode as t}
<FilterButton group="ionmode" val={t.value}>{t.value} ({t.count})</FilterButton>
{/each}
<h3>Compound Start</h3>
{#each filters.compound_start as t}
<FilterButton group="compoundstart" val={t.value}>{t.value} ({t.count})</FilterButton>
{/each}
<FilterButton bind:result={ionMode} group="imode" values={filters.ion_mode}></FilterButton>
</div>
{:catch error}
<div class="error">Error during Filter loading</div>
Expand Down

0 comments on commit d72e23f

Please sign in to comment.