Skip to content

Commit

Permalink
Merge branch 'release/1.20.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
RMPall committed Apr 25, 2022
2 parents 445bc99 + af0ef4a commit 250ff30
Show file tree
Hide file tree
Showing 65 changed files with 2,329 additions and 1,437 deletions.
4 changes: 2 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ linters-settings:
golint:
min-confidence: 0
gocyclo:
min-complexity: 15
min-complexity: 20
gocognit:
min-complexity: 40
maligned:
Expand Down Expand Up @@ -69,7 +69,7 @@ linters:
- staticcheck
- structcheck
- stylecheck
- typecheck
# - typecheck
- unconvert
- unused
- varcheck
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ audit:

.PHONY: lint
lint:
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.43.0
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.45.2
golangci-lint run ./...

.PHONY: fmt
Expand Down
22 changes: 6 additions & 16 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import (
"context"
"net/http"

"github.com/gorilla/mux"
"github.com/pkg/errors"

"github.com/ONSdigital/dp-authorisation/auth"
"github.com/ONSdigital/dp-search-api/query"
"github.com/gorilla/mux"
)

var (
Expand Down Expand Up @@ -64,18 +62,12 @@ type ResponseTransformer interface {
TransformSearchResponse(ctx context.Context, responseData []byte, query string, highlight bool) ([]byte, error)
}

type ReleaseResponseTransformer interface {
TransformSearchResponse(ctx context.Context, responseData []byte, req query.ReleaseSearchRequest, highlight bool) ([]byte, error)
}

// NewSearchAPI returns a new Search API struct after registering the routes
func NewSearchAPI(router *mux.Router, dpESClient DpElasticSearcher, deprecatedESClient ElasticSearcher, queryBuilder QueryBuilder, transformer ResponseTransformer, permissions AuthHandler) (*SearchAPI, error) {
errData := SetupData()
if errData != nil {
return nil, errors.Wrap(errData, "Failed to setup data templates")
}

errTimeseries := SetupTimeseries()
if errTimeseries != nil {
return nil, errors.Wrap(errTimeseries, "Failed to setup timeseries templates")
}

api := &SearchAPI{
Router: router,
QueryBuilder: queryBuilder,
Expand All @@ -86,14 +78,12 @@ func NewSearchAPI(router *mux.Router, dpESClient DpElasticSearcher, deprecatedES
}

router.HandleFunc("/search", SearchHandlerFunc(queryBuilder, api.deprecatedESClient, api.Transformer)).Methods("GET")
router.HandleFunc("/timeseries/{cdid}", TimeseriesLookupHandlerFunc(api.deprecatedESClient)).Methods("GET")
router.HandleFunc("/data", DataLookupHandlerFunc(api.deprecatedESClient)).Methods("GET")
createSearchIndexHandler := permissions.Require(update, api.CreateSearchIndexHandlerFunc)
router.HandleFunc("/search", createSearchIndexHandler).Methods("POST")
return api, nil
}

func (a *SearchAPI) AddSearchReleaseAPI(validator QueryParamValidator, builder ReleaseQueryBuilder, searcher ElasticSearcher, transformer ResponseTransformer) *SearchAPI {
func (a *SearchAPI) AddSearchReleaseAPI(validator QueryParamValidator, builder ReleaseQueryBuilder, searcher ElasticSearcher, transformer ReleaseResponseTransformer) *SearchAPI {
a.Router.HandleFunc("/search/releases", SearchReleasesHandlerFunc(validator, builder, searcher, transformer)).Methods("GET")

return a
Expand Down
82 changes: 0 additions & 82 deletions api/data.go

This file was deleted.

132 changes: 0 additions & 132 deletions api/data_test.go

This file was deleted.

56 changes: 35 additions & 21 deletions api/releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import (
)

// SearchReleasesHandlerFunc returns a http handler function handling release calendar search api requests.
func SearchReleasesHandlerFunc(validator QueryParamValidator, builder ReleaseQueryBuilder, searcher ElasticSearcher, transformer ResponseTransformer) http.HandlerFunc {
func SearchReleasesHandlerFunc(validator QueryParamValidator, builder ReleaseQueryBuilder, searcher ElasticSearcher, transformer ReleaseResponseTransformer) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()
params := req.URL.Query()

q, err := url.QueryUnescape(params.Get("query"))
queryString, err := url.QueryUnescape(params.Get("query"))
if err != nil {
log.Warn(ctx, err.Error(), log.Data{"param": "query", "value": params.Get("query")})
http.Error(w, "Bad url encoding of the query parameter", http.StatusBadRequest)
Expand Down Expand Up @@ -64,34 +64,40 @@ func SearchReleasesHandlerFunc(validator QueryParamValidator, builder ReleaseQue
return
}

if time.Time(fromDate.(query.Date)).After(time.Time(toDate.(query.Date))) {
if fromAfterTo(fromDate.(query.Date), toDate.(query.Date)) {
log.Warn(ctx, "fromDate after toDate", log.Data{"fromDate": fromDateParam, "toDate": toDateParam})
http.Error(w, "Invalid date parameters", http.StatusBadRequest)
http.Error(w, "invalid dates - 'from' after 'to'", http.StatusBadRequest)
return
}

relTypeParam := paramGet(params, "release-type", query.Published.String())
relType, err := validator.Validate(ctx, "release-type", relTypeParam)
if err != nil {
log.Warn(ctx, err.Error(), log.Data{"param": "release-type", "value": relTypeParam})
http.Error(w, "Invalid release-type parameter", http.StatusBadRequest)
return
}
upcoming := paramGetBool(params, "type-upcoming", false)
highlight := paramGetBool(params, "highlight", true)

formattedQuery, err := builder.BuildSearchQuery(ctx,
query.ReleaseSearchRequest{
Term: q,
From: offset.(int),
Size: limit.(int),
SortBy: sort.(query.Sort),
ReleasedAfter: fromDate.(query.Date),
ReleasedBefore: toDate.(query.Date),
Upcoming: upcoming,
Published: !upcoming,
Highlight: highlight,
Now: query.Date(time.Now()),
})
searchReq := query.ReleaseSearchRequest{
Term: queryString,
From: offset.(int),
Size: limit.(int),
SortBy: sort.(query.Sort),
ReleasedAfter: fromDate.(query.Date),
ReleasedBefore: toDate.(query.Date),
Type: relType.(query.ReleaseType),
Highlight: highlight,
}

formattedQuery, err := builder.BuildSearchQuery(ctx, searchReq)
if err != nil {
log.Error(ctx, "creation of search release query failed", err, log.Data{"q": q, "sort": sort, "limit": limit, "offset": offset})
log.Error(ctx, "creation of search release query failed", err, log.Data{"q": queryString, "sort": sort, "limit": limit, "offset": offset})
http.Error(w, "Failed to create search release query", http.StatusInternalServerError)
return
}

responseData, err := searcher.Search(ctx, "ons", "", formattedQuery)
responseData, err := searcher.MultiSearch(ctx, "ons", "", formattedQuery)
if err != nil {
log.Error(ctx, "elasticsearch release query failed", err)
http.Error(w, "Failed to run search release query", http.StatusInternalServerError)
Expand All @@ -105,7 +111,7 @@ func SearchReleasesHandlerFunc(validator QueryParamValidator, builder ReleaseQue
}

if !paramGetBool(params, "raw", false) {
responseData, err = transformer.TransformSearchResponse(ctx, responseData, q, highlight)
responseData, err = transformer.TransformSearchResponse(ctx, responseData, searchReq, highlight)
if err != nil {
log.Error(ctx, "transformation of response data failed", err)
http.Error(w, "Failed to transform search result", http.StatusInternalServerError)
Expand All @@ -122,3 +128,11 @@ func SearchReleasesHandlerFunc(validator QueryParamValidator, builder ReleaseQue
}
}
}

func fromAfterTo(from, to query.Date) bool {
if !time.Time(from).IsZero() && !time.Time(to).IsZero() && time.Time(from).After(time.Time(to)) {
return true
}

return false
}
Loading

0 comments on commit 250ff30

Please sign in to comment.