Skip to content

Commit

Permalink
rework
Browse files Browse the repository at this point in the history
  • Loading branch information
wendelfabianchinsamy committed Jun 19, 2024
1 parent 5396762 commit 0cc78b8
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 19 deletions.
3 changes: 2 additions & 1 deletion cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ func main() {

// Get configuration from environment variables.
var config = app.GetConfigFromEnv()
config.ValidateDoi = false
config.Validate.DoiExistence = false
config.Validate.DoiUrl = false

// Setup database connection
conn := createDB(config)
Expand Down
8 changes: 4 additions & 4 deletions internal/app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package app

import (
"os"
"strconv"
"strings"
)

Expand Down Expand Up @@ -33,8 +34,6 @@ type Config struct {
DoiExistence bool
DoiUrl bool
}

ValidateDoi bool
}

func getEnv(key, fallback string) string {
Expand All @@ -54,14 +53,15 @@ func GetConfigFromEnv() *Config {
config.DataCite.JWTPublicKey = strings.Replace(getEnv("JWT_PUBLIC_KEY", ""), `\n`, "\n", -1)

// Database config
config.AnalyticsDatabase.Host = getEnv("ANALYTICS_DATABASE_HOST", "localhost")
config.AnalyticsDatabase.Host = getEnv("ANALYTICS_DATABASE_HOST", "wendels-mbp.lan")
config.AnalyticsDatabase.Port = getEnv("ANALYTICS_DATABASE_PORT", "9000")
config.AnalyticsDatabase.User = getEnv("ANALYTICS_DATABASE_USER", "keeshond")
config.AnalyticsDatabase.Dbname = getEnv("ANALYTICS_DATABASE_DBNAME", "keeshond")
config.AnalyticsDatabase.Password = getEnv("ANALYTICS_DATABASE_PASSWORD", "keeshond")

// Validate DOI
config.ValidateDoi = getEnv("VALIDATE_DOI", "true") == "true"
config.Validate.DoiExistence, _ = strconv.ParseBool(getEnv("VALIDATE_DOI_EXISTENCE", "true"))
config.Validate.DoiUrl, _ = strconv.ParseBool(getEnv("VALIDATE_DOI_URL", "false"))

return &config
}
14 changes: 7 additions & 7 deletions internal/app/event/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,7 @@ func (service *EventService) CreateRaw(event Event) (Event, error) {
func (service *EventService) Validate(eventRequest *EventRequest) error {
var err error

if eventRequest.Name != "view" {
return err
}

if !service.shouldValidate() {
if !shouldValidate(service, eventRequest) {
return err
}

Expand All @@ -127,8 +123,12 @@ func (service *EventService) Validate(eventRequest *EventRequest) error {
return err
}

func (service *EventService) shouldValidate() bool {
return service.config.Validate.DoiExistence && service.config.Validate.DoiUrl
func shouldValidate(service *EventService, eventRequest *EventRequest) bool {
if eventRequest.Name != "view" {
return false
}

return service.config.Validate.DoiExistence || service.config.Validate.DoiUrl
}

func getDoi(doi string, dataciteApiUrl string) (*http.Response, error) {
Expand Down
18 changes: 12 additions & 6 deletions internal/app/stats/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ func setupTestDB(config *app.Config) (*gorm.DB, error) {
func setup() TestState {
// Test config
config := app.GetConfigFromEnv()
config.ValidateDoi = false
config.Validate.DoiExistence = false
config.Validate.DoiUrl = false
config.AnalyticsDatabase.Dbname = "keeshond_test"

conn, err := setupTestDB(config)
Expand Down Expand Up @@ -127,7 +128,8 @@ func TestMain(m *testing.M) {
func TestStatsService_Aggregate(t *testing.T) {
// Test config
config := app.GetConfigFromEnv()
config.ValidateDoi = false
config.Validate.DoiExistence = false
config.Validate.DoiUrl = false
config.AnalyticsDatabase.Dbname = "keeshond_test"

conn, err := setupTestDB(config)
Expand Down Expand Up @@ -174,7 +176,8 @@ func TestStatsService_Aggregate(t *testing.T) {
func TestStatsService_Timeseries(t *testing.T) {
// Test config
config := app.GetConfigFromEnv()
config.ValidateDoi = false
config.Validate.DoiExistence = false
config.Validate.DoiUrl = false
config.AnalyticsDatabase.Dbname = "keeshond_test"

conn, err := setupTestDB(config)
Expand Down Expand Up @@ -230,7 +233,8 @@ func TestStatsService_Timeseries(t *testing.T) {
func TestStatsService_BreakdownByPID(t *testing.T) {
// Test config
config := app.GetConfigFromEnv()
config.ValidateDoi = false
config.Validate.DoiExistence = false
config.Validate.DoiUrl = false
config.AnalyticsDatabase.Dbname = "keeshond_test"

conn, err := setupTestDB(config)
Expand Down Expand Up @@ -265,7 +269,8 @@ func TestStatsService_BreakdownByPID(t *testing.T) {
func TestStatsService_CountUniquePID(t *testing.T) {
// Test config
config := app.GetConfigFromEnv()
config.ValidateDoi = false
config.Validate.DoiExistence = false
config.Validate.DoiUrl = false
config.AnalyticsDatabase.Dbname = "keeshond_test"

conn, err := setupTestDB(config)
Expand Down Expand Up @@ -300,7 +305,8 @@ func TestStatsService_CountUniquePID(t *testing.T) {
func TestStatsService_LastEvent(t *testing.T) {
// Test config
config := app.GetConfigFromEnv()
config.ValidateDoi = false
config.Validate.DoiExistence = false
config.Validate.DoiUrl = false
config.AnalyticsDatabase.Dbname = "keeshond_test"

conn, err := setupTestDB(config)
Expand Down
8 changes: 7 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ Configuration is taken from the environment

### Web tracking Config

- VALIDATE_DOI - Can enable/disable DOI validation for event tracking - default to true.
- VALIDATE_DOI_EXISTENCE - Can enable/disable DOI existence validation for event tracking - default to true.
- VALIDATE_DOI_URL - Can enable/disable DOI URL validation for event tracking - default to false.
- DATACITE_API_URL - This is used only when storing events as part of DOI validation
- JWT_PUBLIC_KEY - This is used on authenticated endpoints to validate valid DataCite JWTs

Expand Down Expand Up @@ -99,4 +100,9 @@ docker build -f ./docker/worker/Dockerfile -t keeshondworker .
# Run docker with env vars
docker run --network="host" --env REPO_ID=datacite.demo --env BEGIN_DATE=2022-01-01 --env END_DATE=2022-12-31 --env PLATFORM=datacite --env PUBLISHER="datacite demo" --env PUBLISHER_ID=datacite.demo keeshondworker

```

```
# Connect to the local docker Clickhouse database container
clickhouse client --user=keeshond --password=keeshond
```

0 comments on commit 0cc78b8

Please sign in to comment.