Skip to content

Commit

Permalink
Merge pull request #7 from seatsio/steve/wip/usage_reports
Browse files Browse the repository at this point in the history
Usage reports
  • Loading branch information
schaloner authored Jul 24, 2023
2 parents 37cb517 + 9e5adda commit b184486
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 41 deletions.
82 changes: 56 additions & 26 deletions reports/chart_reports.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,68 +46,98 @@ type ChartReportItem struct {
HasRestrictedView bool `json:"hasRestrictedView,omitempty"`
}

func (reports *ChartReports) SummaryByObjectType(chartKey string, bookWholeTablesMode string) (*ChartSummaryReport, error) {
return reports.fetchSummaryChartReport("byObjectType", chartKey, bookWholeTablesMode)
type bookWholeTables struct {
mode map[string]string
}

func (reports *ChartReports) SummaryByCategoryKey(chartKey string, bookWholeTablesMode string) (*ChartSummaryReport, error) {
return reports.fetchSummaryChartReport("byCategoryKey", chartKey, bookWholeTablesMode)
type bookWholeTablesModeOption func(params *bookWholeTables)

type bookWholeTablesNS struct{}

var BookWholeTables bookWholeTablesNS

func (reports *ChartReports) SummaryByObjectType(chartKey string, bookWholeTablesMode ...bookWholeTablesModeOption) (*ChartSummaryReport, error) {
return reports.fetchSummaryChartReport("byObjectType", chartKey, bookWholeTablesMode...)
}

func (reports *ChartReports) SummaryByCategoryKey(chartKey string, bookWholeTablesMode ...bookWholeTablesModeOption) (*ChartSummaryReport, error) {
return reports.fetchSummaryChartReport("byCategoryKey", chartKey, bookWholeTablesMode...)
}

func (reports *ChartReports) SummaryByCategoryLabel(chartKey string, bookWholeTablesMode string) (*ChartSummaryReport, error) {
return reports.fetchSummaryChartReport("byCategoryLabel", chartKey, bookWholeTablesMode)
func (reports *ChartReports) SummaryByCategoryLabel(chartKey string, bookWholeTablesMode ...bookWholeTablesModeOption) (*ChartSummaryReport, error) {
return reports.fetchSummaryChartReport("byCategoryLabel", chartKey, bookWholeTablesMode...)
}

func (reports *ChartReports) SummaryBySection(chartKey string, bookWholeTablesMode string) (*ChartSummaryReport, error) {
return reports.fetchSummaryChartReport("bySection", chartKey, bookWholeTablesMode)
func (reports *ChartReports) SummaryBySection(chartKey string, bookWholeTablesMode ...bookWholeTablesModeOption) (*ChartSummaryReport, error) {
return reports.fetchSummaryChartReport("bySection", chartKey, bookWholeTablesMode...)
}

func (reports *ChartReports) ByLabel(chartKey string, bookWholeTablesMode string) (*ChartReport, error) {
return reports.fetchChartReport("byLabel", chartKey, bookWholeTablesMode)
func (reports *ChartReports) ByLabel(chartKey string, bookWholeTablesMode ...bookWholeTablesModeOption) (*ChartReport, error) {
return reports.fetchChartReport("byLabel", chartKey, bookWholeTablesMode...)
}

func (reports *ChartReports) ByObjectType(chartKey string, bookWholeTablesMode string) (*ChartReport, error) {
return reports.fetchChartReport("byObjectType", chartKey, bookWholeTablesMode)
func (reports *ChartReports) ByObjectType(chartKey string, bookWholeTablesMode ...bookWholeTablesModeOption) (*ChartReport, error) {
return reports.fetchChartReport("byObjectType", chartKey, bookWholeTablesMode...)
}

func (reports *ChartReports) ByCategoryKey(chartKey string, bookWholeTablesMode string) (*ChartReport, error) {
return reports.fetchChartReport("byCategoryKey", chartKey, bookWholeTablesMode)
func (reports *ChartReports) ByCategoryKey(chartKey string, bookWholeTablesMode ...bookWholeTablesModeOption) (*ChartReport, error) {
return reports.fetchChartReport("byCategoryKey", chartKey, bookWholeTablesMode...)
}

func (reports *ChartReports) ByCategoryLabel(chartKey string, bookWholeTablesMode string) (*ChartReport, error) {
return reports.fetchChartReport("byCategoryLabel", chartKey, bookWholeTablesMode)
func (reports *ChartReports) ByCategoryLabel(chartKey string, bookWholeTablesMode ...bookWholeTablesModeOption) (*ChartReport, error) {
return reports.fetchChartReport("byCategoryLabel", chartKey, bookWholeTablesMode...)
}

func (reports *ChartReports) BySection(chartKey string, bookWholeTablesMode string) (*ChartReport, error) {
return reports.fetchChartReport("bySection", chartKey, bookWholeTablesMode)
func (reports *ChartReports) BySection(chartKey string, bookWholeTablesMode ...bookWholeTablesModeOption) (*ChartReport, error) {
return reports.fetchChartReport("bySection", chartKey, bookWholeTablesMode...)
}

func (reports *ChartReports) fetchChartReport(reportType string, chartKey string, bookWholeTablesMode string) (*ChartReport, error) {
func (reports *ChartReports) fetchChartReport(reportType string, chartKey string, bookWholeTablesMode ...bookWholeTablesModeOption) (*ChartReport, error) {
tableMode := bookWholeTables{mode: map[string]string{}}
for _, mode := range bookWholeTablesMode {
mode(&tableMode)
}
var report map[string][]ChartReportItem
result, err := reports.Client.R().
SetSuccessResult(&report).
SetPathParam("reportItemType", "charts").
SetPathParam("key", chartKey).
SetPathParam("reportType", reportType).
SetQueryParams(toQueryParams(bookWholeTablesMode)).
SetQueryParams(tableMode.mode).
Get("/reports/{reportItemType}/{key}/{reportType}")
return shared.AssertOk(result, err, &ChartReport{Items: report})
}

func (reports *ChartReports) fetchSummaryChartReport(reportType string, chartKey string, bookWholeTablesMode string) (*ChartSummaryReport, error) {
func (reports *ChartReports) fetchSummaryChartReport(reportType string, chartKey string, bookWholeTablesMode ...bookWholeTablesModeOption) (*ChartSummaryReport, error) {
tableMode := bookWholeTables{mode: map[string]string{}}
for _, mode := range bookWholeTablesMode {
mode(&tableMode)
}
var report map[string]ChartSummaryReportItem
result, err := reports.Client.R().
SetSuccessResult(&report).
SetPathParam("reportItemType", "charts").
SetPathParam("key", chartKey).
SetPathParam("reportType", reportType).
SetQueryParams(toQueryParams(bookWholeTablesMode)).
SetQueryParams(tableMode.mode).
Get("/reports/{reportItemType}/{key}/{reportType}/summary")
return shared.AssertOk(result, err, &ChartSummaryReport{Items: report})
}

func toQueryParams(bookWholeTablesMode string) map[string]string {
m := make(map[string]string)
m["bookWholeTables"] = bookWholeTablesMode
return m
func (bookWholeTablesNS) Chart() bookWholeTablesModeOption {
return func(mode *bookWholeTables) {
mode.mode["bookWholeTables"] = "chart"
}
}

func (bookWholeTablesNS) True() bookWholeTablesModeOption {
return func(mode *bookWholeTables) {
mode.mode["bookWholeTables"] = "true"
}
}

func (bookWholeTablesNS) False() bookWholeTablesModeOption {
return func(mode *bookWholeTables) {
mode.mode["bookWholeTables"] = "false"
}
}
121 changes: 121 additions & 0 deletions reports/usage_reports.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package reports

import (
"encoding/json"
"fmt"
"github.com/imroc/req/v3"
"github.com/seatsio/seatsio-go/shared"
"time"
)

type UsageReports struct {
Client *req.Client
}

type UsageReason string

const (
StatusChanged UsageReason = "STATUS_CHANGED"
Selected UsageReason = "SELECTED"
AssignedToChannel UsageReason = "ASSIGNED_TO_CHANNEL"
)

type UsageForObjectV1 struct {
Object string `json:"object"`
NumFirstBookings int32 `json:"numFirstBookings"`
FirstBookingDate time.Time `json:"firstBookingDate"`
NumFirstSelections int32 `json:"numFirstSelections"`
NumFirstBookingsOrSelections int32 `json:"numFirstBookingsOrSelections"`
}

type UsageForObjectV2 struct {
Object string `json:"object"`
NumUsedObjects int32 `json:"numUsedObjects"`
UsageByReason map[UsageReason]int32 `json:"usageByReason"`
}

type UsageChart struct {
Name string `json:"name"`
Key string `json:"key"`
}

type UsageEvent struct {
Id int64 `json:"id"`
Key string `json:"key"`
}

type UsageByEvent struct {
Event UsageEvent `json:"event"`
NumUsedObjects int32 `json:"numUsedObjects"`
}

type UsageByChart struct {
Chart UsageChart `json:"chart"`
UsageByEvent []UsageByEvent `json:"usageByEvent"`
}

type UsageDetails struct {
Workspace int64 `json:"workspace"`
UsageByChart []UsageByChart `json:"usageByChart"`
}

type Month struct {
Month int32 `json:"month"`
Year int32 `json:"year"`
}

type UsageSummaryForMonth struct {
Month Month `json:"month"`
NumUsedObjects int32 `json:"numUsedObjects"`
}

func (usageReports *UsageReports) SummaryForAllMonths() ([]UsageSummaryForMonth, error) {
var summaries []UsageSummaryForMonth
result, err := usageReports.Client.R().
SetSuccessResult(&summaries).
Get("/reports/usage")
return shared.AssertOkArray(result, err, &summaries)
}

func (usageReports *UsageReports) DetailsForMonth(year int, month int) ([]UsageDetails, error) {
var details []UsageDetails
result, err := usageReports.Client.R().
SetSuccessResult(&details).
SetPathParam("month", formatMonth(year, month)).
Get("/reports/usage/month/{month}")
return shared.AssertOkArray(result, err, &details)
}

func (usageReports *UsageReports) DetailsForEventInMonth(year int, month int) ([]UsageForObjectV1, []UsageForObjectV2, error) {
result, err := usageReports.Client.R().
SetPathParam("month", formatMonth(year, month)).
Get("/reports/usage/month/{month}")
err = shared.AssertOkWithoutResult(result, err)
if err != nil {
return nil, nil, err
}

var jsonContent []interface{}
bodyAsBytes := result.Bytes()
_ = json.Unmarshal(bodyAsBytes, &jsonContent)
if len(jsonContent) == 0 {
return []UsageForObjectV1{}, []UsageForObjectV2{}, nil
}

var sampleUsageMap = jsonContent[0].(map[string]interface{})
_, containsKey := sampleUsageMap["usageByReason"]
if containsKey {
var results []UsageForObjectV2
err = json.Unmarshal(bodyAsBytes, &results)
return nil, results, nil
} else {
var results []UsageForObjectV1
err = json.Unmarshal(bodyAsBytes, &results)
return results, nil, nil
}

}

func formatMonth(year int, month int) string {
return fmt.Sprintf("%d-%02d", year, month)
}
10 changes: 5 additions & 5 deletions reports_test/chart_reports_summary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestSummaryByObjectType(t *testing.T) {
client := seatsio.NewSeatsioClient(company.Admin.SecretKey, test_util.BaseUrl)
chartKey := test_util.CreateTestChart(t, company.Admin.SecretKey)

summaryChartReport, err := client.ChartReports.SummaryByObjectType(chartKey, "false")
summaryChartReport, err := client.ChartReports.SummaryByObjectType(chartKey)

require.NoError(t, err)
seatReportItem := reports.ChartSummaryReportItem{
Expand Down Expand Up @@ -59,7 +59,7 @@ func TestSummaryByObjectType_BookWholeTablesTrue(t *testing.T) {
client := seatsio.NewSeatsioClient(company.Admin.SecretKey, test_util.BaseUrl)
chartKey := test_util.CreateTestChartWithTables(t, company.Admin.SecretKey)

summaryChartReport, err := client.ChartReports.SummaryByObjectType(chartKey, "true")
summaryChartReport, err := client.ChartReports.SummaryByObjectType(chartKey, reports.BookWholeTables.True())

require.NoError(t, err)
tableReportItem := reports.ChartSummaryReportItem{
Expand Down Expand Up @@ -90,7 +90,7 @@ func TestSummaryByCategoryKey(t *testing.T) {
client := seatsio.NewSeatsioClient(company.Admin.SecretKey, test_util.BaseUrl)
chartKey := test_util.CreateTestChart(t, company.Admin.SecretKey)

summaryChartReport, err := client.ChartReports.SummaryByCategoryKey(chartKey, "false")
summaryChartReport, err := client.ChartReports.SummaryByCategoryKey(chartKey)

require.NoError(t, err)
cat9Report := reports.ChartSummaryReportItem{
Expand Down Expand Up @@ -131,7 +131,7 @@ func TestSummaryByCategoryLabel(t *testing.T) {
client := seatsio.NewSeatsioClient(company.Admin.SecretKey, test_util.BaseUrl)
chartKey := test_util.CreateTestChart(t, company.Admin.SecretKey)

summaryChartReport, err := client.ChartReports.SummaryByCategoryLabel(chartKey, "false")
summaryChartReport, err := client.ChartReports.SummaryByCategoryLabel(chartKey)

require.NoError(t, err)
cat1Report := reports.ChartSummaryReportItem{
Expand Down Expand Up @@ -172,7 +172,7 @@ func TestSummaryBySection(t *testing.T) {
client := seatsio.NewSeatsioClient(company.Admin.SecretKey, test_util.BaseUrl)
chartKey := test_util.CreateTestChart(t, company.Admin.SecretKey)

summaryChartReport, err := client.ChartReports.SummaryBySection(chartKey, "false")
summaryChartReport, err := client.ChartReports.SummaryBySection(chartKey)

require.NoError(t, err)
noSectionReport := reports.ChartSummaryReportItem{
Expand Down
21 changes: 11 additions & 10 deletions reports_test/chart_reports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package reports
import (
"github.com/seatsio/seatsio-go"
"github.com/seatsio/seatsio-go/events"
"github.com/seatsio/seatsio-go/reports"
"github.com/seatsio/seatsio-go/test_util"
"github.com/stretchr/testify/require"
"testing"
Expand All @@ -14,7 +15,7 @@ func TestReportItemProperties(t *testing.T) {
client := seatsio.NewSeatsioClient(company.Admin.SecretKey, test_util.BaseUrl)
chartKey := test_util.CreateTestChart(t, company.Admin.SecretKey)

chartReport, err := client.ChartReports.ByLabel(chartKey, "false")
chartReport, err := client.ChartReports.ByLabel(chartKey)
require.NoError(t, err)
require.Len(t, chartReport.Items["A-1"], 1)
item := chartReport.Items["A-1"][0]
Expand Down Expand Up @@ -44,7 +45,7 @@ func TestReportItemPropertiesForGA(t *testing.T) {
client := seatsio.NewSeatsioClient(company.Admin.SecretKey, test_util.BaseUrl)
chartKey := test_util.CreateTestChart(t, company.Admin.SecretKey)

chartReport, err := client.ChartReports.ByLabel(chartKey, "false")
chartReport, err := client.ChartReports.ByLabel(chartKey)
require.NoError(t, err)
require.Len(t, chartReport.Items["GA1"], 1)
item := chartReport.Items["GA1"][0]
Expand All @@ -59,7 +60,7 @@ func TestReportItemPropertiesForTable(t *testing.T) {
client := seatsio.NewSeatsioClient(company.Admin.SecretKey, test_util.BaseUrl)
chartKey := test_util.CreateTestChartWithTables(t, company.Admin.SecretKey)

chartReport, err := client.ChartReports.ByLabel(chartKey, "true")
chartReport, err := client.ChartReports.ByLabel(chartKey, reports.BookWholeTables.True())
require.NoError(t, err)
require.Len(t, chartReport.Items["T1"], 1)
item := chartReport.Items["T1"][0]
Expand All @@ -73,7 +74,7 @@ func TestByLabel(t *testing.T) {
client := seatsio.NewSeatsioClient(company.Admin.SecretKey, test_util.BaseUrl)
chartKey := test_util.CreateTestChart(t, company.Admin.SecretKey)

chartReport, err := client.ChartReports.ByLabel(chartKey, "false")
chartReport, err := client.ChartReports.ByLabel(chartKey)
require.NoError(t, err)
require.Len(t, chartReport.Items["A-1"], 1)
require.Len(t, chartReport.Items["A-2"], 1)
Expand All @@ -85,7 +86,7 @@ func TestByLabel_BookWholeTablesTrue(t *testing.T) {
client := seatsio.NewSeatsioClient(company.Admin.SecretKey, test_util.BaseUrl)
chartKey := test_util.CreateTestChartWithTables(t, company.Admin.SecretKey)

chartReport, err := client.ChartReports.ByLabel(chartKey, "true")
chartReport, err := client.ChartReports.ByLabel(chartKey, reports.BookWholeTables.True())
require.NoError(t, err)
require.Len(t, chartReport.Items, 2)
require.Nil(t, chartReport.Items["T1-1"])
Expand All @@ -100,7 +101,7 @@ func TestByLabel_BookWholeTablesChart(t *testing.T) {
client := seatsio.NewSeatsioClient(company.Admin.SecretKey, test_util.BaseUrl)
chartKey := test_util.CreateTestChartWithTables(t, company.Admin.SecretKey)

chartReport, err := client.ChartReports.ByLabel(chartKey, "chart")
chartReport, err := client.ChartReports.ByLabel(chartKey, reports.BookWholeTables.Chart())
require.NoError(t, err)
require.Len(t, chartReport.Items, 7)
require.NotNil(t, chartReport.Items["T1-1"])
Expand All @@ -118,7 +119,7 @@ func TestByObjectType(t *testing.T) {
client := seatsio.NewSeatsioClient(company.Admin.SecretKey, test_util.BaseUrl)
chartKey := test_util.CreateTestChart(t, company.Admin.SecretKey)

chartReport, err := client.ChartReports.ByObjectType(chartKey, "false")
chartReport, err := client.ChartReports.ByObjectType(chartKey)
require.NoError(t, err)
require.Len(t, chartReport.Items["seat"], 32)
require.Len(t, chartReport.Items["generalAdmission"], 2)
Expand All @@ -130,7 +131,7 @@ func TestByCategoryKey(t *testing.T) {
client := seatsio.NewSeatsioClient(company.Admin.SecretKey, test_util.BaseUrl)
chartKey := test_util.CreateTestChart(t, company.Admin.SecretKey)

chartReport, err := client.ChartReports.ByCategoryKey(chartKey, "false")
chartReport, err := client.ChartReports.ByCategoryKey(chartKey)
require.NoError(t, err)
require.Len(t, chartReport.Items["9"], 17)
require.Len(t, chartReport.Items["10"], 17)
Expand All @@ -142,7 +143,7 @@ func TestByCategoryLabel(t *testing.T) {
client := seatsio.NewSeatsioClient(company.Admin.SecretKey, test_util.BaseUrl)
chartKey := test_util.CreateTestChart(t, company.Admin.SecretKey)

chartReport, err := client.ChartReports.ByCategoryLabel(chartKey, "false")
chartReport, err := client.ChartReports.ByCategoryLabel(chartKey)
require.NoError(t, err)
require.Len(t, chartReport.Items["Cat1"], 17)
require.Len(t, chartReport.Items["Cat2"], 17)
Expand All @@ -154,7 +155,7 @@ func TestBySection(t *testing.T) {
client := seatsio.NewSeatsioClient(company.Admin.SecretKey, test_util.BaseUrl)
chartKey := test_util.CreateTestChartWithSections(t, company.Admin.SecretKey)

chartReport, err := client.ChartReports.BySection(chartKey, "false")
chartReport, err := client.ChartReports.BySection(chartKey)
require.NoError(t, err)
require.Len(t, chartReport.Items["Section A"], 36)
require.Len(t, chartReport.Items["Section B"], 35)
Expand Down
Loading

0 comments on commit b184486

Please sign in to comment.