Skip to content

Commit

Permalink
Merge pull request #181 from COS301-SE-2024/feat/backend/cache-databa…
Browse files Browse the repository at this point in the history
…se-functions

Feat/backend/cache database functions
  • Loading branch information
waveyboym authored Jul 10, 2024
2 parents 53287f6 + ffdfe57 commit 860fde1
Show file tree
Hide file tree
Showing 17 changed files with 939 additions and 375 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint-test-build-golang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ jobs:
- name: Run tests
run: |
gotestsum --format testname -- -v -coverpkg=github.com/COS301-SE-2024/occupi/occupi-backend/pkg/utils,github.com/COS301-SE-2024/occupi/occupi-backend/pkg/authenticator,github.com/COS301-SE-2024/occupi/occupi-backend/pkg/middleware ./tests/... -coverprofile=coverage.out
gotestsum --format testname -- -v -coverpkg=github.com/COS301-SE-2024/occupi/occupi-backend/pkg/authenticator,github.com/COS301-SE-2024/occupi/occupi-backend/pkg/database,github.com/COS301-SE-2024/occupi/occupi-backend/pkg/middleware,github.com/COS301-SE-2024/occupi/occupi-backend/pkg/utils ./tests/... -coverprofile=coverage.out
- name: Upload coverage reports to Codecov
uses: codecov/[email protected]
Expand Down
10 changes: 10 additions & 0 deletions occupi-backend/configs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
OtpExpiration = "OTP_EXPIRATION"
FrontendURL = "FRONTEND_URL"
ConfigLicense = "CONFIG_LICENSE"
CacheEviction = "CACHE_EVICTION"
OtpGenReqEviction = "OTP_GEN_REQ_EVICTION"
AllowOriginsVal = "ALLOW_ORIGINS"
AllowMethodsVal = "ALLOW_METHODS"
Expand Down Expand Up @@ -258,6 +259,15 @@ func GetConfigLicense() string {
return license
}

// gets the cache eviction time as defined in the config.yaml file in seconds
func GetCacheEviction() int {
time := viper.GetInt(CacheEviction)
if time == 0 {
time = 600
}
return time
}

