-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
flashback.go
181 lines (149 loc) · 4.77 KB
/
flashback.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"context"
"errors"
"fmt"
"log"
"math/rand"
"os"
"time"
"github.com/dstotijn/go-notion"
)
type FlashbackConfig struct {
DatabaseID string `yaml:"databaseID"`
DatabaseQuery string `yaml:"databaseQuery"`
OldestTimestamp time.Time `yaml:"oldestTimestamp"` // Format time.RFC3339 2006-01-02T15:04:05Z07:00
FlashbackNum int `yaml:"flashbackNum"` // Number of flashback entries
FlashbackPageID string `yaml:"flashbackPageID"` // Page to write the flashback
FlashbackJournalID string `yaml:"flashbackJournalID"` // Use daily journal database ID, this will overwrite FlashbackPageID
FlashbackTextBlock string `yaml:"flashbackTextBlock"` // Format https://pkg.go.dev/github.com/dstotijn/go-notion#ParagraphBlock
FlashbackChainFile string `yaml:"flashbackChainFile"` // Filename for chain with LLM cmd
}
type Flashback struct {
DebugMode bool
Client *notion.Client
FlashbackConfig
}
func (f *Flashback) Validate() error {
if f.FlashbackPageID == "" && f.FlashbackJournalID == "" {
return errors.Join(ErrConfigRequired, fmt.Errorf("set flashbackPageID or flashbackJournalID"))
}
if f.FlashbackTextBlock == "" {
return errors.Join(ErrConfigRequired, fmt.Errorf("set flashbackTextBlock"))
}
return nil
}
func (f *Flashback) Run() error {
f.SetFlashbackPageID()
maxHours := int(time.Since(f.OldestTimestamp).Hours())
// use a random hour to lookback
lookbackHour := rand.Intn(maxHours)
pages, err := f.GetPages(time.Duration(lookbackHour) * time.Hour)
if err != nil {
return err
}
log.Printf("Lookback %v Hours/%v Day, Queried pages: %+v", lookbackHour, lookbackHour/24, len(pages))
if len(pages) < 1 { // try again with max hours
lookbackHour = maxHours
pages, err = f.GetPages(time.Duration(lookbackHour) * time.Hour)
if err != nil {
return err
}
log.Printf("Lookback (max) %v Hours/%v Day, Queried pages: %+v", lookbackHour, lookbackHour/24, len(pages))
}
if len(pages) < 1 { // give up
log.Printf("Skipped. no pages fetched")
return nil
}
if f.FlashbackNum < 1 {
f.FlashbackNum = 1
} else if f.FlashbackNum > len(pages) {
f.FlashbackNum = len(pages)
}
picked := map[int]struct{}{}
for i := 0; i < f.FlashbackNum; i++ {
n := rand.Intn(len(pages))
for {
if _, found := picked[n]; found {
n = (n + 1) % len(pages)
} else {
picked[n] = struct{}{}
break
}
}
}
for n := range picked { // write out block
if block, err := f.WriteBlock(pages[n].ID); err == nil {
if len(block.Results) > 0 {
log.Printf("Append block child %v", block.Results[0].ID())
}
}
}
if f.FlashbackChainFile != "" { // write out chain file
file, err := os.Create(f.FlashbackChainFile)
if err != nil {
return fmt.Errorf("create file: %v, err: %v", f.FlashbackChainFile, err)
}
defer file.Close()
for n := range picked {
if _, err = file.WriteString(pages[n].ID + "\n"); err != nil {
log.Printf("Failed writing to file, err: %v", err)
}
}
}
return nil
}
func (f *Flashback) GetPages(lookback time.Duration) ([]notion.Page, error) {
q := NewDatabaseQuery(f.Client, f.DatabaseID)
if err := q.SetQuery(f.DatabaseQuery, QueryBuilder{
Date: time.Now().Add(-lookback).Format(layoutDate),
Today: time.Now().Format(layoutDate),
}); err != nil {
log.Panicf("Invalid query: %v, err: %v", f.DatabaseQuery, err)
}
if f.DebugMode {
log.Printf("DatabaseQuery Filter: %+v", q.Query.Filter)
log.Printf("DatabaseQuery Sorter: %+v", q.Query.Sorts)
}
return q.Once(context.TODO())
}
func (f *Flashback) SetFlashbackPageID() {
if f.FlashbackJournalID == "" {
return
}
now := time.Now()
title := now.Format(layoutDate)
q := NewDatabaseQuery(f.Client, f.FlashbackJournalID)
q.Query = ¬ion.DatabaseQuery{
Filter: ¬ion.DatabaseQueryFilter{
Property: "title",
DatabaseQueryPropertyFilter: notion.DatabaseQueryPropertyFilter{
Title: ¬ion.TextPropertyFilter{Equals: title},
},
},
Sorts: []notion.DatabaseQuerySort{
{Timestamp: notion.SortTimeStampCreatedTime, Direction: notion.SortDirAsc},
},
}
pages, err := q.Once(context.TODO())
if err != nil {
log.Panicf("No journal found: %v, err: %v", title, err)
}
if len(pages) > 1 {
log.Printf("Multiple journal found: %v, cnt: %v, uses: %v", title, len(pages), pages[0].ID)
}
if f.DebugMode {
log.Printf("Journal by title: %v, found: %v, uses: %v", title, len(pages), pages[0].ID)
}
f.FlashbackPageID = pages[0].ID
}
func (f *Flashback) WriteBlock(pageID string) (notion.BlockChildrenResponse, error) {
w := NewAppendBlock(f.Client, f.FlashbackPageID)
if err := w.AddParagraph("Flashback", f.FlashbackTextBlock, BlockBuilder{
Date: time.Now().Format(layoutDate),
PageID: pageID,
}); err != nil {
return notion.BlockChildrenResponse{}, err
}
return w.Do(context.TODO())
}