-
timetracker
How would you go about making a timetracker? Now the counter should be start/stopable from multiple devices so I'm obviously thiking of simply saving the start time, after stop comparing it to the current time and update the entry.
Could probably use localstorage to make it work offline on mobiles too.
Can you think of a better way of doing it?
Obviously all using js and php btw.
should perhaps use websockets as well
-
Re: timetracker
Quickly thrown together, this would be a quick first draft that would take around 1 hr to have working:
Schema:
users:
email, name
authentications:
email, key, issued, expires
times:
email, began, completed, duration
Back-end
======
auth.php
---------
GET /auth.php?key=<key>
1. Determine if key exists in authentication table
2. If not exists, goto 6
3. If not still valid, goto 5
4. Return redirect to index, set cookie with session id
5. Return redirect to index with error message indicating key is no longer valid. Do not set cookie.
6. Return redirect to index with error message indicating that key does not exist. Do not set cookie.
GET /auth.php?email=<email>
1. Determine if email exists in users table
2. If not exists, goto 5
3. Create a new entry in authentication table with a randomly generated key, then send an e-mail to the user with the URI http://site.com/auth.php?key=<key>
4. Return 200
5. Return 400 and a message indicating that the user does not exists.
POST /auth.php
email=<email>&name=<string>
1. Check if email already exists in users table
2. If not exists, goto 4
3. Return 400 and message indicating the user already exists.
4. Add email and name in users table, return 200 and set cookie with session id.
time.php
----------
GET /time.php?from=<date>&to=<date>
1. die if session invalid
2. Return aggregate sum of durations that lie between from and to, optionally each time so they can be displayed in a table.
POST /time.php
began=<date>&completed=<date>&duration=<integer>
1. die if session invalid
2. Insert the record into the table for the current user. Dates should be in UTC, ISO-8601 format.
Front-end
======
index.html
------------
Checks for the session cookie. If present, shows the time view and queries time.php to get information to display. Synchronize returned value with values already in local storage. This can be optimized by only asking for any events between the latest value in local storage and the current time, and performing a single-step map/reduce in the browser to update the aggregate and retrieve all times. Begins a timer running every minute or so that will attempt to push any latent time events in local storage to the server. Has a start button that when clicked begins a counter and changes the start button to an end button. When the end button is clicked, the start time, end time, and duration are saved to local storage with a randomly generated key. A POST to time.php is made to save this time. In the callback, if the request was successful, delete the local storage for that time event.
If there is no session cookie, render the login form with a register link. Registration/login is fairly straightforward here. The login form is just an email input box. Performs a GET against auth.php with the given e-mail. If a 200 is received, display a message telling the user to check their inbox. If a 400 is received, display that the email is invalid.
-
Re: timetracker
it's going to be a part of an app, where registration and auth is going to be taken care of
also you can have only one running tracking.. so what I'd do is
user press start button, it makes a request to say start.php.. it probably doesn't need to containt any parameter, it makes an entry with user_id, start_time and status
now let's say the guy switches device, reload page any whatever, when he enters the page, it's gonna check for any entry with status==running, if there's such entry, it's returns start_time and renders running timer
when user clicks the stop button, it's makes request to stop.php again either with no param or with id of the running entry.. on server side it updates the entry with end_time, and duration, changes status to finished and that's it
I want to do something http://toggl.com/ like btw
-
Re: timetracker
The only problem with that is that it won't work offline, and may have issues if used on a device with a latent or unreliable connection, like a phone.