-
Notifications
You must be signed in to change notification settings - Fork 3
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
Showing
35 changed files
with
3,432 additions
and
7 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,43 @@ | ||
package cmd | ||
|
||
import ( | ||
"log" | ||
|
||
lib "github.com/justmiles/athena-cli/lib" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
historyCmdMaxResults int | ||
historyCmdWorkgroup string | ||
) | ||
|
||
// HistoryCmd ... | ||
var HistoryCmd = &cobra.Command{ | ||
Use: "history", | ||
Short: "export Athena execution history", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
h, err := lib.ListHistory(historyCmdMaxResults) | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
err = lib.RenderHistoryResults(h, q.Format) | ||
// err = lib.RenderAsTable(h) | ||
// err = lib.RenderAsCSV(h) | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
HistoryCmd.PersistentFlags().IntVarP(&historyCmdMaxResults, "max", "m", 100, "maximum results") | ||
HistoryCmd.PersistentFlags().StringVarP(&q.Format, "format", "f", "csv", "format the output as either json, csv, or table") | ||
// TODO: Add support for selecting a workgroup | ||
// HistoryCmd.PersistentFlags().StringVarP(&historyCmdWorkgroup, "workgroup", "w", "default", "workgroup") | ||
} |
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
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,103 @@ | ||
package lib | ||
|
||
import ( | ||
"log" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/athena" | ||
) | ||
|
||
// HistoricalExecution .. | ||
type HistoricalExecution struct { | ||
Query string | ||
Catalog string | ||
Database string | ||
QueryExecutionID string | ||
OutputLocation string | ||
State string | ||
WorkGroup string | ||
TotalExecutionTimeInMillis int64 | ||
DataScannedInBytes int64 | ||
Cost float64 | ||
SubmissionDateTime time.Time | ||
} | ||
|
||
// ListHistory returns query results per workgroup | ||
func ListHistory(maxResults int) (h []HistoricalExecution, err error) { | ||
params := &athena.ListQueryExecutionsInput{ | ||
// WorkGroup | ||
} | ||
|
||
if maxResults < 50 { | ||
params.MaxResults = aws.Int64(int64(maxResults)) | ||
} | ||
|
||
err = svc.ListQueryExecutionsPages(params, func(page *athena.ListQueryExecutionsOutput, lastPage bool) bool { | ||
h = append(h, getQueryExecution(page.QueryExecutionIds)...) | ||
return len(h) <= maxResults | ||
}) | ||
if err != nil { | ||
return h, err | ||
} | ||
|
||
if len(h) > maxResults { | ||
return h[0:maxResults], nil | ||
} | ||
return h, nil | ||
} | ||
|
||
func getQueryExecution(ids []*string) (h []HistoricalExecution) { | ||
o, err := svc.BatchGetQueryExecution(&athena.BatchGetQueryExecutionInput{ | ||
QueryExecutionIds: ids, | ||
}) | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// fmt.Println(o.GoString(), err) | ||
for _, e := range o.QueryExecutions { | ||
|
||
he := HistoricalExecution{ | ||
Query: *e.Query, | ||
QueryExecutionID: *e.QueryExecutionId, | ||
OutputLocation: *e.ResultConfiguration.OutputLocation, | ||
State: *e.Status.State, | ||
WorkGroup: *e.WorkGroup, | ||
TotalExecutionTimeInMillis: *e.Statistics.TotalExecutionTimeInMillis, | ||
SubmissionDateTime: *e.Status.SubmissionDateTime, | ||
} | ||
|
||
if e.Statistics.DataScannedInBytes != nil { | ||
he.DataScannedInBytes = *e.Statistics.DataScannedInBytes | ||
he.Cost = float64((float64(*e.Statistics.DataScannedInBytes) / 1024 / 1024 / 1024 / 1024) * 5) | ||
} | ||
|
||
if e.QueryExecutionContext.Database != nil { | ||
he.Database = *e.QueryExecutionContext.Database | ||
} | ||
|
||
if e.QueryExecutionContext.Catalog != nil { | ||
he.Catalog = *e.QueryExecutionContext.Catalog | ||
} else { | ||
he.Catalog = "AwsDataCatalog" | ||
} | ||
|
||
h = append(h, he) | ||
} | ||
|
||
return h | ||
} | ||
|
||
// RenderHistoryResults .. | ||
func RenderHistoryResults(h []HistoricalExecution, outputFormat string) error { | ||
switch outputFormat { | ||
case "csv": | ||
return RenderAsCSV(h) | ||
case "table": | ||
return RenderAsTable(h) | ||
default: | ||
return RenderAsJSON(h) | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.