The Motivation

Off The Clock was initially built to analyze phone usage data pulled from a database, but I also wanted to be able to analyze transient data. Storing transient data in a database is a great way to accomplish this; it allows the analysis code to treat the transient data as if it were persistent, thus avoiding unnecessary duplication of code and providing a consistent analysis.

The Solution

Storing transient data in a database was actually quite simple. The first step was to understand that all of the user’s data has to be tied to their current session. For all intents and purposes, their current session acts like their “account”. That means that the session id works as the account id.

With that in mind, I decided that until a user registered, I could just store the relevant data in the database along with the current session id, which is automatically generated by PHP.

So, the corresponding table would look a little like this:


CREATE TABLE usage_data
(
  call_id INT NOT NULL PRIMARY KEY,
  call_duration INT,
  the_session_id TEXT
)

And I can insert the values into the database like this:


mysql_query("
  INSERT INTO usage_data
    (call_duration, the_session_id)
  VALUES (" .
    "'" . $callDuration . "'," .
    "'" . session_id() . "'" .
  ")"
);

With this design, the code that fetches and reports on the data can match the current session id with the session id in the database, and report only on data uploaded during the current session. Every query just needs to have the current session id in the WHERE clause, like so:


mysql_query("
  SELECT call_duration
  FROM usage_data
  WHERE the_session_id = '" . session_id() . "'" .
  ")"
);

As an extra bonus, the code doesn’t need to be duplicated and re-written to report on one-off data sets. This also means that a user can upload and get stats and multiple phone bills within a single session, without having to register for an account.

In short, this allows the tool to function in demo mode by default.

The next step is to determine the best way to allow a user to save their session by way of registering for an account.