mirror of
https://github.com/mail-in-a-box/mailinabox.git
synced 2025-04-03 00:07:05 +00:00
60 lines
2.0 KiB
Python
60 lines
2.0 KiB
Python
#####
|
|
##### This file is part of Mail-in-a-Box-LDAP which is released under the
|
|
##### terms of the GNU Affero General Public License as published by the
|
|
##### Free Software Foundation, either version 3 of the License, or (at
|
|
##### your option) any later version. See file LICENSE or go to
|
|
##### https://github.com/downtownallday/mailinabox-ldap for full license
|
|
##### details.
|
|
#####
|
|
|
|
import datetime
|
|
from .DictCache import DictCache
|
|
|
|
#
|
|
# because of the table scan (select_2 below), cache stats for 5
|
|
# minutes
|
|
#
|
|
last_stats = DictCache(datetime.timedelta(minutes=5))
|
|
|
|
def clear_cache():
|
|
last_stats.reset()
|
|
|
|
|
|
def capture_db_stats(conn):
|
|
|
|
stats = last_stats.get()
|
|
if stats:
|
|
return stats
|
|
|
|
select_1 = 'SELECT min(`min`) AS `min`, max(`max`) AS `max`, sum(`count`) AS `count` FROM (SELECT min(connect_time) AS `min`, max(connect_time) AS `max`, count(*) AS `count` FROM mta_connection UNION SELECT min(connect_time) AS `min`, max(connect_time) AS `max`, count(*) AS `count` FROM imap_connection)'
|
|
|
|
# table scan
|
|
select_2 = 'SELECT disposition, sum(count) as `count` FROM (SELECT disposition, count(*) AS `count` FROM mta_connection GROUP BY disposition UNION SELECT disposition, count(*) AS `count` FROM imap_connection GROUP BY disposition) GROUP BY disposition'
|
|
|
|
c = conn.cursor()
|
|
stats = {
|
|
# all times are in this format: "YYYY-MM-DD HH:MM:SS" (utc)
|
|
'date_parse_format': '%Y-%m-%d %H:%M:%S'
|
|
}
|
|
try:
|
|
row = c.execute(select_1).fetchone()
|
|
stats['db_stats'] = {
|
|
'connect_time': {
|
|
'min': row['min'],
|
|
'max': row['max'], # YYYY-MM-DD HH:MM:SS (utc)
|
|
},
|
|
'count': row['count'],
|
|
'disposition': {}
|
|
}
|
|
|
|
for row in c.execute(select_2):
|
|
stats['db_stats']['disposition'][row['disposition']] = {
|
|
'count': row['count']
|
|
}
|
|
|
|
finally:
|
|
c.close()
|
|
|
|
last_stats.set(stats)
|
|
return stats
|