Skip to content

Commit

Permalink
This is 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
berrnd committed Jul 27, 2017
1 parent ab5b16e commit 053a375
Show file tree
Hide file tree
Showing 28 changed files with 2,003 additions and 339 deletions.
4 changes: 4 additions & 0 deletions .htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
61 changes: 23 additions & 38 deletions LOCH.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
var LOCH = {};
LOCH.DefaultFrom = moment().subtract(1, "days");
LOCH.DefaultTo = moment().subtract(1, "days");

$(function()
{
var menuItem = $('.nav').find("[data-nav-for-page='" + LOCH.ContentPage + "']");
menuItem.addClass('active');

LOCH.DefaultFrom = moment().subtract(1, "days");
LOCH.DefaultTo = moment().subtract(1, "days");

$.timeago.settings.allowFuture = true;
$('time.timeago').timeago();
});

LOCH.FetchJson = function(url, success, error)
{
Expand Down Expand Up @@ -44,43 +54,18 @@ LOCH.SetupMap = function(mapElementId)
LOCH.Map.addLayer(LOCH.LocationPointsLayer);
}

LOCH.ReloadMap = function(from, to)
LOCH.GetUriParam = function(key)
{
if (moment.isMoment(from) && moment.isMoment(to))
{
LOCH.Map.removeLayer(LOCH.LocationPointsLayer);
LOCH.LocationPointsLayer = new L.FeatureGroup();
LOCH.Map.addLayer(LOCH.LocationPointsLayer);
var currentUri = decodeURIComponent(window.location.search.substring(1));
var vars = currentUri.split('&');

LOCH.FetchJson("/api/get/locationpoints/" + from.format("YYYY-MM-DD") + "/" + to.format("YYYY-MM-DD"),
function(points)
{
for (point of points)
{
L.marker([point.latitude, point.longitude]).addTo(LOCH.LocationPointsLayer);
}

LOCH.Map.fitBounds(LOCH.LocationPointsLayer.getBounds());
document.getElementById("summary-location-points").innerText = points.length;
for (i = 0; i < vars.length; i++)
{
var currentParam = vars[i].split('=');

LOCH.FetchJson("/api/get/statistics/" + from.format("YYYY-MM-DD") + "/" + to.format("YYYY-MM-DD"),
function(statistics)
{
document.getElementById("summary-accuracy-min").innerText = parseFloat(statistics.AccuracyMin).toFixed(0);
document.getElementById("summary-accuracy-max").innerText = parseFloat(statistics.AccuracyMax).toFixed(0);
document.getElementById("summary-accuracy-average").innerText = parseFloat(statistics.AccuracyAverage).toFixed(0);
document.getElementById("summary-distance").innerText = (parseFloat(statistics.Distance) / 1000).toFixed(1);
},
function(xhr)
{
console.error(xhr);
}
);
},
function(xhr)
{
console.error(xhr);
}
);
if (currentParam[0] === key)
{
return currentParam[1] === undefined ? true : currentParam[1];
}
}
}
};
119 changes: 101 additions & 18 deletions LOCH.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,42 @@
use Location\Coordinate;
use Location\Distance\Vincenty;

require_once 'config.php';

class LOCH
{
private $DbConnection;

function GetDbConnection()
private static $DbConnection;
/**
* @return PDO
*/
public static function GetDbConnection($doMigrations = false)
{
if ($this->DbConnection == null)
if ($doMigrations === true)
{
self::$DbConnection = null;
}

if (self::$DbConnection == null)
{
$this->DbConnection = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASSWORD);
self::$DbConnection = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASSWORD);
self::$DbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

if ($doMigrations === true)
{
self::$DbConnection->exec("CREATE TABLE IF NOT EXISTS migrations (migration SMALLINT NOT NULL, execution_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (migration))");
LochDbMigrator::MigrateDb(self::$DbConnection);

if (self::IsDemoInstallation())
{
LochDemoDataGenerator::PopulateDemoData(self::$DbConnection);
}
}
}

return $this->DbConnection;
return self::$DbConnection;
}

function AddLocationPoint($time, $latitude, $longitude, $accuracy)
public static function AddLocationPoint($time, $latitude, $longitude, $accuracy)
{
$db = $this->GetDbConnection();
$db = self::GetDbConnection();

$statement = $db->prepare('INSERT INTO locationpoints (time, latitude, longitude, accuracy) VALUES (:time, :latitude, :longitude, :accuracy)');
$statement->bindValue(':time', $time);
Expand All @@ -32,20 +49,20 @@ function AddLocationPoint($time, $latitude, $longitude, $accuracy)
$statement->execute();
}

function AddCsvData($csvString)
public static function AddCsvData($csvString)
{
$lines = explode(PHP_EOL, $csvString);

foreach ($lines as $line)
{
$parsedLine = str_getcsv($line);
$this->AddLocationPoint($parsedLine[0], $parsedLine[1], $parsedLine[2], $parsedLine[3]);
self::AddLocationPoint($parsedLine[0], $parsedLine[1], $parsedLine[2], $parsedLine[3]);
}
}

