Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sql/report.py
blob: 6af6797beac73dda46e6ffce2a446d91c9a1ac8f (plain)
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
#!/usr/bin/env  python

# Copyright (C) 2012, Gustavo Duarte
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function

from stats_consolidation.db import *



class Report:
	STAT={}
	STAT ['Get_resource_name'] = ("SELECT name FROM `Resources`")

	STAT ['Get_suma_uptime'] = ( "SELECT SUM (`data`) FROM Usages (WHERE `resource_name` = %s AND data_type = 'active')")

	STAT ['Get_frequency_usage'] = ("SELECT SUM(`data`) FROM Usages ((WHERE `resource_name` = `system`) AND (start_date > start) AND (start_date < end))")
	

	def __init__ (self,  db_name, user, password):
		self.db_name  = db_name
		self.user = user
		self.password = password
		"""
		try:
			self.connect()
		except Exception as e:
			print ("INIT:")
		"""
	def connect (self):
		print ("Try connect to db")
		try:
			self.cnx = mysql.connector.connect(user=self.user, password=self.password)
			cursor = self.cnx.cursor()
		    	self.cnx.database = self.db_name
			cursor.close()
		except mysql.connector.Error as err:
			print("CONNECT FAIL {}".format (err))

	def most_activity_use (self):
		print ("most_activity_used")
                try:
			res_tmp =('', 0)
			res = ('', 0)
			cursor = self.cnx.cursor()
                        cursor.execute(self.STAT['Get_resource_name'])
			for (name) in cursor:
				cursor.execute (self.STAT['Get_suma_uptime'], (name,))
				res_tmp =(name, cursor.fetchone())
				print ("activity: {}  uptime: {}".format(res_tmp[0], res_tmpe[1]))
				if res_tmp[1] > res[1]:
					res = res_tmp
                except mysql.connector.Error as err:
			print("Fail {}: {}".format(cursor.statement, err))
		except Exception as e:
                        print("most_activity_used Fail ")
                cursor.close()
		return res

	def frequency_usage(self):
		cursor = self.cnx.cursor()
                try:
			cursor.execute(self.STAT['Get_frequency_usage'])
			res = cursor.fetchone()
		except mysql.connector.Error as err:
                        print("ferquency_usage")
		cursor.close()	

		return res