-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmysqldata.py
75 lines (56 loc) · 2.21 KB
/
mysqldata.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
# from db import getCursor
# import mysql.connector
from db import getCursor
class MySQLData:
def __init__( self ):
from db import getCursor
d = getCursor()
self.con = d[0]
self.cursor = d[1]
def gravaEstatistica ( self, valores ):
sql = "INSERT INTO estatistica ( tipo, bloco, categorias, device, browser, response, status, avgsize, sumsize, latencymobile, latencydesktop, data ) VALUES \
( %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s )"
self.cursor.execute(sql,valores)
def gravaAcesso (self, valores ):
sql = "INSERT INTO acesso ( bloco, referer, contagem, mes, ano ) VALUES \
( %s, %s, %s, %s, %s )"
for dados in valores:
try:
self.cursor.execute(sql,dados)
except mysql.connector.Error as err:
print(dados)
def getData( self, date ):
# o parametro bind %s não está funcionando, descobrir o motivo
sql = ("SELECT bloco FROM estatistica WHERE data = '"+date+"'")
self.cursor.execute( sql, (date) )
data = []
for bloco in self.cursor:
data.append(bloco[0])
self.fecharCursor()
return data
def getMonthData( self, month, year, tipo ):
sql = ("SELECT * FROM estatistica WHERE MONTH(data) = "+str(month)+" and YEAR(data) = " + str(year) + " AND tipo = '"+ tipo +"'")
print( sql )
self.cursor.execute( sql )
data = []
cols = ( 'id','tipo','bloco','categorias','device','browser','response','status','avgsize','sumsize','latencymobile','latencydesktop','data' )
for linha in self.cursor:
o = {}
for idx, cel in enumerate(linha):
o[cols[idx]] = cel
data.append( o )
self.fecharCursor()
return data
def getSummary( self ):
# o parametro bind %s não está funcionando, descobrir o motivo
sql = ("SELECT data, count(*) as total FROM estatistica group by data order by data desc")
self.cursor.execute( sql )
_data = []
for (data, total) in self.cursor:
_data.append( (data.isoformat(), total) )
self.fecharCursor()
return _data
def fecharCursor( self ):
self.cursor.close()
self.con.commit()
self.con.close()