Skip to content

Commit 23f5413

Browse files
committed
Include timestamp in polling to reduce server load
1 parent 517e227 commit 23f5413

5 files changed

Lines changed: 76 additions & 31 deletions

File tree

src/controllers/poll-controller.php

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ private function getValue($session, $vote)
1919
return intval($value);
2020
}
2121

22+
// Check if the session as changed since the last polling call
23+
private function sessionUnchanged($session)
24+
{
25+
// Check if anything changed since the last polling call
26+
return isset($_GET['last']) && $_GET['last'] >= $session->getLastAction()->getTimestamp();
27+
}
28+
2229
// Start a new poll in the session
2330
private function startPoll($sessionId, $topic)
2431
{
@@ -47,6 +54,7 @@ private function placeVote($sessionId, $memberId, $voteValue)
4754

4855
// Fetch entities
4956
$session = $this->getSession($sessionId);
57+
$session->setLastAction(new DateTime());
5058
$currentPoll = $session->getCurrentPoll();
5159
$member = $this->getMember($memberId);
5260

@@ -57,8 +65,11 @@ private function placeVote($sessionId, $memberId, $voteValue)
5765
// Find or create vote
5866
foreach($currentPoll->getVotes() as $vote)
5967
{
60-
if($vote->getMember() == $member)
61-
$match = $vote;
68+
if($vote->getMember() != $member)
69+
continue;
70+
71+
$match = $vote;
72+
break;
6273
}
6374

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

8394
// Save all to db
84-
$this->saveAll([$match, $currentPoll]);
95+
$this->saveAll([$session, $match, $currentPoll]);
8596
$this->saveAll($currentPoll->getVotes()->toArray());
8697
}
8798

@@ -90,14 +101,23 @@ private function current($sessionId)
90101
{
91102
// Load the user-vote.php required for this
92103
include __DIR__ . "/user-vote.php";
104+
105+
// Create reponse object
106+
$response = new stdClass();
93107

94108
$session = $this->getSession($sessionId);
109+
110+
// Check if anything changed since the last polling call
111+
if($this->sessionUnchanged($session))
112+
{
113+
$response->unchanged = true;
114+
return $response;
115+
}
95116

96-
// Create reponse object
97-
$response = new stdClass();
117+
// Fill response object
98118
$response->name = $session->getName();
119+
$response->timestamp = $session->getLastAction()->getTimestamp();
99120
$response->votes = array();
100-
101121
// Include votes in response
102122
$currentPoll = $session->getCurrentPoll();
103123
if ($currentPoll == null)
@@ -131,23 +151,32 @@ private function current($sessionId)
131151

132152
private function topic($sessionId)
133153
{
134-
$session = $this->getSession($sessionId);
135-
$currentPoll = $session->getCurrentPoll();
136-
137-
// Result object. Only votable until all votes received
138-
$result = new stdClass();
139-
if ($currentPoll == null)
140-
{
141-
$result->topic = "No topic";
142-
$result->votable = false;
143-
}
144-
else
145-
{
146-
$result->topic = $currentPoll->getTopic();
147-
$result->votable = $currentPoll->getResult() < 0;
148-
}
154+
$session = $this->getSession($sessionId);
149155

156+
// Check if anything changed since the last polling call
157+
if($this->sessionUnchanged($session))
158+
{
159+
$result->unchanged = true;
150160
return $result;
161+
}
162+
163+
$currentPoll = $session->getCurrentPoll();
164+
165+
// Result object. Only votable until all votes received
166+
$result = new stdClass();
167+
$result->timestamp = $session->getLastAction()->getTimestamp();
168+
if ($currentPoll == null)
169+
{
170+
$result->topic = "No topic";
171+
$result->votable = false;
172+
}
173+
else
174+
{
175+
$result->topic = $currentPoll->getTopic();
176+
$result->votable = $currentPoll->getResult() < 0;
177+
}
178+
179+
return $result;
151180
}
152181

153182
public function execute()

src/controllers/session-controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ private function getAllSessions()
99
{
1010
// Create query finding all active sessions
1111
$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');
12-
$query->setParameter(1, new DateTime('-2 hour'));
12+
$query->setParameter(1, new DateTime('-1 hour'));
1313
$sessions = $query->getArrayResult();
1414
return $sessions;
1515
}

src/controllers/statistics-controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ private function loadPlugins($filter)
1111
foreach(glob(__DIR__ . '/statistics/*.php') as $file) {
1212
// Check if the plugin was selected in the filter
1313
$key = basename($file, ".php");
14-
if($filter == null || in_array($key, $filter)) {
14+
if($filter == null || sizeof($filter) == 0 || in_array($key, $filter)) {
1515
$plugin = include $file;
1616
$plugins[$key] = $plugin;
1717
}

src/js/main.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -378,12 +378,19 @@ scrum.app.controller('MasterController', function ($http, $routeParams, $locatio
378378
if (scrum.current !== self)
379379
return;
380380

381-
$http.get("/api/poll/current?id=" + self.id).then(function(response){
381+
$http.get("/api/poll/current?id=" + self.id + "&last=" + self.timestamp).then(function(response){
382382
var data = response.data;
383383
var result = data.result;
384-
if(!data.success) {
385-
// Error handling
386-
return;
384+
385+
// Call succeeded, but execution failed
386+
if (!data.success) {
387+
setTimeout(pollVotes, scrum.pollingScale.scale(300));
388+
return;
389+
}
390+
// Session was not modified
391+
if (result.unchanged) {
392+
setTimeout(pollVotes, scrum.pollingScale.scale(300));
393+
return;
387394
}
388395

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

394401
// Copy poll values
395402
self.name = result.name;
403+
self.timestamp = result.timestamp;
396404
self.votes = result.votes;
397405
self.flipped = result.flipped;
398406
self.consensus = result.consensus;
@@ -403,10 +411,10 @@ scrum.app.controller('MasterController', function ($http, $routeParams, $locatio
403411
}
404412

405413
scrum.pollingScale.success();
406-
setTimeout(pollVotes, scrum.pollingScale.scale(300));
414+
setTimeout(pollVotes, scrum.pollingScale.scale(400));
407415
}, function(){
408416
scrum.pollingScale.failed();
409-
setTimeout(pollVotes, scrum.pollingScale.scale(300));
417+
setTimeout(pollVotes, scrum.pollingScale.scale(400));
410418
});
411419
}
412420

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

493501
// Update topic
494-
$http.get("/api/poll/topic?sid=" + self.id).then(function(response){
502+
$http.get("/api/poll/topic?sid=" + self.id + "&last=" + self.timestamp).then(function(response){
495503
var data = response.data;
496504
if(!data.success)
497505
{
498506
self.reset();
507+
setTimeout(update, scrum.pollingScale.scale(500));
499508
return;
500509
}
501510

502511
var result = data.result;
503512

513+
if (result.unchanged) {
514+
setTimeout(update, scrum.pollingScale.scale(500));
515+
return
516+
}
517+
518+
self.timestamp = result.timestamp;
519+
504520
// Voting was closed, get our peers votes
505521
if(self.votable && !result.votable) {
506522

src/model/session.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Session
2929
/** @OneToMany(targetEntity="Poll", mappedBy="session", orphanRemoval=true) **/
3030
protected $polls;
3131

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

3535
public function getId()

0 commit comments

Comments
 (0)