Improve bidi sync between notion and db

This commit is contained in:
2024-09-18 10:25:02 +02:00
parent 6280711f77
commit 336485a309

View File

@@ -13,6 +13,7 @@ import (
"log/slog"
"github.com/go-co-op/gocron/v2"
"github.com/jomei/notionapi"
"github.com/spf13/viper"
"github.com/t-liu93/home-automation-backend/util/homeassistantutil"
"github.com/t-liu93/home-automation-backend/util/notion"
@@ -209,6 +210,14 @@ func notionDbSync() {
rowsDbMap[row.Timestamp] = row
}
// 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
for _, rowNotion := range rowsNotion {
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"))
}
// db to notion
counter = 0
func syncDbToNotion(headerId string, tableId string, rowsNotion []notionapi.TableRowBlock) {
counter := 0
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 {
slog.Error(fmt.Sprintln("PooRecorderSyncDb Failed to get db rows", err))
return
@@ -260,8 +270,8 @@ func notionDbSync() {
}
rowsDbSlice = append(rowsDbSlice, row)
}
startFromId := header.GetID().String()
for iNotion, iDb := 0, 0; iNotion < len(rowsNotion) || iDb < len(rowsDbSlice); {
startFromId := headerId
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
notionTime, err := time.ParseInLocation("2006-01-02T15:04", notionTimeStamp, time.Now().Location())
if err != nil {
@@ -284,6 +294,18 @@ func notionDbSync() {
iDb++
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{
dbTimeDate,
dbTimeTime,