From 982764b3cb1ee0876a9720f857e77b728d38085c Mon Sep 17 00:00:00 2001 From: Gaurav Verma <92243746+sc-gv@users.noreply.github.com> Date: Fri, 22 Oct 2021 10:12:57 +1100 Subject: [PATCH] [INTG-1977] - Added retry-timeout flag for report command (#171) * INTG-1977 - Initial commit * INTG-1977 - Clean up * [INTG-1977]: Added retry-timeout flag for the report command. Allows to pass a value to increase the time between polls. * Update cmd/iauditor-exporter/cmd/root.go Updated description Co-authored-by: Charles Lee * [INTG-1977]: Removed time.Duration as the function already returns time.Duration type. * [INTG-1977]: Added tests for the GetWaitTime function. * [INTG-1977]: Corrected codeclimate issue. * [INTG-1977]: Regenerated docs. * [INTG-1977]: Formatting issues. Co-authored-by: Charles Lee --- cmd/iauditor-exporter/cmd/root.go | 2 ++ docs/iauditor-exporter.md | 3 ++- docs/iauditor-exporter_configure.md | 3 ++- docs/iauditor-exporter_csv.md | 3 ++- docs/iauditor-exporter_inspection-json.md | 3 ++- docs/iauditor-exporter_report.md | 4 +++- docs/iauditor-exporter_schema.md | 2 +- docs/iauditor-exporter_sql.md | 3 ++- internal/app/feed/exporter_report.go | 20 ++++++++++++++++++-- internal/app/feed/exporter_report_test.go | 13 +++++++++++-- 10 files changed, 45 insertions(+), 11 deletions(-) diff --git a/cmd/iauditor-exporter/cmd/root.go b/cmd/iauditor-exporter/cmd/root.go index f632a523..3dabe4e5 100644 --- a/cmd/iauditor-exporter/cmd/root.go +++ b/cmd/iauditor-exporter/cmd/root.go @@ -134,6 +134,7 @@ func configFlags() { reportFlags.StringSlice("format", []string{"PDF"}, "Export format (PDF,WORD)") reportFlags.String("filename-convention", "INSPECTION_TITLE", "The name of the report exported, either INSPECTION_TITLE or INSPECTION_ID") reportFlags.String("preference-id", "", "The report preference to apply to the document") + reportFlags.Int("retry-timeout", 15, "Specify the time in seconds spent retrieving each report. Values greater than 60 seconds will be treated as 60 seconds.") sitesFlags = flag.NewFlagSet("sites", flag.ContinueOnError) sitesFlags.Bool("site-include-deleted", false, "Include deleted sites in the sites table (default false)") @@ -175,6 +176,7 @@ func bindFlags() { util.Check(viper.BindPFlag("report.format", reportFlags.Lookup("format")), "while binding flag") util.Check(viper.BindPFlag("report.filename_convention", reportFlags.Lookup("filename-convention")), "while binding flag") util.Check(viper.BindPFlag("report.preference_id", reportFlags.Lookup("preference-id")), "while binding flag") + util.Check(viper.BindPFlag("report.retry_timeout", reportFlags.Lookup("retry-timeout")), "while binding flag") } func addCmd(cmd *cobra.Command, flags ...*flag.FlagSet) { diff --git a/docs/iauditor-exporter.md b/docs/iauditor-exporter.md index eced0721..196f6616 100644 --- a/docs/iauditor-exporter.md +++ b/docs/iauditor-exporter.md @@ -15,6 +15,7 @@ A CLI tool for extracting your iAuditor data ### SEE ALSO +* [iauditor-exporter completion](iauditor-exporter_completion.md) - generate the autocompletion script for the specified shell * [iauditor-exporter configure](iauditor-exporter_configure.md) - Initialises your configuration file. * [iauditor-exporter csv](iauditor-exporter_csv.md) - Export iAuditor data to CSV files * [iauditor-exporter inspection-json](iauditor-exporter_inspection-json.md) - Export iAuditor inspections to json files @@ -22,4 +23,4 @@ A CLI tool for extracting your iAuditor data * [iauditor-exporter schema](iauditor-exporter_schema.md) - Print iAuditor table schemas * [iauditor-exporter sql](iauditor-exporter_sql.md) - Export iAuditor data to SQL database -###### Auto generated by spf13/cobra on 5-Aug-2021 +###### Auto generated by spf13/cobra on 21-Oct-2021 diff --git a/docs/iauditor-exporter_configure.md b/docs/iauditor-exporter_configure.md index 97a995f6..f0f62c67 100644 --- a/docs/iauditor-exporter_configure.md +++ b/docs/iauditor-exporter_configure.md @@ -14,6 +14,7 @@ iauditor-exporter configure [flags] ``` -t, --access-token string API Access Token + --action-limit int Number of actions fetched at once. Lower this number if the exporter fails to load the data (default 100) --api-url string API URL (default "https://api.safetyculture.io") --db-connection-string string Database connection string --db-dialect string Database dialect. mysql, postgres and sqlserver are the only valid options. (default "mysql") @@ -44,4 +45,4 @@ iauditor-exporter configure [flags] * [iauditor-exporter](iauditor-exporter.md) - A CLI tool for extracting your iAuditor data -###### Auto generated by spf13/cobra on 5-Aug-2021 +###### Auto generated by spf13/cobra on 21-Oct-2021 diff --git a/docs/iauditor-exporter_csv.md b/docs/iauditor-exporter_csv.md index 90b24d9e..8474e8ea 100644 --- a/docs/iauditor-exporter_csv.md +++ b/docs/iauditor-exporter_csv.md @@ -20,6 +20,7 @@ iauditor-exporter csv --export-path /path/to/export/to ``` -t, --access-token string API Access Token + --action-limit int Number of actions fetched at once. Lower this number if the exporter fails to load the data (default 100) --api-url string API URL (default "https://api.safetyculture.io") --create-schema-only Create schema only (default false) --export-media Export media @@ -53,4 +54,4 @@ iauditor-exporter csv --export-path /path/to/export/to * [iauditor-exporter](iauditor-exporter.md) - A CLI tool for extracting your iAuditor data -###### Auto generated by spf13/cobra on 5-Aug-2021 +###### Auto generated by spf13/cobra on 21-Oct-2021 diff --git a/docs/iauditor-exporter_inspection-json.md b/docs/iauditor-exporter_inspection-json.md index f4cf6bca..7f1246ee 100644 --- a/docs/iauditor-exporter_inspection-json.md +++ b/docs/iauditor-exporter_inspection-json.md @@ -20,6 +20,7 @@ iauditor-exporter inspection-json --export-path /path/to/export/to ``` -t, --access-token string API Access Token + --action-limit int Number of actions fetched at once. Lower this number if the exporter fails to load the data (default 100) --api-url string API URL (default "https://api.safetyculture.io") --export-path string File Export Path (default "./export/") -h, --help help for inspection-json @@ -47,4 +48,4 @@ iauditor-exporter inspection-json --export-path /path/to/export/to * [iauditor-exporter](iauditor-exporter.md) - A CLI tool for extracting your iAuditor data -###### Auto generated by spf13/cobra on 5-Aug-2021 +###### Auto generated by spf13/cobra on 21-Oct-2021 diff --git a/docs/iauditor-exporter_report.md b/docs/iauditor-exporter_report.md index 770c9c05..168e1142 100644 --- a/docs/iauditor-exporter_report.md +++ b/docs/iauditor-exporter_report.md @@ -19,6 +19,7 @@ iauditor-exporter report [flags] ``` -t, --access-token string API Access Token + --action-limit int Number of actions fetched at once. Lower this number if the exporter fails to load the data (default 100) --api-url string API URL (default "https://api.safetyculture.io") --export-path string File Export Path (default "./export/") --filename-convention string The name of the report exported, either INSPECTION_TITLE or INSPECTION_ID (default "INSPECTION_TITLE") @@ -34,6 +35,7 @@ iauditor-exporter report [flags] --modified-after string Return inspections modified after this date (see readme for supported formats) --preference-id string The report preference to apply to the document --proxy-url string Proxy URL for making API requests through + --retry-timeout int Specify the time in seconds spent retrieving each report. Values greater than 60 seconds will be treated as 60 seconds. (default 15) --template-ids strings Template IDs to filter inspections and schedules by (default all) --tls-cert string Custom root CA certificate to use when making API requests --tls-skip-verify Skip verification of API TLS certificates @@ -49,4 +51,4 @@ iauditor-exporter report [flags] * [iauditor-exporter](iauditor-exporter.md) - A CLI tool for extracting your iAuditor data -###### Auto generated by spf13/cobra on 5-Aug-2021 +###### Auto generated by spf13/cobra on 21-Oct-2021 diff --git a/docs/iauditor-exporter_schema.md b/docs/iauditor-exporter_schema.md index 717c2e7b..eccbb5ce 100644 --- a/docs/iauditor-exporter_schema.md +++ b/docs/iauditor-exporter_schema.md @@ -28,4 +28,4 @@ iauditor-exporter schema * [iauditor-exporter](iauditor-exporter.md) - A CLI tool for extracting your iAuditor data -###### Auto generated by spf13/cobra on 5-Aug-2021 +###### Auto generated by spf13/cobra on 21-Oct-2021 diff --git a/docs/iauditor-exporter_sql.md b/docs/iauditor-exporter_sql.md index b1b52ac0..b3bffad1 100644 --- a/docs/iauditor-exporter_sql.md +++ b/docs/iauditor-exporter_sql.md @@ -20,6 +20,7 @@ iauditor-exporter sql --export-path /path/to/export/to ``` -t, --access-token string API Access Token + --action-limit int Number of actions fetched at once. Lower this number if the exporter fails to load the data (default 100) --api-url string API URL (default "https://api.safetyculture.io") --create-schema-only Create schema only (default false) --db-connection-string string Database connection string @@ -54,4 +55,4 @@ iauditor-exporter sql --export-path /path/to/export/to * [iauditor-exporter](iauditor-exporter.md) - A CLI tool for extracting your iAuditor data -###### Auto generated by spf13/cobra on 5-Aug-2021 +###### Auto generated by spf13/cobra on 21-Oct-2021 diff --git a/internal/app/feed/exporter_report.go b/internal/app/feed/exporter_report.go index 3a83e36f..adcf1ca9 100644 --- a/internal/app/feed/exporter_report.go +++ b/internal/app/feed/exporter_report.go @@ -13,6 +13,7 @@ import ( "github.com/SafetyCulture/iauditor-exporter/internal/app/api" "github.com/pkg/errors" + "github.com/spf13/viper" "go.uber.org/zap" "gorm.io/gorm" "gorm.io/gorm/clause" @@ -218,8 +219,11 @@ func (e *ReportExporter) exportInspection(ctx context.Context, apiClient *api.Cl tries := 0 for { - // wait for a second before checking for report completion - time.Sleep(1 * time.Second) + + retryTimeout := viper.GetInt("report.retry_timeout") + // wait for stipulated time before checking for report completion + time.Sleep(GetWaitTime(retryTimeout) * time.Second) + rec, cErr := apiClient.CheckInspectionReportExportCompletion(ctx, inspection.ID, messageID) if cErr != nil { err = cErr @@ -313,6 +317,18 @@ func getFileExtension(format string) string { } } +// GetWaitTime - Default wait time is 1 second otherwise specified timeout/15. Can't be more than 4 seconds. +func GetWaitTime(retryTimeout int) time.Duration { + var waitTime = 1 + + if retryTimeout > 15 && retryTimeout <= 60 { + waitTime = retryTimeout / 15 + } else if retryTimeout > 60 { + waitTime = 4 + } + return time.Duration(waitTime) +} + func getFilePath(exportPath string, inspection *Inspection, format string, filenameConvention string) (string, error) { dupIndex := 0 for { diff --git a/internal/app/feed/exporter_report_test.go b/internal/app/feed/exporter_report_test.go index 74813339..5c4b5f27 100644 --- a/internal/app/feed/exporter_report_test.go +++ b/internal/app/feed/exporter_report_test.go @@ -6,13 +6,13 @@ import ( "os" "path/filepath" "testing" + "time" "github.com/SafetyCulture/iauditor-exporter/internal/app/api" "github.com/SafetyCulture/iauditor-exporter/internal/app/feed" - "gopkg.in/h2non/gock.v1" - "github.com/spf13/viper" "github.com/stretchr/testify/assert" + "gopkg.in/h2non/gock.v1" ) const ( @@ -484,3 +484,12 @@ func TestExportReports_should_return_error_for_unsupported_format(t *testing.T) err = feed.ExportInspectionReports(viperConfig, apiClient, exporter) assert.EqualError(t, err, "No valid export format specified") } + +func Test_GetWaitTime(t *testing.T) { + assert.Equal(t, time.Duration(1), feed.GetWaitTime(0)) + assert.Equal(t, time.Duration(1), feed.GetWaitTime(10)) + assert.Equal(t, time.Duration(2), feed.GetWaitTime(30)) + assert.Equal(t, time.Duration(3), feed.GetWaitTime(50)) + assert.Equal(t, time.Duration(4), feed.GetWaitTime(60)) + assert.Equal(t, time.Duration(4), feed.GetWaitTime(90)) +}