Skip to content

Commit

Permalink
fix: fix incorrect order in which graphs are drawn
Browse files Browse the repository at this point in the history
  • Loading branch information
ginokent committed Oct 13, 2022
1 parent 8dedb50 commit 712f929
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 131 deletions.
9 changes: 7 additions & 2 deletions .githooks/pre-push
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#!/usr/bin/env bash
set -Eeu -o pipefail

REPO_ROOT=$(git rev-parse --show-toplevel)

cd "${REPO_ROOT:?}" || exit 1
__main__() {
if ! cd "${REPO_ROOT:?}"; then
return 1
fi

make ci
make ci
}
14 changes: 7 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ TIMESTAMP := $(shell git log -1 --format='%cI')

.DEFAULT_GOAL := help
.PHONY: help
help: githooks ## display this help documents.
help: githooks ## display this help documents
@grep -E '^[0-9a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-40s\033[0m %s\n", $$1, $$2}'

.PHONY: githooks
githooks: ## githooks をインストールします。
githooks:
@[[ -f "${PRE_PUSH}" ]] || cp -ai "${GITROOT}/.githooks/pre-push" "${PRE_PUSH}"

.PHONY: lint
lint: ## go mod tidy の後に golangci-lint を実行します。
lint: ## Run golangci-lint after go mod tidy
# tidy
go mod tidy
git diff --exit-code go.mod go.sum
Expand All @@ -27,21 +27,21 @@ lint: ## go mod tidy の後に golangci-lint を実行します。
git diff --exit-code

.PHONY: credits
credits: ## CREDITS ファイルを生成します。
credits: ## Generate CREDITS file.
command -v gocredits || go install github.com/Songmu/gocredits/cmd/gocredits@latest
gocredits . > CREDITS
git diff --exit-code

.PHONY: test
test: githooks ## go test を実行し coverage を出力します。
test: githooks ## Run go test and display coverage
# test
go test -v -race -p=4 -parallel=8 -timeout=300s -cover -coverprofile=./coverage.txt ./...
go tool cover -func=./coverage.txt

.PHONY: ci
ci: lint credits test ## CI 上で実行する lint や test のコマンドセットです。
ci: lint credits test ## CI command set

.PHONY: goxz
goxz: ci ## goxz を用いて release 用ファイルを生成します。
goxz: ci ## Run goxz for release
command -v goxz || go install github.com/Songmu/goxz/cmd/goxz@latest
goxz -d ./.tmp -os=linux,darwin,windows -arch=amd64,arm64 -pv ${VERSION} -build-ldflags "-X ${GOMODULE}/pkg/config.version=${VERSION} -X ${GOMODULE}/pkg/config.revision=${REVISION} -X ${GOMODULE}/pkg/config.branch=${BRANCH} -X ${GOMODULE}/pkg/config.timestamp=${TIMESTAMP}" ./cmd/ccc
30 changes: 15 additions & 15 deletions pkg/domain/plot.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,18 @@ func WithTicker(ticker plot.Ticker) Option {
}

type PlotGraphParameters struct {
GraphTitle string
XLabelText string
YLabelText string
Width float64
Hight float64
XAxisPointsCount int
From time.Time
To time.Time
TimeZone *time.Location
OrderedLegends []string
LegendValuesMap map[string]plotter.Values
ImageFormat string
GraphTitle string
XLabelText string
YLabelText string
Width float64
Hight float64
XAxisPointsCount int
From time.Time
To time.Time
TimeZone *time.Location
OrderedLegendsAsc []string
LegendValuesMap map[string]plotter.Values
ImageFormat string
}

