Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit 25caccf

Browse files
author
Gab
committed
Personalized groups in queue monitoring
1 parent 56fb72e commit 25caccf

File tree

3 files changed

+82
-31
lines changed

3 files changed

+82
-31
lines changed

README.md

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@ redis-priority-queue is a simple work queue similar to [Redis lists](https://red
55
- An item can be added with a priority (between -9007199254740992 and 9007199254740992)
66
- Queues are automatically de-duplicated (duplicate items are voided when pushing them)
77
- Multiple items can be popped from the queue at the same time
8-
- A queue monitoring tool to easily see how many items are in each queue
8+
- A [queue monitoring tool](#queue-monitoring) to easily see how many items are in each queue
99

1010
redis-priority-queue is based on [Redis sorted sets](https://redis.io/commands#sorted_set) and all sorted sets commands can be used alongside this project.
1111

12+
## Clients
13+
14+
- [Python client](clients/python/)
15+
- [PHP client](clients/php/)
16+
1217
## Basic usage
1318

1419
### Bash example
@@ -104,19 +109,27 @@ To use the queue monitor, you need to ensure python is installed and use the fol
104109
### Usage example
105110

106111
```
112+
# Basic usage
107113
./src/queue_monitor.py -H [host] -p [port] (-a [auth] -n [dbnum])
108-
+-------------------+-------+-----------------+----------------+
109-
| Queue Name | Total | Priority <= 100 | Priority > 100 |
110-
+-------------------+-------+-----------------+----------------+
111-
| late_fees_pending | 44 | 12 | 32 |
112-
| new_books | 223 | 123 | 100 |
113-
| book_recycle | 13 | 13 | 0 |
114-
| book_orders | 112 | 56 | 56 |
115-
| book_returns | 1,144 | 1,120 | 24 |
116-
+-------------------+-------+-----------------+----------------+
114+
+-------------------+-------+-----------+----------+
115+
| Queue name | Total | Up to 100 | From 101 |
116+
+-------------------+-------+-----------+----------+
117+
| book_orders | 44 | 12 | 32 |
118+
| book_recycle | 223 | 123 | 100 |
119+
| book_returns | 13 | 13 | 0 |
120+
| late_fees_pending | 112 | 56 | 56 |
121+
| new_books | 1,144 | 1,120 | 24 |
122+
+-------------------+-------+-----------+----------+
123+
124+
# Specify your own groups
125+
./src/queue_monitor.py -H [host] -p [port] (-a [auth] -n [dbnum]) -s "0->1000" -s "1001->2000" -s "2001->3000"
126+
+-------------------+-------+------------+----------------+----------------+
127+
| Queue name | Total | 0 to 1,000 | 1,001 to 2,000 | 2,001 to 3,000 |
128+
+-------------------+-------+------------+----------------+----------------+
129+
| book_orders | 44 | 24 | 9 | 11 |
130+
| book_recycle | 223 | 127 | 40 | 56 |
131+
| book_returns | 13 | 13 | 0 | 0 |
132+
| late_fees_pending | 112 | 58 | 13 | 41 |
133+
| new_books | 1,144 | 1,142 | 2 | 0 |
134+
+-------------------+-------+------------+----------------+----------------+
117135
```
118-
119-
## Clients
120-
121-
- [PHP client](clients/php/)
122-
- [Python client](clients/python/)

src/queue_monitor.py

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#!/usr/bin/env python3
22

3+
# redis-priority-queue -> queue monitor
4+
# Author: Gabriel Bordeaux (gabfl)
5+
# Github: https://github.com/gabfl/redis-priority-queue
6+
# Compatible with python 2 & 3
7+
38
import sys, getopt, redis, argparse
49
from prettytable import PrettyTable
510

@@ -13,35 +18,68 @@
1318
help="Redis server authentification")
1419
parser.add_argument("-n", "--dbnum", type=int, default=0,
1520
help="Redis server database number")
21+
parser.add_argument("-s", "--sort_groups", action='append',
22+
help="Sort groups")
1623
args = parser.parse_args()
1724

25+
def setSortGroups(sortGroups = None):
26+
"""Return the sorting groups, either user defined or from the default list"""
27+
if sortGroups is None: # Default groups
28+
return [('-inf', '+inf'), ('-inf', 100), (101, '+inf')];
29+
else:
30+
groups = [('-inf', '+inf')]; # Default mandatory group (Total)
31+
groups.extend([tuple(map(int, sortGroup.split('->'))) for sortGroup in sortGroups])
32+
return groups;
33+
34+
def getCount(queueName, min, max):
35+
"""Fetches set count from Redis"""
36+
return r.zcount(queueName, min, max);
37+
38+
def getColumnTitle(min, max):
39+
"""Human readable column titles"""
40+
if str(min) == '-inf' and str(max) == '+inf':
41+
return 'Total';
42+
elif str(min) == '-inf':
43+
return 'Up to ' + '{0:,}'.format(max);
44+
elif str(max) == '+inf':
45+
return 'From ' + '{0:,}'.format(min);
46+
else:
47+
return '{0:,}'.format(min) + ' to ' + '{0:,}'.format(max);
48+
49+
def setColumnAlign(titles):
50+
"""Set PrettyTable column alignment"""
51+
global t;
52+
for i, title in enumerate(titles):
53+
if i == 0: # First column (title)
54+
t.align[title] = 'l'; # Left
55+
else: # Any other column
56+
t.align[title] = 'r'; # Right
57+
1858
# Redis connection
1959
r = redis.StrictRedis(host=args.host, port=args.port, db=args.dbnum, password=args.auth)
2060

21-
# Get queues
22-
queueNames = r.smembers('rpq|names')
61+
# Sort groups
62+
sortGroups = setSortGroups(args.sort_groups);
2363

24-
# Table titles
25-
titles=['Queue Name', 'Total', 'Priority <= 100', 'Priority > 100']
64+
# Column titles (queue name, then a column per sorting group)
65+
titles = ['Queue name'];
66+
titles.extend([getColumnTitle(sortGroup[0], sortGroup[1]) for sortGroup in sortGroups]);
2667

2768
# Create table
28-
t = PrettyTable(titles)
69+
t = PrettyTable(titles);
70+
setColumnAlign(titles);
2971

30-
# Column alignment
31-
t.align['Queue Name'] = "l" # Left align queue names
32-
t.align['Total'] = "r" # Right align numbers
33-
t.align['Priority <= 100'] = "r" # Right align numbers
34-
t.align['Priority > 100'] = "r" # Right align numbers
72+
# Get queues
73+
queueNames = r.smembers('rpq|names')
3574

3675
# Add a row par queue
3776
for queueName in sorted(queueNames):
38-
# Get counts
39-
countA = r.zcount(queueName, '-inf', '+inf')
40-
countB = r.zcount(queueName, '-inf', 100)
41-
countC = r.zcount(queueName, 101, '+inf')
77+
# Get row
78+
row = [queueName.decode("utf-8")]
79+
row.extend(['{0:,}'.format(getCount(queueName, sortGroup[0], sortGroup[1])) for sortGroup in sortGroups]);
4280

4381
# Add row
44-
t.add_row([queueName.decode("utf-8"), '{0:,}'.format(countA), '{0:,}'.format(countB), '{0:,}'.format(countC)])
82+
t.add_row(row);
4583

4684
# Print table
4785
print (t);

src/redis-priority-queue.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- redis-priority-queue
22
-- Author: Gabriel Bordeaux (gabfl)
33
-- Github: https://github.com/gabfl/redis-priority-queue
4-
-- Version: 1.0.2
4+
-- Version: 1.0.3
55
-- (can only be used in 3.2+)
66

77
-- Get mandatory vars

0 commit comments

Comments
 (0)