def add_items_to_database(items, verbose): db = DB() # db.query("SET NAMES utf8;") for item in items: url_path = urlparse.urlparse(item.url).path[1:] url_path_groups = url_path.split('/') github_repo_path = '/'.join(url_path_groups[:2]) cmd = """ INSERT INTO hacker_news(id, added_at, submission_time, title, url, github_repo_name) VALUES (%d, '%s', '%s', '%s', '%s', '%s') """ % ( item.item_id, date_for_mysql(datetime.now()), date_for_mysql(item.submission_time), str_for_mysql(item.title), str_for_mysql(item.url), str_for_mysql(github_repo_path) ) if verbose: print cmd # db.query(cmd) try: db.query(cmd) except MySQLError as err: print "ERROR:", err db.conn.rollback() else: db.commit() return True
def repos_to_update(): db = DB() last_job_time = db.query(""" SELECT max(added_at) from github_user_starred_repos """ ).fetchone()[0] last_job_time = '2016-02-21' repos_raw = db.query(""" SELECT id from github_repos where added_at > '%s' """ % last_job_time ).fetchall() repos = list(zip(*repos_raw)[0]) if repos_raw else [] db.close() return repos
def get_hn_repos(): db = DB() last_job_time = db.query(""" SELECT max(added_at) from github_repos where from_hacker_news=1 """ ).fetchone()[0] if not last_job_time: last_job_time = '1970-01-01' hn_repos_raw = db.query(""" SELECT github_repo_name from hacker_news where added_at > '%s' """ % last_job_time ).fetchall() if hn_repos_raw: hn_repos = list(zip(*hn_repos_raw)[0]) else: hn_repos = [] db.close() return hn_repos
class VirtualMachine: def __init__(self, id): self.db = DB("emmet", "78636756dD.", "vmweb") sql = f"select * from vmachine where id={id}" query = self.db.query(sql) self.name = query[0]["name"] self.ram = query[0]["ram"] self.cpu = query[0]["cpu"] self.hdd = query[0]["hdd"] self.os = query[0]["os"] self.id = id def stop(self): sql = f"delete from process where vmachine_id={self.id}" self.db.run(sql) sql = f"update vmachine set status=0 where id={self.id}" self.db.run(sql) def start(self): sql = f"update vmachine set status=1 where id={self.id}" self.db.run(sql) def suspend(self): sql = f"update vmachine set status=2 where id={self.id}" self.db.run(sql) self.status = 2 def reboot(self): self.stop() self.start() def get_processes(self): sql = "select * from process where vmachine_id=1" return self.db.query(sql) def run(self, pid, ram, cpu, hdd): sql = f"insert into process (pid, ram, cpu, hdd, vmachine_id) values ({pid}, {ram}, {cpu}, {hdd}, {self.id})" self.db.run(sql) def ram_usage(self): ram = 0 for p in self.get_processes(): ram += p["ram"] return ram * 100 / self.ram def cpu_usage(self): cpu = 0 for p in self.get_processes(): cpu += p["cpu"] return cpu * 100 / self.cpu def hdd_usage(self): hdd = 0 for p in self.get_processes(): hdd += p["hdd"] return hdd * 100 / self.hdd def get_status(self): sql = "select status from vmachine where id=1" query = self.db.query(sql) status = query[0]["status"] if status == 0: return "Stopped" elif status == 1: return "Running" elif status == 2: return "Suspended" def __str__(self): return """ {} <{}> [{}] {:.2f}% RAM used | {:.2f}% CPU used | {:.2f}% HDD used """.format(self.os, self.name, self.get_status(), self.ram_usage(), self.cpu_usage(), self.hdd_usage())
from mysql import DB db = DB("gandalf", "Aragorn_3", "commands") cmd = input("Introduzca el comando: ") desc = input("Introduzca la descripción: ") sql = "insert into commands values ('{}', '{}')".format(cmd, desc) db.run(sql) sql = "select * from commands order by name" print(db.query(sql))
class VirtualMachine: def __init__(self, id): self.db = DB("abraham", "79072387cC$", "db_web") sql = f"select * from vmachine where id={id}" run = self.db.query(sql) self.id = run[0]["id"] self.name = run[0]["name"] self.ram = run[0]["ram"] self.cpu = run[0]["cpu"] self.hdd = run[0]["hdd"] self.os = run[0]["os"] self.status = 0 # self.proc = list() def stop(self): sql = "update vmachine set status=0 where id=1" self.db.run(sql) sql = "delete from process where id=1" self.db.run(sql) self.status = 0 # self.proc = list() def start(self): sql = "update vmachine set status=1 where id=1" self.db.run(sql) self.status = 1 def suspend(self): sql = "update vmachine set status=2 where id=1" self.db.run(sql) self.status = 2 def reboot(self): self.stop() self.start() def get_process(self): sql = f"select * from process where vmachine_id={self.id}" return self.db.query(sql) def run(self, pid, ram, cpu, hdd): sql = f"insert into process (pid, ram, cpu, hdd, vmachine_id) values ({pid}, {ram}, {cpu}, {hdd}, 1)" self.db.run(sql) # self.proc.append( # { # "pid": pid, # "ram": ram, # "cpu": cpu, # "hdd": hdd # } # ) def ram_usage(self): ram = 0 for p in self.get_process(): ram += p["ram"] return ram * 100 / self.ram def cpu_usage(self): cpu = 0 for p in self.get_process(): cpu += p["cpu"] return cpu * 100 / self.cpu def hdd_usage(self): hdd = 0 for p in self.get_process(): hdd += p["hdd"] return hdd * 100 / self.hdd def get_status(self): if self.status == 0: return "Stopped" elif self.status == 1: return "Running" elif self.status == 2: return "Suspended" def __str__(self): return """ {} <{}> [{}] {:.2f}% RAM used | {:.2f}% CPU used | {:.2f}% HDD used """.format(self.os, self.name, self.get_status(), self.ram_usage(), self.cpu_usage(), self.hdd_usage())