Skip to content

Commit

Permalink
Allow logging + analytic page
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Yao committed Apr 8, 2016
1 parent 3193760 commit 2ee1520
Show file tree
Hide file tree
Showing 11 changed files with 247,948 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ Symfony/composer.phar
.idea/workspace.xml
.idea/tasks.xml
.vagrant

Symfony/web/punchCardArray.csv
3 changes: 3 additions & 0 deletions Symfony/app/Resources/views/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
<li>
<a href="/upload">Upload Firmware</a>
</li>
<li>
<a href="/analytic">Analytics</a>
</li>
<li>
<a href="/logout">Logout</a>
</li>
Expand Down
50 changes: 50 additions & 0 deletions Symfony/src/AppBundle/Controller/AnalyticController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Validator\Constraints\DateTime;

class AnalyticController extends Controller
{
/**
* @Route("/analytic", name="analytic")
*/
public function showAction()
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$user = $this->get('security.token_storage')->getToken()->getUser();
$logs = $this->getDoctrine()
->getRepository('AppBundle:Log')
->findByOwner($user->getId());
$punchCardArray = [];
$daysInWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
$csvOpen = fopen("punchCardArray.csv", "w");
fwrite($csvOpen, ",0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23" . PHP_EOL);
foreach ($logs as $log) {
$epoch = intval($log->getTimestamp());
$datetime = new \DateTime("@$epoch");
$day = $datetime->format('N');
$hour = $datetime->format('G');
if (isset($punchCardArray[$day][$hour])) {
$punchCardArray[$day][$hour]++;
} else {
$punchCardArray[$day][$hour] = 1;
}
}
for ($days = 0; $days < 7; $days++) {
fwrite($csvOpen, $daysInWeek[$days]);
for ($hours = 0; $hours < 24; $hours++) {
if (isset($punchCardArray[$days][$hours])) {
fwrite($csvOpen, "," . $punchCardArray[$days][$hours]);
} else {
fwrite($csvOpen, ",0");
}
}
fwrite($csvOpen, PHP_EOL);
}
fclose($csvOpen);
return $this->render('AppBundle:Analytic:index.html.twig',
array('user' => $user, 'logs' => $logs));
}
}
13 changes: 11 additions & 2 deletions Symfony/src/AppBundle/Controller/FlashController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Session\Session;
use AppBundle\Entity\Log;

