Skip to content

Commit

Permalink
[INTG-1977] - Added retry-timeout flag for report command (#171)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* [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 <[email protected]>
  • Loading branch information
sc-gv and hkf57 authored Oct 21, 2021
1 parent 6b1d30f commit 982764b
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 11 deletions.
2 changes: 2 additions & 0 deletions cmd/iauditor-exporter/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion docs/iauditor-exporter.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ 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
* [iauditor-exporter report](iauditor-exporter_report.md) - Export inspection report
* [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
3 changes: 2 additions & 1 deletion docs/iauditor-exporter_configure.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
3 changes: 2 additions & 1 deletion docs/iauditor-exporter_csv.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
3 changes: 2 additions & 1 deletion docs/iauditor-exporter_inspection-json.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
4 changes: 3 additions & 1 deletion docs/iauditor-exporter_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion docs/iauditor-exporter_schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion docs/iauditor-exporter_sql.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
20 changes: 18 additions & 2 deletions internal/app/feed/exporter_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 11 additions & 2 deletions internal/app/feed/exporter_report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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))
}

0 comments on commit 982764b

Please sign in to comment.