Commit aeab22a293
aeab22a293d720abc53fb2f1ee9d648ffdb77a7e
parent: 4234268fe5
Unsigned
cmc <hello@cleberg.net> · 2026-10-21 06:08 UTC
Observing log: shared store read off main, keys indexed once, hand-written designation parser (!76)
Every log screen re-read and parsed all Org files on the main thread when it appeared: `ObservingLog.all()` from `LogScreen.onAppear`, `ProgramsScreen.onAppear` (also on every return from a program) and `ProgramDetail.reload`, and `EquipmentDetail.reload`. `ProgramsScreen.body` built a `CatalogResolver` and a `ProgramProgress` per program on every evaluation, and `CatalogResolver.designation` ran a Swift `Regex` (~28 µs, 80 allocations a call). Saving from the editor also read every file on main (`ObservingLog.delete(id:)`).
## Changes
- `ObservingLogStore` (`LogScreen.swift`, one shared instance): entries, programs, resolver and per-program `ProgramProgress`. A read (`programs()`, `all()`, index, progress) runs in a detached task. A generation counter drops reads started before a later reload or delete, and published values are only replaced when entries or programs changed.
- `LogScreen`, `ProgramsScreen`, `ProgramDetail` and `EquipmentDetail` read the store and call `reload()` on appear, so files changed in the Files app are still picked up, without blocking.
- Save: `ObservationEditor` awaits `store.save` (write in a detached task, names resolved against freshly read programs as before), then dismisses. Errors show in the editor as before. The `onSave` parameter is gone; the store reloads itself.
- Delete: the row leaves `entries` at once, then the files are deleted off main and the log is read again.
- The Programs folder is still created when Programs appears, from a detached task.
- `ObservableObject`, not `@Observable`: the macro expands to `Observation.…`, which resolves to `ObservingLog.Observation` in files that import `ObservingLog`.
- `LogIndex` (MoonriseKit): earliest entry per catalogue key, built once per read and shared by all programs. `ProgramProgress(program:index:)` uses it; `ProgramProgress(program:log:resolver:)` builds one.
- `CatalogResolver.designation`: scans `Character`s (ASCII letters, spaces, `Character.isNumber`, leading "0"s dropped) instead of the regex. Results are identical: new test `designationMatchesRegex` compares it with the regex over Library page titles and subtitles, Messier names and aliases, odd inputs ("M 31", "Messier 31", "m031", "NGC 6205", "NGC6205A", NBSP, thin, ideographic and zero-width spaces, BOM, Arabic, Devanagari, fullwidth and mathematical digits, "²", "½", combining marks) and every scalar U+0000–U+2FFF in four positions. A one-off run outside the test over all 1,112,064 scalars in six positions found no differences.
- `ObservingProgram` and `ObservingProgram.Object` are `Equatable` (change detection in the store).
- `ProgramDetail`'s "up tonight" list drops logged objects when it is drawn instead of in `reload()`.
## Measurements
Same method before and after. Load (`uptime`, 12 cores) is given per run; other work was running on the host.
**Package benchmark** (M2 Pro, `-c release`, 300 entries from the perf generator, Messier + two 50-object programs, medians, before and after back to back, load 4.86 → 4.71):
| Case | main | branch |
|---|---:|---:|
| `designation` × 300 | 8.26 ms, 24,550 allocs | 0.13 ms, 1 alloc |
| `keys(for:)` × 300 entries | 6.85 ms | 0.48 ms |
| `ProgramProgress`, Messier, 300 entries | 7.12 ms | 0.59 ms |
| Old Programs body: resolver + 3 × `ProgramProgress` | 21.75 ms | 1.88 ms (no longer in body) |
| `LogIndex` + 3 × `ProgramProgress` (once per read, off main) | — | 0.60 ms |
| `ObservingLog.programs()`, Messier + 2 files | 3.49 ms | 0.51 ms |
| `ObservingLog.all()`, 300 files | 20.96 ms | 20.76 ms (off main now) |
| Whole read: programs + all + progress | 50.71 ms | 21.45 ms (off main now) |
Programs `body` now does one dictionary lookup per program.
**iPhone 18 Pro simulator** (iOS 27, Release build with a temporary frame probe, not committed). A UI test seeds 300 entries, then twice: open the log, Programs, Messier, back, Messier, back, back, open an entry, Save, Done. Two unprofiled runs each, one run each under Time Profiler (1 ms).
Main-thread time in log code, whole tour (Time Profiler):
| | main | branch |
|---|---:|---:|
| `ObservingLog.all` | 375 ms | 0 (381 ms on other threads) |
| `ObservingLog.save` / `delete` | 63 ms | 0 (57 ms on other threads) |
| `ProgramsScreen` body | 227 ms | 0 samples |
| `ProgramDetail` | 135 ms | 4 ms |
| `LogScreen` | 127 ms | 5 ms |
| `CatalogResolver.designation` | 90 ms | 0 |
Main-thread CPU per step, test-driver accessibility code removed (profiled runs, round 1 / round 2):
| Step | main | branch |
|---|---:|---:|
| Log open | 824 / 478 ms | 511 / 332 ms |
| Programs open | 267 / 272 | 194 / 175 |
| Messier open | 280 / 176 | 189 / 130 |
| Messier back | 143 / 154 | 103 / 93 |
| Messier re-open | 187 / 182 | 140 / 146 |
| Messier back (2nd) | 139 / 133 | 76 / 97 |
| Programs back | 196 / 233 | 161 / 172 |
| Entry save | 216 / 221 | 111 / 81 |
Longest frame per step, unprofiled, 2 runs × 2 rounds (ms):
| Step | main | branch |
|---|---:|---:|
| Log open, first / second | 723–762 / 196 | 644–675 / 139–140 |
| Programs open | 118–227 | 125–204 |
| Messier open | 122–160 | 78–110 |
| Messier back | 77–178 | 44–51 |
| Messier re-open | 113–178 | 70–87 |
| Messier back (2nd) | 73–80 | 46–48, one 135 |
| Programs back | 56–99 | 53–72 |
| Entry save | 100–106 | 78–107 |
| Log close | 57–70 | 60–78 |
Frames over 50 ms remain when opening and closing. In the profiled branch run the app's own code in those frames is under 5 ms; the rest is SwiftUI/UIKit (first use of each view type, sheet and push transitions) and the UI test's accessibility snapshots. The 135 ms frame was in the second branch run while load rose to 7.7; it was not profiled.
Load: main 6.86 → 16.07 and 4.40 → 5.63, profiled 7.63 → 5.08; branch 5.54 → 5.46 and 4.67 → 7.73, profiled 9.19 → 4.29.
Checked by hand in the simulator: an Org file added to Observations shows on open; a second file added while the log was open showed after visiting Programs and coming back, and counted in Messier progress; swipe delete removed the row and the file.
## Tests
- `swift test` (MoonriseKit): 193 pass.
- `npm test` (server): 55 pass.
- `xcodebuild test` (iPhone 18 Pro): pass. The accessibility audit found 64 issues: Sky 4, Settings 2, System 7, Sun 3, News & launches 21, Library 14, Library page: Saturn 13. It does not visit the log screens.
## Not verified
- On a device.
- `EquipmentDetail` usage with real equipment (none was set up in the runs).
- Programs from files in the Programs folder in the app (covered only by the package benchmark).
- The accessibility audit was not run on main in this session.
Layout: unified · split
App/Sources/EquipmentScreen.swift
+6 −2
| @@ -90,10 +90,14 @@ struct EquipmentScreen: View { |
| 90 | 90 | struct EquipmentDetail: View { |
| 91 | 91 | @State var item: Equipment |
| 92 | 92 | var onChange: () -> Void |
| 93 | | @State private var usage: EquipmentUsage? |
| 93 | @ObservedObject private var store = observingLogStore |
| 94 | 94 | @State private var names: [String: String] = [:] |
| 95 | 95 | @State private var editing: Equipment? |
| 96 | 96 | |
| 97 | private var usage: EquipmentUsage? { |
| 98 | store.loaded ? EquipmentUsage(of: item.id, in: store.entries, resolver: store.resolver) : nil |
| 99 | } |
| 100 | |
| 97 | 101 | var body: some View { |
| 98 | 102 | List { |
| 99 | 103 | Section { |
| @@ -150,7 +154,7 @@ struct EquipmentDetail: View { |
| 150 | 154 | |
| 151 | 155 | private func reload() { |
| 152 | 156 | names = Dictionary(equipmentInventory.all().map { ($0.id, $0.name) }, uniquingKeysWith: { a, _ in a }) |
| 153 | | usage = EquipmentUsage(of: item.id, in: observingLog.all(), resolver: CatalogResolver(programs: observingLog.programs())) |
| 157 | store.reload() |
| 154 | 158 | } |
| 155 | 159 | } |
| 156 | 160 | |
App/Sources/LogScreen.swift
+86 −17
| @@ -4,17 +4,91 @@ import SwiftUI |
| 4 | 4 | /// The observing log lives in Documents/Observations, one Org file per entry, visible in the Files app. |
| 5 | 5 | let observingLog = ObservingLog(folder: URL.documentsDirectory.appending(path: "Observations")) |
| 6 | 6 | |
| 7 | @MainActor let observingLogStore = ObservingLogStore(log: observingLog) |
| 8 | |
| 9 | /// The parsed log and programs, shared by the log screens. Files are read off the main thread. Screens call |
| 10 | /// `reload` when they appear, so files changed in the Files app are picked up. |
| 11 | @MainActor final class ObservingLogStore: ObservableObject { |
| 12 | struct Snapshot: Sendable { |
| 13 | var entries: [Observation] |
| 14 | var programs: [ObservingProgram] |
| 15 | var resolver: CatalogResolver |
| 16 | var progress: [String: ProgramProgress] |
| 17 | } |
| 18 | |
| 19 | /// Newest first. |
| 20 | @Published private(set) var entries: [Observation] = [] |
| 21 | /// Messier, then the files in the Programs folder. |
| 22 | @Published private(set) var programs: [ObservingProgram] = [] |
| 23 | @Published private(set) var resolver = CatalogResolver() |
| 24 | /// By program ID. |
| 25 | @Published private(set) var progress: [String: ProgramProgress] = [:] |
| 26 | /// False until the first read finishes. |
| 27 | @Published private(set) var loaded = false |
| 28 | private let log: ObservingLog |
| 29 | /// Results of reads started before the latest reload or delete are dropped. |
| 30 | private var generation = 0 |
| 31 | /// The read the published values were last set from. |
| 32 | private var shown: Snapshot? |
| 33 | |
| 34 | init(log: ObservingLog) { self.log = log } |
| 35 | |
| 36 | func reload() { |
| 37 | generation += 1 |
| 38 | let g = generation, log = log |
| 39 | Task { |
| 40 | let s = await Task.detached(priority: .userInitiated) { ObservingLogStore.read(log) }.value |
| 41 | guard g == generation, s.entries != shown?.entries || s.programs != shown?.programs else { return } |
| 42 | shown = s |
| 43 | entries = s.entries |
| 44 | programs = s.programs |
| 45 | resolver = s.resolver |
| 46 | progress = s.progress |
| 47 | loaded = true |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /// Writes as `ObservingLog.save`, with names resolved against the current programs, then reloads. |
| 52 | func save(_ o: Observation) async throws { |
| 53 | let log = log |
| 54 | try await Task.detached(priority: .userInitiated) { try log.save(o, resolver: CatalogResolver(programs: log.programs())) }.value |
| 55 | reload() |
| 56 | } |
| 57 | |
| 58 | /// Removes the entries from `entries` at once, then deletes their files and reloads. |
| 59 | func delete(ids: [String]) { |
| 60 | entries.removeAll { ids.contains($0.id) } |
| 61 | generation += 1 |
| 62 | let log = log |
| 63 | Task { |
| 64 | await Task.detached(priority: .userInitiated) { for id in ids { try? log.delete(id: id) } }.value |
| 65 | reload() |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | nonisolated private static func read(_ log: ObservingLog) -> Snapshot { |
| 70 | let programs = log.programs() |
| 71 | let resolver = CatalogResolver(programs: programs) |
| 72 | let entries = log.all() |
| 73 | let index = LogIndex(log: entries, resolver: resolver) |
| 74 | let progress = Dictionary(programs.map { ($0.id, ProgramProgress(program: $0, index: index)) }, uniquingKeysWith: { a, _ in a }) |
| 75 | return Snapshot(entries: entries, programs: programs, resolver: resolver, progress: progress) |
| 76 | } |
| 77 | } |
| 78 | |
| 7 | 79 | struct LogScreen: View { |
| 8 | | @State private var entries: [Observation] = [] |
| 80 | @ObservedObject private var store = observingLogStore |
| 9 | 81 | @State private var editing: Observation? |
| 10 | 82 | var newEntry: () -> Observation |
| 11 | 83 | @Environment(\.dismiss) private var dismiss |
| 12 | 84 | |
| 85 | private var entries: [Observation] { store.entries } |
| 86 | |
| 13 | 87 | var body: some View { |
| 14 | 88 | NavigationStack { |
| 15 | 89 | List { |
| 16 | 90 | NavigationLink("Programs") { ProgramsScreen(newEntry: newEntry) } |
| 17 | | if entries.isEmpty { |
| 91 | if store.loaded, entries.isEmpty { |
| 18 | 92 | Text("No observations yet. Add one here, or use Log on an object's card in the Sky tab.") |
| 19 | 93 | .font(.footnote).foregroundStyle(.secondary) |
| 20 | 94 | } |
| @@ -28,10 +102,7 @@ struct LogScreen: View { |
| 28 | 102 | } |
| 29 | 103 | .buttonStyle(.plain) |
| 30 | 104 | } |
| 31 | | .onDelete { idx in |
| 32 | | for i in idx { try? observingLog.delete(id: entries[i].id) } |
| 33 | | reload() |
| 34 | | } |
| 105 | .onDelete { idx in store.delete(ids: idx.map { entries[$0].id }) } |
| 35 | 106 | } |
| 36 | 107 | .navigationTitle("Observing log") |
| 37 | 108 | .navigationBarTitleDisplayMode(.inline) |
| @@ -51,18 +122,15 @@ struct LogScreen: View { |
| 51 | 122 | .font(.caption2).foregroundStyle(.secondary).padding(8) |
| 52 | 123 | } |
| 53 | 124 | .sheet(item: $editing) { o in |
| 54 | | ObservationEditor(observation: o) { reload() } |
| 125 | ObservationEditor(observation: o) |
| 55 | 126 | } |
| 56 | 127 | } |
| 57 | | .onAppear(perform: reload) |
| 128 | .onAppear { store.reload() } |
| 58 | 129 | } |
| 59 | | |
| 60 | | private func reload() { entries = observingLog.all() } |
| 61 | 130 | } |
| 62 | 131 | |
| 63 | 132 | struct ObservationEditor: View { |
| 64 | 133 | @State var observation: Observation |
| 65 | | var onSave: () -> Void |
| 66 | 134 | @Environment(\.dismiss) private var dismiss |
| 67 | 135 | @State private var error: String? |
| 68 | 136 | @State private var names: [String: String] = [:] |
| @@ -102,12 +170,13 @@ struct ObservationEditor: View { |
| 102 | 170 | ToolbarItem(placement: .cancellationAction) { Button("Cancel") { dismiss() } } |
| 103 | 171 | ToolbarItem(placement: .confirmationAction) { |
| 104 | 172 | Button("Save") { |
| 105 | | do { |
| 106 | | try observingLog.save(observation, resolver: CatalogResolver(programs: observingLog.programs())) |
| 107 | | onSave() |
| 108 | | dismiss() |
| 109 | | } catch { |
| 110 | | self.error = "Couldn't save: \(error.localizedDescription)" |
| 173 | Task { |
| 174 | do { |
| 175 | try await observingLogStore.save(observation) |
| 176 | dismiss() |
| 177 | } catch { |
| 178 | self.error = "Couldn't save: \(error.localizedDescription)" |
| 179 | } |
| 111 | 180 | } |
| 112 | 181 | } |
| 113 | 182 | .disabled(observation.object.trimmingCharacters(in: .whitespaces).isEmpty) |
App/Sources/ProgramsScreen.swift
+14 −19
| @@ -5,15 +5,13 @@ import SwiftUI |
| 5 | 5 | /// Observing programs: the bundled Messier list plus Org files in Documents/Observations/Programs. |
| 6 | 6 | struct ProgramsScreen: View { |
| 7 | 7 | var newEntry: () -> Observation |
| 8 | | @State private var programs: [ObservingProgram] = [] |
| 9 | | @State private var log: [Observation] = [] |
| 8 | @ObservedObject private var store = observingLogStore |
| 10 | 9 | |
| 11 | 10 | var body: some View { |
| 12 | | let resolver = CatalogResolver(programs: programs) |
| 13 | 11 | List { |
| 14 | 12 | Section { |
| 15 | | ForEach(programs) { p in |
| 16 | | let n = ProgramProgress(program: p, log: log, resolver: resolver).count |
| 13 | ForEach(store.programs) { p in |
| 14 | let n = store.progress[p.id]?.count ?? 0 |
| 17 | 15 | NavigationLink { ProgramDetail(program: p, newEntry: newEntry) } label: { |
| 18 | 16 | VStack(alignment: .leading, spacing: 6) { |
| 19 | 17 | HStack { |
| @@ -34,9 +32,9 @@ struct ProgramsScreen: View { |
| 34 | 32 | .navigationTitle("Programs") |
| 35 | 33 | .navigationBarTitleDisplayMode(.inline) |
| 36 | 34 | .onAppear { |
| 37 | | try? FileManager.default.createDirectory(at: observingLog.programsFolder, withIntermediateDirectories: true) |
| 38 | | programs = observingLog.programs() |
| 39 | | log = observingLog.all() |
| 35 | let folder = observingLog.programsFolder |
| 36 | Task.detached { try? FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true) } |
| 37 | store.reload() |
| 40 | 38 | } |
| 41 | 39 | } |
| 42 | 40 | } |
| @@ -50,18 +48,21 @@ struct ProgramDetail: View { |
| 50 | 48 | |
| 51 | 49 | var program: ObservingProgram |
| 52 | 50 | var newEntry: () -> Observation |
| 53 | | @State private var progress: ProgramProgress? |
| 51 | @ObservedObject private var store = observingLogStore |
| 54 | 52 | @State private var upTonight: [Candidate]? |
| 55 | 53 | @State private var editing: Observation? |
| 56 | 54 | |
| 55 | private var progress: ProgramProgress? { store.progress[program.id] } |
| 56 | |
| 57 | 57 | var body: some View { |
| 58 | 58 | List { |
| 59 | 59 | Section { |
| 60 | 60 | if let upTonight { |
| 61 | | if upTonight.isEmpty { |
| 61 | let pending = upTonight.filter { progress?.first[$0.object.id] == nil } |
| 62 | if pending.isEmpty { |
| 62 | 63 | Text("None of the objects still to log gets 25° up in full darkness tonight.").font(.footnote).foregroundStyle(.secondary) |
| 63 | 64 | } |
| 64 | | ForEach(Array(upTonight.enumerated()), id: \.offset) { _, c in |
| 65 | ForEach(Array(pending.enumerated()), id: \.offset) { _, c in |
| 65 | 66 | row(c.object, trailing: "\(c.best.formatted(date: .omitted, time: .shortened)) · \(Int(c.position.altitude.rounded()))° \(SkyEngine.compass(c.position.azimuth))") |
| 66 | 67 | } |
| 67 | 68 | } else { |
| @@ -81,9 +82,9 @@ struct ProgramDetail: View { |
| 81 | 82 | .navigationTitle(program.title) |
| 82 | 83 | .navigationBarTitleDisplayMode(.inline) |
| 83 | 84 | .sheet(item: $editing) { o in |
| 84 | | ObservationEditor(observation: o) { reload() } |
| 85 | ObservationEditor(observation: o) |
| 85 | 86 | } |
| 86 | | .onAppear(perform: reload) |
| 87 | .onAppear { store.reload() } |
| 87 | 88 | .task { await findUpTonight() } |
| 88 | 89 | } |
| 89 | 90 | |
| @@ -114,12 +115,6 @@ struct ProgramDetail: View { |
| 114 | 115 | .accessibilityHint(logged == nil ? "Logs this object" : "Opens the entry") |
| 115 | 116 | } |
| 116 | 117 | |
| 117 | | private func reload() { |
| 118 | | let programs = observingLog.programs() |
| 119 | | progress = ProgramProgress(program: program, log: observingLog.all(), resolver: CatalogResolver(programs: programs)) |
| 120 | | if let upTonight, let progress { self.upTonight = upTonight.filter { progress.first[$0.object.id] == nil } } |
| 121 | | } |
| 122 | | |
| 123 | 118 | private func findUpTonight() async { |
| 124 | 119 | let here = newEntry() |
| 125 | 120 | guard let lat = here.latitude, let lon = here.longitude else { upTonight = []; return } |
App/Sources/SkyScreen.swift
+1 −1
| @@ -91,7 +91,7 @@ struct SkyScreen: View { |
| 91 | 91 | LogScreen { newObservation("") }.preferredColorScheme(.dark).tint(.blue) |
| 92 | 92 | } |
| 93 | 93 | .sheet(item: $logEntry) { o in |
| 94 | | ObservationEditor(observation: o) {}.preferredColorScheme(.dark).tint(.blue) |
| 94 | ObservationEditor(observation: o).preferredColorScheme(.dark).tint(.blue) |
| 95 | 95 | } |
| 96 | 96 | .sheet(isPresented: $showEvents) { |
| 97 | 97 | EventsScreen(observer: model.observer, placeName: model.placeName, forecast: model.forecast) |
Packages/MoonriseKit/Sources/ObservingLog/Programs.swift
+31 −10
| @@ -27,10 +27,18 @@ public struct CatalogResolver: Sendable { |
| 27 | 27 | public static let messier = CatalogResolver(programs: [.messier]) |
| 28 | 28 | |
| 29 | 29 | /// "M 31", "Messier 31" and "m031" give "M31"; "NGC 6205" gives "NGC6205". Nil when `s` is not letters then a number. |
| 30 | /// Letters are ASCII, then optional spaces, then one or more `Character.isNumber` characters. |
| 30 | 31 | public static func designation(_ s: String) -> String? { |
| 31 | | guard let m = s.trimmingCharacters(in: .whitespaces).wholeMatch(of: /([A-Za-z]+) *0*(\d+)/) else { return nil } |
| 32 | | let prefix = m.1.uppercased() |
| 33 | | return (prefix == "MESSIER" ? "M" : prefix) + m.2 |
| 32 | let t = s.trimmingCharacters(in: .whitespaces) |
| 33 | var i = t.startIndex |
| 34 | while i < t.endIndex, t[i].isASCII, t[i].isLetter { t.formIndex(after: &i) } |
| 35 | guard i > t.startIndex else { return nil } |
| 36 | let prefix = t[..<i].uppercased() |
| 37 | while i < t.endIndex, t[i] == " " { t.formIndex(after: &i) } |
| 38 | var number = t[i...] |
| 39 | guard !number.isEmpty, number.allSatisfy(\.isNumber) else { return nil } |
| 40 | while number.count > 1, number.first == "0" { number = number.dropFirst() } |
| 41 | return (prefix == "MESSIER" ? "M" : prefix) + number |
| 34 | 42 | } |
| 35 | 43 | |
| 36 | 44 | /// Case, diacritics, curly apostrophes and runs of spaces ignored. |
| @@ -69,8 +77,8 @@ public struct CatalogResolver: Sendable { |
| 69 | 77 | /// `CatalogResolver`, else the object is matched by name. `:RA:` and `:DEC:` are J2000 degrees; Messier |
| 70 | 78 | /// objects without them take the bundled position. Other lines and lower-level headings are ignored. |
| 71 | 79 | /// Without `#+TITLE:` the file name is the title. |
| 72 | | public struct ObservingProgram: Sendable, Identifiable { |
| 73 | | public struct Object: Sendable, Identifiable { |
| 80 | public struct ObservingProgram: Sendable, Identifiable, Equatable { |
| 81 | public struct Object: Sendable, Identifiable, Equatable { |
| 74 | 82 | /// Catalogue IDs; the first is `id`. Empty when the object is matched by name. |
| 75 | 83 | public var ids: [String] |
| 76 | 84 | public var name: String |
| @@ -148,6 +156,19 @@ public struct ObservingProgram: Sendable, Identifiable { |
| 148 | 156 | } |
| 149 | 157 | } |
| 150 | 158 | |
| 159 | /// Earliest entry for each catalogue key in a log. Build it once per log and share it between programs. |
| 160 | public struct LogIndex: Sendable { |
| 161 | public var earliest: [String: Observation] |
| 162 | |
| 163 | public init(log: [Observation], resolver: CatalogResolver) { |
| 164 | var earliest: [String: Observation] = [:] |
| 165 | for o in log { |
| 166 | for k in resolver.keys(for: o) where o.date < earliest[k]?.date ?? .distantFuture { earliest[k] = o } |
| 167 | } |
| 168 | self.earliest = earliest |
| 169 | } |
| 170 | } |
| 171 | |
| 151 | 172 | /// Which objects of a program the log has, derived each time and never stored. |
| 152 | 173 | public struct ProgramProgress: Sendable { |
| 153 | 174 | /// Earliest entry for each logged object, by object ID. |
| @@ -156,13 +177,13 @@ public struct ProgramProgress: Sendable { |
| 156 | 177 | public var count: Int { first.count } |
| 157 | 178 | |
| 158 | 179 | public init(program: ObservingProgram, log: [Observation], resolver: CatalogResolver) { |
| 159 | | var byKey: [String: Observation] = [:] |
| 160 | | for o in log { |
| 161 | | for k in resolver.keys(for: o) where o.date < byKey[k]?.date ?? .distantFuture { byKey[k] = o } |
| 162 | | } |
| 180 | self.init(program: program, index: LogIndex(log: log, resolver: resolver)) |
| 181 | } |
| 182 | |
| 183 | public init(program: ObservingProgram, index: LogIndex) { |
| 163 | 184 | var first: [String: Observation] = [:] |
| 164 | 185 | for object in program.objects { |
| 165 | | if let o = object.keys.compactMap({ byKey[$0] }).min(by: { $0.date < $1.date }) { first[object.id] = o } |
| 186 | if let o = object.keys.compactMap({ index.earliest[$0] }).min(by: { $0.date < $1.date }) { first[object.id] = o } |
| 166 | 187 | } |
| 167 | 188 | self.first = first |
| 168 | 189 | } |
Packages/MoonriseKit/Tests/ObservingLogTests/ProgramsTests.swift
+27
| @@ -1,4 +1,5 @@ |
| 1 | 1 | import Foundation |
| 2 | import Library |
| 2 | 3 | import Testing |
| 3 | 4 | @testable import ObservingLog |
| 4 | 5 | |
| @@ -13,6 +14,32 @@ import Testing |
| 13 | 14 | #expect(CatalogResolver.designation("M31 and M32") == nil) |
| 14 | 15 | } |
| 15 | 16 | |
| 17 | /// The regex `designation` used to be, as the reference for the hand-written parser. |
| 18 | private func regexDesignation(_ s: String) -> String? { |
| 19 | guard let m = s.trimmingCharacters(in: .whitespaces).wholeMatch(of: /([A-Za-z]+) *0*(\d+)/) else { return nil } |
| 20 | let prefix = m.1.uppercased() |
| 21 | return (prefix == "MESSIER" ? "M" : prefix) + m.2 |
| 22 | } |
| 23 | |
| 24 | @Test func designationMatchesRegex() { |
| 25 | var inputs = Library.pages.flatMap { [$0.title, $0.subtitle] + $0.subtitle.components(separatedBy: " · ") } |
| 26 | inputs += ObservingProgram.messier.objects.flatMap { [$0.name] + $0.aliases } |
| 27 | inputs += ["M 31", "Messier 31", "m031", "NGC 6205", "NGC6205A", "M", "31", "", " ", "M000", "M00 7", "M 0", "messier 0031", |
| 28 | "MESSIER31", "M 31", "M\t31", "\tM31\n", "M\r\n31", "M31 ", " M31", "M-31", "M.31", "M31.5", "M31a", "Sh2-155", |
| 29 | "M\u{00A0}31", "\u{00A0}M31\u{2009}", "M\u{2009}31", "M\u{3000}31", "\u{3000}M31\u{3000}", "M\u{200B}31", "\u{200B}M31", |
| 30 | "M\u{FEFF}31", "\u{FEFF}M31", "M\u{202F}31", |
| 31 | "M٣١", "M३१", "M31", "M𝟑𝟏", "M0٣", "M²", "M①", "M½", "MⅫ", "M三", "M31", "ß31", "Dž1", "ff1", |
| 32 | "M3\u{0301}", "M0\u{0301}31", "M31\u{0301}", "M \u{0301}31", "M\u{0301}31", "e\u{0301}31", "A\u{0308}1", "M 3\u{20E3}"] |
| 33 | for v in 0..<0x3000 { |
| 34 | guard let u = Unicode.Scalar(v) else { continue } |
| 35 | let c = String(Character(u)) |
| 36 | inputs += ["M" + c + "1", "M0" + c, c + "31", "M " + c] |
| 37 | } |
| 38 | for s in inputs { |
| 39 | #expect(CatalogResolver.designation(s) == regexDesignation(s), "\(s.unicodeScalars.map { String($0.value, radix: 16) })") |
| 40 | } |
| 41 | } |
| 42 | |
| 16 | 43 | @Test func messierNamesResolve() { |
| 17 | 44 | let r = CatalogResolver.messier |
| 18 | 45 | for name in ["M31", "M 31", "Messier 31", "Andromeda Galaxy", "andromeda galaxy"] { |