diff --git a/common/common.go b/common/common.go index e7d0b08a..5e791e86 100644 --- a/common/common.go +++ b/common/common.go @@ -46,10 +46,11 @@ type Config struct { type TimeGrain string const ( - Day TimeGrain = "day" - Week TimeGrain = "week" - Month TimeGrain = "month" - Year TimeGrain = "year" + Day TimeGrain = "day" + Week TimeGrain = "week" + Month TimeGrain = "month" + Quarter TimeGrain = "quarter" + Year TimeGrain = "year" ) type Metric struct { diff --git a/database/notion/repository.go b/database/notion/repository.go index ed2b19c0..9b4b226b 100644 --- a/database/notion/repository.go +++ b/database/notion/repository.go @@ -177,33 +177,42 @@ func AssertDatabaseHasDatadriftProperties(databaseID, apiKey string) error { shouldCreateProperties := shouldCreateDatadriftPropertyId || shouldCreateDatadriftPropertyPeriod || shouldCreateDatadriftPropertyTimeGrain if shouldCreateProperties { params := notion.UpdateDatabaseParams{ - Properties: map[string]*notion.DatabaseProperty{ - PROPERTY_DATADRIFT_ID: { - Type: notion.DBPropTypeRichText, - RichText: ¬ion.EmptyMetadata{}, - }, - PROPERTY_DATADRIFT_PERIOD: { - Type: notion.DBPropTypeRichText, - RichText: ¬ion.EmptyMetadata{}, - }, - PROPERTY_DATADRIFT_TIMEGRAIN: { - Type: notion.DBPropTypeSelect, - Select: ¬ion.SelectMetadata{ - Options: []notion.SelectOptions{ - {Name: string(common.Day)}, - {Name: string(common.Month)}, - {Name: string(common.Week)}, - {Name: string(common.Year)}, - }, - }, - }, - }, + Properties: map[string]*notion.DatabaseProperty{}, } for _, propertyToDelete := range propertiesToDelete { params.Properties[propertyToDelete] = nil } + if shouldCreateDatadriftPropertyId { + params.Properties[PROPERTY_DATADRIFT_ID] = ¬ion.DatabaseProperty{ + Type: notion.DBPropTypeRichText, + RichText: ¬ion.EmptyMetadata{}, + } + } + + if shouldCreateDatadriftPropertyPeriod { + params.Properties[PROPERTY_DATADRIFT_PERIOD] = ¬ion.DatabaseProperty{ + Type: notion.DBPropTypeRichText, + RichText: ¬ion.EmptyMetadata{}, + } + } + + if shouldCreateDatadriftPropertyTimeGrain { + params.Properties[PROPERTY_DATADRIFT_TIMEGRAIN] = ¬ion.DatabaseProperty{ + Type: notion.DBPropTypeSelect, + Select: ¬ion.SelectMetadata{ + Options: []notion.SelectOptions{ + {Name: string(common.Day), Color: notion.ColorYellow}, + {Name: string(common.Month), Color: notion.ColorOrange}, + {Name: string(common.Week), Color: notion.ColorRed}, + {Name: string(common.Quarter), Color: notion.ColorPink}, + {Name: string(common.Year), Color: notion.ColorPurple}, + }, + }, + } + } + fmt.Println("Creating property", params) _, err := client.UpdateDatabase(ctx, databaseID, params) if err != nil { diff --git a/history/process-history.go b/history/process-history.go index 978729c0..f245ad30 100644 --- a/history/process-history.go +++ b/history/process-history.go @@ -102,6 +102,8 @@ func ProcessHistory(client *github.Client, repoOwner string, repoName string, me periodKey = PeriodId(fmt.Sprintf("%d-W%d", periodTime.Year(), week)) case common.Month: periodKey = PeriodId(periodTime.Format("2006-01")) + case common.Quarter: + periodKey = PeriodId(fmt.Sprintf("%d-Q%d", periodTime.Year(), (periodTime.Month()-1)/3+1)) case common.Year: periodKey = PeriodId(periodTime.Format("2006")) default: diff --git a/json-schema.json b/json-schema.json index da7a575b..68485337 100644 --- a/json-schema.json +++ b/json-schema.json @@ -40,7 +40,7 @@ "type": "array", "items": { "type": "string", - "enum": ["day", "week", "month", "year"] + "enum": ["day", "week", "month", "quarter", "year"] }, "description": "The timegrain for the metric data (day, week, month or year)" }, diff --git a/reports/reports.go b/reports/reports.go index 0782a241..bf2969e8 100644 --- a/reports/reports.go +++ b/reports/reports.go @@ -2,6 +2,8 @@ package reports import ( "fmt" + "strconv" + "strings" "time" "github.com/data-drift/kpi-git-history/common" @@ -228,9 +230,37 @@ func GetTimeGrain(periodKey string) (common.TimeGrain, error) { if err == nil { return common.Month, nil } + _, err = ParseQuarterDate(periodKey) + if err == nil { + return common.Quarter, nil + } _, err = time.Parse("2006", periodKey) if err == nil { return common.Year, nil } return "", fmt.Errorf("invalid period key: %s", periodKey) } + +func ParseQuarterDate(s string) (time.Time, error) { + parts := strings.Split(s, "-") + if len(parts) != 2 { + return time.Time{}, fmt.Errorf("invalid quarter date format: %s", s) + } + year, err := strconv.Atoi(parts[0]) + if err != nil { + return time.Time{}, fmt.Errorf("invalid year format in quarter date: %s", s) + } + quarter := parts[1] + switch quarter { + case "Q1": + return time.Date(year, time.March, 31, 0, 0, 0, 0, time.UTC), nil + case "Q2": + return time.Date(year, time.June, 30, 0, 0, 0, 0, time.UTC), nil + case "Q3": + return time.Date(year, time.September, 30, 0, 0, 0, 0, time.UTC), nil + case "Q4": + return time.Date(year, time.December, 31, 0, 0, 0, 0, time.UTC), nil + default: + return time.Time{}, fmt.Errorf("invalid quarter format in quarter date: %s", s) + } +}