Skip to content

Commit

Permalink
Include timestamp in polling to reduce server load
Browse files Browse the repository at this point in the history
  • Loading branch information
Toxantron committed May 19, 2017
1 parent 517e227 commit 23f5413
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 31 deletions.
71 changes: 50 additions & 21 deletions src/controllers/poll-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ private function getValue($session, $vote)
return intval($value);
}

// Check if the session as changed since the last polling call
private function sessionUnchanged($session)
{
// Check if anything changed since the last polling call
return isset($_GET['last']) && $_GET['last'] >= $session->getLastAction()->getTimestamp();
}

// Start a new poll in the session
private function startPoll($sessionId, $topic)
{
Expand Down Expand Up @@ -47,6 +54,7 @@ private function placeVote($sessionId, $memberId, $voteValue)

// Fetch entities
$session = $this->getSession($sessionId);
$session->setLastAction(new DateTime());
$currentPoll = $session->getCurrentPoll();
$member = $this->getMember($memberId);

Expand All @@ -57,8 +65,11 @@ private function placeVote($sessionId, $memberId, $voteValue)
// Find or create vote
foreach($currentPoll->getVotes() as $vote)
{
if($vote->getMember() == $member)
$match = $vote;
if($vote->getMember() != $member)
continue;

$match = $vote;
break;
}

// Create vote if not found
Expand All @@ -81,7 +92,7 @@ private function placeVote($sessionId, $memberId, $voteValue)
}

// Save all to db
$this->saveAll([$match, $currentPoll]);
$this->saveAll([$session, $match, $currentPoll]);
$this->saveAll($currentPoll->getVotes()->toArray());
}

Expand All @@ -90,14 +101,23 @@ private function current($sessionId)
{
// Load the user-vote.php required for this
include __DIR__ . "/user-vote.php";

// Create reponse object
$response = new stdClass();

$session = $this->getSession($sessionId);

// Check if anything changed since the last polling call
if($this->sessionUnchanged($session))
{
$response->unchanged = true;
return $response;
}

// Create reponse object
$response = new stdClass();
// Fill response object
$response->name = $session->getName();
$response->timestamp = $session->getLastAction()->getTimestamp();
$response->votes = array();

// Include votes in response
$currentPoll = $session->getCurrentPoll();
if ($currentPoll == null)
Expand Down Expand Up @@ -131,23 +151,32 @@ private function current($sessionId)

private function topic($sessionId)
{
$session = $this->getSession($sessionId);
$currentPoll = $session->getCurrentPoll();

// Result object. Only votable until all votes received
$result = new stdClass();
if ($currentPoll == null)
{
$result->topic = "No topic";
$result->votable = false;
}
else
{
$result->topic = $currentPoll->getTopic();
$result->votable = $currentPoll->getResult() < 0;
}
$session = $this->getSession($sessionId);

// Check if anything changed since the last polling call
if($this->sessionUnchanged($session))
{
$result->unchanged = true;
return $result;
}

$currentPoll = $session->getCurrentPoll();

// Result object. Only votable until all votes received
$result = new stdClass();
$result->timestamp = $session->getLastAction()->getTimestamp();
if ($currentPoll == null)
{
$result->topic = "No topic";
$result->votable = false;
}
else
{
$result->topic = $currentPoll->getTopic();
$result->votable = $currentPoll->getResult() < 0;
}

return $result;
}

public function execute()
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/session-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ private function getAllSessions()
{
// Create query finding all active sessions
$query = $this->entityManager->createQuery('SELECT s.id, s.name, s.isPrivate, count(m.id) memberCount FROM Session s LEFT JOIN s.members m WHERE s.lastAction > ?1 GROUP BY s.id');
$query->setParameter(1, new DateTime('-2 hour'));
$query->setParameter(1, new DateTime('-1 hour'));
$sessions = $query->getArrayResult();
return $sessions;
}
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/statistics-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ private function loadPlugins($filter)
foreach(glob(__DIR__ . '/statistics/*.php') as $file) {
// Check if the plugin was selected in the filter
$key = basename($file, ".php");
if($filter == null || in_array($key, $filter)) {
if($filter == null || sizeof($filter) == 0 || in_array($key, $filter)) {
$plugin = include $file;
$plugins[$key] = $plugin;
}
Expand Down
30 changes: 23 additions & 7 deletions src/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,19 @@ scrum.app.controller('MasterController', function ($http, $routeParams, $locatio
if (scrum.current !== self)
return;

$http.get("/api/poll/current?id=" + self.id).then(function(response){
$http.get("/api/poll/current?id=" + self.id + "&last=" + self.timestamp).then(function(response){
var data = response.data;
var result = data.result;
if(!data.success) {
// Error handling
return;

// Call succeeded, but execution failed
if (!data.success) {
setTimeout(pollVotes, scrum.pollingScale.scale(300));
return;
}
// Session was not modified
if (result.unchanged) {
setTimeout(pollVotes, scrum.pollingScale.scale(300));
return;
}

// Query statistics
Expand All @@ -393,6 +400,7 @@ scrum.app.controller('MasterController', function ($http, $routeParams, $locatio

// Copy poll values
self.name = result.name;
self.timestamp = result.timestamp;
self.votes = result.votes;
self.flipped = result.flipped;
self.consensus = result.consensus;
Expand All @@ -403,10 +411,10 @@ scrum.app.controller('MasterController', function ($http, $routeParams, $locatio
}

scrum.pollingScale.success();
setTimeout(pollVotes, scrum.pollingScale.scale(300));
setTimeout(pollVotes, scrum.pollingScale.scale(400));
}, function(){
scrum.pollingScale.failed();
setTimeout(pollVotes, scrum.pollingScale.scale(300));
setTimeout(pollVotes, scrum.pollingScale.scale(400));
});
}

Expand Down Expand Up @@ -491,16 +499,24 @@ scrum.app.controller('MemberController', function MemberController ($http, $loca
if (scrum.current !== self) return;

// Update topic
$http.get("/api/poll/topic?sid=" + self.id).then(function(response){
$http.get("/api/poll/topic?sid=" + self.id + "&last=" + self.timestamp).then(function(response){
var data = response.data;
if(!data.success)
{
self.reset();
setTimeout(update, scrum.pollingScale.scale(500));
return;
}

var result = data.result;

if (result.unchanged) {
setTimeout(update, scrum.pollingScale.scale(500));
return
}

self.timestamp = result.timestamp;

// Voting was closed, get our peers votes
if(self.votable && !result.votable) {

Expand Down
2 changes: 1 addition & 1 deletion src/model/session.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Session
/** @OneToMany(targetEntity="Poll", mappedBy="session", orphanRemoval=true) **/
protected $polls;

/** @ManyToOne(targetEntity="Poll", fetch="EAGER", cascade={"remove"}) **/
/** @ManyToOne(targetEntity="Poll", cascade={"remove"}) **/
protected $currentPoll;

public function getId()
Expand Down

0 comments on commit 23f5413

Please sign in to comment.