-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1f88fcb
commit f0de649
Showing
8 changed files
with
245 additions
and
81 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
"github.com/stellar/stellar-etl/internal/input" | ||
"github.com/stellar/stellar-etl/internal/transform" | ||
"github.com/stellar/stellar-etl/internal/utils" | ||
) | ||
|
||
var externalDataCmd = &cobra.Command{ | ||
Use: "export_external_data", | ||
Short: "Exports external data updated over a specified timestamp range", | ||
Long: "Exports external data updated over a specified timestamp range to an output file.", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
cmdLogger.SetLevel(logrus.InfoLevel) | ||
timestampArgs := utils.MustTimestampRangeFlags(cmd.Flags(), cmdLogger) | ||
path := utils.MustBucketFlags(cmd.Flags(), cmdLogger) | ||
provider := utils.MustProviderFlags(cmd.Flags(), cmdLogger) | ||
cloudStorageBucket, cloudCredentials, cloudProvider := utils.MustCloudStorageFlags(cmd.Flags(), cmdLogger) | ||
|
||
outFile := mustOutFile(path) | ||
numFailures := 0 | ||
totalNumBytes := 0 | ||
|
||
switch provider { | ||
case "retool": | ||
entities, err := input.GetEntityData[utils.RetoolEntityDataTransformInput](nil, provider, timestampArgs.StartTime, timestampArgs.EndTime) | ||
if err != nil { | ||
cmdLogger.Fatal("could not read entity data: ", err) | ||
} | ||
|
||
for _, transformInput := range entities { | ||
transformed, err := transform.TransformRetoolEntityData(transformInput) | ||
if err != nil { | ||
numFailures += 1 | ||
continue | ||
} | ||
|
||
numBytes, err := exportEntry(transformed, outFile, nil) | ||
if err != nil { | ||
cmdLogger.LogError(fmt.Errorf("could not export entity data: %v", err)) | ||
numFailures += 1 | ||
continue | ||
} | ||
totalNumBytes += numBytes | ||
} | ||
outFile.Close() | ||
cmdLogger.Info("Number of bytes written: ", totalNumBytes) | ||
|
||
printTransformStats(len(entities), numFailures) | ||
|
||
default: | ||
panic("unsupported provider: " + provider) | ||
} | ||
|
||
maybeUpload(cloudCredentials, cloudStorageBucket, cloudProvider, path) | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(externalDataCmd) | ||
utils.AddArchiveFlags("entity", externalDataCmd.Flags()) | ||
utils.AddCloudStorageFlags(externalDataCmd.Flags()) | ||
utils.AddTimestampRangeFlags(externalDataCmd.Flags()) | ||
utils.AddProviderFlags(externalDataCmd.Flags()) | ||
externalDataCmd.MarkFlagRequired("provider") | ||
externalDataCmd.MarkFlagRequired("start-time") | ||
externalDataCmd.MarkFlagRequired("end-time") | ||
} |
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,20 @@ | ||
package cmd | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestExportExternalData(t *testing.T) { | ||
tests := []cliTest{ | ||
{ | ||
name: "external data from retool", | ||
args: []string{"export_external_data", "--provider", "retool", "--start-time", "", "--end-time", "", "-o", gotTestDir(t, "external_data_retool.txt")}, | ||
golden: "external_data_retool.golden", | ||
wantErr: nil, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
runCLITest(t, test, "testdata/external_data/") | ||
} | ||
} |
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,82 @@ | ||
package input | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/url" | ||
|
||
"github.com/stellar/go/utils/apiclient" | ||
"github.com/stellar/stellar-etl/internal/utils" | ||
) | ||
|
||
type ProviderConfig struct { | ||
BaseURL string | ||
AuthType string | ||
AuthKeyEnv string | ||
AuthHeaders map[string]interface{} | ||
Endpoint string | ||
QueryParams url.Values | ||
RequestType string | ||
} | ||
|
||
func GetProviderConfig(provider string) ProviderConfig { | ||
var providerConfig ProviderConfig | ||
switch provider { | ||
case "retool": | ||
providerConfig = ProviderConfig{ | ||
BaseURL: "https://xrri-vvsg-obfa.n7c.xano.io/api:glgSAjxV", | ||
AuthType: "api_key", | ||
AuthHeaders: map[string]interface{}{"api_key": utils.GetEnv("RETOOL_API_KEY", "test-api-key")}, | ||
RequestType: "GET", | ||
Endpoint: "apps_details", | ||
QueryParams: url.Values{}, | ||
} | ||
default: | ||
panic("unsupported provider: " + provider) | ||
} | ||
return providerConfig | ||
} | ||
|
||
func GetEntityData[T any](client *apiclient.APIClient, provider string, startTime string, endTime string) ([]T, error) { | ||
providerConfig := GetProviderConfig(provider) | ||
|
||
if client == nil { | ||
client = &apiclient.APIClient{ | ||
BaseURL: providerConfig.BaseURL, | ||
AuthType: providerConfig.AuthType, | ||
AuthHeaders: providerConfig.AuthHeaders, | ||
} | ||
} | ||
reqParams := apiclient.RequestParams{ | ||
RequestType: providerConfig.RequestType, | ||
Endpoint: providerConfig.Endpoint, | ||
QueryParams: providerConfig.QueryParams, | ||
} | ||
result, err := client.CallAPI(reqParams) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to call API: %w", err) | ||
} | ||
|
||
// Assert that the result is a slice of interfaces | ||
resultSlice, ok := result.([]interface{}) | ||
if !ok { | ||
return nil, fmt.Errorf("Result is not a slice of interface") | ||
} | ||
dataSlice := []T{} | ||
|
||
for i, item := range resultSlice { | ||
if itemMap, ok := item.(map[string]interface{}); ok { | ||
var resp T | ||
err := utils.MapToStruct(itemMap, &resp) | ||
if err != nil { | ||
log.Printf("Error converting map to struct: %v", err) | ||
continue | ||
} | ||
dataSlice = append(dataSlice, resp) | ||
} else { | ||
fmt.Printf("Item %d is not a map\n", i) | ||
} | ||
} | ||
|
||
return dataSlice, nil | ||
} |
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 was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
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