class Ipv_Top: def __init__(self,files,top_x=20,db_settings=local_db_settings): self.data=IpCounter(files).get_data() self.top_x=sorted(self.data.items(),key=lambda x:x[1],reverse=True)[:top_x] self.connection = MySQL_Connection(db_settings) def store(self): keys='(day,topn,ip,freq)' for i,(ip,counts) in enumerate(self.top_x): self.connection.insert('top_ip',keys,(today,i+1,ip,counts))
class IpLocationService: def __init__(self,files,db_settings=server_183_db_settings): self.data=IpLocationCounter(files).get_data() # self.data=cPickle.load(open('CityIpData')) self.connection=MySQL_Connection(db_settings) cPickle.dump(self.data,open('CityIpData','wb')) def store(self): province_data={} for (city,province),count in self.data.items(): self.connection.insert('city_ip',('day','city','count'),(today,city,count)) province_data.setdefault(province,0) province_data[province]+=count for province,count in province_data.items(): self.connection.insert('province_ip',('day','province','count'),(today,province,count)) # print self.data # print province_data