// gets the otp request eviction time as defined in the config.yaml file in seconds
func GetOTPReqEviction() int {
time := viper.GetInt(OtpGenReqEviction)
Expand Down
Binary file modified occupi-backend/configs/config.yaml.gpg
Binary file not shown.
Binary file modified occupi-backend/configs/dev.deployed.yaml.gpg
Binary file not shown.
Binary file modified occupi-backend/configs/dev.localhost.yaml.gpg
Binary file not shown.
Binary file modified occupi-backend/configs/prod.yaml.gpg
Binary file not shown.
4 changes: 2 additions & 2 deletions occupi-backend/configs/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ func ConnectToDatabase(args ...string) *mongo.Client {

// Create cache
func CreateCache() *bigcache.BigCache {
config := bigcache.DefaultConfig(10 * time.Minute) // Set the eviction time to 5 seconds
config.CleanWindow = 10 * time.Minute // Set the cleanup interval to 5 seconds
config := bigcache.DefaultConfig(time.Duration(GetCacheEviction()) * time.Second) // Set the eviction time to 5 seconds
config.CleanWindow = time.Duration(GetCacheEviction()/2) * time.Second // Set the cleanup interval to 5 seconds
cache, err := bigcache.New(context.Background(), config)
if err != nil {
logrus.Fatal(err)
Expand Down
Binary file modified occupi-backend/configs/test.yaml.gpg
Binary file not shown.
14 changes: 14 additions & 0 deletions occupi-backend/data/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,17 @@ func insertData(collection *mongo.Collection, documents []interface{}) {
log.Printf("Collection %s already has %d documents, skipping seeding\n", collection.Name(), count)
}
}

func CleanDatabase() {
// connect to the database
db := configs.ConnectToDatabase(constants.AdminDBAccessOption)

// Drop all collections
collections := []string{"OTPS", "RoomBooking", "Rooms", "Users"}
for _, collection := range collections {
if err := db.Database(configs.GetMongoDBName()).Collection(collection).Drop(context.Background()); err != nil {
log.Fatalf("Failed to drop collection %s: %v", collection, err)
}
log.Printf("Successfully dropped collection %s\n", collection)
}
}
4 changes: 2 additions & 2 deletions occupi-backend/occupi.bat
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ if "%1 %2" == "run dev" (
gotestsum --format testname -- -v ./tests/...
exit /b 0
) else if "%1 %2" == "test codecov" (
gotestsum --format testname -- -v -coverpkg=github.com/COS301-SE-2024/occupi/occupi-backend/pkg/utils,github.com/COS301-SE-2024/occupi/occupi-backend/pkg/authenticator,github.com/COS301-SE-2024/occupi/occupi-backend/pkg/middleware ./tests/... -coverprofile=coverage.out
gotestsum --format testname -- -v -coverpkg=github.com/COS301-SE-2024/occupi/occupi-backend/pkg/authenticator,github.com/COS301-SE-2024/occupi/occupi-backend/pkg/database,github.com/COS301-SE-2024/occupi/occupi-backend/pkg/middleware,github.com/COS301-SE-2024/occupi/occupi-backend/pkg/utils ./tests/... -coverprofile=coverage.out
exit /b 0
) else if "%1" == "lint" (
golangci-lint run
Expand All @@ -40,7 +40,7 @@ echo run prod : go run cmd/occupi-backend/main.go -env=dev.localhost
echo build dev : go build -v cmd/occupi-backend/main.go
echo build prod : go build cmd/occupi-backend/main.go
echo test : gotestsum --format testname -- -v ./tests/...
echo test codecov : gotestsum --format testname -- -v -coverpkg=github.com/COS301-SE-2024/occupi/occupi-backend/pkg/utils,github.com/COS301-SE-2024/occupi/occupi-backend/pkg/authenticator,github.com/COS301-SE-2024/occupi/occupi-backend/pkg/middleware ./tests/... -coverprofile=coverage.out
echo test codecov : gotestsum --format testname -- -v -coverpkg=github.com/COS301-SE-2024/occupi/occupi-backend/pkg/authenticator,github.com/COS301-SE-2024/occupi/occupi-backend/pkg/database,github.com/COS301-SE-2024/occupi/occupi-backend/pkg/middleware,github.com/COS301-SE-2024/occupi/occupi-backend/pkg/utils ./tests/... -coverprofile=coverage.out
echo lint : golangci-lint run
echo help : Show this help message
exit /b 0
Expand Down
4 changes: 2 additions & 2 deletions occupi-backend/occupi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ print_help() {
echo " build dev -> go build -v cmd/occupi-backend/main.go"
echo " build prod -> go build cmd/occupi-backend/main.go"
echo " test -> gotestsum --format testname -- -v ./tests/..."
echo " test codecov -> gotestsum --format testname -- -v -coverpkg=github.com/COS301-SE-2024/occupi/occupi-backend/pkg/utils,github.com/COS301-SE-2024/occupi/occupi-backend/pkg/authenticator,github.com/COS301-SE-2024/occupi/occupi-backend/pkg/middleware ./tests/... -coverprofile=coverage.out"
echo " test codecov -> gotestsum --format testname -- -v -coverpkg=github.com/COS301-SE-2024/occupi/occupi-backend/pkg/authenticator,github.com/COS301-SE-2024/occupi/occupi-backend/pkg/database,github.com/COS301-SE-2024/occupi/occupi-backend/pkg/middleware,github.com/COS301-SE-2024/occupi/occupi-backend/pkg/utils ./tests/... -coverprofile=coverage.out"
echo " lint -> golangci-lint run"
echo " decrypt env -> cd scripts && chmod +x decrypt_env_variables.sh && ./decrypt_env_variables.sh"
echo " encrypt env -> cd scripts && chmod +x encrypt_env_variables.sh && ./encrypt_env_variables.sh"
Expand All @@ -27,7 +27,7 @@ elif [ "$1" = "build" ] && [ "$2" = "prod" ]; then
elif [ "$1" = "test" ]; then
gotestsum --format testname -- -v ./tests/...
elif [ "$1" = "test" ] && [ "$2" = "codecov" ]; then
gotestsum --format testname -- -v -coverpkg=github.com/COS301-SE-2024/occupi/occupi-backend/pkg/utils,github.com/COS301-SE-2024/occupi/occupi-backend/pkg/authenticator,github.com/COS301-SE-2024/occupi/occupi-backend/pkg/middleware ./tests/... -coverprofile=coverage.out
gotestsum --format testname -- -v -coverpkg=github.com/COS301-SE-2024/occupi/occupi-backend/pkg/authenticator,github.com/COS301-SE-2024/occupi/occupi-backend/pkg/database,github.com/COS301-SE-2024/occupi/occupi-backend/pkg/middleware,github.com/COS301-SE-2024/occupi/occupi-backend/pkg/utils ./tests/... -coverprofile=coverage.out
elif [ "$1" = "lint" ]; then
golangci-lint run
elif [ "$1" = "decrypt" ] && [ "$2" = "env" ]; then
Expand Down
Loading

0 comments on commit 860fde1

Please sign in to comment.