Skip to content

Commit

Permalink
Sammyteillet/dat 54 aau i can see the drift in a property of (#26)
Browse files Browse the repository at this point in the history
* feature: add property drift value

* feature: add property drift-value
  • Loading branch information
Samox committed May 10, 2023
1 parent 799451a commit 09f9504
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
22 changes: 20 additions & 2 deletions database/notion_database/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
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"

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

Expand Down Expand Up @@ -150,6 +151,7 @@ func AssertDatabaseHasDatadriftProperties(databaseID, apiKey string) error {
shouldCreateDatadriftPropertyId := true
shouldCreateDatadriftPropertyPeriod := true
shouldCreateDatadriftPropertyTimeGrain := true
shouldCreateDatadriftPropertyDriftValue := true

propertiesToDelete := []string{}

Expand All @@ -164,6 +166,9 @@ func AssertDatabaseHasDatadriftProperties(databaseID, apiKey string) error {
if property.Name == PROPERTY_DATADRIFT_TIMEGRAIN {
shouldCreateDatadriftPropertyTimeGrain = false
}
if property.Name == PROPERTY_DATADRIFT_DRIFT_VALUE {
shouldCreateDatadriftPropertyDriftValue = false
}

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

}
fmt.Println("hasDatadriftProperty:", shouldCreateDatadriftPropertyId)
shouldCreateProperties := shouldCreateDatadriftPropertyId || shouldCreateDatadriftPropertyPeriod || shouldCreateDatadriftPropertyTimeGrain
shouldCreateProperties := shouldCreateDatadriftPropertyId || shouldCreateDatadriftPropertyPeriod || shouldCreateDatadriftPropertyTimeGrain || shouldCreateDatadriftPropertyDriftValue
if shouldCreateProperties {
params := notion.UpdateDatabaseParams{
Properties: map[string]*notion.DatabaseProperty{},
Expand Down Expand Up @@ -213,6 +218,15 @@ func AssertDatabaseHasDatadriftProperties(databaseID, apiKey string) error {
}
}

if shouldCreateDatadriftPropertyDriftValue {
params.Properties[PROPERTY_DATADRIFT_DRIFT_VALUE] = &notion.DatabaseProperty{
Type: notion.DBPropTypeNumber,
Number: &notion.NumberMetadata{
Format: notion.NumberFormatNumberWithCommas,
},
}
}

fmt.Println("Creating property", params)
_, err := client.UpdateDatabase(ctx, databaseID, params)
if err != nil {
Expand Down Expand Up @@ -245,7 +259,7 @@ func AssertDatabaseHasDatadriftProperties(databaseID, apiKey string) error {
return err
}

func UpdateReport(apiKey string, reportNotionPageId string, children []notion.Block) error {
func UpdateReport(apiKey string, reportNotionPageId string, children []notion.Block, pageProperties *notion.DatabasePageProperties) error {
fmt.Println("Updating report", reportNotionPageId)
buf := &bytes.Buffer{}
ctx := context.Background()
Expand All @@ -256,6 +270,10 @@ func UpdateReport(apiKey string, reportNotionPageId string, children []notion.Bl
}
client := notion.NewClient(apiKey, notion.WithHTTPClient(httpClient))

_, updateErr := client.UpdatePage(ctx, reportNotionPageId, notion.UpdatePageParams{DatabasePageProperties: *pageProperties})
if updateErr != nil {
fmt.Println("[DATADRIFT_ERROR]: err during update", updateErr.Error())
}
existingReport, err := client.FindBlockChildrenByID(ctx, reportNotionPageId, &notion.PaginationQuery{PageSize: 100})
if err != nil {
return err
Expand Down
9 changes: 8 additions & 1 deletion reports/reports.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ func CreateReport(syncConfig common.SyncConfig, KPIInfo common.KPIReport) error
reportNotionPageId, _ := notion_database.FindOrCreateReportPageId(syncConfig.NotionAPIKey, syncConfig.NotionDatabaseID, KPIInfo.KPIName, KPIInfo.PeriodId, timeGrain)
fmt.Println(reportNotionPageId)

diffFloat64, _ := KPIInfo.LatestValue.Sub(KPIInfo.InitialValue).Float64()

params := notion.CreatePageParams{
DatabasePageProperties: &notion.DatabasePageProperties{
notion_database.PROPERTY_DATADRIFT_DRIFT_VALUE: notion.DatabasePageProperty{
Number: &diffFloat64,
},
},
Children: []notion.Block{
notion.Heading1Block{
RichText: []notion.RichText{
Expand Down Expand Up @@ -193,7 +200,7 @@ func CreateReport(syncConfig common.SyncConfig, KPIInfo common.KPIReport) error
}
params.Children = append(params.Children, children...)

err := notion_database.UpdateReport(syncConfig.NotionAPIKey, reportNotionPageId, params.Children)
err := notion_database.UpdateReport(syncConfig.NotionAPIKey, reportNotionPageId, params.Children, params.DatabasePageProperties)
if err != nil {
return fmt.Errorf("failed to create page: %v", err)
}
Expand Down

0 comments on commit 09f9504

Please sign in to comment.