Code Monkey home page Code Monkey logo

heartbeat's Introduction

heartbeat

Heartbeat is a simple, semi-automated working hours tracker -- with multiuser support -- designed to address the basic problem with other simple time recording tools: that no-one can be bothered to fill them in properly, and when they do, they often misjudge their own hours.

It works by fixing the earliest time you log on, and the latest time you log off. You can manually correct any entry.

Licence

Copyright (c) 2017 Harry Burt http://www.harryburt.co.uk.

Made available under the terms of the MIT licence. See LICENSE for more details.

Modules

  • heartbeat (JS, core)
  • heartbeat/api (PHP, core)
  • heartbeat/api/oauth (PHP, optional)
  • heartbeat/table (JS, optional but recommended)
  • heartbeat/timeanddate (JS, required by heartbeat/table)
  • heartbeat/oauth (JS, optional)

Dependencies

  • PHP installed on your server
  • A recent version of jQuery
  • FontAwesome (used in heartbeat/table)
  • js.cookie.js (optional but used in the multi user example below)

Setup

Clone all files into your chosen installation directory. Note that heartbeat.settings.php contains customisable settings.

Creating the database

Using your IDE of choice, create a new SQLite database called heartbeat.db in the installation directory.

Execute the following SQL:

CREATE TABLE hours
(
    id TEXT NOT NULL,
    start INTEGER DEFAULT 0 NOT NULL,
    end INTEGER DEFAULT 0 NOT NULL,
    date INTEGER NOT NULL,
    counter INTEGER DEFAULT 0 NOT NULL,
    adj_start INTEGER DEFAULT 0,
    adj_end INTEGER DEFAULT 0,
    adj_gaps INTEGER DEFAULT 0,
    CONSTRAINT hours_id_date_key PRIMARY KEY (id, date)
);
CREATE TABLE users
(
    id TEXT PRIMARY KEY,
    salt TEXT NOT NULL,
    secret TEXT NOT NULL
);

Creating the frontend

In index.html (or similar), add the dependencies listed above. Then, in a new code section make use of Heartbeat.

Example (single user, $requireAuthentication = false)

$(function() {
	var id = 1, secret = false, init = function(){
			$.heartbeat( { 'id': id }, function( data ) {
				// Success

				// Show us our data now...
				$.query( { 'id': id }, $.setTableData );

				// ...and every sixty seconds from now
				setInterval( function () {
					$.heartbeat( { 'id': id }, function ( data ) {
						var tableData = $.getTableData();
						if( tableData[data.date] === undefined ) {
							// Something's wrong. Fall back to full query.
							$.query( { 'id': id }, $.setTableData );
							return;
						}
						tableData[data.date].end = data.end;
						tableData[data.date].counter += 60;
						$.setTableData( tableData );
					} );
				}, 60 * 1000 );
			}, function () {
				// Failure: retry in 60 seconds
				setTimeout( init, 60 * 1000 );

				// In the meantime show us our old data
				$.query( { 'id': id }, $.setTableData );
			});
		};
		
		$.initAPI( id, secret );
		$.initTable( $( 'body' ), id );
		
		// Update now...
		init();
});

Example (multi user, Google oAuth)

$(function() {
	var id = Cookies.get( 'heartbeat-id' ),
		secret = Cookies.get( 'heartbeat-secret' ),
		init =  function () { /* SAME AS FOR SINGLE USER */ };

	if( id !== undefined ) {
		$.initAPI( id, secret );
		$.initTable( $( 'body' ), id );
		// Update now...
		init();
		return;
	}

	var clientId = YOUR_CLIENT_ID,
		redirectUrl = document.location.href.replace( document.location.hash, '' );
		$.initOauth( clientId, redirectUrl );
		if( $.isRedirectUrl() ) {
			$.verify( function( data ) {
				Cookies.set( 'heartbeat-id', data.id, {expires: 365, path: '', domain: 'harryburt.co.uk'} );
				Cookies.set( 'heartbeat-secret', data.secret, {expires: 365, path: '', domain: 'harryburt.co.uk' } );
				document.location.href = redirectUrl;
			} )
		} else {
			$( '#authorize-button' ).click( $.doRedirect );
		}
});

heartbeat's People

Contributors

jarry1250 avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Forkers

dalavancloud

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.