Created
August 21, 2018 19:19
-
-
Save whilp/0f72e96a5b95a9330bb5c1938bfa177f to your computer and use it in GitHub Desktop.
AWS dashboard to CSV
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
package main | |
import ( | |
"encoding/csv" | |
"encoding/json" | |
"log" | |
"net/http" | |
"os" | |
"strconv" | |
"strings" | |
"time" | |
) | |
var statusURL = "http://status.aws.amazon.com/data.json" | |
var client = &http.Client{Timeout: 1 * time.Second} | |
type status struct { | |
Archive []*event | |
} | |
type event struct { | |
RawDate string `json:"date"` | |
RawService string `json:"service"` | |
Summary string `json:"summary"` | |
} | |
func (e *event) Date() time.Time { | |
i, err := strconv.ParseInt(e.RawDate, 10, 64) | |
if err != nil { | |
return time.Time{} | |
} | |
return time.Unix(i, 0) | |
} | |
func (e *event) GlobalService() bool { | |
return e.RawService == "management-console" | |
} | |
func (e *event) Region() string { | |
if e.GlobalService() { | |
return "global" | |
} | |
fields := strings.Split(e.RawService, "-") | |
return strings.Join(fields[1:], "-") | |
} | |
func (e *event) Service() string { | |
if e.GlobalService() { | |
return e.RawService | |
} | |
fields := strings.Split(e.RawService, "-") | |
return fields[0] | |
} | |
func (e *event) HeaderRecord() []string { | |
return []string{ | |
"date", | |
"region", | |
"service", | |
"summary", | |
} | |
} | |
func (e *event) Record() []string { | |
return []string{ | |
e.Date().Format(time.RFC3339), | |
e.Region(), | |
e.Service(), | |
e.Summary, | |
} | |
} | |
func run() error { | |
s, err := getStatus(statusURL) | |
if err != nil { | |
return err | |
} | |
begin, _ := time.Parse(time.RFC3339, "2018-04-01T00:00:00Z") | |
end, _ := time.Parse(time.RFC3339, "2018-07-01T00:00:00Z") | |
choose := func(e *event) bool { | |
if e.Date().IsZero() { | |
return false | |
} | |
if e.Date().Before(begin) || e.Date().After(end) { | |
return false | |
} | |
return true | |
} | |
w := csv.NewWriter(os.Stdout) | |
defer w.Flush() | |
for _, record := range filter(s.Archive, choose) { | |
if err := w.Write(record.Record()); err != nil { | |
log.Print(err) | |
} | |
} | |
return nil | |
} | |
type predicate func(*event) bool | |
func filter(events []*event, p predicate) []*event { | |
result := []*event{} | |
for _, event := range events { | |
if p(event) { | |
result = append(result, event) | |
} | |
} | |
return result | |
} | |
func getStatus(url string) (*status, error) { | |
resp, err := http.Get(url) | |
if err != nil { | |
return nil, err | |
} | |
defer resp.Body.Close() | |
s := &status{} | |
err = json.NewDecoder(resp.Body).Decode(s) | |
if err != nil { | |
return nil, err | |
} | |
return s, nil | |
} | |
func main() { | |
err := run() | |
if err != nil { | |
log.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment