// An export of page views as an Avro file for the data team, and the import that reads it.
//
//   quillo run avro.qwe      then open http://localhost:8080/export

use "avro.qwer"

let SCHEMA = {
    type: "record", name: "PageView", namespace: "shop.analytics",
    fields: [
        { name: "path", type: "string" },
        { name: "visitor", type: "long" },
        { name: "seconds", type: "double" },
        { name: "country", type: ["null", "string"], default: null },
        { name: "device", type: { type: "enum", name: "Device", symbols: ["DESKTOP", "MOBILE", "TABLET"] } },
    ],
}

let VIEWS = [
    { path: "/", visitor: 1001, seconds: 12.5, country: "NL", device: "DESKTOP" },
    { path: "/products/b7", visitor: 1002, seconds: 48.0, country: null, device: "MOBILE" },
    { path: "/checkout", visitor: 1001, seconds: 95.25, country: "NL", device: "DESKTOP" },
]

route GET /export {
    let n = avro_write("data/views.avro", SCHEMA, VIEWS)?
    { written: n, read_back: avro_read("data/views.avro", 0)?, schema_name: avro_schema("data/views.avro")?.name }
}

route GET /bad = { wrong_type: avro_write("data/bad.avro", SCHEMA, [{ path: "/", visitor: "not a number", seconds: 1.5, device: "DESKTOP" }]).error }
