Skip to content

Commit

Permalink
fix: added support for CSV
Browse files Browse the repository at this point in the history
  • Loading branch information
karl-cardenas-coding committed Aug 16, 2024
1 parent ed19c92 commit f00ffaa
Show file tree
Hide file tree
Showing 17 changed files with 1,023 additions and 248 deletions.
10 changes: 5 additions & 5 deletions cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func dump(ctx context.Context) error {
return err
}

sleep.NextToken = ""
sleep.NextToken = nil
user.SleepCollection = *sleep

recovery, err := user.GetRecoveryCollection(ctx, client, internal.DEFAULT_WHOOP_API_RECOVERY_DATA_URL, token.AccessToken, filter, ua)
Expand All @@ -130,7 +130,7 @@ func dump(ctx context.Context) error {
return err
}

recovery.NextToken = ""
recovery.NextToken = nil
user.RecoveryCollection = *recovery

workout, err := user.GetWorkoutCollection(ctx, client, internal.DEFAULT_WHOOP_API_WORKOUT_DATA_URL, token.AccessToken, filter, ua)
Expand All @@ -143,7 +143,7 @@ func dump(ctx context.Context) error {
return err
}

workout.NextToken = ""
workout.NextToken = nil
user.WorkoutCollection = *workout

cycle, err := user.GetCycleCollection(ctx, client, internal.DEFAULT_WHOOP_API_CYCLE_DATA_URL, token.AccessToken, filter, ua)
Expand All @@ -155,7 +155,7 @@ func dump(ctx context.Context) error {
}
return err
}
cycle.NextToken = ""
cycle.NextToken = nil
user.CycleCollection = *cycle

var finalDataRaw []byte
Expand All @@ -172,7 +172,7 @@ func dump(ctx context.Context) error {
}

case "xlsx":
finalDataRaw, err = internal.ConvertToCSV(user)
finalDataRaw, err = internal.ConvertToExcel(user)
if err != nil {
internal.LogError(err)
notifyErr := notificationMethod.Publish(client, []byte(err.Error()), internal.EventErrors.String())
Expand Down
15 changes: 15 additions & 0 deletions cmd/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,18 @@ func determineExporterExtension(cfg internal.ConfigurationData, client *http.Cli
return exporter, nil

}

// getFileType determines the file type to use for the export based on the configuration and command line flags.
func getFileType(cfg internal.ConfigurationData) string {

if cfg.Export.FileExport.FileType != "" {
return cfg.Export.FileExport.FileType
}

if cfg.Export.AWSS3.FileConfig.FileType != "" {
return cfg.Export.AWSS3.FileConfig.FileType
}

return "json"

}
84 changes: 84 additions & 0 deletions cmd/extensions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,87 @@ func setEnvCreds(setPassword, setToken, setAWS bool) {
}

}

