Python server-side upload script is now live and functional.

Note to devs who want to help me test this out: change the host at
upload_log.cpp:41 to "cornmander.com" and change upload_log.cpp:42 to
"/wesstats/upload" . I still need to add an error page/message for
failed uploads.

Also added a file from 2009-05-08T07:39:20Z!cornmander@cornmander.com that regenerates sample
databases. I accidentally deleted it from svn a while ago.
This commit is contained in:
Gregory Shikhman 2009-07-29 06:38:38 +00:00
parent 5c196334eb
commit 7c3a3ae88f
2 changed files with 102 additions and 1 deletions

View File

@ -0,0 +1,71 @@
# $Id$
"""
Copyright (C) 2009 by Gregory Shikhman <cornmander@cornmander.com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
or at your option any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
"""
import MySQLdb
import random
import configuration
TBLSTRING = "SMPL"
def sample(size):
conn = MySQLdb.connect(configuration.DB_HOSTNAME,configuration.DB_USERNAME,configuration.DB_PASSWORD,configuration.DB_NAME)
curs = conn.cursor()
#find the maximum game_id in the DB
curs.execute("SELECT MAX(game_id) FROM GAMES")
results = curs.fetchall()
max_id = results[0][0]
#look for an existing table with this sample size and drop it if it exists, then create a new one
tblname = configuration.DB_TABLE_PREFIX+TBLSTRING+str(size)
curs.execute("SELECT count(*) FROM information_schema.tables WHERE table_schema = 'corn' AND table_name = '"+tblname+"'")
results = curs.fetchall()
exists = results[0][0] == 1
if exists:
curs.execute("DROP TABLE IF EXISTS "+tblname)
curs.execute("""
CREATE TABLE `"""+tblname+"""` (
`game_id` int(11) NOT NULL auto_increment,
`timestamp` datetime NOT NULL,
`user_id` char(32) NOT NULL,
`serial` char(18) NOT NULL,
`platform` char(8) default NULL,
`version` char(14) default NULL,
`campaign` char(40) default NULL,
`difficulty` char(20) default NULL,
`gold` int(11) default NULL,
`turns` int(11) default NULL,
`scenario` char(40) default NULL,
`start_turn` int(11) default NULL,
`time` int(11) default NULL,
`result` enum('victory','defeat','quit') default NULL,
`end_time` int(11) default NULL,
`end_gold` int(11) default NULL,
`end_turn` int(11) default NULL,
PRIMARY KEY (`game_id`)
) ENGINE=MyISAM AUTO_INCREMENT=2450740 DEFAULT CHARSET=utf8 """)
#randomly pick size number of entries from the main DB and put them into this sample
choices = random.sample(range(1,max_id),size)
for c in choices:
curs.execute("SELECT * FROM GAMES WHERE `game_id`=%s",c)
results = curs.fetchall()
if len(results) != 0:
#print results[0]
curs.execute("""INSERT INTO %s (game_id,timestamp,user_id,serial,platform,version,
campaign,difficulty,gold,turns,scenario,start_turn,time,result,end_time,end_gold,end_turn) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)""",results[0])
conn.close()
sample(100000)

View File

@ -50,7 +50,37 @@ class RootController(BaseController):
@expose()
def upload(self, **kw):
raw_log = pylons.request.body
print helperlib.build_tree(raw_log.split('\n'))
wml_tree = helperlib.build_tree(raw_log.split('\n'))
if not wml_tree.has_key("platform"):
wml_tree["platform"] = "unknown"
result_type = "victory"
if not wml_tree.has_key("victory"):
result_type = "defeat"
if not wml_tree.has_key("defeat"):
result_type = "quit"
conn = MySQLdb.connect(configuration.DB_HOSTNAME,configuration.DB_USERNAME,configuration.DB_PASSWORD,configuration.DB_NAME,use_unicode=True)
curs = conn.cursor()
params = (
wml_tree["id"],
wml_tree["serial"],
wml_tree.setdefault("platform","unknown"),
wml_tree["version"],
wml_tree["game"]["campaign"],
wml_tree["game"]["difficulty"],
int(wml_tree["game"]["gold"]),
int(wml_tree["game"]["num_turns"]),
wml_tree["game"]["scenario"],
int(wml_tree["game"]["start_turn"]),
int(wml_tree["game"]["time"]),
result_type,
int(wml_tree["game"][result_type]["time"]),
int(wml_tree["game"][result_type].setdefault("gold","0")),
int(wml_tree["game"][result_type]["end_turn"])) #15 cols
curs.execute("INSERT INTO GAMES VALUES (DEFAULT,NOW(),%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",params)
conn.close()
return dict()
@expose(template="wesstats.templates.deleteview")