A derived measure, always up to date.
Define a layer of your own with a measure computed from two others. Write the inputs; read the result. The server recomputes it in the same transaction, and refuses anyone who tries to write it.
STARCO_KEY it runs as it is. The TypeScript and CLI tabs use the SDK and the command line, which are not published yet.The idea#
A layer schema lists typed measures. One with an expression is derived: a formula over other measures of the same layer, in a small closed grammar (arithmetic and a few functions such as round). If an input has no value, the derived measure has none either.
The whole program#
export API="https://api.starco.dev/v1"
AUTH="Authorization: Bearer $STARCO_KEY"
JSON="Content-Type: application/json"
SCHEMA=$(curl -s -X POST "$API/schemas" -H "$AUTH" -H "$JSON" -d '{"layer": "hydration", "measures": [
{"name": "water_ml", "type": "integer", "unit": "ml", "min": 0, "max": 10000},
{"name": "weight_kg", "type": "number", "unit": "kg", "min": 1, "max": 400},
{"name": "ml_per_kg", "type": "number", "unit": "ml/kg", "expression": "round(water_ml / weight_kg, 1)"}]}' | jq -r .schema.id)
TWIN=$(curl -s -X POST "$API/twins" -H "$AUTH" -H "$JSON" \
-d '{"template": "food_hydration", "layers": ["wellness"], "schemas": ["'$SCHEMA'"]}' | jq -r .twin.id)
curl -s -X PATCH "$API/twins/$TWIN/state" -H "$AUTH" -H "$JSON" \
-d '{"values": {"hydration.water_ml": 1750, "hydration.weight_kg": 70}}' > /dev/null
curl -s "$API/twins/$TWIN/state/hydration.ml_per_kg" -H "$AUTH" | jq -c '{measure, value, version}'
curl -s -X PUT "$API/twins/$TWIN/state/hydration.ml_per_kg" -H "$AUTH" -H "$JSON" -d '{"value": 30}' | jq -c '{code, inputs}'import os, requests
API = "https://api.starco.dev/v1"
H = {"Authorization": f"Bearer {os.environ['STARCO_KEY']}"}
schema = requests.post(f"{API}/schemas", headers=H, json={"layer": "hydration", "measures": [
{"name": "water_ml", "type": "integer", "unit": "ml", "min": 0, "max": 10000},
{"name": "weight_kg", "type": "number", "unit": "kg", "min": 1, "max": 400},
{"name": "ml_per_kg", "type": "number", "unit": "ml/kg", "expression": "round(water_ml / weight_kg, 1)"}]}).json()["schema"]
twin = requests.post(f"{API}/twins", headers=H, json={
"template": "food_hydration", "layers": ["wellness"], "schemas": [schema["id"]]}).json()["twin"]["id"]
requests.patch(f"{API}/twins/{twin}/state", headers=H, json={
"values": {"hydration.water_ml": 1750, "hydration.weight_kg": 70}}).raise_for_status()
s = requests.get(f"{API}/twins/{twin}/state/hydration.ml_per_kg", headers=H).json()
print(s["measure"], s["value"], s["version"])
r = requests.put(f"{API}/twins/{twin}/state/hydration.ml_per_kg", headers=H, json={"value": 30})
print(r.status_code, r.json()["code"], r.json()["inputs"])import { Client, ValidationError } from '@starco/sdk';
const c = new Client({ baseUrl: "https://api.starco.dev/v1", apiKey: process.env.STARCO_KEY });
const schema = await c.schemas.create("hydration", [
{ name: "water_ml", type: "integer", unit: "ml", min: 0, max: 10000 },
{ name: "weight_kg", type: "number", unit: "kg", min: 1, max: 400 },
{ name: "ml_per_kg", type: "number", unit: "ml/kg", expression: "round(water_ml / weight_kg, 1)" }]);
const twin = (await c.twins.create({ template: "food_hydration", layers: ["wellness"], schemas: [schema.id] })).id;
await c.state.mset(twin, { "hydration.water_ml": 1750, "hydration.weight_kg": 70 });
const s = await c.state.get(twin, "hydration.ml_per_kg");
console.log(s.measure, s.value, s.version);
try {
await c.state.set(twin, "hydration.ml_per_kg", 30);
} catch (e) {
if (!(e instanceof ValidationError)) throw e;
console.log(e.status, e.code); // a derived measure is computed, never written
}export STARCO_BASE_URL="https://api.starco.dev/v1" STARCO_API_KEY="$STARCO_KEY"
cat > hydration.json <<'EOF'
{"layer": "hydration", "measures": [
{"name": "water_ml", "type": "integer", "unit": "ml", "min": 0, "max": 10000},
{"name": "weight_kg", "type": "number", "unit": "kg", "min": 1, "max": 400},
{"name": "ml_per_kg", "type": "number", "unit": "ml/kg", "expression": "round(water_ml / weight_kg, 1)"}]}
EOF
SCHEMA=$(starco --json schemas create hydration.json | jq -r .id)
TWIN=$(starco --json twins create --template food_hydration --layers wellness --schema $SCHEMA | jq -r .id)
starco twin mset $TWIN hydration.water_ml=1750 hydration.weight_kg=70
starco twin get $TWIN hydration.ml_per_kg
starco twin set $TWIN hydration.ml_per_kg 30 || echo "refused: a derived measure is computed, never written"{"measure":"hydration.ml_per_kg","value":25,"version":1}
{"code":"measure_is_derived","inputs":["hydration.water_ml","hydration.weight_kg"]}The other tabs print the same facts in their own format. Ids, hashes and counts depend on your project.
What just happened
1750 ml over 70 kg is 25 ml/kg, computed by the server when the inputs were written. Writing the derived measure answered 422 measure_is_derived and named the inputs to change instead.