Skip to content

Commit

Permalink
allow all matches, all sources options in web UI (close #58)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimus committed Oct 28, 2021
1 parent 0280cad commit d0c96f6
Show file tree
Hide file tree
Showing 12 changed files with 292 additions and 219 deletions.
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [v0.5.2]

- Add [#58]: add all matches/all sources to web interface.

## [v0.5.1]

- Fix: command line and configuration options
Expand Down Expand Up @@ -87,6 +91,12 @@

This document follows [changelog guidelines]

[v0.5.2]: https://github.com/gnames/gnverifier/compare/v0.5.1...v0.5.2
[v0.5.1]: https://github.com/gnames/gnverifier/compare/v0.5.0...v0.5.1
[v0.5.0]: https://github.com/gnames/gnverifier/compare/v0.4.1...v0.5.0
[v0.4.1]: https://github.com/gnames/gnverifier/compare/v0.4.0...v0.4.1
[v0.4.0]: https://github.com/gnames/gnverifier/compare/v0.3.3...v0.4.0
[v0.3.3]: https://github.com/gnames/gnverifier/compare/v0.3.2...v0.3.3
[v0.3.2]: https://github.com/gnames/gnverifier/compare/v0.3.1...v0.3.2
[v0.3.1]: https://github.com/gnames/gnverifier/compare/v0.3.0...v0.3.1
[v0.3.0]: https://github.com/gnames/gnverifier/compare/v0.2.5...v0.3.0
Expand All @@ -98,6 +108,16 @@ This document follows [changelog guidelines]
[v0.2.0]: https://github.com/gnames/gnverifier/compare/v0.1.0...v0.2.0
[v0.1.0]: https://github.com/gnames/gnverifier/tree/v0.1.0

[#70]: https://github.com/gnames/gnverifier/issues/70
[#69]: https://github.com/gnames/gnverifier/issues/69
[#68]: https://github.com/gnames/gnverifier/issues/68
[#67]: https://github.com/gnames/gnverifier/issues/67
[#66]: https://github.com/gnames/gnverifier/issues/66
[#65]: https://github.com/gnames/gnverifier/issues/65
[#64]: https://github.com/gnames/gnverifier/issues/64
[#63]: https://github.com/gnames/gnverifier/issues/63
[#62]: https://github.com/gnames/gnverifier/issues/62
[#61]: https://github.com/gnames/gnverifier/issues/61
[#60]: https://github.com/gnames/gnverifier/issues/60
[#59]: https://github.com/gnames/gnverifier/issues/59
[#58]: https://github.com/gnames/gnverifier/issues/58
Expand Down
14 changes: 8 additions & 6 deletions gnverifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sync"
"time"

"github.com/gnames/gnlib/ent/gnvers"
vlib "github.com/gnames/gnlib/ent/verifier"
"github.com/gnames/gnverifier/config"
"github.com/gnames/gnverifier/ent/verifier"
Expand All @@ -26,6 +27,11 @@ func New(cnf config.Config, vfr verifier.Verifier) GNverifier {
}
}

// GetVersion returns version and build of GNverifier
func (gnv gnverifier) GetVersion() gnvers.Version {
return gnvers.Version{Version: Version, Build: Build}
}

// DataSources returns meta-information about aggregated data-sources.
func (gnv gnverifier) DataSources() ([]vlib.DataSource, error) {
return gnv.verifier.DataSources(context.Background())
Expand All @@ -52,12 +58,7 @@ func (gnv gnverifier) Config() config.Config {
// VerifyOne verifies one input string and returns results
// as a string in JSON or CSV format.
func (gnv gnverifier) VerifyOne(name string) (vlib.Verification, error) {
params := vlib.VerifyParams{
NameStrings: []string{name},
PreferredSources: gnv.config.PreferredSources,
WithAllMatches: gnv.config.WithAllMatches,
WithCapitalization: gnv.config.WithCapitalization,
}
params := gnv.setParams([]string{name})
verif := gnv.verifier.Verify(context.Background(), params)
if len(verif) < 1 {
return vlib.Verification{}, errors.New("no verification results")
Expand Down Expand Up @@ -145,6 +146,7 @@ func (gnv gnverifier) setParams(names []string) vlib.VerifyParams {
NameStrings: names,
PreferredSources: gnv.config.PreferredSources,
WithCapitalization: gnv.config.WithCapitalization,
WithAllMatches: gnv.config.WithAllMatches,
}
return res
}
4 changes: 4 additions & 0 deletions interface.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gnverifier

import (
"github.com/gnames/gnlib/ent/gnvers"
vlib "github.com/gnames/gnlib/ent/verifier"
"github.com/gnames/gnverifier/config"
)
Expand Down Expand Up @@ -32,4 +33,7 @@ type GNverifier interface {
// DataSource uses ID input to return meta-information about a particular
// data-source.
DataSource(id int) (vlib.DataSource, error)

// GetVersion returns version of the gnverifier
GetVersion() gnvers.Version
}
33 changes: 23 additions & 10 deletions io/web/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type formInput struct {
Names string `query:"names" form:"names"`
Format string `query:"format" form:"format"`
PreferredOnly string `query:"preferred_only" form:"preferred_only"`
AllSources string `query:"all_sources" form:"all_sources"`
AllMatches string `query:"all_matches" form:"all_matches"`
Capitalize string `query:"capitalize" form:"capitalize"`
DS []int `query:"ds" form:"ds"`
}
Expand Down Expand Up @@ -51,8 +53,8 @@ func Run(gnv gnverifier.GNverifier, port int) {
e.POST("/", homePOST(gnv))
e.GET("/data_sources", dataSources(gnv))
e.GET("/data_sources/:id", dataSource(gnv))
e.GET("/about", about())
e.GET("/api", api())
e.GET("/about", about(gnv))
e.GET("/api", api(gnv))

fs := http.FileServer(http.FS(static))
e.GET("/static/*", echo.WrapHandler(fs))
Expand All @@ -71,29 +73,31 @@ type Data struct {
Input string
Format string
Preferred []int
AllMatches bool
Verified []vlib.Verification
DataSources []vlib.DataSource
DataSource vlib.DataSource
Version string
}

func about() func(echo.Context) error {
func about(gnv gnverifier.GNverifier) func(echo.Context) error {
return func(c echo.Context) error {
data := Data{Page: "about"}
data := Data{Page: "about", Version: gnv.GetVersion().Version}
return c.Render(http.StatusOK, "layout", data)
}
}

func api() func(echo.Context) error {
func api(gnv gnverifier.GNverifier) func(echo.Context) error {
return func(c echo.Context) error {
data := Data{Page: "api"}
data := Data{Page: "api", Version: gnv.GetVersion().Version}
return c.Render(http.StatusOK, "layout", data)
}
}

func dataSources(gnv gnverifier.GNverifier) func(echo.Context) error {
return func(c echo.Context) error {
var err error
data := Data{Page: "data_sources"}
data := Data{Page: "data_sources", Version: gnv.GetVersion().Version}
data.DataSources, err = gnv.DataSources()
if err != nil {
return err
Expand All @@ -105,7 +109,7 @@ func dataSources(gnv gnverifier.GNverifier) func(echo.Context) error {
func dataSource(gnv gnverifier.GNverifier) func(echo.Context) error {
return func(c echo.Context) error {
var err error
data := Data{Page: "data_source"}
data := Data{Page: "data_source", Version: gnv.GetVersion().Version}
idStr := c.Param("id")
id, err := strconv.Atoi(idStr)
if err != nil {
Expand All @@ -121,7 +125,7 @@ func dataSource(gnv gnverifier.GNverifier) func(echo.Context) error {

func homeGET(gnv gnverifier.GNverifier) func(echo.Context) error {
return func(c echo.Context) error {
data := Data{Page: "home", Format: "html"}
data := Data{Page: "home", Format: "html", Version: gnv.GetVersion().Version}

inp := new(formInput)
err := c.Bind(inp)
Expand All @@ -140,7 +144,7 @@ func homeGET(gnv gnverifier.GNverifier) func(echo.Context) error {
func homePOST(gnv gnverifier.GNverifier) func(echo.Context) error {
return func(c echo.Context) error {
inp := new(formInput)
data := Data{Page: "home", Format: "html"}
data := Data{Page: "home", Format: "html", Version: gnv.GetVersion().Version}

err := c.Bind(inp)
if err != nil {
Expand Down Expand Up @@ -186,6 +190,8 @@ func redirectToHomeGET(c echo.Context, inp *formInput) error {
q := make(url.Values)
q.Set("names", inp.Names)
q.Set("format", inp.Format)
q.Set("all_sources", inp.AllSources)
q.Set("all_matches", inp.AllMatches)
if prefOnly {
q.Set("preferred_only", inp.PreferredOnly)
}
Expand All @@ -208,9 +214,15 @@ func verificationResults(
var names []string
prefOnly := inp.PreferredOnly == "on"
caps := inp.Capitalize == "on"
data.AllMatches = inp.AllMatches == "on"

data.Input = inp.Names

data.Preferred = inp.DS
if inp.AllSources == "on" {
data.Preferred = []int{0}
}

format := inp.Format
if format == "csv" || format == "json" || format == "tsv" {
data.Format = format
Expand All @@ -233,6 +245,7 @@ func verificationResults(
opts := []config.Option{
config.OptPreferredSources(data.Preferred),
config.OptWithCapitalization(caps),
config.OptWithAllMatches(data.AllMatches),
}
gnv = gnv.ChangeConfig(opts...)

Expand Down
16 changes: 14 additions & 2 deletions io/web/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,27 @@ func handlerGET(path string, t *testing.T) (echo.Context, *httptest.ResponseReco
func TestAbout(t *testing.T) {
c, rec := handlerGET("/about", t)

assert.Nil(t, about()(c))
verifs := verifications(t)
cfg := config.New()
vfr := new(vtest.FakeVerifier)
vfr.VerifyReturns(verifs)
gnv := gnverifier.New(cfg, vfr)

assert.Nil(t, about(gnv)(c))
assert.Equal(t, rec.Code, http.StatusOK)
assert.Contains(t, rec.Body.String(), "Matching Process")
}

func TestAPI(t *testing.T) {
c, rec := handlerGET("/api", t)

assert.Nil(t, api()(c))
verifs := verifications(t)
cfg := config.New()
vfr := new(vtest.FakeVerifier)
vfr.VerifyReturns(verifs)
gnv := gnverifier.New(cfg, vfr)

assert.Nil(t, api(gnv)(c))
assert.Equal(t, rec.Code, http.StatusOK)
assert.Contains(t, rec.Body.String(), "OpenAPI Schema")
}
Expand Down
14 changes: 7 additions & 7 deletions io/web/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,19 @@ func addFuncs(tmpl *template.Template) {
"matchType": func(mt vlib.MatchTypeValue, ed int) template.HTML {
var res string
clr := map[string]string{
"green": "#080",
"yellow": "#a80",
"red": "#800",
"green": "green",
"yellow": "orange",
"red": "red",
}
switch mt {
case vlib.Exact:
res = fmt.Sprintf("<span style='color: %s'>%s match by canonical form</span>", clr["green"], mt)
res = fmt.Sprintf("<span style='color: %s'>✔</span> %s match by canonical form", clr["green"], mt)
case vlib.NoMatch:
res = fmt.Sprintf("<span style='color: %s'>%s</span>", clr["red"], mt)
res = fmt.Sprintf("<span style='color: %s'></span> %s", clr["red"], mt)
case vlib.Fuzzy, vlib.PartialFuzzy:
res = fmt.Sprintf("<span style='color: %s'>%s match, edit distance: %d</span>", clr["yellow"], mt, ed)
res = fmt.Sprintf("<span style='color: %s'>✔</span> %s match, edit distance: %d", clr["yellow"], mt, ed)
default:
res = fmt.Sprintf("<span style='color: %s'>%s match</span>", clr["yellow"], mt)
res = fmt.Sprintf("<span style='color: %s'>✔</span> %s match</span>", clr["yellow"], mt)
}
return template.HTML(res)
},
Expand Down
Loading

0 comments on commit d0c96f6

Please sign in to comment.