Improve bidi sync between notion and db
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
|
|
||||||
"github.com/go-co-op/gocron/v2"
|
"github.com/go-co-op/gocron/v2"
|
||||||
|
"github.com/jomei/notionapi"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"github.com/t-liu93/home-automation-backend/util/homeassistantutil"
|
"github.com/t-liu93/home-automation-backend/util/homeassistantutil"
|
||||||
"github.com/t-liu93/home-automation-backend/util/notion"
|
"github.com/t-liu93/home-automation-backend/util/notion"
|
||||||
@@ -209,6 +210,14 @@ func notionDbSync() {
|
|||||||
rowsDbMap[row.Timestamp] = row
|
rowsDbMap[row.Timestamp] = row
|
||||||
}
|
}
|
||||||
// notion to db
|
// notion to db
|
||||||
|
syncNotionToDb(rowsNotion, rowsDbMap)
|
||||||
|
|
||||||
|
// db to notion
|
||||||
|
syncDbToNotion(header.GetID().String(), tableId, rowsNotion)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func syncNotionToDb(rowsNotion []notionapi.TableRowBlock, rowsDbMap map[string]pooStatusDbEntry) {
|
||||||
counter := 0
|
counter := 0
|
||||||
for _, rowNotion := range rowsNotion {
|
for _, rowNotion := range rowsNotion {
|
||||||
rowNotionTimestamp := rowNotion.TableRow.Cells[0][0].PlainText + "T" + rowNotion.TableRow.Cells[1][0].PlainText
|
rowNotionTimestamp := rowNotion.TableRow.Cells[0][0].PlainText + "T" + rowNotion.TableRow.Cells[1][0].PlainText
|
||||||
@@ -241,11 +250,12 @@ func notionDbSync() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
slog.Info(fmt.Sprintln("PooRecorderSyncDb Inserted", counter, "new rows from Notion to DB"))
|
slog.Info(fmt.Sprintln("PooRecorderSyncDb Inserted", counter, "new rows from Notion to DB"))
|
||||||
|
}
|
||||||
|
|
||||||
// db to notion
|
func syncDbToNotion(headerId string, tableId string, rowsNotion []notionapi.TableRowBlock) {
|
||||||
counter = 0
|
counter := 0
|
||||||
var rowsDbSlice []pooStatusDbEntry
|
var rowsDbSlice []pooStatusDbEntry
|
||||||
rowsDb, err = db.Query(`SELECT * FROM poo_records ORDER BY timestamp DESC`)
|
rowsDb, err := db.Query(`SELECT * FROM poo_records ORDER BY timestamp DESC`)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error(fmt.Sprintln("PooRecorderSyncDb Failed to get db rows", err))
|
slog.Error(fmt.Sprintln("PooRecorderSyncDb Failed to get db rows", err))
|
||||||
return
|
return
|
||||||
@@ -260,8 +270,8 @@ func notionDbSync() {
|
|||||||
}
|
}
|
||||||
rowsDbSlice = append(rowsDbSlice, row)
|
rowsDbSlice = append(rowsDbSlice, row)
|
||||||
}
|
}
|
||||||
startFromId := header.GetID().String()
|
startFromId := headerId
|
||||||
for iNotion, iDb := 0, 0; iNotion < len(rowsNotion) || iDb < len(rowsDbSlice); {
|
for iNotion, iDb := 0, 0; iNotion < len(rowsNotion) && iDb < len(rowsDbSlice); {
|
||||||
notionTimeStamp := rowsNotion[iNotion].TableRow.Cells[0][0].PlainText + "T" + rowsNotion[iNotion].TableRow.Cells[1][0].PlainText
|
notionTimeStamp := rowsNotion[iNotion].TableRow.Cells[0][0].PlainText + "T" + rowsNotion[iNotion].TableRow.Cells[1][0].PlainText
|
||||||
notionTime, err := time.ParseInLocation("2006-01-02T15:04", notionTimeStamp, time.Now().Location())
|
notionTime, err := time.ParseInLocation("2006-01-02T15:04", notionTimeStamp, time.Now().Location())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -284,6 +294,18 @@ func notionDbSync() {
|
|||||||
iDb++
|
iDb++
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if iNotion != len(rowsNotion)-1 {
|
||||||
|
notionNextTimeStamp := rowsNotion[iNotion+1].TableRow.Cells[0][0].PlainText + "T" + rowsNotion[iNotion+1].TableRow.Cells[1][0].PlainText
|
||||||
|
notionNextTime, err := time.ParseInLocation("2006-01-02T15:04", notionNextTimeStamp, time.Now().Location())
|
||||||
|
if err != nil {
|
||||||
|
slog.Warn(fmt.Sprintln("PooRecorderSyncDb Failed to parse next notion timestamp", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if notionNextTime.After(notionTime) {
|
||||||
|
slog.Error(fmt.Sprintf("PooRecorderSyncDb Notion timestamp %s is after next timestamp %s, checking, aborting", notionTimeStamp, notionNextTimeStamp))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
id, err := notion.WriteTableRow([]string{
|
id, err := notion.WriteTableRow([]string{
|
||||||
dbTimeDate,
|
dbTimeDate,
|
||||||
dbTimeTime,
|
dbTimeTime,
|
||||||
|
|||||||
Reference in New Issue
Block a user