def g_copy_history(self, endclock): lastclock = self._get_lastcopyclock() startclock = cf.clockaddminutes(endclock, -D_HISTORY_COPY_CLOCK_RANGE) if lastclock <= 0: lastclock = startclock if startclock > lastclock and lastclock > 0: endclock2 = cf.clockaddminutes(lastclock, +D_HISTORY_COPY_CLOCK_RANGE*2) endclock = min(endclock, endclock2) startclock = startclock condlist = ["itemid in (%s)" % ",".join(cf.list2strlist(self._itemids))] condlist.append("clock >= %s" % str(startclock)) condlist.append("clock <= %s" % str(endclock)) strwhere = sf.list2where(condlist) strsql = "select itemid, clock, value from history %s;" % (strwhere) if DEBUG: print strsql cf.log(strsql) g_zbx = sf.g_get_data(MYSQL_ZBX, strsql) strsql = "" for row in g_zbx: (itemid, clock, value) = row yield row strsql = "%sreplace into history (itemid, clock, value) values(%s,%s,%s);\n" \ % (strsql, itemid, clock, value) sf.exec_updsql(MYSQL_JUBATUS, strsql) self._upd_lastcopyclock(endclock)
def remove_history(self, basendclock): limitclock = basendclock - D_DATA_KEEP_PERIOD*60 condlist = ["itemid in (%s)" % ",".join(cf.list2strlist(self._itemids))] condlist.append("clock <= %s" % str(limitclock)) strwhere = sf.list2where(condlist) strsql = "delete from history %s;" % (strwhere) sf.exec_updsql(MYSQL_JUBATUS, strsql)
from config import * from lib.db_connector import * import lib.sql_funcs as sf user = confget("mysql_jubatus", "user") password = confget("mysql_jubatus", "password") database = confget("mysql_jubatus", "database") sf.exec_updsql("mysql", "create database if not exists %s;" % (database)) sf.exec_updsql("mysql", "grant select on " + database + ".* to " + user + " identified by '" + password + "';") sf.create_table("mysql_jubatus", "history") sf.create_table("mysql_jubatus", "last_clock") sf.create_table("mysql_jubatus", "target_items")
def _upd_lastcopyclock(self, clock): sf.exec_updsql(MYSQL_JUBATUS, "truncate table last_copy;\ninsert into last_copy(clock) values(%s);" % (str(clock)))