Skip to content

Commit

Permalink
New component - Visitors Chart.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzuk committed Apr 12, 2017
1 parent 89094ae commit addcd6b
Show file tree
Hide file tree
Showing 10 changed files with 247 additions and 25 deletions.
30 changes: 29 additions & 1 deletion app/components/visitors/visitorsController.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
angular.module('visitorsController', ['visitorsService', 'config', 'paginService'])
angular.module('visitorsController', ['visitorsService', 'config', 'paginService', 'chart.js'])

.controller('VisitorsController', ['$scope', 'Visitors', 'Paginator', function($scope, Visitors, Paginator) {

Expand Down Expand Up @@ -38,6 +38,34 @@ angular.module('visitorsController', ['visitorsService', 'config', 'paginService
});
};

$scope.viewChart = function() {
$scope.action = 'chart';
$scope.state = null;
$scope.processing = true;
Visitors.statistics().then(function(response) {
$scope.statisticsData = response.data;
$scope.labels = [];
$scope.series = ['Unique Visitors', 'Navigations Count'];
$scope.colors = ['#97BBCD', '#F2C099'];
$scope.data = [[], []];
angular.forEach($scope.statisticsData, function(value, key) {
$scope.labels.push(value.date);
$scope.data[0].push(value.count);
$scope.data[1].push(value.sum);
});
$scope.datasetOverride = [{ yAxisID: 'y-axis-left' }, { yAxisID: 'y-axis-right' }];
$scope.options = {
scales: {
yAxes: [
{ id: 'y-axis-left', type: 'linear', display: true, position: 'left' },
{ id: 'y-axis-right', type: 'linear', display: true, position: 'right' }
]
}
};
$scope.processing = false;
});
};

$scope.cancelVisitor = function() {
$scope.visitorView = null;
$scope.action = 'list';
Expand Down
4 changes: 4 additions & 0 deletions app/components/visitors/visitorsService.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@ angular.module('visitorsService', [])
return $http.get(config.apiUrl + componentName + '/get_visitor.php?id=' + id);
};

visitorsFactory.statistics = function() {
return $http.get(config.apiUrl + componentName + '/get_statistics.php');
};

return visitorsFactory;
}]);
22 changes: 22 additions & 0 deletions app/components/visitors/visitorsView.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
</div>
<div class="col-sm-8 text-right buttons">
<a href="/searches" class="btn btn-warning">Wyszukiwania</a>
<a href="#" class="btn btn-primary" ng-click="viewChart()">Wykres</a>
<a href="/admin" class="btn btn-danger">Zamknij</a>
</div>
</div>
Expand Down Expand Up @@ -102,6 +103,27 @@
</form>
</div>

<div class="col-lg-12" ng-show="action == 'chart'">
<form class="form" role="form">
<div class="panel panel-primary">
<div class="panel-heading">Wizyty użytkowników oraz ilość nawigacji</div>
<div class="panel-body">
<div ng-if="processing">
<div class="screen-centered">
<div class="text-center">
<div class="loading"><img src="public/img/loader.gif"></div>
</div>
</div>
</div>
<canvas id="line" class="chart chart-line" chart-data="data" chart-labels="labels" chart-series="series" chart-options="options" chart-dataset-override="datasetOverride" chart-click="onClick" chart-colors="colors"></canvas>
</div>
<div class="panel-footer text-center">
<input class="btn btn-warning" type="button" ng-disabled="processing" ng-click="cancelVisitor()" value="Anuluj">
</div>
</div>
</form>
</div>

</div>

</div>
Expand Down
62 changes: 62 additions & 0 deletions app/models/store_visit.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,67 @@
{
}

$date_from = date("Y-m-d", strtotime('-1 days'));
$date_to = date("Y-m-d", time());

