Skip to content

Commit

Permalink
Merge pull request #4 from datacite/split-validation-into-two-distinc…
Browse files Browse the repository at this point in the history
…t-paths

Split validation into two distinct paths
  • Loading branch information
wendelfabianchinsamy authored Jun 20, 2024
2 parents f46e563 + 492d769 commit d4374e9
Show file tree
Hide file tree
Showing 21 changed files with 464 additions and 273 deletions.
21 changes: 11 additions & 10 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

func main() {
app := &cli.App{
app := &cli.App{
Commands: []*cli.Command{
{
Name: "event",
Expand All @@ -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 Expand Up @@ -95,9 +96,9 @@ func main() {
}

// Create shared data used for all datasets
sharedData := reports.SharedData {
Platform: platform,
Publisher: publisher,
sharedData := reports.SharedData{
Platform: platform,
Publisher: publisher,
PublisherId: publisherId,
}

Expand Down Expand Up @@ -155,11 +156,11 @@ func main() {
},
},
},
}
}

if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}

}

Expand Down Expand Up @@ -198,4 +199,4 @@ func migrateDB(conn *gorm.DB) {
if err := db.AutoMigrate(conn); err != nil {
log.Println(err)
}
}
}
6 changes: 3 additions & 3 deletions cmd/worker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ func report_job(repoId string, beginDate time.Time, endDate time.Time, platform
reportsService := reports.NewReportsService(statsService)

// Create shared data used for all datasets
sharedData := reports.SharedData {
Platform: platform,
Publisher: publisher,
sharedData := reports.SharedData{
Platform: platform,
Publisher: publisher,
PublisherId: publisherId,
}

Expand Down
2 changes: 1 addition & 1 deletion internal/app/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ func GetAuthToken(config *app.Config) *jwtauth.JWTAuth {

// Private key is nil because we are only using the public key to verify the token.
return jwtauth.New("RS256", nil, publicKey)
}
}
23 changes: 14 additions & 9 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 All @@ -11,25 +12,28 @@ type Config struct {
}

AnalyticsDatabase struct {
Host string
Port string
User string
Dbname string
Host string
Port string
User string
Dbname string
Password string
Sslmode string
Sslmode string
}

Plausible struct {
Url string
}

DataCite struct {
Url string
JWT string
Url string
JWT string
JWTPublicKey string
}

ValidateDoi bool
Validate struct {
DoiExistence bool
DoiUrl bool
}
}

func getEnv(key, fallback string) string {
Expand All @@ -56,7 +60,8 @@ func GetConfigFromEnv() *Config {
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
}
80 changes: 40 additions & 40 deletions internal/app/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,62 +16,62 @@ import (

// Format a clickhouse dsn from seperate config fields
func CreateClickhouseDSN(host, port, user, password, dbname string) string {
return fmt.Sprintf("clickhouse://%s:%s@%s:%s/%s", user, password, host, port, dbname)
return fmt.Sprintf("clickhouse://%s:%s@%s:%s/%s", user, password, host, port, dbname)
}

func NewGormClickhouseConnection(dsn string) (*gorm.DB, error) {

// Setup a custom logger for gorm
newLogger := logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
logger.Config{
SlowThreshold: time.Second, // Slow SQL threshold
LogLevel: logger.Error, // Log level
IgnoreRecordNotFoundError: true, // Ignore ErrRecordNotFound error for logger
Colorful: false, // Disable color
},
)
// Setup a custom logger for gorm
newLogger := logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
logger.Config{
SlowThreshold: time.Second, // Slow SQL threshold
LogLevel: logger.Error, // Log level
IgnoreRecordNotFoundError: true, // Ignore ErrRecordNotFound error for logger
Colorful: false, // Disable color
},
)

// Open the connection with the custom logger
db, err := gorm.Open(clickhouse.Open(dsn), &gorm.Config{
Logger: newLogger,
})
// Open the connection with the custom logger
db, err := gorm.Open(clickhouse.Open(dsn), &gorm.Config{
Logger: newLogger,
})

if err != nil {
return db, err
}
if err != nil {
return db, err
}

// Gives us special access to do a common table expression with gorm i.e. "with"
db.Use(extraClausePlugin.New())
// Gives us special access to do a common table expression with gorm i.e. "with"
db.Use(extraClausePlugin.New())

return db, nil
return db, nil
}

// Test if the database connection is working
func TestConnection(db *gorm.DB) error {
sqlDB, err := db.DB()
if err != nil {
return err
}
err = sqlDB.Ping()
if err != nil {
return err
}
return nil
sqlDB, err := db.DB()
if err != nil {
return err
}
err = sqlDB.Ping()
if err != nil {
return err
}
return nil
}

// Migrate models
func AutoMigrate(db *gorm.DB) error{
var err error
func AutoMigrate(db *gorm.DB) error {
var err error

err = db.Set("gorm:table_options", event.TABLE_OPTIONS).AutoMigrate(&event.Event{})
err = db.Set("gorm:table_options", event.TABLE_OPTIONS).AutoMigrate(&event.Event{})

if err != nil {
return err
}
if err != nil {
return err
}

err = db.AutoMigrate (
&session.Salt{},
err = db.AutoMigrate(
&session.Salt{},
)
return err
}
return err
}
2 changes: 1 addition & 1 deletion internal/app/event/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ type Event struct {
Useragent string `gorm:"-:all" json:"useragent"`
}

const TABLE_OPTIONS = "ENGINE=MergeTree PARTITION BY toYYYYMM(timestamp) ORDER BY (repo_id, toDate(timestamp), user_id) SAMPLE BY user_id"
const TABLE_OPTIONS = "ENGINE=MergeTree PARTITION BY toYYYYMM(timestamp) ORDER BY (repo_id, toDate(timestamp), user_id) SAMPLE BY user_id"
7 changes: 3 additions & 4 deletions internal/app/event/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ type EventRepositoryReader interface {
//

type EventRepository struct {
db *gorm.DB
config *app.Config
db *gorm.DB
config *app.Config
}

// NewRepository creates a new event repository
func NewEventRepository(db *gorm.DB, config *app.Config) *EventRepository {
return &EventRepository{
db: db,
db: db,
config: config,
}
}
Expand All @@ -38,7 +38,6 @@ func (repository *EventRepository) Create(event *Event) error {
return repository.db.Create(event).Error
}


//
// Plausible implementation of the event repository
//
Expand Down
Loading

0 comments on commit d4374e9

Please sign in to comment.