def send_data(self, _data): """Send data to splunk.""" try: print('teste') data_payload = [] for d in _data: full_time = int( datetime.datetime.strptime( d.split("\"time_start\": \"")[1].split('"')[0], "%Y-%m-%d %H:%M:%S").timestamp()) - 3 * 3600 data_payload.append(self.payload.format(full_time, d)) with open('/tmp/dpay', 'w') as dpay: dpay.write("\n".join(data_payload)) _ = requests.request("POST", self.url, data="\n".join(data_payload), headers=self.headers) print(_.text) if len(self.id_list) > 1000: self.id_list.pop(0) except Exception as error: full_time = datetime.datetime.now().strftime('%d/%m/%Y %T') rc_logger(self.error_msg.format(full_time, error, 'send_data')).log_data()
def t_send_data(self, _t_data): try: t_conn = self.t_conn() t_cur = t_conn.cursor() for t_item in _t_data: t_cur.execute( "insert into callcent_queuecalls ({}) values ({});".format( ",".join(self.column_names), self.t_query.format(*list(t_item)))) t_conn.commit() except Exception as error: full_time = datetime.datetime.now().strftime('%d/%m/%Y %T') rc_logger(self.error_msg.format(full_time, error, 't_send_data')).log_data()
def t_conn(self): """Connect on the RC database.""" try: _t_conn = connect(host="", user="", password="", port=5432, database="rc_calls") return _t_conn except Exception as error: full_time = datetime.datetime.now().strftime('%d/%m/%Y %T') rc_logger(self.error_msg.format(full_time, error, 't_conn')).log_data()
def get_last_id(self): """Get the last id from the table callcent_queuecalls""" try: cur = self.conn() cur.execute( "SELECT idcallcent_queuecalls FROM callcent_queuecalls order by idcallcent_queuecalls desc limit 1" ) _last_id = cur.fetchall() return _last_id[0][0] except Exception as error: full_time = datetime.datetime.now().strftime('%d/%m/%Y %T') rc_logger(self.error_msg.format(full_time, error, 'conn')).log_data()
def conn(self): """Connect on the RC database.""" try: _conn = connect(host=self.host, user=self.user, password=self.passwd, port=self.port, database=self.db) _cur = _conn.cursor() return _cur except Exception as error: full_time = datetime.datetime.now().strftime('%d/%m/%Y %T') rc_logger(self.error_msg.format(full_time, error, 'conn')).log_data()
def get_data(self): """Return output data to be collect by splunk and generate a index to be indexed.""" try: print('teste0') if self.last_id_call == "": self.last_id_call = self.get_last_id() cur = self.conn() cur.execute(self.query.format(self.last_id_call)) get_data = cur.fetchall() if len(get_data) > 0: self.t_send_data(get_data) items = [dict(zip(self.column_names, i)) for i in get_data] self.last_id_call = items[-1]['idcallcent_queuecalls'] list_items = [] for item in items: _n = {} _ = [ _n.update({"{}".format(y): "{}".format(z)}) for y, z in item.items() if y in self.column_utils ] _n.update({"type_name": "QueueCalls"}) self.id_last = _n['call_history_id'] self.id_list.append(self.id_last) _n = self.format_fields(_n) if self.id_list.count(self.id_last) < 2: list_items.append( str(_n).rstrip("}").lstrip("{").replace("'", '\"')) if len(list_items) > 1000: self.send_data(list_items) list_items = [] self.send_data(list_items) except Exception as error: full_time = datetime.datetime.now().strftime('%d/%m/%Y %T') rc_logger(self.error_msg.format(full_time, error, 'get_data')).log_data()