Skip to content

Commit d707fcb

Browse files
Add function to returns conuts over a period
1 parent def4ace commit d707fcb

2 files changed

Lines changed: 50 additions & 10 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
/docs/_build
44
/coverage
55
.idea
6-
/nbproject/private/
6+
/nbproject

src/Provider/DoctrineProvider.php

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
class DoctrineProvider extends AbstractProvider
3131
{
3232

33+
const DEFAULT_PERIOD = 300;
34+
3335
protected $em;
3436
protected $repository;
3537
protected static $entityName = 'Uecode\Bundle\QPushBundle\Entity\DoctrineMessage';
@@ -113,7 +115,7 @@ public function getRepository()
113115
if (!$this->repository) {
114116
return;
115117
}
116-
118+
117119
return $this->repository;
118120
}
119121

@@ -127,7 +129,7 @@ public function create()
127129
{
128130
$sm = $this->em->getConnection()->getSchemaManager();
129131
$table = $this->em->getClassMetadata(self::$entityName)->getTableName();
130-
132+
131133
return $sm->tablesExist(array($table));
132134
}
133135

@@ -178,8 +180,7 @@ public function receive(array $options = [])
178180
}
179181

180182
$doctrineMessages = $this->repository->findBy(
181-
array('delivered' => false, 'queue' => $this->name),
182-
array('id' => 'ASC')
183+
array('delivered' => false, 'queue' => $this->name), array('id' => 'ASC')
183184
);
184185

185186
$messages = [];
@@ -202,7 +203,7 @@ public function delete($id)
202203
$doctrineMessage = $this->repository->findById($id);
203204
$doctrineMessage->setDelivered(true);
204205
$this->em->flush();
205-
206+
206207
return true;
207208
}
208209

@@ -218,7 +219,7 @@ public function destroy()
218219
$qb->where('dm.queue = :queue');
219220
$qb->setParameter('queue', $this->name);
220221
$qb->getQuery()->execute();
221-
222+
222223
return true;
223224
}
224225

@@ -248,10 +249,10 @@ public function findBy($data)
248249
$qb->where('p.queue = :queue');
249250
$qb->setParameter('queue', $this->name);
250251

251-
$field = (isset($data['field']))?$data['field']:'message';
252-
252+
$field = (isset($data['field'])) ? $data['field'] : 'message';
253+
253254
if (isset($data['search']) && $data['search'] !== null) {
254-
$qb->andWhere('p.'.$field.' LIKE :contains');
255+
$qb->andWhere('p.' . $field . ' LIKE :contains');
255256
$qb->setParameter('contains', '%' . $data['search'] . '%');
256257
}
257258

@@ -267,4 +268,43 @@ public function findBy($data)
267268

268269
return $qb->getQuery();
269270
}
271+
272+
/*
273+
* Returns an array of times and messgae counts
274+
* @praram $data ['from' => date, 'to' => date, 'period' => seconds
275+
* @return ['time', 'count']
276+
*/
277+
278+
public function counts($data)
279+
{
280+
if (isset($data['period']) && $data['period'] !== null) {
281+
$period = $data['period'];
282+
} else {
283+
$period = self::DEFAULT_PERIOD;
284+
}
285+
286+
$sql = 'SELECT from_unixtime(floor(unix_timestamp(created)/'
287+
. $period . ') * ' . $period . ') as time,
288+
count(*) as count
289+
FROM uecode_qpush_message
290+
where queue = "' . $this->name . '"';
291+
292+
if (isset($data['from']) && $data['from'] !== null) {
293+
$sql = $sql . ' and created >= "' . $data['from'] . '"';
294+
}
295+
296+
if (isset($data['to']) && $data['to'] !== null) {
297+
$sql = $sql . ' and created <= "' . $data['to'] . '"';
298+
}
299+
300+
$sql = $sql . ' group by floor(unix_timestamp(created)/' . $period . ')';
301+
$sql = $sql . ' order by floor(unix_timestamp(created)/' . $period . ') ASC';
302+
303+
$statement = $this->em->getConnection() > prepare($sql);
304+
$statement->execute();
305+
$results = $statement->fetchAll();
306+
307+
return $results;
308+
}
309+
270310
}

0 commit comments

Comments
 (0)