#Time and real time in eLucid8
This page is for anyone whose data arrive over time. It follows one story from start to end: a delivery van reports its position along its route at irregular times; the depot changes the delivery plan now and then; and a traffic data service, which sometimes answers slowly, estimates the delay ahead. Each section adds to the same program. It assumes the basics of the tutorial: values over dimensions, bounds and inputs.
#1. Two kinds of time
The van's reports are numbered as they come: report 0, report 1, report 2. That number, r, is a position in the stream: "the third report", the way a row number says "the third row". It says nothing about how many seconds have passed.
The time each report was made is its timestamp, in milliseconds, given with the data. Every value computed from the reports carries the time of the newest report it read (Values and timestamps).
eLucid8 keeps the two apart, and connects them only where the program says so. The reason is simple: the same data always give the same answer, whenever and however fast the answer is computed. Counting the reports does not depend on when they were made, so two very different sets of times give the same counts; only the timestamps differ:
-- The van's reports, counted: with reports a minute apart.
position = input("position", r : 4)
reports = restrict 1 fby.r reports + 1 to (r : 4) end
gone = position - first.r position
-- table position = r,value,ts; 0,0,0; 1,1,60000; 2,2,120000; 3,3,180000
-- expect reports[r = 3] = 4
-- expect gone[r = 3] = 3, at time 180000 ms
-- The same reports, at irregular times: the same answers.
position = input("position", r : 4)
reports = restrict 1 fby.r reports + 1 to (r : 4) end
gone = position - first.r position
-- table position = r,value,ts; 0,0,0; 1,1,5; 2,2,7; 3,3,86400000
-- expect reports[r = 3] = 4
-- expect gone[r = 3] = 3, at time 86400000 ms
Timestamps change answers only where the program reads them: time(e), and the three ways of choosing data by time, asof, hold and window (section 3).
#2. Where times come from, and missing reports
Times come with the data: in a table, a ts column gives each row's timestamp, in milliseconds (in a deployment, the column deploy.json names). A row without one has timestamp 0.
The time now comes the same way, as data. eLucid8 has no now: a value that changed with the moment it was asked would give two answers to one question, so nothing could be reused, recomputed exactly after a correction or explained. A program that needs the time reads a clock input: in deploy.json, {"from": {"builtin": "clock", "every": "1s"}} gives it a row a second while elucid8 serve runs, at t = 0, 1, 2, …, each row's value and timestamp the tick's UTC time in milliseconds since 1970 (section 5 follows a program live). Recorded like any input, the clock can be replayed, and gives the same answers again. Here the depot asks, at each tick, how long ago each report was made:
-- How long ago each report was made, at each tick of the clock.
clock = input("clock", t : inf)
position = input("position", r : 3)
since = time(clock) - time(position)
-- table clock = t,value,ts; 0,60000,60000; 1,120000,120000
-- table position = r,value,ts; 0,0,0; 1,3.5,70000; 2,8,100000
-- expect since[t = 1, r = 1] = 50000, at time 120000 ms
-- expect since[t = 1, r = 2] = 20000, at time 120000 ms
-- expect since[t = 2, r = 1] = eod, at time 70000 ms
since is over t and r: at each tick, every report's age. Past the clock's last tick it is eod; under serve, the newest tick is last, so since[t = last, r = 2] is the age now. In the playground or a test, a table stands in for the clock, as here.
Sometimes the van reports but its position is missing (the GPS lost its fix): the report's row has an empty cell. That is missing data, an error of kind missing, at the time the report was made; it is not eod, which means the data have ended. Anything computed from it is missing too, and says so, never a guess:
-- A report without a position: missing data, at the time it was made.
position = input("position", r : 4)
moved = position - position[r = #r - 1]
known = not iserror(position, "missing")
-- table position = r,value,ts; 0,0,0; 1,3.5,70000; 2,,130000; 3,8,190000
-- expect moved[r = 1] = 3.5, at time 70000 ms
-- expect moved[r = 2] = error missing from position[r = 2]: input position has no data at this point, at time 130000 ms
-- expect known[r = 2] = false, at time 130000 ms
-- expect moved[r = 3] = error missing from position[r = 2]: input position has no data at this point, at time 190000 ms
A report with no row at all, inside the 4 reports declared, is missing data too (with no time of its own). Where a value is wanted in place of a missing one, the program or the deployment says which: default(position, 0, "missing") in an equation, or "fill": {"missing": 0} in the input's entry in deploy.json (Where data comes from and where it goes); for the last known position instead, a search steps back over the gap with skip missing (section 3).
#3. Streams at different rates
The depot's plan says how far along the route the van should get: 12 km at first, 20 km from 260 s, 25 km from 450 s. Plans come at their own times, not at the reports' times, so the two streams are combined by time:
time (s) 0 70 190 250 260 400 450 460
reports r0 r1 r2 r3 (no position) r4 r5
plans p0 (12 km) p1 (20 km) p2 (25 km)
Three questions, three ways of choosing by time:
- The plan in force at each report:
asof.p plan at T endis the plan with the latest timestamp at or before T. - The last known position at each change of plan:
hold.r position at T within M skip missing endis the same, but it steps back over missing reports (holdsteps back over eod;skip missingadds missing data); if the last known one isMms older than T or more, it gives missing data while a later report exists (the reports stopped and resumed), and eod past the newest report. - The reports in the last 5 minutes:
window.r 5min at Tis a constraint of a region: the positions alongrwhose timestamps are in (T − 5 minutes, T].count position over (r : window.r 5min at T) endcounts them. A missing one is left out, with a warning saying how many were;skip missingbeforeendleaves them out without it.
-- The van, the plan in force, and the reports of the last 5 minutes.
position = input("position", r : 6)
plan = input("plan", p : 3)
target = asof.p plan at time(position) end
to_go = target - position
last_seen = hold.r position at time(plan) within 300000 skip missing end
recent = count position over (r : window.r 300000 at time(position)) skip missing end
-- table position = r,value,ts; 0,0,0; 1,3.5,70000; 2,8,190000; 3,,250000; 4,11,400000; 5,14.5,460000
-- table plan = p,value,ts; 0,12,0; 1,20,260000; 2,25,450000
-- expect to_go[r = 2] = 4, at time 190000 ms
-- expect to_go[r = 4] = 9, at time 400000 ms
-- expect to_go[r = 5] = 10.5, at time 460000 ms
-- expect last_seen[p = 1] = 8, at time 260000 ms
-- expect recent[r = 4] = 2, at time 400000 ms
- At report 4 (400 s) the plan in force is p1 (from 260 s): 20 − 11 = 9 km to go. At report 5 (460 s) it is p2: 25 − 14.5 = 10.5.
- When plan p1 came (260 s), the latest report (250 s) had no position, so
holdstepped back to report 2: 8 km, made at 190 s, within 5 minutes. - At report 4 the last 5 minutes are (100 s, 400 s]: reports 2, 3 and 4, of which 3 has no position, so 2.
Each answer's timestamp is the newest data it read: an answer never claims to know more than its data did.
A stale hold at a stream's newest tick. A live answer reads the data present. Two thermometers report once a second, each reading stamped with its tick's time; room 1's goes quiet after tick 1. Followed live, at tick 5, the newest, hold finds no reading of room 1 in (2 s, 5 s] and none after it yet: the data end there for now, and the answer is eod:
-- Two rooms' thermometers, followed live: the data so far end at tick 5.
temp = input("temp", room : 2, tick : 10)
current = restrict hold.tick temp at #tick * 1000 within 3000 end
to (room : 2, tick : 10) end
-- table temp = tick,room,value,ts; 0,0,20,0; 0,1,18,0; 1,0,20.5,1000; 1,1,18.2,1000; 2,0,21,2000; 3,0,21,3000; 4,0,21.5,4000; 5,0,22,5000
-- expect current[room = 0, tick = 5] = 22, at time 5000 ms
-- expect current[room = 1, tick = 5] = eod
Room 1 reports again at tick 6. Now the same point has readings before its window and after it, so it is missing data, stamped with the last reading before the gap:
-- The same, once tick 6 has arrived.
temp = input("temp", room : 2, tick : 10)
current = restrict hold.tick temp at #tick * 1000 within 3000 end
to (room : 2, tick : 10) end
-- table temp = tick,room,value,ts; 0,0,20,0; 0,1,18,0; 1,0,20.5,1000; 1,1,18.2,1000; 2,0,21,2000; 3,0,21,3000; 4,0,21.5,4000; 5,0,22,5000; 6,0,22,6000; 6,1,18.5,6000
-- expect current[room = 1, tick = 5] = error missing: hold: no data within 3000 ms before 5000, at time 1000 ms
-- expect current[room = 1, tick = 6] = 18.5, at time 6000 ms
A batch run over all the data gives missing data at tick 5 at once. Each answer is right for the data it has: elucid8 serve (section 5) answers each tick from the readings it holds, and the same query after the next advance reads the new ones.
#4. A data service with a deadline
A traffic data service estimates the delay ahead, in minutes, from the distance to go. It is an outside program, asked at a point as any input is read, traffic[km = to_go], and it sometimes answers slowly. Its deadline is 500 ms; an answer that comes later is late, an error that says so, and the program falls back to 2 minutes a kilometre:
-- The traffic data service, and an estimate that keeps its deadline.
position = input("position", r : 6)
plan = input("plan", p : 3)
target = asof.p plan at time(position) end
to_go = target - position
traffic = input("traffic", km : inf)
delay = traffic[km = to_go]
eta = if iserror(delay) then 2 * to_go else 2 * to_go + delay
-- table position = r,value,ts; 0,0,0; 1,3.5,70000; 2,8,190000; 3,,250000; 4,11,400000; 5,14.5,460000
-- table plan = p,value,ts; 0,12,0; 1,20,260000; 2,25,450000
-- service traffic: answers formula x1 / 2; takes 100 to 800 ms; seed 5; deadline 500 ms
-- expect delay[r = 2] = 2, at time 190227 ms
-- expect eta[r = 2] = 10, at time 190227 ms
-- expect delay[r = 1] = error late from traffic[km = 8.5]: late (70764 ms > deadline 70500 ms), at time 70500 ms
-- expect eta[r = 1] = 17, at time 70500 ms
A question starts at the time of the data it is asked with (190 s for report 2), and its answer is stamped with the time it arrived, 227 ms later. The answer for report 1 took 764 ms, past its deadline, so eta used the rule: 2 × 8.5 = 17. That traffic is answered on demand, its deadline, what to try again and whether to reuse answers are the deployment's settings, not part of the program (Where data comes from and where it goes); retries are described in the Data services reference.
#5. Following it live with elucid8 serve
elucid8 serve keeps the program loaded and takes new data as they arrive (The long-running service). A standing query, a subscription, is answered at once and then again whenever new data change its answer; last in a query is the newest report.
The van followed live. The program, compiled to its plan (the playground's Download gives it):
-- The van's distance to go, by the plan in force at each report.
position = input("position", r : 1000)
plan = input("plan", p : 100)
target = asof.p plan at time(position) end
to_go = target - position
So far, three reports, in position.csv:
r,value,ts
0,0,0
1,3.5,70000
2,8,190000
and one plan, in plan.csv:
p,value,ts
0,12,0
The configuration, deploy.json, says where each input comes from and its time column, and keeps a point cache, so that each answer reuses what earlier ones computed:
{ "format": "elucid8-deploy/2",
"inputs": { "position": { "from": "position.csv", "time": "ts" }, "plan": { "from": "plan.csv", "time": "ts" } },
"options": { "pointCache": true } }
Start it with ./elucid8 serve plan.json --config deploy.json. The client subscribes to the distance to go at the newest report; then a report arrives at 400 s, the depot sends a new plan from 260 s, and the client asks for the last three reports. It waits for each reply before sending the next request:
{"id": 1, "op": "subscribe", "query": "to_go[r = last]"}
{"id": 2, "op": "advance", "input": "position", "rows": "3,11,400000\n"}
{"id": 3, "op": "advance", "input": "plan", "rows": "1,20,260000\n"}
{"id": 4, "op": "query", "query": "to_go[r = last - 2 .. last]"}
{"id": 5, "op": "unsubscribe", "target": 1}
The replies, in the order they come (each answer's report, with timings and counts, is shortened to {…}):
{"id":1,"status":"done","run":1,"unchanged":0,"results":[{"name":"to_go","context":{"r":2},"outcome":{"kind":"value","value":4,"ts":190000}}],"versions":{"plan":"7f13cb5fc3877396","position":"60a66390f8189d5c"},"report":{…}}
{"id":2,"status":"done","input":"position","version":"d650b01d9545c27a","added":["d650b01d9545c27a"],"rows":1,"versions":{"plan":"7f13cb5fc3877396","position":"d650b01d9545c27a"}}
{"id":1,"status":"done","run":2,"unchanged":0,"results":[{"name":"to_go","context":{"r":3},"outcome":{"kind":"value","value":1,"ts":400000}}],"versions":{"plan":"7f13cb5fc3877396","position":"d650b01d9545c27a"},"report":{…}}
{"id":3,"status":"done","input":"plan","version":"4a6345c0607fe247","added":["4a6345c0607fe247"],"rows":1,"versions":{"plan":"4a6345c0607fe247","position":"d650b01d9545c27a"}}
{"id":1,"status":"done","run":3,"unchanged":0,"results":[{"name":"to_go","context":{"r":3},"outcome":{"kind":"value","value":9,"ts":400000}}],"versions":{"plan":"4a6345c0607fe247","position":"d650b01d9545c27a"},"report":{…}}
{"id":4,"status":"done","results":[{"name":"to_go","context":{"r":1},"outcome":{"kind":"value","value":8.5,"ts":70000}},{"name":"to_go","context":{"r":2},"outcome":{"kind":"value","value":4,"ts":190000}},{"name":"to_go","context":{"r":3},"outcome":{"kind":"value","value":9,"ts":400000}}],"versions":{"plan":"4a6345c0607fe247","position":"d650b01d9545c27a"},"report":{…}}
{"id":5,"status":"done","target":1,"found":true}
{"id":1,"status":"ended","runs":3,"reason":"unsubscribed"}
How to read it:
- Run 1 answers at once: at report 2 the plan is 12 km, so 12 − 8 = 4 km to go, at 190 s.
- Request 2 adds report 3 at 400 s, so
lastmoves, and the subscription answers for it: still the first plan, 12 − 11 = 1 km. - Request 3 adds the plan from 260 s. Report 3 was made after it, so its answer changes, to 20 − 11 = 9, and the subscription sends it. Reports made before 260 s keep their answers.
- Request 4 gives the last three reports: 8.5, 4 and 9.
- Every reply names the versions of the data it used, and every answer carries its timestamp. Each answer is the one a fresh run of the same query gives on those versions.
The sensor-field example follows a 256 × 256 grid of sensors this way, one tick a second.
#6. Watchdogs, rate limits and sessions
Three common real-time tasks, each a few equations. Each is also an example in the playground's Real time group, with a picture: open it, ask for any value at any point, and change the program or the data.
A watchdog on a heartbeat. A service sends a beat about once a second; a watchdog checks every second how long it has been since the last beat, and raises an alarm after 3 seconds of silence. The checks are a clock written in the program: the check at coordinate second is made at #second * 1s. asof finds the last beat at or before each check, so every answer carries the time of the beat it was decided from. Before the first beat nothing has been heard, and the answer is eod, not an alarm:
-- A watchdog: how long since the last beat, checked every second.
beat = input("beat", b : 10)
sent = restrict time(beat) to (b : 10) end
last_beat = restrict asof.b sent at #second * 1s end
to (second : 16) end
silent = #second * 1s - last_beat
alarm = silent > 3s
-- table beat = b,value,ts; 0,1,500; 1,1,1500; 2,1,2500; 3,1,3500; 4,1,4500; 5,1,11500; 6,1,12500; 7,1,13500; 8,1,14500; 9,1,15500
-- expect silent[second = 0] = eod
-- expect alarm[second = 7] = false, at time 4500 ms
-- expect silent[second = 10] = 5500, at time 4500 ms
-- expect alarm[second = 10] = true, at time 4500 ms
-- expect silent[second = 12] = 500, at time 11500 ms
-- expect alarm[second = 12] = false, at time 11500 ms
- The service went quiet after the beat at 4.5 s. At second 10, the last beat heard is still that one: 5.5 s of silence, and the alarm, both stamped 4500 ms.
- The beats resume at 11.5 s; at second 12 the silence is 0.5 s and the alarm is off, stamped with the new beat's time.
- Open it in the playground:
silentis drawn as a line oversecondthat climbs through the gap.
Rate limiting logins. Each user may try to log in at most 3 times in any 10 seconds. A window by time counts the user's attempts in the 10 seconds up to each attempt; the count reads only attempts up to that one, so its timestamp is the attempt's own time. Where an attempt is blocked, the oldest attempt in the window says how long to wait:
-- At most 3 login attempts in any 10 seconds, per user.
attempt = input("logins", user : {"ann", "bob", "cy"}, i : 6)
tried = restrict time(attempt)
to (user : {"ann", "bob", "cy"}, i : 6) end
recent = restrict count attempt over (i : window.i 10s at time(attempt)) end
to (i : 6) end
blocked = recent > 3
oldest = min tried over (i : window.i 10s at time(attempt)) end
retry_in = if blocked then oldest + 10s - time(attempt) else 0
n_blocked = count attempt over (i : 6) if blocked end
-- table logins = user,i,value,ts; ann,0,1,1000; ann,1,1,15000; ann,2,1,31000; ann,3,1,47000; ann,4,1,52000; ann,5,1,70000; bob,0,1,2000; bob,1,1,20000; bob,2,1,21000; bob,3,1,22500; bob,4,1,23000; bob,5,1,24000; cy,0,1,5000; cy,1,1,6000; cy,2,1,7000; cy,3,1,40000; cy,4,1,41000; cy,5,1,60000
-- expect recent[user = "bob", i = 3] = 3, at time 22500 ms
-- expect recent[user = "bob", i = 4] = 4, at time 23000 ms
-- expect blocked[user = "bob", i = 4] = true, at time 23000 ms
-- expect retry_in[user = "bob", i = 4] = 7000, at time 23000 ms
-- expect recent[user = "cy", i = 2] = 3, at time 7000 ms
-- expect n_blocked[user = "bob"] = 2, at time 24000 ms
-- expect n_blocked[user = "ann"] = 0, at time 70000 ms
- bob's attempts at 20, 21, 22.5 and 23 s are four in 10 seconds: the fourth is blocked, and bob may try again 7 s later, when the attempt at 20 s is 10 seconds old.
- cy's three attempts at 5, 6 and 7 s are within the limit.
- Open it in the playground:
recentis drawn as a grid over user and attempt, where bob's burst stands out.
Sessions from clicks. A visitor's clicks on a website; a gap of more than 30 minutes ends a session, and the next click starts a new one. The time since the click before is time(click) - time(click[i = #i - 1]), and the session's number and start are carried from click to click with fby. restrict … to (i : 12) end bounds starts to the twelve clicks; the values computed from it share that bound:
-- Sessions: a gap of more than 30 minutes starts a new one.
click = input("clicks", i : 12)
gap = time(click) - time(click[i = #i - 1])
starts = restrict if #i == 0 then true else gap > 30min
to (i : 12) end
session = if starts then (0 fby.i session) + 1
else 0 fby.i session
began = if starts then time(click) else (0 fby.i began)
minutes = (time(click) - began) / 1min
pages = if starts then 1 else (0 fby.i pages) + 1
-- table clicks = i,value,ts; 0,1,0; 1,1,120000; 2,1,600000; 3,1,900000; 4,1,4800000; 5,1,4860000; 6,1,5400000; 7,1,8400000; 8,1,8700000; 9,1,8820000; 10,1,9000000; 11,1,9600000
-- expect gap[i = 0] = eod
-- expect session[i = 3] = 1, at time 900000 ms
-- expect gap[i = 4] = 3900000, at time 4800000 ms
-- expect session[i = 4] = 2, at time 4800000 ms
-- expect session[i = 11] = 3, at time 9600000 ms
-- expect minutes[i = 11] = 20, at time 9600000 ms
-- expect pages[i = 11] = 5, at time 9600000 ms
- The gaps of 65 minutes (before click 4) and 50 minutes (before click 7) start sessions 2 and 3. The third session began at 140 minutes and has lasted 20 minutes, with 5 pages, by click 11.
- Each answer is stamped with its click's time: a session's number is known as soon as its click arrives, from the clicks up to it.
- Open it in the playground:
sessionis drawn as a line over the clicks that steps up at each new session.
To follow any of these live, keep the program loaded with elucid8 serve, as in section 5: a standing query is answered again whenever new beats, attempts or clicks change its answer.
#7. What is not promised
eLucid8 states timing; it does not guarantee it.
- No hard real-time guarantee. Nothing bounds how long a query takes to compute: it depends on how much the query needs, what is already cached, and the machine.
- Timing is reported, not enforced. A late answer is a result, not something prevented. A slow question is not cancelled at its deadline; its answer is marked late when it comes. Only inputs answered on demand have deadlines; computing a value has none.
- Simulated time is reproducible; real time is not. The playground's data services and the built-in ones take a stated time, so
lateis the same on every run. A real program that does not report how long it took is timed as it runs, so whether its answer is late can differ between runs. - Each query sees fixed data. Data change between queries (with
advance), never during one. - One request at a time.
elucid8 serveanswers requests in turn.
#8. Where to read more
- Values and timestamps: every rule for timestamps, and what they are good for.
- Where data comes from and where it goes: data sources, data services and outputs, in the playground and in a deployment.
- The long-running service:
advance,lastand subscriptions in full, versions, caches and budgets. - Data services: what every question does, deadlines and retries.
- Examples: a sensor field at full size, followed live.
About this page: every program on it is checked by a test (real-time.test.ts) in the playground's engine, and the session in section 5 against the elucid8 command. The programs run as written in the playground, in the language's notation v0.2. Comment lines give their data and answers, as in the tutorial: -- table x = r,value,ts; 0,0,0; … is an input's data as a table, one row per ;, each row's timestamp in the ts column; -- service traffic: … says that an input is answered on demand, with its settings; -- expect v[r = 3] = … is a query and its answer, ending , at time N ms when the answer's timestamp is not 0.