func TestGetFileType(t *testing.T) {

tests := []struct {
name string
cfg internal.ConfigurationData
expected string
}{
{
name: "File - json",
expected: "json",
cfg: internal.ConfigurationData{
Export: internal.ConfigExport{
FileExport: export.FileExport{
FileType: "json",
},
},
},
},
{
name: "File - xlsx",
expected: "xlsx",
cfg: internal.ConfigurationData{
Export: internal.ConfigExport{
FileExport: export.FileExport{
FileType: "xlsx",
},
},
},
},
{
name: "File - No Value Specified",
expected: "json",
cfg: internal.ConfigurationData{
Export: internal.ConfigExport{},
},
},
{
name: "AWS S3 - xlsx",
expected: "xlsx",
cfg: internal.ConfigurationData{
Export: internal.ConfigExport{
AWSS3: export.AWS_S3{
FileConfig: export.FileExport{
FileType: "xlsx",
},
},
},
},
},
{
name: "AWS S3 - json",
expected: "json",
cfg: internal.ConfigurationData{
Export: internal.ConfigExport{
AWSS3: export.AWS_S3{
FileConfig: export.FileExport{
FileType: "json",
},
},
},
},
},
{
name: "AWS S3 - No Value Specified",
expected: "json",
cfg: internal.ConfigurationData{
Export: internal.ConfigExport{
AWSS3: export.AWS_S3{},
},
},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {

result := getFileType(test.cfg)
if result != test.expected {
t.Errorf("expected: %s, got: %s", test.expected, result)
}
})
}
}
37 changes: 27 additions & 10 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ func downloadWhoopData(ctx context.Context, config internal.ConfigurationData, c

var user internal.User

finalDataRaw, err := getData(ctx, user, client, token, ua)
finalDataRaw, err := getData(ctx, user, client, token, ua, getFileType(config))
if err != nil {
slog.Error("unable to get data", "error", err)
notifyErr := notify.Publish(client, []byte(fmt.Sprintf("Failed to get data from the Whoop API. Additional context below: \n %s", err)), internal.EventErrors.String())
Expand Down Expand Up @@ -365,7 +365,7 @@ func refreshJWT(ctx context.Context, client *http.Client, credentialsFilePath st
}

// getData queries the Whoop API and gets the user data
func getData(ctx context.Context, user internal.User, client *http.Client, token oauth2.Token, ua string) ([]byte, error) {
func getData(ctx context.Context, user internal.User, client *http.Client, token oauth2.Token, ua, fileType string) ([]byte, error) {

startTime, endTime := internal.GenerateLast24HoursString()
filterString := fmt.Sprintf("start=%s&end=%s", startTime, endTime)
Expand All @@ -378,7 +378,7 @@ func getData(ctx context.Context, user internal.User, client *http.Client, token
return []byte{}, err
}

sleep.NextToken = ""
sleep.NextToken = nil
user.SleepCollection = *sleep

recovery, err := user.GetRecoveryCollection(ctx, client, internal.DEFAULT_WHOOP_API_RECOVERY_DATA_URL, token.AccessToken, filterString, ua)
Expand All @@ -387,7 +387,7 @@ func getData(ctx context.Context, user internal.User, client *http.Client, token
return []byte{}, err
}

recovery.NextToken = ""
recovery.NextToken = nil
user.RecoveryCollection = *recovery

workout, err := user.GetWorkoutCollection(ctx, client, internal.DEFAULT_WHOOP_API_WORKOUT_DATA_URL, token.AccessToken, filterString, ua)
Expand All @@ -396,7 +396,7 @@ func getData(ctx context.Context, user internal.User, client *http.Client, token
return []byte{}, err
}

workout.NextToken = ""
workout.NextToken = nil
user.WorkoutCollection = *workout

cycle, err := user.GetCycleCollection(ctx, client, internal.DEFAULT_WHOOP_API_CYCLE_DATA_URL, token.AccessToken, filterString, ua)
Expand All @@ -405,13 +405,30 @@ func getData(ctx context.Context, user internal.User, client *http.Client, token
return []byte{}, err
}

cycle.NextToken = ""
cycle.NextToken = nil
user.CycleCollection = *cycle

finalDataRaw, err := json.MarshalIndent(user, "", " ")
if err != nil {
internal.LogError(err)
return finalDataRaw, err
var finalDataRaw []byte
switch fileType {
case "json":
finalDataRaw, err = json.MarshalIndent(user, "", " ")
if err != nil {
internal.LogError(err)
return finalDataRaw, err
}
case "xlsx":
finalDataRaw, err = internal.ConvertToExcel(user)
if err != nil {
internal.LogError(err)
return finalDataRaw, err
}
default:
finalDataRaw, err = json.MarshalIndent(user, "", " ")
if err != nil {
internal.LogError(err)
return finalDataRaw, err
}

}

return finalDataRaw, nil
Expand Down
27 changes: 24 additions & 3 deletions export/aws_s3_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"net/http"
"os"
"path"
"slices"

"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
Expand Down Expand Up @@ -121,7 +122,7 @@ func (f *AWS_S3) Export(data []byte) error {
Bucket: &f.Bucket,
Key: &fileName,
Body: largeBuffer,
Metadata: map[string]string{"Content-Type": "application/json"},
Metadata: map[string]string{"Content-Type": determineContentType(f.FileConfig.FileType)},
})

if err != nil {
Expand All @@ -137,7 +138,7 @@ func (f *AWS_S3) Export(data []byte) error {
Bucket: &f.Bucket,
Key: &fileName,
Body: bytes.NewReader(data),
Metadata: map[string]string{"Content-Type": "application/json"},
Metadata: map[string]string{"Content-Type": determineContentType(f.FileConfig.FileType)},
})
if err != nil {
return err
Expand All @@ -155,6 +156,8 @@ func (f *AWS_S3) CleanUp() error {
// fileExportDefaults sets the default values for the file export
func fileExportDefaults(f *FileExport) error {

supportedFileTypes := []string{"json", "xlsx"}

h, err := os.UserHomeDir()
if err != nil {
return errors.New("unable to get user home directory")
Expand All @@ -177,10 +180,13 @@ func fileExportDefaults(f *FileExport) error {
f.FilePath = path.Join(h, "data")
}

if f.FileType == "" || f.FileType != "json" {
if f.FileType == "" {
f.FileType = "json"
}
}

if !slices.Contains(supportedFileTypes, f.FileType) {
f.FileType = "json"
}

return nil
Expand Down Expand Up @@ -240,3 +246,18 @@ func uploadCheck(data *[]byte, f *FileExport, bucket string) error {

return nil
}

// determineContentType determines the content type based on the file type
func determineContentType(fileType string) string {

switch fileType {
case "json":
return "application/json"
case "csv":
return "text/csv"
case "xlsx":
return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
default:
return "application/json"
}
}
19 changes: 19 additions & 0 deletions export/aws_s3_export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,25 @@ func TestFileAWSS3ExportDefaults(t *testing.T) {
ServerMode: true,
},
},
{

0,
"Test case 7: File export with Excel file type",
&FileExport{
FilePath: "/tmp",
FileType: "xlsx",
FileName: "user",
FileNamePrefix: "",
ServerMode: true,
},
&FileExport{
FilePath: "/tmp",
FileType: "xlsx",
FileName: "user",
FileNamePrefix: "",
ServerMode: true,
},
},
}

for index, tc := range tests {
Expand Down
27 changes: 6 additions & 21 deletions export/file_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,11 @@ func (f *FileExport) Export(data []byte) error {
f.FileName = "user"
}

switch f.FileType {
case "json":
// write the data to a file in the data folder in the current directory
err = writeToFile(*f, data)
if err != nil {
slog.Error("unable to write to JSON file", "error", err)
return err
}
case "xlsx":
// err = writeToExcelFile(*f, data)
// if err != nil {
// slog.Error("unable to write to Excel file", "error", err)
// return err
// }
default:
err = writeToFile(*f, data)
if err != nil {
slog.Error("unable to write to JSON file", "error", err)
return err
}
// write the data to a file in the data folder in the current directory
err = writeToFile(*f, data)
if err != nil {
slog.Error("unable to write to JSON file", "error", err)
return err
}

return nil
Expand All @@ -89,7 +74,7 @@ func generateName(cfg FileExport) string {
if cfg.FileNamePrefix != "" {
return cfg.FileNamePrefix + "_" + cfg.FileName + "." + cfg.FileType
}
slog.Warn("File Type", "Type", cfg.FileType)

return cfg.FileName + "." + cfg.FileType

}
Expand Down
Loading

0 comments on commit f00ffaa

Please sign in to comment.