function GetLocationPoints($from, $to)
public static function GetLocationPoints($from, $to)
{
$db = $this->GetDbConnection();
$db = self::GetDbConnection();

$statement = $db->prepare('SELECT * FROM locationpoints WHERE time >= :from AND time <= :to');
$statement->bindValue(':from', $from);
Expand All @@ -61,9 +78,9 @@ function GetLocationPoints($from, $to)
return $rows;
}

function GetLocationPointStatistics($from, $to)
public static function GetLocationPointStatistics($from, $to)
{
$db = $this->GetDbConnection();
$db = self::GetDbConnection();

$statement = $db->prepare('SELECT MIN(accuracy) AS AccuracyMin, MAX(accuracy) AS AccuracyMax, AVG(accuracy) AS AccuracyAverage, SUM(distance_to_point_before) AS Distance FROM locationpoints WHERE time >= :from AND time <= :to');
$statement->bindValue(':from', $from);
Expand All @@ -73,9 +90,9 @@ function GetLocationPointStatistics($from, $to)
return $statement->fetch(PDO::FETCH_ASSOC);
}

function CalculateLocationPointDistances()
public static function CalculateLocationPointDistances()
{
$db = $this->GetDbConnection();
$db = self::GetDbConnection();
$distanceCalculator = new Vincenty();

$statementNotCalculatedRows = $db->prepare('SELECT id, time, latitude, longitude FROM locationpoints WHERE distance_to_point_before IS NULL ORDER BY time');
Expand Down Expand Up @@ -115,4 +132,70 @@ function CalculateLocationPointDistances()
$longitudePreviousRow = $row['longitude'];
}
}

/**
* @return boolean
*/
public static function IsDemoInstallation()
{
return file_exists(__DIR__ . '/data/demo.txt');
}

private static $InstalledVersion;
/**
* @return string
*/
public static function GetInstalledVersion()
{
if (self::$InstalledVersion == null)
{
self::$InstalledVersion = file_get_contents(__DIR__ . '/version.txt');
}

return self::$InstalledVersion;
}

/**
* @return boolean
*/
public static function IsValidSession($sessionKey)
{
if ($sessionKey === null || empty($sessionKey))
{
return false;
}
else
{
return file_exists(__DIR__ . "/data/sessions/$sessionKey.txt");
}
}

/**
* @return string
*/
public static function CreateSession()
{
if (!file_exists(__DIR__ . '/data/sessions'))
{
mkdir(__DIR__ . '/data/sessions');
}

$now = time();
foreach (new FilesystemIterator(__DIR__ . '/data/sessions') as $file)
{
if ($now - $file->getCTime() >= 2678400) //31 days
{
unlink(__DIR__ . '/data/sessions/' . $file->getFilename());
}
}

$newSessionKey = uniqid() . uniqid() . uniqid();
file_put_contents(__DIR__ . "/data/sessions/$newSessionKey.txt", '');
return $newSessionKey;
}

public static function RemoveSession($sessionKey)
{
unlink(__DIR__ . "/data/sessions/$sessionKey.txt");
}
}
15 changes: 14 additions & 1 deletion LOCH.phpproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,26 @@
<Compile Include="config-dist.php" />
<Compile Include="index.php" />
<Compile Include="LOCH.php" />
<Compile Include="LochDbMigrator.php" />
<Compile Include="LochDemoDataGenerator.php" />
<Compile Include="views\layout.php" />
<Compile Include="views\login.php" />
</ItemGroup>
<ItemGroup>
<Content Include="bower.json" />
<Content Include="build.bat" />
<Content Include="composer.json" />
<Content Include="LOCH.js" />
<Content Include="mainpage.php" />
<Content Include="LOCH.png" />
<Content Include="views\dashboard.js" />
<Content Include="views\dashboard.php" />
<Content Include="README.md" />
<Content Include="robots.txt" />
<Content Include="style.css" />
<Content Include="version.txt" />
<Content Include="views\login.js" />
</ItemGroup>
<ItemGroup>
<Folder Include="views\" />
</ItemGroup>
</Project>
Binary file added LOCH.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions LochDbMigrator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

class LochDbMigrator
{
public static function MigrateDb(PDO $pdo)
{
self::ExecuteMigrationWhenNeeded($pdo, 1, "
CREATE TABLE IF NOT EXISTS locationpoints (
id INT(11) NOT NULL AUTO_INCREMENT,
time DATETIME NOT NULL,
latitude DECIMAL(10, 8) NOT NULL,
longitude DECIMAL(11, 8) NOT NULL,
accuracy DECIMAL(8, 3) NOT NULL,
import_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
distance_to_point_before INT(11) DEFAULT NULL,
PRIMARY KEY (id),
KEY time (time)
)"
);
}

private static function ExecuteMigrationWhenNeeded(PDO $pdo, int $migrationId, string $sql)
{
$rowCount = $pdo->query('SELECT COUNT(*) FROM migrations WHERE migration = ' . $migrationId)->fetchColumn();
if (intval($rowCount) === 0)
{
$pdo->exec(utf8_encode($sql));
$statement = $pdo->prepare('INSERT INTO migrations (migration) VALUES (:id)');
$statement->bindValue(':id', $migrationId);
$statement->execute();
}
}
}
Loading

0 comments on commit 053a375

Please sign in to comment.