SPB Git

Repository Public

Scenes.swift 9.9 KB 146 lines swift

1import AppKit2import SwiftUI3import SheetCore4import SheetData5import SheetUI6import FeatureData78/// Demo states, one per screen.9@MainActor enum Scenes {10    static func sidebar(_ session: WorkbookSession) -> some View {11        let pane = Workspace.shared.panes(.sidebar).first { $0.id == DataFeature.datasetsPane }12        return VStack(spacing: 0) { pane?.content(session) }.frame(maxWidth: .infinity, maxHeight: .infinity).background(ORIX.Palette.canvas)13    }1415    /// Imports the sample files: the big orders CSV as a table and the customers Parquet as a lazy view.16    static func importSamples(_ session: WorkbookSession, rows: Int) async throws {17        let workspace = DataWorkspace.of(session)18        let started = Date()19        let orders = try Samples.ordersCSV(rows: rows)20        let generated = Date().timeIntervalSince(started)21        let importStart = Date()22        try await workspace.importFile(ImportRequest(url: orders, name: "orders", format: .csv))23        print(String(format: "sample CSV: %d rows (%.1f s to generate or reuse), imported in %.2f s", rows, generated, Date().timeIntervalSince(importStart)))24        try await workspace.importFile(ImportRequest(url: try Samples.customersParquet(), name: "customers", format: .parquet, lazy: true))25    }2627    static func prepare(_ pane: String, session: WorkbookSession, rows: Int) async throws {28        let workspace = DataWorkspace.of(session)29        switch pane {30        case "empty":31            session.studioPane = DataFeature.dataPane32        case "import":33            try await importSamples(session, rows: min(rows, 50_000))34            DataAutomation.showDataset("orders", in: session)35            DataAutomation.prepareImport(try Samples.semicolonCSV(), in: session)36        case "missing":37            try await importSamples(session, rows: min(rows, 50_000))38            // a link whose file is gone, as after moving a workbook to another Mac39            let ghost = Samples.directory.appendingPathComponent("archive_2022.parquet")40            session.workbook.attachments[DatasetCatalog.attachmentKey] = try JSONEncoder().encode(DatasetCatalog(datasets: workspace.catalog.datasets + [41                DatasetDescriptor(name: "archive_2022", origin: .file(path: ghost.path, bookmark: nil, format: .parquet, options: .init(), lazy: false)),42            ]))43            let reopened = WorkbookSession(workbook: session.workbook, locale: Locale(identifier: "en_US"))44            reopened.showSidebar = false45            reopened.inspectorPane = nil46            delegate.session = reopened47            delegate.window.contentView = NSHostingView(rootView: WorkbookView(session: reopened))48            await DataWorkspace.of(reopened).restoreAll()49            DataAutomation.showDataset("archive_2022", in: reopened, profile: false)50        case "sql", "sql-error":51            try await importSamples(session, rows: rows)52            DataAutomation.showDataset("orders", in: session)53            let good = """54                -- revenue by region and category, delivered orders only55                SELECT o.region, o.category, count(*) AS orders,56                       round(sum(o.quantity * o.unit_price * (1 - coalesce(o.discount, 0))), 2) AS revenue,57                       round(avg(o.quantity), 2) AS avg_quantity58                FROM orders AS o JOIN customers AS c USING (customer_id)59                WHERE o.status = 'delivered' AND c.segment <> 'Education'60                GROUP BY ALL ORDER BY revenue DESC61                """62            DataAutomation.openSQL(pane == "sql" ? good : "SELECT region, sum(amount) FROM orders GROUP BY region", in: session, run: true)63        case "insert":64            try await importSamples(session, rows: rows)65            DataAutomation.showDataset("orders", in: session, filters: [ColumnFilter(column: "status", kind: .equals, text: "returned"), ColumnFilter(column: "quantity", kind: .range, text: "10")])66            while !DataAutomation.isSettled(session) { try await Task.sleep(for: .milliseconds(50)) }67            DataAutomation.beginInsert(in: session, newSheet: true)68        case "refused":69            // a result larger than a worksheet: every order next to its customer's two neighbours70            try await importSamples(session, rows: rows)71            DataAutomation.showDataset("orders", in: session)72            DataAutomation.openSQL("SELECT o.order_id, o.order_date, o.region, c.customer_name, c.segment\nFROM orders AS o JOIN customers AS c\n  ON c.customer_id BETWEEN o.customer_id AND o.customer_id + 2", in: session, run: true)73            while !DataAutomation.isSettled(session) { try await Task.sleep(for: .milliseconds(50)) }74            DataAutomation.beginInsert(in: session, newSheet: true)75        case "sidebar":76            try await importSamples(session, rows: min(rows, 50_000))77            _ = Samples.fillSales(session, sheet: 0)78            try workspace.importRange(sheetIndex: 0, range: RangeRef(r0: 0, c0: 0, r1: 240, c1: 5), hasHeader: true, name: "sales")79        case "transform", "transform-sql", "transform-error":80            try await importSamples(session, rows: rows)81            DataAutomation.showPipeline(Scenes.pipeline(broken: pane == "transform-error"), in: session, step: pane == "transform-error" ? 2 : 4, showSQL: pane == "transform-sql")82        case "pivot", "pivot-create", "pivot-compact":83            let range = Samples.fillSales(session, sheet: 0)84            session.select(RangeRef(CellRef(3, 2)))85            session.showSidebar = false86            if pane == "pivot-create" { DataAutomation.showPivot(nil, in: session); return }87            let source = PivotSource.range(sheetID: session.workbook.sheets[0].id, range: range, hasHeader: true, dateColumns: [0])88            let spec = PivotSpec(name: "Revenue by region", source: source,89                                 rows: [PivotField("Region"), PivotField("Category")],90                                 columns: [PivotField("Date", grouping: .datePart(.quarter))],91                                 values: [PivotValue("Revenue", .sum, numberFormat: "#,##0")],92                                 filters: [PivotFilter("Product", .exclude([.string("Support plan")]))],93                                 calculatedFields: [PivotCalculatedField(name: "Price per unit", expression: "Revenue / Units")],94                                 layout: pane == "pivot-compact" ? .compact : .tabular)95            let object = try await PivotController.of(session).create(source: source, destination: .cell(CellRef(1, 7)), spec: spec)96            session.select(RangeRef(CellRef(3, 9)))97            DataAutomation.showPivot(object.id, in: session)98        default:99            try await importSamples(session, rows: rows)100            DataAutomation.showDataset("orders", in: session, sort: [SortSpec("unit_price", ascending: false)],101                                       filters: [ColumnFilter(column: "region", kind: .equals, text: "West")])102        }103    }104105    /// A six-step pipeline over the sample orders.106    static func pipeline(broken: Bool) -> TransformPipeline {107        TransformPipeline(name: "Revenue by segment", source: .dataset("orders"), steps: [108            .filter(FilterGroup(.all, [FilterCondition("status", .notIn, [.string("cancelled"), .string("returned")]),109                                       FilterCondition("order_date", .greaterOrEqual, .date(year: 2024, month: 1, day: 1))])),110            .calculatedColumn(name: "revenue", expression: "quantity * unit_price * (1 - coalesce(discount, 0))"),111            .join(dataset: "customers", kind: .left, keys: [JoinKey(broken ? "client_id" : "customer_id", "customer_id")], suffix: "_customer"),112            .groupBy(keys: ["segment", "region"], aggregations: [Aggregation(.sum, "revenue", as: "revenue"), Aggregation(.countRows, nil, as: "orders"),113                                                                 Aggregation(.average, "quantity", as: "avg_quantity")]),114            .sort([SortSpec("revenue", ascending: false)]),115            .limit(15),116        ])117    }118}119120/// Measures how long the Data Studio grid takes to bring rows on screen at arbitrary scroll positions.121@MainActor enum Bench {122    static func run(session: WorkbookSession, rows: Int) async {123        func settle() async { while !DataAutomation.isSettled(session) { try? await Task.sleep(for: .milliseconds(20)) } }124        func measure(_ label: String, positions: [Int]) async {125            await settle()126            var samples = [Double]()127            for row in positions { if let t = await DataAutomation.fetchPage(containing: row, in: session) { samples.append(t) } }128            guard !samples.isEmpty else { print("\(label): no samples"); return }129            samples.sort()130            let median = samples[samples.count / 2], p95 = samples[min(samples.count - 1, Int(Double(samples.count) * 0.95))]131            print(String(format: "%@: %d pages of 200 rows, median %.1f ms, p95 %.1f ms, max %.1f ms", label, samples.count, median * 1000, p95 * 1000, samples.last! * 1000))132        }133        var g = Samples.Generator(state: 99)134        let random = (0..<60).map { _ in g.below(max(rows - 200, 1)) }135        DataAutomation.showDataset("orders", in: session, profile: false)136        await measure("unsorted, random offsets in \(rows) rows", positions: random)137        await measure("unsorted, sequential scroll", positions: (0..<60).map { 400_000 % max(rows, 1) + $0 * 200 })138        DataAutomation.showDataset("orders", in: session, sort: [SortSpec("unit_price", ascending: false)], profile: false)139        await measure("sorted by unit_price DESC, random offsets", positions: Array(random.prefix(20)))140        DataAutomation.showDataset("orders", in: session, filters: [ColumnFilter(column: "region", kind: .equals, text: "West")], profile: false)141        await measure("filtered region = West, random offsets", positions: random.prefix(30).map { $0 / 6 })142        DataAutomation.showDataset("customers", in: session, profile: false)143        await measure("Parquet lazy view (50,000 rows), random offsets", positions: (0..<30).map { _ in g.below(49_000) })144    }145}146
Files main
Menu

Theme