-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(purge): Adding purge ability (#26)
* Adding purge ability * Adding purge ability * Adding init purge
- Loading branch information
1 parent
7d266c7
commit 6800b1a
Showing
31 changed files
with
2,133 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"log/slog" | ||
|
||
"github.com/Jacobbrewer1/puppet-summary/pkg/logging" | ||
"github.com/google/subcommands" | ||
) | ||
|
||
type serveCmd struct { | ||
// uploadToken is the token used to authenticate requests to the upload endpoint. If empty, the endpoint is not secure. | ||
uploadToken string | ||
|
||
// autoPurge is the number of days to keep data for. If 0 (or not set), data will not be purged. | ||
autoPurge int | ||
|
||
// dbType is the type of database to use. | ||
dbType string | ||
|
||
// gcs is whether to use Google Cloud Storage. | ||
gcs bool | ||
} | ||
|
||
func (s *serveCmd) Name() string { | ||
return "serve" | ||
} | ||
|
||
func (s *serveCmd) Synopsis() string { | ||
return "Start the web server" | ||
} | ||
|
||
func (s *serveCmd) Usage() string { | ||
return `serve: | ||
Start the web server. | ||
` | ||
} | ||
|
||
func (s *serveCmd) SetFlags(f *flag.FlagSet) { | ||
f.StringVar(&s.uploadToken, "upload-token", "", "The Bearer token used to authenticate requests to the upload endpoint.") | ||
f.IntVar(&s.autoPurge, "auto-purge", 0, "The number of days to keep data for. If 0 (or not set), data will not be purged.") | ||
f.StringVar(&s.dbType, "db", "sqlite", "The type of database to use. Valid values are 'sqlite', 'mysql', and 'mongodb'.") | ||
f.BoolVar(&s.gcs, "gcs", false, "Whether to use Google Cloud Storage.") | ||
} | ||
|
||
func (s *serveCmd) Execute(_ context.Context, _ *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { | ||
a, err := initializeApp() | ||
if err != nil { | ||
slog.Error("Error initializing application", slog.String(logging.KeyError, err.Error())) | ||
return subcommands.ExitFailure | ||
} | ||
if err := s.generateConfig(); err != nil { | ||
slog.Error("Error generating configuration", slog.String(logging.KeyError, err.Error())) | ||
return subcommands.ExitFailure | ||
} | ||
slog.Debug("Starting application") | ||
if err := a.run(s.autoPurge); err != nil { | ||
slog.Error("Error running application", slog.String(logging.KeyError, err.Error())) | ||
return subcommands.ExitFailure | ||
} | ||
return subcommands.ExitSuccess | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"runtime" | ||
|
||
"github.com/google/subcommands" | ||
) | ||
|
||
type versionCmd struct{} | ||
|
||
func (v versionCmd) Name() string { | ||
return "version" | ||
} | ||
|
||
func (v versionCmd) Synopsis() string { | ||
return "Print application version information and exit" | ||
} | ||
|
||
func (v versionCmd) Usage() string { | ||
return `version: | ||
Print application version information and exit. | ||
` | ||
} | ||
|
||
func (v versionCmd) SetFlags(f *flag.FlagSet) {} | ||
|
||
func (v versionCmd) Execute(_ context.Context, _ *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { | ||
fmt.Printf( | ||
"Commit: %s\nRuntime: %s %s/%s\nDate: %s\n", | ||
Commit, | ||
runtime.Version(), | ||
runtime.GOOS, | ||
runtime.GOARCH, | ||
Date, | ||
) | ||
return subcommands.ExitSuccess | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log/slog" | ||
"time" | ||
|
||
"github.com/Jacobbrewer1/puppet-summary/pkg/dataaccess" | ||
"github.com/Jacobbrewer1/puppet-summary/pkg/entities" | ||
"github.com/robfig/cron" | ||
) | ||
|
||
func setupPurge(purgeDays int) error { | ||
if purgeDays == 0 { | ||
slog.Info("Auto purge not set, data will not be purged") | ||
return nil | ||
} | ||
|
||
c := cron.New() | ||
|
||
// Add a new entry to the cron scheduler to purge every (autoPurge) days at 03:00. | ||
if err := c.AddFunc(fmt.Sprintf("0 3 */%d * *", purgeDays), func() { purgeData(purgeDays) }); err != nil { | ||
return fmt.Errorf("error adding purge job to cron scheduler: %w", err) | ||
} | ||
|
||
c.Start() | ||
slog.Debug("Cron scheduler started") | ||
return nil | ||
} | ||
|
||
func purgeData(purgeDays int) { | ||
slog.Info("Purging data") | ||
|
||
// Get the start and end dates for the purge. | ||
now := time.Now() | ||
from := now.AddDate(0, 0, -purgeDays) | ||
|
||
err := dataaccess.Purge(entities.Datetime(from)) | ||
if err != nil { | ||
slog.Error(fmt.Sprintf("Error purging data: %s", err)) | ||
} | ||
|
||
slog.Info("Purging complete") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.