All selected work

Case study · Electron, Next.js, PostgreSQL

WLR Track: a time tracker that cannot trust the clock

An in-house time tracker built instead of paying per seat: an Electron app that keeps working offline, and a database that makes overlapping time impossible to store.

By Ali Raza, Senior AI EngineerElectron, React, SQLite, Next.js, Prisma, PostgreSQL, S3

Nothing about a time tracker is difficult until you remember where it runs: on a computer you do not control, which sleeps mid-afternoon, loses wifi on a train, and has a clock the person can change.

The job

We needed time tracking across a remote team at MindRind. The tools you can buy charge per seat every month, forever, and the screenshots they take of your own staff live on someone else's servers. For a team that was going to keep growing, buying meant a bill that scales with headcount and no say in where the data sits.

So I built it: an Electron app on each computer, one Next.js application serving both the REST API and the dashboard, and a shared package of zod schemas so the two sides can never disagree about what they send each other.

The clock cannot be trusted

Durations are measured with the monotonic clock, which only moves forward and which nobody can set. They are never calculated by subtracting one wall-clock reading from another. The app also keeps an offset between server time and local time, refreshed on every successful API call and saved to disk, so recorded timestamps still mean something on a machine whose clock is forty minutes out.

Every second, the engine compares elapsed wall time against elapsed monotonic time. The difference tells it what happened:

  • The tick is more than 5 seconds late by both clocks: the computer slept or the app froze. The open segment is closed at the last good tick, and the gap is not tracked.
  • Monotonic time advanced normally but the wall clock jumped: someone changed the clock. The segment is closed against its original anchor and a new one starts.

Neither case invents time. Neither case silently loses it.

The network is optional

Everything is written to a local SQLite queue first: time segments, app usage, screenshots and their image files. A worker syncs every 60 seconds and after each segment closes, oldest first, backing off from 30 seconds to 10 minutes while the server is unreachable.

Synced rows are cleared from the device after 7 days. Unsynced rows are never cleared automatically. A tracker that stops is annoying; a tracker that quietly discards a day of someone's work is finished.

Retries must not double-count

Segment and screenshot IDs are generated on the device, not the server, which is what makes an upload safe to repeat. A segment whose ID already exists returns a duplicate and changes nothing. Screenshot upload is presign, PUT, complete, and any step can be retried because the ID keeps it idempotent.

Overlapping time is impossible, not merely unlikely

Time is divided into fixed 10-minute slots aligned to UTC, and a segment never crosses a slot boundary. A PostgreSQL exclusion constraint over the segment's time range, using btree_gist, rejects any two segments of the same user that overlap. Two laptops tracking at once hit it. So does time a manager already deleted, because deleted rows stay inside the constraint, which means a device still holding that time in its queue cannot put it back.

That one constraint replaced a pile of application checks that every new code path would have had to remember. The database remembers instead.

Payroll gets the same treatment: the person's hourly rate is copied onto each segment when the server receives it, so changing someone's rate changes what they earn from then on, not what they earned last month.

What it deliberately does not collect

Activity is the count of seconds in which at least one keyboard or mouse event happened. Key codes, typed text and click positions are never read or stored. A manager can see that someone was active for 480 seconds out of 600. They cannot see what was written.

Screenshots are kept for three days and then deleted by a daily job, from storage and from the database, and an administrator can turn capture off per person.

Idle time stays on the device until the person decides. When input stops for the idle timeout, the tracker asks: keep it, discard and continue, or discard and stop. The segment is not uploaded until that question is answered, so undecided idle time can still be thrown away while it exists only on their own machine.

The rule I took from it

If a correctness rule can live in the schema, it should not live in application code.

Clocks drift, networks fail and retries happen. Each of those is a place where application logic can forget a rule. Pushing the rule down to the monotonic clock, to a client-generated ID or to a database constraint means it holds whether or not the next code path remembers it.

Building something like this?

I am open to remote AI engineering roles and contract work. Tell me about the system you need.