Skip to content

Commit

Permalink
feat(*): add the table of categories/completions per test to the report.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thibaut-Padok committed Jan 8, 2023
1 parent b0038c3 commit 8e8e738
Show file tree
Hide file tree
Showing 5 changed files with 236 additions and 13 deletions.
12 changes: 7 additions & 5 deletions ReportPage.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import "github.com/jomei/notionapi"
import (
"github.com/jomei/notionapi"
)

func createReportPageRequest(client *notionapi.Client, db_id string) notionapi.PageCreateRequest {
notionTitle := notionapi.Text{
Expand All @@ -10,7 +12,7 @@ func createReportPageRequest(client *notionapi.Client, db_id string) notionapi.P
Content: "YATAS !!",
}
notionPagetext := notionapi.Text{
Content: "This is an new YATAS report, enjoy ! 😉",
Content: "This is an new YATAS report.",
}
pageYatasCreateRequest := notionapi.PageCreateRequest{
Parent: notionapi.Parent{
Expand All @@ -31,12 +33,12 @@ func createReportPageRequest(client *notionapi.Client, db_id string) notionapi.P
},

Children: []notionapi.Block{
notionapi.Heading3Block{
notionapi.Heading2Block{
BasicBlock: notionapi.BasicBlock{
Type: notionapi.BlockType("heading_3"),
Type: notionapi.BlockType("heading_2"),
Object: notionapi.ObjectType("block"),
},
Heading3: notionapi.Heading{
Heading2: notionapi.Heading{
RichText: []notionapi.RichText{
{
Text: &notionPageHeader,
Expand Down
29 changes: 29 additions & 0 deletions ReportUpdate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"context"
"log"

"github.com/jomei/notionapi"
"github.com/stangirard/yatas/plugins/commons"
)

func addCategoriesTable(client *notionapi.Client, blockID notionapi.BlockID, test commons.Tests) {
updateRequest := updatePageRequestWithCategories(test)
blocks, err := client.Block.AppendChildren(context.Background(), blockID, &updateRequest)
if err != nil {
log.Printf("Error while triing to add categories table ... %v", err)
} else {
log.Printf("New blocks added ... %v", blocks)
}
}

func addTitleTable(client *notionapi.Client, blockID notionapi.BlockID, test commons.Tests) {
updateRequest := updatePageRequestWithTitle(test)
blocks, err := client.Block.AppendChildren(context.Background(), blockID, &updateRequest)
if err != nil {
log.Printf("Error while triing to add a title ... %v", err)
} else {
log.Printf("New Title added ... %v", blocks)
}
}
124 changes: 124 additions & 0 deletions categoriesBlock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package main

import (
"strconv"

"github.com/jomei/notionapi"
"github.com/stangirard/yatas/plugins/commons"
)

func CalculatePercent(success int, failure int) string {
total := success + failure
if total == 0 {
return "0"
}
return strconv.Itoa((success * 100) / total)
}

func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}

func createCategoriesBlock(test commons.Tests) notionapi.Block {
// Find the categories
categories := []string{}
categoriesSuccess := map[string]int{}
categoriesFailure := map[string]int{}
for _, check := range test.Checks {
for _, category := range check.Categories {
if !contains(categories, category) {
categories = append(categories, category)
categoriesSuccess[category] = 0
categoriesFailure[category] = 0
}
if check.Status == "OK" {
categoriesSuccess[category]++
} else {
categoriesFailure[category]++
}
}
}
// Calculate the completion scores
scores := []string{}
for _, category := range categories {
scores = append(scores, CalculatePercent(categoriesSuccess[category], categoriesFailure[category])+" %")
}
// Write the categories
block := createCategorieTable(categories, scores)
return block
}

func createCategorieTable(categories, scores []string) notionapi.Block {
table := notionapi.TableBlock{
BasicBlock: notionapi.BasicBlock{
Type: notionapi.BlockType("table"),
Object: notionapi.ObjectType("block"),
},
Table: notionapi.Table{
TableWidth: 2,
HasColumnHeader: true,
Children: createCategorieTableRows(categories, scores),
},
}
return table
}

func createCategorieTableRows(categories, scores []string) []notionapi.Block {
rows := []notionapi.Block{}
rows = append(rows, createCategorieTableHeader())
for key, category := range categories {
score := scores[key]
r := createCategorieTableRow(category, score)
rows = append(rows, r)
}
return rows
}

func createCategorieTableHeader() notionapi.TableRowBlock {
category := notionapi.Text{
Content: "Category",
}
completion := notionapi.Text{
Content: "Completion",
}
row := notionapi.TableRowBlock{
BasicBlock: notionapi.BasicBlock{
Type: notionapi.BlockType("table_row"),
Object: notionapi.ObjectType("block"),
},
TableRow: notionapi.TableRow{
Cells: [][]notionapi.RichText{
{{Text: &category}},
{{Text: &completion}},
},
},
}
return row
}

func createCategorieTableRow(category, score string) notionapi.TableRowBlock {
cat := notionapi.Text{
Content: category,
}
completion := notionapi.Text{
Content: score,
}
row := notionapi.TableRowBlock{
BasicBlock: notionapi.BasicBlock{
Type: notionapi.BlockType("table_row"),
Object: notionapi.ObjectType("block"),
},
TableRow: notionapi.TableRow{
Cells: [][]notionapi.RichText{
{{Text: &cat}},
{{Text: &completion}},
},
},
}
return row
}
21 changes: 13 additions & 8 deletions export.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"log"

"github.com/jomei/notionapi"
"github.com/stangirard/yatas/plugins/commons"
)

Expand All @@ -20,19 +21,23 @@ func CreateNotionReport(tests []commons.Tests, account NotionAccount, client *No
if err != nil {
log.Printf("Error during the creation of the Yamas page ... %v", err)
} else {
for _, test := range tests {
// Add a separator and a title for each 'Test'
addTitleTable(defaultClient, notionapi.BlockID(page.ID), test)

// Create the database inside the yatas report page
dbCreate := createInlineDatabase(page.ID.String())
db, err := clientV1.Database.Create(context.Background(), &dbCreate)
if err != nil {
log.Printf("Error during the creation of the database ... %v", err)
} else {
// Create a new inline database for each 'Test'
dbCreate := createInlineDatabase(page.ID.String())
db, err := clientV1.Database.Create(context.Background(), &dbCreate)
if err != nil {
log.Printf("Error during the creation of the database ... %v", err)
} else {

// Create a new page for each test
for _, test := range tests {
for _, check := range test.Checks {
// Create a new page in the inline database for each 'Check'
createPageCheck(client, check, db.ID.String())
}
// Create a new Categories table for each 'Test'
addCategoriesTable(defaultClient, notionapi.BlockID(page.ID), test)
}
}
// Try to lock page if notionapi/v3 available
Expand Down
63 changes: 63 additions & 0 deletions pageUpdateRequest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package main

import (
"github.com/jomei/notionapi"
"github.com/stangirard/yatas/plugins/commons"
)

func updatePageRequestWithTitle(test commons.Tests) notionapi.AppendBlockChildrenRequest {
categoryTitle := notionapi.Text{
Content: test.Account,
}
request := notionapi.AppendBlockChildrenRequest{
Children: []notionapi.Block{
notionapi.DividerBlock{
BasicBlock: notionapi.BasicBlock{
Type: notionapi.BlockType("divider"),
Object: notionapi.ObjectType("block"),
},
Divider: notionapi.Divider{},
},
notionapi.Heading2Block{
BasicBlock: notionapi.BasicBlock{
Type: notionapi.BlockType("heading_2"),
Object: notionapi.ObjectType("block"),
},
Heading2: notionapi.Heading{
RichText: []notionapi.RichText{
{
Text: &categoryTitle,
},
},
},
},
},
}
return request
}

func updatePageRequestWithCategories(test commons.Tests) notionapi.AppendBlockChildrenRequest {
categoryTitle := notionapi.Text{
Content: "Categories",
}
categoriesBlock := createCategoriesBlock(test)
request := notionapi.AppendBlockChildrenRequest{
Children: []notionapi.Block{
notionapi.Heading2Block{
BasicBlock: notionapi.BasicBlock{
Type: notionapi.BlockType("heading_2"),
Object: notionapi.ObjectType("block"),
},
Heading2: notionapi.Heading{
RichText: []notionapi.RichText{
{
Text: &categoryTitle,
},
},
},
},
categoriesBlock,
},
}
return request
}

0 comments on commit 8e8e738

Please sign in to comment.