-
Notifications
You must be signed in to change notification settings - Fork 46
/
stats.py
107 lines (92 loc) · 3.44 KB
/
stats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
##############################################################################
#
# Copyright (c) 2009 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Stats implementations
"""
class PostgreSQLStats(object):
def __init__(self, connmanager):
self.connmanager = connmanager
def get_object_count(self):
"""Returns the number of objects in the database"""
# do later
return 0
def get_db_size(self):
"""Returns the approximate size of the database in bytes"""
def callback(conn, cursor):
cursor.execute("SELECT pg_database_size(current_database())")
return cursor.fetchone()[0]
return self.connmanager.open_and_call(callback)
class MySQLStats(object):
def __init__(self, connmanager):
self.connmanager = connmanager
def get_object_count(self):
"""Returns the number of objects in the database"""
# do later
return 0
def get_db_size(self):
"""Returns the approximate size of the database in bytes"""
conn, cursor = self.connmanager.open()
try:
cursor.execute("SHOW TABLE STATUS")
description = [i[0] for i in cursor.description]
rows = cursor.fetchall()
finally:
self.connmanager.close(conn, cursor)
data_column = description.index('Data_length')
index_column = description.index('Index_length')
return sum([row[data_column] + row[index_column] for row in rows], 0)
class OracleStats(object):
def __init__(self, connmanager):
self.connmanager = connmanager
def get_object_count(self):
"""Returns the number of objects in the database"""
# The tests expect an exact number, but the code below generates
# an estimate, so this is disabled for now.
if True:
return 0
else:
conn, cursor = self.connmanager.open(
self.connmanager.isolation_read_only)
try:
stmt = """
SELECT NUM_ROWS
FROM USER_TABLES
WHERE TABLE_NAME = 'CURRENT_OBJECT'
"""
cursor.execute(stmt)
res = cursor.fetchone()[0]
if res is None:
res = 0
else:
res = int(res)
return res
finally:
self.connmanager.close(conn, cursor)
def get_db_size(self):
"""Returns the approximate size of the database in bytes"""
conn, cursor = self.connmanager.open(
self.connmanager.isolation_read_only)
try:
stmt = """
SELECT SUM(BYTES)
FROM USER_SEGMENTS
"""
cursor.execute(stmt)
res = cursor.fetchone()[0]
if res is None:
res = 0
else:
res = int(res)
return res
finally:
self.connmanager.close(conn, cursor)