Created
June 1, 2022 06:20
-
-
Save dubeyji10/a570d10b2b3c590f4df3bec65614e65a to your computer and use it in GitHub Desktop.
instead of connection at top using with() since every cursor is a pointer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import date, datetime ,timedelta | |
from itertools import count | |
import sys | |
import time | |
import logging | |
import mysql.connector | |
from mysql.connector import Error | |
import json | |
import re | |
# from apiCall import pushInto | |
global clientCounter, convCounter , leadsCounter ,invoiceInfoCounter | |
''' | |
queries | |
''' | |
queryClients = "select id,company_name,phone_no,email,user_id,added_on FROM clients" | |
queryConversation = "select s_n,with_email,user_id,added_on,msg FROM conversation" | |
queryLeads = "select id,client_id,lead_source,importance,added_on,user_id,requirement,status,invoice_id FROM leads" | |
queryInvoiceInfo = "select Id,invoice_no,invoice_of,user_id,added_on,Email,payment_in,sale_rule,sale_amount FROM invoice_info" | |
# info has Id not id but it is item[index=0] | |
# queryInvoiceItems = "select * FROM invoice_items" | |
alpha = re.compile(r"[a-z|A-Z]|[!@#$%^&*/]") | |
counter = 0 | |
clientCounter = 0 | |
convCounter = 0 | |
leadsCounter = 0 | |
invoiceInfoCounter = 0 | |
''' date variables ''' | |
# aDate = datetime(2015,1,3,11,00) | |
aDate = datetime(2015,1,3,10,00) | |
secondDate = aDate | |
# 480 minutes - till 19:00 so loop breaks at counter = 49 | |
def updateTime(): | |
global aDate , secondDate , counter | |
counter+=1 | |
# d1 = aDate.strftime('%Y-%m-%d %H:%M:%S') | |
# d2 = secondDate.strftime('%Y-%m-%d %H:%M:%S') | |
# print("before update aDate = {} , secondDate = {}".format(d1,d2)) | |
diff = secondDate + timedelta(minutes=10) | |
aDate = secondDate | |
secondDate = diff | |
# print("now after update : aDate = {} , secondDate = {}".format( aDate.strftime('%Y-%m-%d %H:%M:%S') , secondDate.strftime('%Y-%m-%d %H:%M:%S'))) | |
return aDate , secondDate | |
while aDate < datetime(2015,1,3,19,00): | |
print('--in the loop--') | |
print('waiting for 10 minutes') | |
time.sleep(1) # 10 seconds now | |
timestamp1 , timestamp2 = updateTime() | |
timeCond = " WHERE added_on between '{}' and '{}'".format(timestamp1, timestamp2) | |
print('condition no {} : {} '.format(counter,timeCond)) | |
# everytime a new query is generated connect to db and perform operations | |
# since keeping a connection alive is not good enough | |
# not possible to keep alive the mysql connection | |
with mysql.connector.connect(host='',database='',user='',password='') as connectPointer: | |
print('-> connection established at timestamp = {}',timestamp2) | |
# clients | |
with connectPointer.cursor() as aCursor: | |
print('1. fetching clients') | |
aCursor.execute(queryClients+timeCond) | |
result = aCursor.fetchall() | |
if not result: | |
print('--> no clients created in the last 10 minutes <--') | |
for rows in result: | |
clientCounter+=1 | |
print(rows,'\n',type(rows)) | |
# conversations | |
with connectPointer.cursor() as aCursor: | |
print('2. fetching conversations') | |
aCursor.execute(queryConversation+timeCond) | |
result = aCursor.fetchall() | |
if not result: | |
print('--> no conversations created in the last 10 minutes <--') | |
for rows in result: | |
convCounter+=1 | |
print(rows,'\n',type(rows)) | |
# leads | |
with connectPointer.cursor() as aCursor: | |
print('3. fetching leads') | |
aCursor.execute(queryLeads+timeCond) | |
result = aCursor.fetchall() | |
if not result: | |
print('--> no leads created in the last 10 minutes <--') | |
for rows in result: | |
leadsCounter+=1 | |
print(rows,'\n',type(rows)) | |
# leads | |
with connectPointer.cursor() as aCursor: | |
print('4. fetching invoice info') | |
aCursor.execute(queryInvoiceInfo+timeCond) | |
result = aCursor.fetchall() | |
if not result: | |
print('--> no invoice info created in the last 10 minutes <--') | |
for rows in result: | |
invoiceInfoCounter+=1 | |
print(rows,'\n',type(rows)) | |
print('[]'*50) | |
print('total\n clients {} ,\n conversations {} ,\n leads {},\n invoice info {}'.format(clientCounter,convCounter,leadsCounter,invoiceInfoCounter)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment