2003-08-23 01:00:34 +00:00
|
|
|
## $Id$
|
|
|
|
|
2003-09-03 08:31:44 +00:00
|
|
|
'''
|
|
|
|
Defines database backend library and database table and object relationships.
|
|
|
|
|
|
|
|
Example usage:
|
|
|
|
|
2004-12-06 22:41:19 +00:00
|
|
|
import database, db_mid
|
2003-09-03 08:31:44 +00:00
|
|
|
|
|
|
|
# get platform with id 7; will raise exception if no such platform.
|
|
|
|
p7 = database.Platforms[7]
|
|
|
|
|
|
|
|
# get platforms with friendly name "commodore 64"
|
|
|
|
p_c64 = database.Platforms.find(user_friendly_name="commodore 64")
|
|
|
|
|
|
|
|
# delete results of workunit with name "dead.wu", and email their users:
|
|
|
|
wu_dead = database.Workunits.find(name="dead.wu")[0]
|
|
|
|
results_dead = database.Results.find(wu=wu_dead)
|
|
|
|
for result in results_dead:
|
|
|
|
print "Removing from db:", result
|
|
|
|
os.system("echo oeps | mail %s" % result.host.user.email_addr)
|
|
|
|
result.remove()
|
|
|
|
|
|
|
|
# multiply the total_credit of each user by 17:
|
|
|
|
for user in database.Users.find():
|
|
|
|
user.total_credit *= 17
|
|
|
|
user.commit()
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
2004-12-06 22:41:19 +00:00
|
|
|
import configxml
|
|
|
|
from util import *
|
|
|
|
from db_base import *
|
2003-08-23 01:00:34 +00:00
|
|
|
|
|
|
|
ID = '$Id$'
|
|
|
|
|
|
|
|
class Platform(DatabaseObject):
|
|
|
|
_table = DatabaseTable(
|
|
|
|
table = 'platform',
|
2003-09-03 08:47:49 +00:00
|
|
|
columns = [ 'create_time',
|
2003-08-23 01:00:34 +00:00
|
|
|
'name',
|
2003-12-11 19:05:52 +00:00
|
|
|
'user_friendly_name',
|
|
|
|
'deprecated' ])
|
2003-08-23 01:00:34 +00:00
|
|
|
|
|
|
|
class App(DatabaseObject):
|
|
|
|
_table = DatabaseTable(
|
|
|
|
table = 'app',
|
2003-09-03 08:47:49 +00:00
|
|
|
columns = [ 'create_time',
|
2003-08-23 01:00:34 +00:00
|
|
|
'name',
|
2004-09-27 04:26:51 +00:00
|
|
|
'min_version',
|
|
|
|
'deprecated',
|
|
|
|
'user_friendly_name',
|
2006-10-22 01:46:33 +00:00
|
|
|
'homogeneous_redundancy',
|
|
|
|
'weight',
|
2008-06-04 23:04:12 +00:00
|
|
|
'beta',
|
2010-11-10 00:10:32 +00:00
|
|
|
'target_nresults',
|
|
|
|
'min_avg_pfc',
|
2012-08-25 04:09:24 +00:00
|
|
|
'host_scale_check',
|
|
|
|
'homogeneous_app_version',
|
|
|
|
'non_cpu_intensive'
|
2006-10-22 01:46:33 +00:00
|
|
|
])
|
2003-08-23 01:00:34 +00:00
|
|
|
|
|
|
|
class AppVersion(DatabaseObject):
|
|
|
|
_table = DatabaseTable(
|
|
|
|
table = 'app_version',
|
2003-09-03 08:47:49 +00:00
|
|
|
columns = [ 'create_time',
|
2003-08-23 01:00:34 +00:00
|
|
|
'appid',
|
|
|
|
'version_num',
|
|
|
|
'platformid',
|
|
|
|
'xml_doc',
|
|
|
|
'min_core_version',
|
2003-12-24 21:49:35 +00:00
|
|
|
'max_core_version',
|
2008-03-13 22:57:24 +00:00
|
|
|
'deprecated',
|
2010-03-29 22:28:20 +00:00
|
|
|
'plan_class',
|
|
|
|
'pfc_n',
|
|
|
|
'pfc_avg',
|
|
|
|
'pfc_scale',
|
|
|
|
'expavg_credit',
|
2014-06-05 16:51:01 +00:00
|
|
|
'expavg_time',
|
|
|
|
'beta'
|
2008-03-13 22:57:24 +00:00
|
|
|
])
|
2003-08-23 01:00:34 +00:00
|
|
|
|
|
|
|
class User(DatabaseObject):
|
|
|
|
_table = DatabaseTable(
|
|
|
|
table = 'user',
|
2003-09-03 08:47:49 +00:00
|
|
|
columns = [ 'create_time',
|
2003-08-23 01:00:34 +00:00
|
|
|
'email_addr',
|
|
|
|
'name',
|
|
|
|
'authenticator',
|
|
|
|
'country',
|
|
|
|
'postal_code',
|
|
|
|
'total_credit',
|
|
|
|
'expavg_credit',
|
|
|
|
'expavg_time',
|
|
|
|
'global_prefs',
|
|
|
|
'project_prefs',
|
|
|
|
'teamid',
|
|
|
|
'venue',
|
|
|
|
'url',
|
|
|
|
'send_email',
|
|
|
|
'show_hosts',
|
2003-11-29 00:13:58 +00:00
|
|
|
'posts',
|
|
|
|
'seti_id',
|
|
|
|
'seti_nresults',
|
|
|
|
'seti_last_result_time',
|
|
|
|
'seti_total_cpu',
|
2003-12-15 02:31:29 +00:00
|
|
|
'signature',
|
2004-04-18 18:40:13 +00:00
|
|
|
'has_profile',
|
2007-02-02 20:14:18 +00:00
|
|
|
'cross_project_id',
|
|
|
|
'passwd_hash',
|
|
|
|
'email_validated',
|
|
|
|
'donated'
|
2003-11-29 00:13:58 +00:00
|
|
|
])
|
2003-08-23 01:00:34 +00:00
|
|
|
|
|
|
|
class Team(DatabaseObject):
|
|
|
|
_table = DatabaseTable(
|
|
|
|
table = 'team',
|
2003-09-03 08:47:49 +00:00
|
|
|
columns = [ 'create_time',
|
2003-08-23 01:00:34 +00:00
|
|
|
'userid',
|
|
|
|
'name',
|
|
|
|
'name_lc',
|
|
|
|
'url',
|
|
|
|
'type',
|
|
|
|
'name_html',
|
|
|
|
'description',
|
|
|
|
'nusers',
|
|
|
|
'country',
|
|
|
|
'total_credit',
|
2007-02-02 20:14:18 +00:00
|
|
|
'expavg_credit',
|
|
|
|
'expavg_time',
|
|
|
|
'seti_id',
|
|
|
|
'ping_user',
|
|
|
|
'ping_time'
|
|
|
|
])
|
2003-08-23 01:00:34 +00:00
|
|
|
|
|
|
|
class Host(DatabaseObject):
|
|
|
|
_table = DatabaseTable(
|
|
|
|
table = 'host',
|
2003-09-03 08:47:49 +00:00
|
|
|
columns = [ 'create_time',
|
2003-08-23 01:00:34 +00:00
|
|
|
'userid',
|
|
|
|
'rpc_seqno',
|
|
|
|
'rpc_time',
|
|
|
|
'total_credit',
|
|
|
|
'expavg_credit',
|
|
|
|
'expavg_time',
|
|
|
|
'timezone',
|
|
|
|
'domain_name',
|
|
|
|
'serialnum',
|
|
|
|
'last_ip_addr',
|
|
|
|
'nsame_ip_addr',
|
|
|
|
'on_frac',
|
|
|
|
'connected_frac',
|
|
|
|
'active_frac',
|
|
|
|
'p_ncpus',
|
|
|
|
'p_vendor',
|
|
|
|
'p_model',
|
|
|
|
'p_fpops',
|
|
|
|
'p_iops',
|
|
|
|
'p_membw',
|
|
|
|
'os_name',
|
|
|
|
'os_version',
|
|
|
|
'm_nbytes',
|
|
|
|
'm_cache',
|
|
|
|
'm_swap',
|
|
|
|
'd_total',
|
|
|
|
'd_free',
|
|
|
|
'd_boinc_used_total',
|
|
|
|
'd_boinc_used_project',
|
|
|
|
'd_boinc_max',
|
|
|
|
'n_bwup',
|
|
|
|
'n_bwdown',
|
|
|
|
'credit_per_cpu_sec',
|
|
|
|
'venue',
|
2004-12-06 22:41:19 +00:00
|
|
|
'projects',
|
|
|
|
'nresults_today',
|
2007-02-02 20:14:18 +00:00
|
|
|
'avg_turnaround',
|
|
|
|
'host_cpid',
|
|
|
|
'external_ip_addr',
|
|
|
|
'max_results_day'
|
2004-12-06 22:41:19 +00:00
|
|
|
])
|
2003-08-23 01:00:34 +00:00
|
|
|
|
|
|
|
class Workunit(DatabaseObject):
|
|
|
|
_table = DatabaseTable(
|
|
|
|
table = 'workunit',
|
2003-09-03 08:47:49 +00:00
|
|
|
columns = [ 'create_time',
|
2003-08-23 01:00:34 +00:00
|
|
|
'appid',
|
|
|
|
'name',
|
|
|
|
'xml_doc',
|
|
|
|
'batch',
|
2003-09-04 05:54:55 +00:00
|
|
|
'rsc_fpops_est',
|
|
|
|
'rsc_fpops_bound',
|
|
|
|
'rsc_memory_bound',
|
|
|
|
'rsc_disk_bound',
|
2003-08-23 01:00:34 +00:00
|
|
|
'need_validate',
|
|
|
|
'canonical_resultid',
|
|
|
|
'canonical_credit',
|
|
|
|
'transition_time',
|
|
|
|
'delay_bound',
|
|
|
|
'error_mask',
|
|
|
|
'file_delete_state',
|
|
|
|
'assimilate_state',
|
2004-12-06 22:41:19 +00:00
|
|
|
'hr_class',
|
2003-08-23 01:00:34 +00:00
|
|
|
'opaque',
|
|
|
|
'min_quorum',
|
|
|
|
'target_nresults',
|
|
|
|
'max_error_results',
|
|
|
|
'max_total_results',
|
|
|
|
'max_success_results',
|
2004-12-06 22:41:19 +00:00
|
|
|
'result_template_file',
|
|
|
|
'priority',
|
|
|
|
'mod_time'
|
|
|
|
])
|
2003-08-23 01:00:34 +00:00
|
|
|
|
|
|
|
class Result(DatabaseObject):
|
|
|
|
_table = DatabaseTable(
|
|
|
|
table = 'result',
|
2003-09-03 08:47:49 +00:00
|
|
|
columns = [ 'create_time',
|
2003-08-23 01:00:34 +00:00
|
|
|
'workunitid',
|
|
|
|
'server_state',
|
|
|
|
'outcome',
|
|
|
|
'client_state',
|
|
|
|
'hostid',
|
2003-10-17 09:05:31 +00:00
|
|
|
'userid',
|
2003-08-23 01:00:34 +00:00
|
|
|
'report_deadline',
|
|
|
|
'sent_time',
|
|
|
|
'received_time',
|
|
|
|
'name',
|
|
|
|
'cpu_time',
|
|
|
|
'xml_doc_in',
|
|
|
|
'xml_doc_out',
|
|
|
|
'stderr_out',
|
|
|
|
'batch',
|
|
|
|
'file_delete_state',
|
|
|
|
'validate_state',
|
|
|
|
'claimed_credit',
|
|
|
|
'granted_credit',
|
|
|
|
'opaque',
|
|
|
|
'random',
|
2003-11-29 00:13:58 +00:00
|
|
|
'client_version_num',
|
2004-01-16 00:12:27 +00:00
|
|
|
'appid',
|
2004-12-06 22:41:19 +00:00
|
|
|
'teamid',
|
|
|
|
'priority',
|
|
|
|
'mod_time'
|
2003-11-29 00:13:58 +00:00
|
|
|
])
|
2003-08-23 01:00:34 +00:00
|
|
|
|
|
|
|
|
2003-10-03 05:53:28 +00:00
|
|
|
def connect(config = None, nodb = False):
|
2003-09-05 21:53:46 +00:00
|
|
|
"""Connect if not already connected, using config values."""
|
2003-10-17 09:04:13 +00:00
|
|
|
if get_dbconnection():
|
2003-09-04 07:16:14 +00:00
|
|
|
return 0
|
2003-09-05 21:53:46 +00:00
|
|
|
config = config or configxml.default_config().config
|
2003-10-03 05:53:28 +00:00
|
|
|
if nodb:
|
|
|
|
db = ''
|
|
|
|
else:
|
|
|
|
db = config.db_name
|
2005-02-17 20:35:41 +00:00
|
|
|
|
|
|
|
host=config.__dict__.get('db_host','')
|
2003-10-17 09:04:13 +00:00
|
|
|
do_connect(db=db,
|
2005-02-17 20:35:41 +00:00
|
|
|
host=host,
|
2003-10-17 09:04:13 +00:00
|
|
|
user=config.__dict__.get('db_user',''),
|
|
|
|
passwd=config.__dict__.get('db_passwd', ''))
|
2003-08-23 01:00:34 +00:00
|
|
|
return 1
|
|
|
|
|
2003-10-03 05:53:28 +00:00
|
|
|
def _execute_sql_script(cursor, filename):
|
|
|
|
for query in open(filename).read().split(';'):
|
|
|
|
query = query.strip()
|
|
|
|
if not query: continue
|
|
|
|
cursor.execute(query)
|
|
|
|
|
2009-06-10 22:38:33 +00:00
|
|
|
def create_database(srcdir, config = None, drop_first = False):
|
2003-10-03 05:53:28 +00:00
|
|
|
''' creates a new database. '''
|
2004-11-24 07:17:46 +00:00
|
|
|
import boinc_path_config
|
2003-10-03 05:53:28 +00:00
|
|
|
config = config or configxml.default_config().config
|
|
|
|
connect(config, nodb=True)
|
2003-10-17 09:04:13 +00:00
|
|
|
cursor = get_dbconnection().cursor()
|
2003-10-03 05:53:28 +00:00
|
|
|
if drop_first:
|
|
|
|
cursor.execute("drop database if exists %s"%config.db_name)
|
|
|
|
cursor.execute("create database %s"%config.db_name)
|
|
|
|
cursor.execute("use %s"%config.db_name)
|
|
|
|
for file in ['schema.sql', 'constraints.sql']:
|
2009-06-10 22:38:33 +00:00
|
|
|
_execute_sql_script(cursor, os.path.join(srcdir, 'db', file))
|
2003-10-03 05:53:28 +00:00
|
|
|
cursor.close()
|
|
|
|
|
2003-09-05 21:53:46 +00:00
|
|
|
# alias
|
|
|
|
connect_default_config = connect
|
2003-09-04 07:16:14 +00:00
|
|
|
|
2004-05-02 15:55:17 +00:00
|
|
|
database_classes_ = [ Platform,
|
2003-10-17 09:04:13 +00:00
|
|
|
App,
|
|
|
|
AppVersion,
|
|
|
|
User,
|
|
|
|
Team,
|
|
|
|
Host,
|
|
|
|
Workunit,
|
2004-02-16 08:10:19 +00:00
|
|
|
Result ]
|
2003-10-17 09:04:13 +00:00
|
|
|
|
|
|
|
Platforms = Platform._table
|
|
|
|
Apps = App._table
|
|
|
|
AppVersions = AppVersion._table
|
|
|
|
Users = User._table
|
|
|
|
Teams = Team._table
|
|
|
|
Hosts = Host._table
|
|
|
|
Workunits = Workunit._table
|
|
|
|
Results = Result._table
|
|
|
|
|
|
|
|
init_table_classes(database_classes_,{'canonical_result': Result})
|