| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "sort" |
| 6 | "strings" |
| 7 | |
| 8 | "amadan.net/oficina/mapa/internal/mapa/notionmd" |
| 9 | ) |
| 10 | |
| 11 | // The run's report (Notion import spec §8.5), for importers only. It |
| 12 | // unseals titles to show them. A count that could not be determined |
| 13 | // says so; it never renders as 0. |
| 14 | |
| 15 | // reportView is templates/import.html's report. |
| 16 | type reportView struct { |
| 17 | // SurveyDone is whether the survey finished: absence is reported |
| 18 | // only after a complete one (spec §13.4). |
| 19 | SurveyDone bool |
| 20 | Seen, Created, Updated, Renamed, Unchanged int |
| 21 | KeptEdited, KeptLinks, Gone, NotFound int |
| 22 | Undetermined, Failed, Rows int |
| 23 | Roots, Databases []string |
| 24 | FailedCodes []countView |
| 25 | Images imageCounts |
| 26 | ImageFailures []countView |
| 27 | // Icons and Covers are the pages' looks imported, and IconFailures |
| 28 | // and CoverFailures why the rest were not, by reason (columns and |
| 29 | // icons spec §6.1). |
| 30 | Icons, Covers int |
| 31 | IconFailures, CoverFailures []countView |
| 32 | Losses []countView |
| 33 | Fallbacks, Neutralised, Unshared, Notion int |
| 34 | Fitted, Kept, FailedPages, NotFoundPages []reportPage |
| 35 | Placeholders, UnderRow []reportPage |
| 36 | // Pinned and Unpinned are the roots this run offered to the |
| 37 | // runner's sidebar, read from their sealed pin marks (columns and |
| 38 | // icons spec §6.3): pinned, and those that found no free slot. |
| 39 | Pinned, Offered int |
| 40 | Unpinned []reportPage |
| 41 | // PinLimit is how many pins a sidebar holds. |
| 42 | PinLimit int |
| 43 | } |
| 44 | |
| 45 | type countView struct { |
| 46 | Label string |
| 47 | Count int |
| 48 | } |
| 49 | |
| 50 | type reportPage struct{ Title, Href string } |
| 51 | |
| 52 | // reportRow is one map row, opened. |
| 53 | type reportRow struct { |
| 54 | row NotionPage |
| 55 | place coordinate |
| 56 | } |
| 57 | |
| 58 | func (store *Store) report(ctx context.Context, |
| 59 | run NotionRun) (*reportView, error) { |
| 60 | var rows []NotionPage |
| 61 | if err := store.database.WithContext(ctx).Order("id"). |
| 62 | Find(&rows).Error; err != nil { |
| 63 | return nil, err |
| 64 | } |
| 65 | view := &reportView{SurveyDone: run.Total > 0 || |
| 66 | run.State == runDone || run.State == runWriting, |
| 67 | Images: imageCounts{Failed: map[string]int{}}, PinLimit: maxPins} |
| 68 | tally := &reportTally{view: view, store: store, ctx: ctx, run: run.ID, |
| 69 | losses: map[string]int{}, codes: map[string]int{}, |
| 70 | icons: map[string]int{}, covers: map[string]int{}, |
| 71 | byKey: map[string]reportRow{}} |
| 72 | opened := make([]reportRow, 0, len(rows)) |
| 73 | for _, row := range rows { |
| 74 | place, err := store.openCoordinate(row) |
| 75 | if err != nil { |
| 76 | return nil, err |
| 77 | } |
| 78 | opened = append(opened, reportRow{row, place}) |
| 79 | tally.byKey[notionKey(place.NotionID)] = opened[len(opened)-1] |
| 80 | } |
| 81 | for _, entry := range opened { |
| 82 | tally.add(entry) |
| 83 | } |
| 84 | tally.finish() |
| 85 | return view, nil |
| 86 | } |
| 87 | |
| 88 | // reportTally adds rows up. |
| 89 | type reportTally struct { |
| 90 | view *reportView |
| 91 | store *Store |
| 92 | ctx context.Context |
| 93 | run int64 |
| 94 | losses map[string]int |
| 95 | codes map[string]int |
| 96 | // icons and covers count the looks not imported, by reason. |
| 97 | icons, covers map[string]int |
| 98 | byKey map[string]reportRow |
| 99 | } |
| 100 | |
| 101 | func (tally *reportTally) add(entry reportRow) { |
| 102 | row, place := entry.row, entry.place |
| 103 | switch row.State { |
| 104 | case mapDatabase: |
| 105 | tally.view.Databases = append(tally.view.Databases, place.Name) |
| 106 | return |
| 107 | case mapRow: |
| 108 | tally.view.Rows++ |
| 109 | return |
| 110 | case mapWorkspace: |
| 111 | return |
| 112 | } |
| 113 | if row.SeenRun != tally.run { |
| 114 | tally.absent(entry) |
| 115 | return |
| 116 | } |
| 117 | tally.view.Seen++ |
| 118 | if place.Parent == "" && row.SurveyedRun == tally.run { |
| 119 | tally.view.Roots = append(tally.view.Roots, place.Title) |
| 120 | } |
| 121 | if row.SurveyedRun == tally.run { |
| 122 | tally.outcome(entry) |
| 123 | } |
| 124 | if mark := place.Pin; mark != nil && mark.Run == tally.run { |
| 125 | tally.view.Offered++ |
| 126 | if mark.Pinned { |
| 127 | tally.view.Pinned++ |
| 128 | } else { |
| 129 | tally.view.Unpinned = append(tally.view.Unpinned, |
| 130 | tally.page(place)) |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // outcome counts what this run did with a page. |
| 136 | func (tally *reportTally) outcome(entry reportRow) { |
| 137 | view, place := tally.view, entry.place |
| 138 | page := tally.page(place) |
| 139 | switch entry.row.State { |
| 140 | case mapCreated: |
| 141 | view.Created++ |
| 142 | case mapUpdated: |
| 143 | view.Updated++ |
| 144 | if place.Renamed { |
| 145 | view.Renamed++ |
| 146 | } |
| 147 | case mapUnchanged: |
| 148 | view.Unchanged++ |
| 149 | case mapGone: |
| 150 | view.Gone++ |
| 151 | case mapKept: |
| 152 | view.Kept = append(view.Kept, page) |
| 153 | if place.Kept == "links" { |
| 154 | view.KeptLinks++ |
| 155 | } else { |
| 156 | view.KeptEdited++ |
| 157 | } |
| 158 | case mapFailed: |
| 159 | view.Failed++ |
| 160 | tally.codes[entry.row.ErrorCode]++ |
| 161 | view.FailedPages = append(view.FailedPages, page) |
| 162 | } |
| 163 | if place.Fitted { |
| 164 | view.Fitted = append(view.Fitted, page) |
| 165 | } |
| 166 | if place.UnderRow { |
| 167 | view.UnderRow = append(view.UnderRow, page) |
| 168 | } |
| 169 | tally.render(place, page) |
| 170 | } |
| 171 | |
| 172 | // render adds a page's last render: its losses, its images, its links. |
| 173 | func (tally *reportTally) render(place coordinate, page reportPage) { |
| 174 | view := tally.view |
| 175 | if images := place.Images; images != nil { |
| 176 | view.Images.Imported += images.Imported |
| 177 | view.Images.Links += images.Links |
| 178 | for reason, count := range images.Failed { |
| 179 | view.Images.Failed[reason] += count |
| 180 | } |
| 181 | tally.look(images.Icon, &view.Icons, tally.icons) |
| 182 | tally.look(images.Cover, &view.Covers, tally.covers) |
| 183 | } |
| 184 | result := place.Render |
| 185 | if result == nil { |
| 186 | return |
| 187 | } |
| 188 | for kind, count := range result.Losses { |
| 189 | // Unshared links have their own row: Unresolved counts them. |
| 190 | if kind != notionmd.LossUnshared { |
| 191 | tally.losses[kind] += count |
| 192 | } |
| 193 | } |
| 194 | view.Fallbacks += result.Fallbacks |
| 195 | view.Neutralised += result.Neutralised |
| 196 | view.Unshared += len(result.Unresolved) |
| 197 | view.Notion += result.NotionLinks |
| 198 | if len(result.Placeholders) > 0 { |
| 199 | view.Placeholders = append(view.Placeholders, page) |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | // absent is a page this run did not reach: not found, if the survey was |
| 204 | // complete and nothing above it failed; else could not be determined. |
| 205 | func (tally *reportTally) absent(entry reportRow) { |
| 206 | if !entry.place.mapped(entry.row) || !tally.view.SurveyDone { |
| 207 | return |
| 208 | } |
| 209 | parent := entry.place.Parent |
| 210 | for hops := 0; parent != "" && hops < 64; hops++ { |
| 211 | above, ok := tally.byKey[notionKey(parent)] |
| 212 | if !ok { |
| 213 | break |
| 214 | } |
| 215 | if above.row.State == mapFailed && |
| 216 | above.row.SurveyedRun == tally.run { |
| 217 | tally.view.Undetermined++ |
| 218 | return |
| 219 | } |
| 220 | parent = above.place.Parent |
| 221 | } |
| 222 | tally.view.NotFound++ |
| 223 | tally.view.NotFoundPages = append(tally.view.NotFoundPages, |
| 224 | tally.page(entry.place)) |
| 225 | } |
| 226 | |
| 227 | // page is a row's title, linked to its Mapa page when there is one. |
| 228 | func (tally *reportTally) page(place coordinate) reportPage { |
| 229 | out := reportPage{Title: firstOf(place.Title, untitled)} |
| 230 | if place.PageID == 0 { |
| 231 | return out |
| 232 | } |
| 233 | page, found, err := tally.store.anyPage(tally.ctx, place.PageID) |
| 234 | if err != nil || !found || page.Gone { |
| 235 | return out |
| 236 | } |
| 237 | if record, err := tally.store.OpenPage(page); err == nil { |
| 238 | out.Title = record.Title |
| 239 | } |
| 240 | out.Href = PagePath(page) |
| 241 | return out |
| 242 | } |
| 243 | |
| 244 | // look counts one page's icon or cover: imported, or why not. |
| 245 | func (tally *reportTally) look(outcome string, imported *int, |
| 246 | why map[string]int) { |
| 247 | switch outcome { |
| 248 | case "": |
| 249 | case lookImported: |
| 250 | *imported++ |
| 251 | default: |
| 252 | why[outcome]++ |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | func (tally *reportTally) finish() { |
| 257 | tally.view.FailedCodes = sortedCounts(tally.codes, nil) |
| 258 | tally.view.ImageFailures = sortedCounts(tally.view.Images.Failed, nil) |
| 259 | tally.view.IconFailures = sortedCounts(tally.icons, nil) |
| 260 | tally.view.CoverFailures = sortedCounts(tally.covers, nil) |
| 261 | tally.view.Losses = sortedCounts(tally.losses, lossLabel) |
| 262 | sort.Strings(tally.view.Databases) |
| 263 | } |
| 264 | |
| 265 | func sortedCounts(counts map[string]int, |
| 266 | label func(string) string) []countView { |
| 267 | var out []countView |
| 268 | for key, count := range counts { |
| 269 | if label != nil { |
| 270 | key = label(key) |
| 271 | } |
| 272 | out = append(out, countView{Label: key, Count: count}) |
| 273 | } |
| 274 | sort.Slice(out, func(left, right int) bool { |
| 275 | return out[left].Label < out[right].Label |
| 276 | }) |
| 277 | return out |
| 278 | } |
| 279 | |
| 280 | // lossLabel is a loss kind in words. |
| 281 | func lossLabel(kind string) string { |
| 282 | if name, ok := strings.CutPrefix(kind, notionmd.LossBlock); ok { |
| 283 | return "Blocks not imported: " + blockName(name) |
| 284 | } |
| 285 | if label, ok := lossLabels[kind]; ok { |
| 286 | return label |
| 287 | } |
| 288 | return strings.ReplaceAll(kind, "_", " ") |
| 289 | } |
| 290 | |
| 291 | // blockName is a Notion block type in words, after "Blocks not |
| 292 | // imported: ". A type not listed reads as its name, spaces for |
| 293 | // underscores. |
| 294 | func blockName(kind string) string { |
| 295 | if name, ok := blockNames[kind]; ok { |
| 296 | return name |
| 297 | } |
| 298 | return strings.ReplaceAll(kind, "_", " ") |
| 299 | } |
| 300 | |
| 301 | var blockNames = map[string]string{ |
| 302 | "breadcrumb": "Breadcrumbs", |
| 303 | "table_of_contents": "Table of contents", |
| 304 | "template": "Templates", |
| 305 | "embed": "Embedded content", |
| 306 | "bookmark": "Bookmarks", |
| 307 | "link_preview": "Link previews", |
| 308 | "button": "Buttons", |
| 309 | "form": "Forms", |
| 310 | "ai_block": "AI blocks", |
| 311 | "transcription": "Transcriptions", |
| 312 | } |
| 313 | |
| 314 | var lossLabels = map[string]string{ |
| 315 | notionmd.LossToggle: "Toggles opened out", |
| 316 | notionmd.LossColumns: "Columns laid out one after another", |
| 317 | notionmd.LossWidths: "Column widths", |
| 318 | notionmd.LossTabs: "Tabs laid out one after another", |
| 319 | notionmd.LossSynced: "Synced blocks, now separate copies", |
| 320 | notionmd.LossIndent: "Indented blocks moved to the left", |
| 321 | notionmd.LossNesting: "Blocks no longer nested in a list", |
| 322 | notionmd.LossHoisted: "Blocks moved out of a quote or callout", |
| 323 | notionmd.LossBlankLines: "Blank lines inside a paragraph", |
| 324 | notionmd.LossColor: "Text colours", |
| 325 | notionmd.LossStrike: "Strikethrough", |
| 326 | notionmd.LossUnderline: "Underline", |
| 327 | notionmd.LossCodeTick: "Code style on text holding a backtick", |
| 328 | notionmd.LossMarkStar: "Bold or italic on text holding a *", |
| 329 | notionmd.LossMarkLink: "Bold or italic around a link", |
| 330 | notionmd.LossCodeFence: "Code with both kinds of fence", |
| 331 | notionmd.LossFile: "Files not imported", |
| 332 | notionmd.LossStartNumber: "List start numbers", |
| 333 | notionmd.LossBlockAnchor: "Links to a block, now to its page", |
| 334 | notionmd.LossUnsafeLink: "Links Mapa won't make clickable", |
| 335 | notionmd.LossLongLink: "Links too long for a link with its own " + |
| 336 | "text, written as text and address", |
| 337 | notionmd.LossDeadLink: "Links to pages deleted or failed in Mapa", |
| 338 | notionmd.LossRowLink: "Links to database rows", |
| 339 | notionmd.LossDatabase: "Databases, mentioned or embedded", |
| 340 | notionmd.LossTableLayout: "Table layout", |
| 341 | notionmd.LossCallout: "Callout icons that aren't emoji", |
| 342 | notionmd.LossMeeting: "Meeting recordings", |
| 343 | } |
| 344 | |