// nolint: cyclop,funlen
Expand All @@ -79,14 +79,14 @@ func (d *Domain) PlotGraph(
barChartWidth := vg.Points((graphWidth - 100) / float64(ps.XAxisPointsCount)) // NOTE: グラフの幅から固定長(95)を引いて X 軸の値数で割る

previousBarChart := (*plotter.BarChart)(nil)
for i, legend := range ps.OrderedLegends {
for i, legend := range ps.OrderedLegendsAsc {
barChart, err := plotter.NewBarChart(ps.LegendValuesMap[legend], barChartWidth)
if err != nil {
return errors.Errorf("plotter.NewBarChart: %w", err)
}
barChart.Width = barChartWidth
barChart.LineStyle.Width = vg.Length(0) // NOTE: グラフの枠線の太さを 0 にする
barChart.Color = constz.GraphColor(i)
barChart.Color = constz.GraphColor(len(ps.OrderedLegendsAsc) - 1 - i)
p.Legend.Add(legend, barChart)

if previousBarChart != nil {
Expand Down Expand Up @@ -120,7 +120,7 @@ func (d *Domain) PlotGraph(
p.Legend.XOffs = 10
p.Legend.YOffs = -10
legendHight := float64(p.Legend.TextStyle.Height("C")) * 8
legendsHight := legendHight * float64(len(ps.OrderedLegends))
legendsHight := legendHight * float64(len(ps.OrderedLegendsAsc))
log.Debugf("legendHight=%f, legendsHight=%f", legendHight, legendsHight)
p.Y.Min = 0
p.Y.Max += legendsHight // NOTE: グラフと Legend が被らないように、 Legend の高さ (文字 C の高さで計算) * Legend 数を足して、 Y 軸の高さを確保している
Expand Down
88 changes: 44 additions & 44 deletions pkg/domain/plot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,16 @@ func TestPlotGraph(t *testing.T) {
<path d="M45.201,435.88L49.201,435.88" style="fill:none;stroke:#000000;stroke-width:0.5" />
<path d="M45.201,525.29L49.201,525.29" style="fill:none;stroke:#000000;stroke-width:0.5" />
<path d="M49.201,33.525L49.201,526.41" style="fill:none;stroke:#000000;stroke-width:0.5" />
<path d="M90.328,33.525L90.328,35.761L950.33,35.761L950.33,33.525Z" style="fill:#FFCABF" />
<path d="M90.328,35.761L90.328,40.231L950.33,40.231L950.33,35.761Z" style="fill:#FFFF80" />
<path d="M90.328,33.525L90.328,35.761L950.33,35.761L950.33,33.525Z" style="fill:#FFFF80" />
<path d="M90.328,35.761L90.328,40.231L950.33,40.231L950.33,35.761Z" style="fill:#FFCABF" />
<path d="M520.33,33.525L520.33,526.41" style="fill:none;stroke:#808080;stroke-width:0.25" />
<path d="M80.656,33.525L960,33.525" style="fill:none;stroke:#000000;stroke-width:0.25;stroke-dasharray:5" />
<path d="M80.656,257.05L960,257.05" style="fill:none;stroke:#000000;stroke-width:0.25;stroke-dasharray:5" />
<path d="M80.656,480.58L960,480.58" style="fill:none;stroke:#000000;stroke-width:0.25;stroke-dasharray:5" />
<path d="M90.656,502.81L90.656,512.8L110.66,512.8L110.66,502.81Z" style="fill:#FFCABF" />
<path d="M90.656,502.81L90.656,512.8L110.66,512.8L110.66,502.81Z" style="fill:#FFFF80" />
<text x="117.86" y="-506.62" transform="scale(1, -1)"
style="font-family:Liberation Mono;font-variant:normal;font-weight:normal;font-style:normal;font-size:12px">legend1</text>
<path d="M90.656,492.82L90.656,502.81L110.66,502.81L110.66,492.82Z" style="fill:#FFFF80" />
<path d="M90.656,492.82L90.656,502.81L110.66,502.81L110.66,492.82Z" style="fill:#FFCABF" />
<text x="117.86" y="-496.63" transform="scale(1, -1)"
style="font-family:Liberation Mono;font-variant:normal;font-weight:normal;font-style:normal;font-size:12px">legend2</text>
</g>
Expand All @@ -73,16 +73,16 @@ func TestPlotGraph(t *testing.T) {
from := time.Date(2022, 2, 2, 2, 22, 22, 0, constz.TimeZone("Asia/Tokyo"))
d := New(WithTicker(plot.DefaultTicks{}))
if err := d.PlotGraph(buf, &PlotGraphParameters{
GraphTitle: "Title",
XLabelText: "XLabel",
YLabelText: "YLabel",
Width: 1280,
Hight: 720,
XAxisPointsCount: 1,
From: from,
To: from.AddDate(0, 0, 1),
TimeZone: constz.TimeZone("Asia/Tokyo"),
OrderedLegends: []string{"legend1", "legend2"},
GraphTitle: "Title",
XLabelText: "XLabel",
YLabelText: "YLabel",
Width: 1280,
Hight: 720,
XAxisPointsCount: 1,
From: from,
To: from.AddDate(0, 0, 1),
TimeZone: constz.TimeZone("Asia/Tokyo"),
OrderedLegendsAsc: []string{"legend1", "legend2"},
LegendValuesMap: map[string]plotter.Values{
"legend1": []float64{1},
"legend2": []float64{2},
Expand All @@ -103,16 +103,16 @@ func TestPlotGraph(t *testing.T) {
from := time.Date(2022, 2, 2, 2, 22, 22, 0, constz.TimeZone("Asia/Tokyo"))
d := New()
if err := d.PlotGraph(buf, &PlotGraphParameters{
GraphTitle: "Title",
XLabelText: "XLabel",
YLabelText: "YLabel",
Width: 1280,
Hight: 720,
XAxisPointsCount: 1,
From: from,
To: from.AddDate(0, 0, 1),
TimeZone: constz.TimeZone("Asia/Tokyo"),
OrderedLegends: []string{"NoData"},
GraphTitle: "Title",
XLabelText: "XLabel",
YLabelText: "YLabel",
Width: 1280,
Hight: 720,
XAxisPointsCount: 1,
From: from,
To: from.AddDate(0, 0, 1),
TimeZone: constz.TimeZone("Asia/Tokyo"),
OrderedLegendsAsc: []string{"NoData"},
LegendValuesMap: map[string]plotter.Values{
"legend1": []float64{1},
},
Expand All @@ -127,16 +127,16 @@ func TestPlotGraph(t *testing.T) {
from := time.Date(2022, 2, 2, 2, 22, 22, 0, constz.TimeZone("Asia/Tokyo"))
d := New()
if err := d.PlotGraph(buf, &PlotGraphParameters{
GraphTitle: "Title",
XLabelText: "XLabel",
YLabelText: "YLabel",
Width: 1280,
Hight: 720,
XAxisPointsCount: 1,
From: from,
To: from.AddDate(0, 0, 1),
TimeZone: constz.TimeZone("Asia/Tokyo"),
OrderedLegends: []string{"legend1", "legend2"},
GraphTitle: "Title",
XLabelText: "XLabel",
YLabelText: "YLabel",
Width: 1280,
Hight: 720,
XAxisPointsCount: 1,
From: from,
To: from.AddDate(0, 0, 1),
TimeZone: constz.TimeZone("Asia/Tokyo"),
OrderedLegendsAsc: []string{"legend1", "legend2"},
LegendValuesMap: map[string]plotter.Values{
"legend1": []float64{1},
"legend2": []float64{2},
Expand All @@ -152,16 +152,16 @@ func TestPlotGraph(t *testing.T) {
from := time.Date(2022, 2, 2, 2, 22, 22, 0, constz.TimeZone("Asia/Tokyo"))
d := New()
err := d.PlotGraph(rw, &PlotGraphParameters{
GraphTitle: "Title",
XLabelText: "XLabel",
YLabelText: "YLabel",
Width: 1280,
Hight: 720,
XAxisPointsCount: 1,
From: from,
To: from.AddDate(0, 0, 1),
TimeZone: constz.TimeZone("Asia/Tokyo"),
OrderedLegends: []string{"legend1", "legend2"},
GraphTitle: "Title",
XLabelText: "XLabel",
YLabelText: "YLabel",
Width: 1280,
Hight: 720,
XAxisPointsCount: 1,
From: from,
To: from.AddDate(0, 0, 1),
TimeZone: constz.TimeZone("Asia/Tokyo"),
OrderedLegendsAsc: []string{"legend1", "legend2"},
LegendValuesMap: map[string]plotter.Values{
"legend1": []float64{1},
"legend2": []float64{2},
Expand Down
6 changes: 3 additions & 3 deletions pkg/repository/bigquery/daily_project_cost_gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ WHERE
AND
DATE(usage_start_time, '{{ .TimeZone }}') >= DATE("{{ .From }}", '{{ .TimeZone }}')
AND
DATE(usage_start_time, '{{ .TimeZone }}') <= DATE("{{ .To }}", '{{ .TimeZone }}')
DATE(usage_start_time, '{{ .TimeZone }}') < DATE("{{ .To }}", '{{ .TimeZone }}')
AND
cost >= {{ .CostThreshold }}
GROUP BY
day, currency
ORDER BY
cost
DESC
day
ASC
;`))

func (c *BigQuery) DailyProjectCostGCP(ctx context.Context, billingTable, billingProject string, from, to time.Time, tz *time.Location, costThreshold float64) ([]domain.GCPCost, error) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/repository/bigquery/daily_service_cost_gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ WHERE
AND
DATE(usage_start_time, '{{ .TimeZone }}') >= DATE("{{ .From }}", '{{ .TimeZone }}')
AND
DATE(usage_start_time, '{{ .TimeZone }}') <= DATE("{{ .To }}", '{{ .TimeZone }}')
DATE(usage_start_time, '{{ .TimeZone }}') < DATE("{{ .To }}", '{{ .TimeZone }}')
AND
cost >= {{ .CostThreshold }}
GROUP BY
day, service, currency
ORDER BY
cost
DESC
day
ASC
;`))

func (c *BigQuery) DailyServiceCostGCP(ctx context.Context, billingTable, billingProject string, from, to time.Time, tz *time.Location, costThreshold float64) ([]domain.GCPServiceCost, error) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/repository/bigquery/daily_sku_cost_gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ WHERE
AND
DATE(usage_start_time, '{{ .TimeZone }}') >= DATE("{{ .From }}", '{{ .TimeZone }}')
AND
DATE(usage_start_time, '{{ .TimeZone }}') <= DATE("{{ .To }}", '{{ .TimeZone }}')
DATE(usage_start_time, '{{ .TimeZone }}') < DATE("{{ .To }}", '{{ .TimeZone }}')
AND
cost >= {{ .CostThreshold }}
GROUP BY
day, service, sku, currency
ORDER BY
cost
DESC
day
ASC
;`))

func (c *BigQuery) DailySKUCostGCP(ctx context.Context, billingTable, billingProject string, from, to time.Time, tz *time.Location, costThreshold float64) ([]domain.GCPSKUCost, error) {
Expand Down
10 changes: 5 additions & 5 deletions pkg/repository/bigquery/sum_service_cost_gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ WHERE
AND
DATE(usage_start_time, '{{ .TimeZone }}') >= DATE("{{ .From }}", '{{ .TimeZone }}')
AND
DATE(usage_start_time, '{{ .TimeZone }}') <= DATE("{{ .To }}", '{{ .TimeZone }}')
DATE(usage_start_time, '{{ .TimeZone }}') < DATE("{{ .To }}", '{{ .TimeZone }}')
AND
cost >= {{ .CostThreshold }}
GROUP BY
service, currency
ORDER BY
cost
DESC
ASC
;`))

func (c *BigQuery) SUMServiceCostGCP(ctx context.Context, billingTable, billingProject string, from, to time.Time, tz *time.Location, costThreshold float64) ([]domain.GCPServiceCost, error) {
func (c *BigQuery) SUMServiceCostGCPAsc(ctx context.Context, billingTable, billingProject string, from, to time.Time, tz *time.Location, costThreshold float64) ([]domain.GCPServiceCost, error) {
q, err := buildQuery(sumServiceCostGCPTemplate, sumServiceCostGCPParameter{
TimeZone: tz,
GCPBillingTable: billingTable,
Expand All @@ -59,10 +59,10 @@ func (c *BigQuery) SUMServiceCostGCP(ctx context.Context, billingTable, billingP

log.Debugf("%s", q)

results, err := query[domain.GCPServiceCost](ctx, c.client, q)
serviceCostAsc, err := query[domain.GCPServiceCost](ctx, c.client, q)
if err != nil {
return nil, errors.Errorf("query: %w", err)
}

return results, nil
return serviceCostAsc, nil
}
20 changes: 3 additions & 17 deletions pkg/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ func WithBigQuery(bigquery *bigquery.BigQuery) Option {
}
}

func (r *Repository) SUMServiceCostGCP(ctx context.Context, billingTable, billingProject string, from, to time.Time, tz *time.Location, costThreshold float64) ([]domain.GCPServiceCost, error) {
serviceCost, err := r.bigquery.SUMServiceCostGCP(ctx, billingTable, billingProject, from, to, tz, costThreshold)
func (r *Repository) SUMServiceCostGCPAsc(ctx context.Context, billingTable, billingProject string, from, to time.Time, tz *time.Location, costThreshold float64) ([]domain.GCPServiceCost, error) {
serviceCostAsc, err := r.bigquery.SUMServiceCostGCPAsc(ctx, billingTable, billingProject, from, to, tz, costThreshold)
if err != nil {
return nil, errors.Errorf("(*bigquery.BigQuery).SUMServiceCostGCP: %w", err)
}

return serviceCost, nil
return serviceCostAsc, nil
}

func (r *Repository) DailyServiceCostGCP(ctx context.Context, billingTable, billingProject string, from, to time.Time, tz *time.Location, costThreshold float64) ([]domain.GCPServiceCost, error) {
Expand All @@ -51,20 +51,6 @@ func (r *Repository) DailyServiceCostGCP(ctx context.Context, billingTable, bill
return serviceCost, nil
}

func (r *Repository) ServicesOrderBySUMServiceCostGCP(googleCloudPlatformServiceSumCost []domain.GCPServiceCost) []string {
return slice.Uniq(
slice.Select(
slice.Sort(
googleCloudPlatformServiceSumCost,
func(a, b domain.GCPServiceCost) bool { return a.Cost > b.Cost },
),
func(index int, source domain.GCPServiceCost) (selected string) {
return source.Service
},
),
)
}

func (r *Repository) DailyServiceCostGCPMapByService(servicesOrderBySUMServiceCostGCP []string, dailyServiceCostGCP []domain.GCPServiceCost) map[string][]domain.GCPServiceCost {
serviceCost := make(map[string][]domain.GCPServiceCost)
for _, service := range servicesOrderBySUMServiceCostGCP {
Expand Down
8 changes: 1 addition & 7 deletions pkg/usecase/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,16 @@ type repositoryMock struct {
SUMServiceCostGCP_GCPServiceCost []domain.GCPServiceCost
SUMServiceCostGCP_error error

ServicesOrderBySUMServiceCostGCP_slice_string []string

DailyServiceCostGCP_GCPServiceCost []domain.GCPServiceCost
DailyServiceCostGCP_error error

DailyServiceCostGCPMapByService_map_string_GCPServiceCost map[string][]domain.GCPServiceCost
}

func (m *repositoryMock) SUMServiceCostGCP(ctx context.Context, billingTable string, billingProject string, from time.Time, to time.Time, tz *time.Location, costThreshold float64) ([]domain.GCPServiceCost, error) {
func (m *repositoryMock) SUMServiceCostGCPAsc(ctx context.Context, billingTable string, billingProject string, from time.Time, to time.Time, tz *time.Location, costThreshold float64) ([]domain.GCPServiceCost, error) {
return m.SUMServiceCostGCP_GCPServiceCost, m.SUMServiceCostGCP_error
}

func (m *repositoryMock) ServicesOrderBySUMServiceCostGCP(googleCloudPlatformServiceSumCost []domain.GCPServiceCost) []string {
return m.ServicesOrderBySUMServiceCostGCP_slice_string
}

func (m *repositoryMock) DailyServiceCostGCP(ctx context.Context, billingTable string, billingProject string, from time.Time, to time.Time, tz *time.Location, costThreshold float64) ([]domain.GCPServiceCost, error) {
return m.DailyServiceCostGCP_GCPServiceCost, m.DailyServiceCostGCP_error
}
Expand Down
Loading

0 comments on commit 712f929

Please sign in to comment.