class FlashController extends Controller
{
Expand All @@ -15,7 +17,14 @@ public function showAction($uniqueUrl)
->getRepository('AppBundle:FirmwareConfig')
->findByUniqueUrl($uniqueUrl);
$decodedFirmware = json_decode($firmware[0]->getBoardUploadConfig());

return $this->render('AppBundle:Flash:index.html.twig', array('firmware' => $firmware[0], 'boardName' => $decodedFirmware->{'name'}, 'boardUpload'=>$decodedFirmware->upload, 'boardBuild'=>$decodedFirmware->build));
$entityManager = $this->getDoctrine()->getManager();
$user = $firmware[0]->getOwner();
$session = new Session();
$log = new Log();
$externalIp = $this->container->get('request_stack')->getCurrentRequest()->getClientIp();
$log->setOwner($user)->setLogType(100)->setReadableLogType("Flash Page Viewed!")->setBrowser(get_browser(null,true)["browser_name_pattern"])->setHasPreviousSession(false)->setIp($externalIp)->setTimestamp(time())->setMetadata(["Logged from FlashController:showAction"])->setSession($session->getId());
$entityManager->persist($log);
$entityManager->flush();
return $this->render('AppBundle:Flash:index.html.twig', array('ipb'=>$externalIp,'ipa'=>$log->getIp(),'firmware' => $firmware[0], 'boardName' => $decodedFirmware->{'name'}, 'boardUpload'=>$decodedFirmware->upload, 'boardBuild'=>$decodedFirmware->build));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use AppBundle\Entity\Log;

class UpdateFlashCountController extends Controller
{
Expand All @@ -17,9 +19,15 @@ public function updateAction(Request $request, $owner)
$entityManager = $this->getDoctrine()->getManager();
$user = $entityManager->getRepository('AppBundle:User')->find($owner);
$user->setTotalFlashCount($user->getTotalFlashCount() + 1);
$session = new Session();
$log = new Log();
$externalIp = $this->container->get('request_stack')->getCurrentRequest()->getClientIp();
$log->setOwner($owner)->setLogType(201)->setReadableLogType("Flash Failed!")->setBrowser(get_browser(null,true)["browser_name_pattern"])->setHasPreviousSession(false)->setIp($externalIp)->setTimestamp(time())->setMetadata(["Logged from UpdateFlashCountController:updateAction"])->setSession($session->getId());
if ($data["result"] == "Upload successful!") {
$user->setSuccessfulFlashCount($user->getSuccessfulFlashCount() + 1);
$log->setLogType(202)->setReadableLogType("Flash Successful!");
}
$entityManager->persist($log);
$entityManager->flush();
return new Response();
}
Expand Down
12 changes: 6 additions & 6 deletions Symfony/src/AppBundle/Entity/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* Logs
*
* @ORM\Entity(repositoryClass="AppBundle\Repository\LogsRepository")
* @ORM\Entity(repositoryClass="AppBundle\Repository\LogRepository")
*
* @SuppressWarnings(PHPMD.ShortVariable)
*/
Expand Down Expand Up @@ -48,14 +48,14 @@ class Log
/**
* @var \DateTime
*
* @ORM\Column(name="timestamp", type="datetime")
* @ORM\Column(name="timestamp", type="integer")
*/
private $timestamp;

/**
* @var int
* @var string
*
* @ORM\Column(name="ip", type="integer", options={"unsigned"=true})
* @ORM\Column(name="ip", type="string", length=255)
*/
private $ip;

Expand Down Expand Up @@ -173,7 +173,7 @@ public function getReadableLogType()
/**
* Set timestamp
*
* @param \DateTime $timestamp
* @param int $timestamp
*
* @return Log
*/
Expand All @@ -187,7 +187,7 @@ public function setTimestamp($timestamp)
/**
* Get timestamp
*
* @return \DateTime
* @return int
*/
public function getTimestamp()
{
Expand Down
16 changes: 16 additions & 0 deletions Symfony/src/AppBundle/Resources/views/Analytic/css.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<style>
body {
padding-top: 90px;
background-color: #fff;
}
text {
fill: black;
font: 16px sans-serif;
cursor: default;
}
.dot-label text {
font-size: 12px;
}
</style>
55 changes: 55 additions & 0 deletions Symfony/src/AppBundle/Resources/views/Analytic/index.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{% extends 'base.html.twig' %}

{% block stylesheets %}
<link rel="stylesheet" href="//cdn.datatables.net/1.10.11/css/dataTables.bootstrap.min.css">
{% include 'AppBundle:Analytic:css.html.twig' %}
{% endblock %}

{% block body %}
<div class="container">
<h3>Activity Punch Card</h3>
<div id="activityChart"></div>
<h3>Logs</h3><br>
{% if logs|length > 0 %}
<div class="table-responsive">
<table id="logsTable" class="table table-condensed table-striped table-hover table-bordered tablesorter">
<thead>
<tr>
<th>Log Description</th>
<th>Timestamp</th>
<th>IP Address</th>
<th>Session ID</th>
<th>Has Previous Session</th>
<th>Browser</th>
</tr>
</thead>
<tbody>
{% for log in logs %}
<tr>
<td>{{ log.readableLogType }}</td>
<td>{{ log.timestamp|date }}</td>
<td>{{ log.ip }}</td>
<td>{{ log.session }}</td>
<td>{{ log.hasPreviousSession?'Yes':'No' }}</td>
<td>{{ log.browser }}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endif %}</h3>
</div>
{% endblock %}

{% block javascripts %}
<script src="//cdn.datatables.net/1.10.11/js/jquery.dataTables.min.js"></script>
<script src="//cdn.datatables.net/1.10.11/js/dataTables.bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.16/d3.min.js"></script>
{% include 'AppBundle:Analytic:js.html.twig' %}
<script>
$(document).ready(function () {
$('#logsTable').DataTable();
});
</script>
{% endblock %}
Loading

0 comments on commit 2ee1520

Please sign in to comment.