try
{
// sprawdza, czy jest wpis w 'stat_main' na wczorajszy dzień:

$query = "SELECT COUNT(*) AS licznik FROM stat_main WHERE date='".$date_from."'";

$statement = $db_connection->prepare($query);

$statement->execute();

$row_item = $statement->fetch(PDO::FETCH_ASSOC);

$data_found = intval($row_item['licznik']);

if ($data_found == 0) // nie ma wpisu - dodajemy dla obu tabel
{
// tabela 'stat_main':

$query = "INSERT INTO stat_main (id, date, start, contact, admin, login, register)
VALUES
(NULL, '".$date_from."',
(SELECT COUNT(*) from visitors WHERE request_uri = '/' AND visited BETWEEN '".$date_from."' AND '".$date_to."'),
(SELECT COUNT(*) from visitors WHERE request_uri = '/contact' AND visited BETWEEN '".$date_from."' AND '".$date_to."'),
(SELECT COUNT(*) from visitors WHERE request_uri = '/admin' AND visited BETWEEN '".$date_from."' AND '".$date_to."'),
(SELECT COUNT(*) from visitors WHERE request_uri = '/login' AND visited BETWEEN '".$date_from."' AND '".$date_to."'),
(SELECT COUNT(*) from visitors WHERE request_uri = '/register' AND visited BETWEEN '".$date_from."' AND '".$date_to."')
)";

$statement = $db_connection->prepare($query);

$statement->execute();

// tabela 'stat_ip':

$query = "SELECT DISTINCT visitor_ip, COUNT(*) FROM visitors
WHERE visited BETWEEN '".$date_from."' AND '".$date_to."'
GROUP BY visitor_ip ORDER BY visitor_ip";

$statement = $db_connection->prepare($query);

$statement->execute();

$rows = $statement->fetchAll(PDO::FETCH_ASSOC);

foreach ($rows as $row)
{
$query = "INSERT INTO stat_ip (id, date, ip, counter) VALUES (NULL, '".$date_from."', '".$row['visitor_ip']."',
(SELECT COUNT(*) FROM visitors WHERE visitor_ip='".$row['visitor_ip']."' AND visited BETWEEN '".$date_from."' AND '".$date_to."'))";

$statement = $db_connection->prepare($query);

$statement->execute();
}
}
}
catch (PDOException $e)
{
}

?>

35 changes: 35 additions & 0 deletions app/models/visitors/get_statistics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

include dirname(__FILE__) . '/../../db/connection.php';
include dirname(__FILE__) . '/../../db/access.php';
include dirname(__FILE__) . '/../../db/setting.php';

$result = array();

$dbc = connect();

if (check_access($dbc)) // if user rights are sufficient, get database content
{
$visitors_period = get_setting_value($dbc, 'visitors_period');
$visitors_excluded = get_setting_value($dbc, 'visitors_excluded');

$date_from = date("Y-m-d", strtotime($visitors_period));
$date_to = date("Y-m-d", time());

$query = "SELECT date AS date, COUNT(*) AS count, SUM(counter) AS sum" .
" FROM stat_ip" .
" WHERE date BETWEEN '".$date_from."' AND '".$date_to."'" .
" AND ip NOT IN (". $visitors_excluded .")" .
" GROUP BY date" .
" ORDER BY date LIMIT 1000";

$statement = $dbc->prepare($query);

$statement->execute();

$result = $statement->fetchAll(PDO::FETCH_ASSOC);
}

echo json_encode($result);

?>
14 changes: 14 additions & 0 deletions assets/js/Chart.min.js

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions assets/js/angular-chart.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions assets/js/angular-chart.min.js.map

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
<script src="assets/js/angular-ui-bootstrap.js"></script>
<script src="assets/js/bootstrap-switch.min.js"></script>
<script src="assets/js/ngMeta.min.js"></script>
<script src="assets/js/Chart.min.js"></script>
<script src="assets/js/angular-chart.min.js"></script>
<script src="public/js/app.js"></script>

<!-- application -->
Expand Down
Loading

0 comments on commit addcd6b

Please sign in to comment.