Skip to content

Commit

Permalink
Sammyteillet/dat 49 aau i can set a timegrain quarter (#24)
Browse files Browse the repository at this point in the history
* feature: add quarter Timegrain

* feature: add quarter timeGrain in json schema

* feature: implement quarter history process

* feature: create report with the right timegrain

* feature: recreate property only if needed

* feature: add color for timegrain property
  • Loading branch information
Samox committed May 10, 2023
1 parent 82521d6 commit dbe7d3d
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 26 deletions.
9 changes: 5 additions & 4 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
51 changes: 30 additions & 21 deletions database/notion/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: &notion.EmptyMetadata{},
},
PROPERTY_DATADRIFT_PERIOD: {
Type: notion.DBPropTypeRichText,
RichText: &notion.EmptyMetadata{},
},
PROPERTY_DATADRIFT_TIMEGRAIN: {
Type: notion.DBPropTypeSelect,
Select: &notion.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] = &notion.DatabaseProperty{
Type: notion.DBPropTypeRichText,
RichText: &notion.EmptyMetadata{},
}
}

if shouldCreateDatadriftPropertyPeriod {
params.Properties[PROPERTY_DATADRIFT_PERIOD] = &notion.DatabaseProperty{
Type: notion.DBPropTypeRichText,
RichText: &notion.EmptyMetadata{},
}
}

if shouldCreateDatadriftPropertyTimeGrain {
params.Properties[PROPERTY_DATADRIFT_TIMEGRAIN] = &notion.DatabaseProperty{
Type: notion.DBPropTypeSelect,
Select: &notion.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 {
Expand Down
2 changes: 2 additions & 0 deletions history/process-history.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion json-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
},
Expand Down
30 changes: 30 additions & 0 deletions reports/reports.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package reports

import (
"fmt"
"strconv"
"strings"
"time"

"github.com/data-drift/kpi-git-history/common"
Expand Down Expand Up @@ -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)
}
}

0 comments on commit dbe7d3d

Please sign in to comment.