| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | |
| 6 | "gorm.io/gorm" |
| 7 | |
| 8 | "amadan.net/oficina/mapa/internal/mapa/notionmd" |
| 9 | "amadan.net/oficina/notion.git/notion" |
| 10 | ) |
| 11 | |
| 12 | // Survey (Notion import spec §8.1, D6): walk Notion from the roots and |
| 13 | // stage each page's tree, sealed, on its map row. Every write is |
| 14 | // bookkeeping, lease-checked. A resumed survey walks the pages already |
| 15 | // staged in this run from their staged trees, asking Notion nothing for |
| 16 | // them (spec §8.3). |
| 17 | |
| 18 | // mapSeen is a row whose survey has begun in this run: it is written |
| 19 | // before the page is fetched, so a page that fails has somewhere to be |
| 20 | // named (memoria §9). |
| 21 | const mapSeen = "seen" |
| 22 | |
| 23 | func (job *importJob) survey(ctx context.Context) error { |
| 24 | search, err := job.client.Search(ctx) |
| 25 | if err != nil { |
| 26 | return err |
| 27 | } |
| 28 | if err := job.recordSearch(ctx, search); err != nil { |
| 29 | return err |
| 30 | } |
| 31 | seq := 0 |
| 32 | err = notion.Walk(ctx, search, func(ctx context.Context, |
| 33 | ref notion.Ref) ([]notion.Ref, error) { |
| 34 | seq++ |
| 35 | return job.visit(ctx, ref, seq) |
| 36 | }) |
| 37 | if err != nil { |
| 38 | return err |
| 39 | } |
| 40 | if err := crashPoint("survey:walked"); err != nil { |
| 41 | return err |
| 42 | } |
| 43 | return job.surveyed(ctx) |
| 44 | } |
| 45 | |
| 46 | // recordSearch keeps what phase 1 does not import: databases, named for |
| 47 | // the report, and rows shared directly, counted (spec §8.1). |
| 48 | func (job *importJob) recordSearch(ctx context.Context, |
| 49 | search notion.Search) error { |
| 50 | for _, kind := range []struct { |
| 51 | state string |
| 52 | refs []notion.Ref |
| 53 | }{{mapDatabase, search.Databases}, {mapRow, search.Rows}} { |
| 54 | for _, ref := range kind.refs { |
| 55 | place := coordinate{NotionID: ref.ID, Name: ref.Title} |
| 56 | if err := job.mark(ctx, ref.ID, kind.state, place); err != nil { |
| 57 | return err |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | return nil |
| 62 | } |
| 63 | |
| 64 | // mark records a row that is not a page this run imports. |
| 65 | func (job *importJob) mark(ctx context.Context, id, state string, |
| 66 | place coordinate) error { |
| 67 | return job.store.bookkeep(ctx, job.lease, func(db *gorm.DB) error { |
| 68 | row, found, err := job.store.mapRowOf(db, id) |
| 69 | if err != nil { |
| 70 | return err |
| 71 | } |
| 72 | if found { |
| 73 | old, err := job.store.openCoordinate(row) |
| 74 | if err != nil || old.mapped(row) { |
| 75 | return err // a page now in a database stays mapped |
| 76 | } |
| 77 | } |
| 78 | sealed, err := job.store.sealCoordinate(place) |
| 79 | if err != nil { |
| 80 | return err |
| 81 | } |
| 82 | row.State, row.SeenRun = state, job.lease.run |
| 83 | row.SealedCoordinate = sealed |
| 84 | return saveRow(db, &row) |
| 85 | }) |
| 86 | } |
| 87 | |
| 88 | // visit surveys one page and answers the child pages in its blocks. |
| 89 | func (job *importJob) visit(ctx context.Context, ref notion.Ref, |
| 90 | seq int) ([]notion.Ref, error) { |
| 91 | row, err := job.begin(ctx, ref.ID) |
| 92 | if err != nil { |
| 93 | return nil, err |
| 94 | } |
| 95 | if row.SurveyedRun == job.lease.run { |
| 96 | return job.replay(row) |
| 97 | } |
| 98 | page, err := job.client.Page(ctx, ref.ID) |
| 99 | if err != nil { |
| 100 | return nil, job.surveyFailed(ctx, row, err) |
| 101 | } |
| 102 | if page.InTrash { |
| 103 | return nil, job.setState(ctx, row, mapTrashed, "") |
| 104 | } |
| 105 | blocks, err := job.client.Blocks(ctx, ref.ID) |
| 106 | if err != nil { |
| 107 | return nil, job.surveyFailed(ctx, row, err) |
| 108 | } |
| 109 | staged := stagedPage(page, blocks, ref.Parent, seq) |
| 110 | if err := crashPoint("survey:read"); err != nil { |
| 111 | return nil, err |
| 112 | } |
| 113 | if err := job.stage(ctx, row, staged, ref.UnderRow); err != nil { |
| 114 | return nil, err |
| 115 | } |
| 116 | return notion.ChildPages(ref.ID, blocks), nil |
| 117 | } |
| 118 | |
| 119 | // begin writes the page's row before anything is fetched, and answers |
| 120 | // it as it was. |
| 121 | func (job *importJob) begin(ctx context.Context, id string) (NotionPage, |
| 122 | error) { |
| 123 | var row NotionPage |
| 124 | err := job.store.bookkeep(ctx, job.lease, func(db *gorm.DB) error { |
| 125 | var found bool |
| 126 | var err error |
| 127 | if row, found, err = job.store.mapRowOf(db, id); err != nil { |
| 128 | return err |
| 129 | } |
| 130 | if !found { |
| 131 | row.State = mapSeen |
| 132 | sealed, err := job.store.sealCoordinate(coordinate{NotionID: id}) |
| 133 | if err != nil { |
| 134 | return err |
| 135 | } |
| 136 | row.SealedCoordinate = sealed |
| 137 | } |
| 138 | row.SeenRun = job.lease.run |
| 139 | return saveRow(db, &row) |
| 140 | }) |
| 141 | return row, err |
| 142 | } |
| 143 | |
| 144 | // replay is a page already decided in this run: its staged tree's |
| 145 | // children, or none for a page that failed or was trashed. |
| 146 | func (job *importJob) replay(row NotionPage) ([]notion.Ref, error) { |
| 147 | if len(row.SealedStaged) == 0 { |
| 148 | return nil, nil |
| 149 | } |
| 150 | staged, err := job.store.openStaged(row) |
| 151 | if err != nil { |
| 152 | return nil, err |
| 153 | } |
| 154 | return childPages(staged), nil |
| 155 | } |
| 156 | |
| 157 | // surveyFailed records a page that could not be read, and goes on: the |
| 158 | // pages beneath it are "could not be determined". An error that is the |
| 159 | // whole run's (the token refused, Notion refusing us, a pin) stops it. |
| 160 | func (job *importJob) surveyFailed(ctx context.Context, row NotionPage, |
| 161 | err error) error { |
| 162 | if notion.Final(err) || ctx.Err() != nil { |
| 163 | return err |
| 164 | } |
| 165 | return job.setState(ctx, row, mapFailed, string(notion.CodeOf(err))) |
| 166 | } |
| 167 | |
| 168 | // setState decides a row for this run without staging a tree. |
| 169 | func (job *importJob) setState(ctx context.Context, row NotionPage, |
| 170 | state, code string) error { |
| 171 | if row.State == mapFinished { |
| 172 | // A finished import's page that this import cannot read: it |
| 173 | // is not this import's page (§25.9; Fable L9). |
| 174 | place, err := job.store.openCoordinate(row) |
| 175 | if err != nil { |
| 176 | return err |
| 177 | } |
| 178 | if row.SealedCoordinate, err = job.store.sealCoordinate( |
| 179 | coordinate{NotionID: place.NotionID}); err != nil { |
| 180 | return err |
| 181 | } |
| 182 | } |
| 183 | return job.store.bookkeep(ctx, job.lease, func(db *gorm.DB) error { |
| 184 | row.State, row.ErrorCode = state, code |
| 185 | row.SurveyedRun, row.SealedStaged = job.lease.run, nil |
| 186 | if err := saveRow(db, &row); err != nil || state != mapFailed { |
| 187 | return err |
| 188 | } |
| 189 | return db.Model(&NotionRun{}).Where("id = ?", job.lease.run). |
| 190 | Update("failed", gorm.Expr("failed + 1")).Error |
| 191 | }) |
| 192 | } |
| 193 | |
| 194 | // stage seals a page's tree on its row, and counts it. |
| 195 | func (job *importJob) stage(ctx context.Context, row NotionPage, |
| 196 | staged notionmd.Page, underRow bool) error { |
| 197 | sealed, err := job.store.sealStaged(staged) |
| 198 | if err != nil { |
| 199 | return err |
| 200 | } |
| 201 | place, err := job.store.openCoordinate(row) |
| 202 | if err != nil { |
| 203 | return err |
| 204 | } |
| 205 | if row.State == mapFinished { |
| 206 | // A finished import's page: a new import does not know it. |
| 207 | place = coordinate{NotionID: place.NotionID} |
| 208 | } |
| 209 | place.Parent, place.UnderRow = staged.Parent, underRow |
| 210 | sealedPlace, err := job.store.sealCoordinate(place) |
| 211 | if err != nil { |
| 212 | return err |
| 213 | } |
| 214 | return job.store.bookkeep(ctx, job.lease, func(db *gorm.DB) error { |
| 215 | row.State, row.ErrorCode, row.SurveyedRun = mapStaged, "", |
| 216 | job.lease.run |
| 217 | row.SealedStaged, row.SealedCoordinate = sealed, sealedPlace |
| 218 | // saveRow writes the coordinate too. |
| 219 | if err := saveRow(db, &row); err != nil { |
| 220 | return err |
| 221 | } |
| 222 | return db.Model(&NotionRun{}).Where("id = ?", job.lease.run). |
| 223 | Updates(map[string]any{"surveyed": gorm.Expr("surveyed + 1"), |
| 224 | "progress_at": job.store.now().UTC()}).Error |
| 225 | }) |
| 226 | } |
| 227 | |
| 228 | // surveyed ends the survey: the total is known now, and the run writes. |
| 229 | func (job *importJob) surveyed(ctx context.Context) error { |
| 230 | return job.store.bookkeep(ctx, job.lease, func(db *gorm.DB) error { |
| 231 | var total int64 |
| 232 | err := db.Model(&NotionPage{}).Where("surveyed_run = ? AND "+ |
| 233 | "sealed_staged IS NOT NULL", job.lease.run).Count(&total).Error |
| 234 | if err != nil { |
| 235 | return err |
| 236 | } |
| 237 | return db.Model(&NotionRun{}).Where("id = ?", job.lease.run). |
| 238 | Updates(map[string]any{"total": total, |
| 239 | "state": runWriting}).Error |
| 240 | }) |
| 241 | } |
| 242 | |