Skip to content

Commit

Permalink
Lucas2vries/dat 63 aau i see a new property dimension in (#37)
Browse files Browse the repository at this point in the history
* feature: Create database property for dimension

* feature: Add dimension value to empty report
  • Loading branch information
Lucasdvrs committed May 19, 2023
1 parent 35d09c0 commit 8573903
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 20 deletions.
17 changes: 9 additions & 8 deletions charts/charts.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ func ProcessCharts(historyFilepath string, metric common.MetricConfig) []common.
// Access the value associated with the key: data[key]
// Additional logic for processing the value
// ...
kpi := OrderDataAndCreateChart(metric.MetricName+" "+key, data[periodIdAndDimensionKey].Period, data[periodIdAndDimensionKey].History)
kpi := OrderDataAndCreateChart(metric.MetricName+" "+key, data[periodIdAndDimensionKey].Period, data[periodIdAndDimensionKey].History, data[periodIdAndDimensionKey].DimensionValue)
kpiInfos = append(kpiInfos, kpi)
}

return kpiInfos
}

func OrderDataAndCreateChart(KPIName string, periodId common.PeriodKey, unsortedResults common.MetricHistory) common.KPIReport {
func OrderDataAndCreateChart(KPIName string, periodId common.PeriodKey, unsortedResults common.MetricHistory, dimensionValue common.DimensionValue) common.KPIReport {
// Extract the values from the map into a slice of struct objects
var dataSortableArray []common.CommitData

Expand Down Expand Up @@ -125,12 +125,13 @@ func OrderDataAndCreateChart(KPIName string, periodId common.PeriodKey, unsorted

chartUrl := createChart(diff, labels, colors, KPIName, minOfChart)
kpi1 := common.KPIReport{
KPIName: KPIName,
PeriodId: periodId,
GraphQLURL: chartUrl,
InitialValue: initialValue,
LatestValue: latestValue,
Events: events,
KPIName: KPIName,
PeriodId: periodId,
DimensionValue: dimensionValue,
GraphQLURL: chartUrl,
InitialValue: initialValue,
LatestValue: latestValue,
Events: events,
}
return kpi1
}
Expand Down
13 changes: 7 additions & 6 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package common
import "github.com/shopspring/decimal"

type KPIReport struct {
KPIName string `json:"kpiName"`
PeriodId PeriodKey `json:"periodId"`
GraphQLURL string `json:"graphqlUrl"`
InitialValue decimal.Decimal `json:"firstRoundedKPI"`
LatestValue decimal.Decimal `json:"lastRoundedKPI"`
Events []EventObject `json:"events"`
KPIName string `json:"kpiName"`
PeriodId PeriodKey `json:"periodId"`
DimensionValue DimensionValue `json:"dimensionValue"`
GraphQLURL string `json:"graphqlUrl"`
InitialValue decimal.Decimal `json:"firstRoundedKPI"`
LatestValue decimal.Decimal `json:"lastRoundedKPI"`
Events []EventObject `json:"events"`
}

type SyncConfig struct {
Expand Down
29 changes: 25 additions & 4 deletions database/notion_database/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@ const PROPERTY_DATADRIFT_ID = "datadrift-id"
const PROPERTY_DATADRIFT_TIMEGRAIN = "datadrift-timegrain"
const PROPERTY_DATADRIFT_PERIOD = "datadrift-period"
const PROPERTY_DATADRIFT_DRIFT_VALUE = "datadrift-drift-value"
const PROPERTY_DATADRIFT_DIMENSION = "datadrift-dimension"

var DefaultPropertiesToDelete = []string{"Tags", "Status", "Étiquette", "Étiquettes"}

func FindOrCreateReportPageId(apiKey string, databaseId string, reportName string, period string, timeGrain common.TimeGrain) (string, error) {
func FindOrCreateReportPageId(apiKey string, databaseId string, reportName string, period string, timeGrain common.TimeGrain, dimensionValue common.DimensionValue) (string, error) {
existingReportId, err := QueryDatabaseWithReportId(apiKey, databaseId, reportName)
if err != nil {
return "", err
}
if existingReportId == "" {
fmt.Println("No existing report found, creating new one")
newReportId, err := CreateEmptyReport(apiKey, databaseId, reportName, period, timeGrain)
newReportId, err := CreateEmptyReport(apiKey, databaseId, reportName, period, timeGrain, dimensionValue)
return newReportId, err
}
return existingReportId, nil
Expand Down Expand Up @@ -74,7 +75,7 @@ func QueryDatabaseWithReportId(apiKey string, databaseId string, reportId string
}
}

func CreateEmptyReport(apiKey string, databaseId string, reportId string, period string, timeGrain common.TimeGrain) (string, error) {
func CreateEmptyReport(apiKey string, databaseId string, reportId string, period string, timeGrain common.TimeGrain, dimensionValue common.DimensionValue) (string, error) {
buf := &bytes.Buffer{}
ctx := context.Background()

Expand Down Expand Up @@ -120,6 +121,15 @@ func CreateEmptyReport(apiKey string, databaseId string, reportId string, period
Name: string(timeGrain),
},
},
PROPERTY_DATADRIFT_DIMENSION: notion.DatabasePageProperty{
RichText: []notion.RichText{
{
Text: &notion.Text{
Content: string(dimensionValue),
},
},
},
},
},
}
newReport, err := client.CreatePage(ctx, params)
Expand Down Expand Up @@ -152,6 +162,7 @@ func AssertDatabaseHasDatadriftProperties(databaseID, apiKey string) error {
shouldCreateDatadriftPropertyPeriod := true
shouldCreateDatadriftPropertyTimeGrain := true
shouldCreateDatadriftPropertyDriftValue := true
shouldCreateDatadriftPropertyDimension := true

propertiesToDelete := []string{}

Expand All @@ -169,6 +180,9 @@ func AssertDatabaseHasDatadriftProperties(databaseID, apiKey string) error {
if property.Name == PROPERTY_DATADRIFT_DRIFT_VALUE {
shouldCreateDatadriftPropertyDriftValue = false
}
if property.Name == PROPERTY_DATADRIFT_DIMENSION {
shouldCreateDatadriftPropertyDimension = false
}

for _, propertyToDelete := range DefaultPropertiesToDelete {
propertyExists := property.Name == propertyToDelete
Expand All @@ -179,7 +193,7 @@ func AssertDatabaseHasDatadriftProperties(databaseID, apiKey string) error {

}
fmt.Println("hasDatadriftProperty:", shouldCreateDatadriftPropertyId)
shouldCreateProperties := shouldCreateDatadriftPropertyId || shouldCreateDatadriftPropertyPeriod || shouldCreateDatadriftPropertyTimeGrain || shouldCreateDatadriftPropertyDriftValue
shouldCreateProperties := shouldCreateDatadriftPropertyId || shouldCreateDatadriftPropertyPeriod || shouldCreateDatadriftPropertyTimeGrain || shouldCreateDatadriftPropertyDriftValue || shouldCreateDatadriftPropertyDimension
if shouldCreateProperties {
params := notion.UpdateDatabaseParams{
Properties: map[string]*notion.DatabaseProperty{},
Expand All @@ -203,6 +217,13 @@ func AssertDatabaseHasDatadriftProperties(databaseID, apiKey string) error {
}
}

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

if shouldCreateDatadriftPropertyTimeGrain {
params.Properties[PROPERTY_DATADRIFT_TIMEGRAIN] = &notion.DatabaseProperty{
Type: notion.DBPropTypeSelect,
Expand Down
2 changes: 1 addition & 1 deletion history/process-history.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func ProcessHistory(client *github.Client, repoOwner string, repoName string, me

periodAndDimensionKey := common.PeriodAndDimensionKey(string(periodKey))
dimension := common.Dimension("none")
dimensionValue := common.DimensionValue("none")
dimensionValue := common.DimensionValue("No dimension")

updateMetric(lineCountAndKPIByDateByVersion, periodAndDimensionKey, timegrain, periodKey, dimension, dimensionValue, record, kpiColumn, commitSha, commitTimestamp, commit, commitMessages)

Expand Down
2 changes: 1 addition & 1 deletion reports/reports.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

func CreateReport(syncConfig common.SyncConfig, KPIInfo common.KPIReport) error {
timeGrain, _ := GetTimeGrain(KPIInfo.PeriodId)
reportNotionPageId, findOrCreateError := notion_database.FindOrCreateReportPageId(syncConfig.NotionAPIKey, syncConfig.NotionDatabaseID, KPIInfo.KPIName, string(KPIInfo.PeriodId), timeGrain)
reportNotionPageId, findOrCreateError := notion_database.FindOrCreateReportPageId(syncConfig.NotionAPIKey, syncConfig.NotionDatabaseID, KPIInfo.KPIName, string(KPIInfo.PeriodId), timeGrain, KPIInfo.DimensionValue)
if findOrCreateError != nil {

return fmt.Errorf("failed to create reportNotionPageId: %v", findOrCreateError.Error())
Expand Down

0 comments on commit 8573903

Please sign in to comment.