Created
May 28, 2022 10:32
-
-
Save dubeyji10/4dcff885f91c49fe8ad70d4b97b91140 to your computer and use it in GitHub Desktop.
generating records with a 15-10 minutes timestamp difference
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
import mysql.connector | |
from mysql.connector import Error | |
from datetime import date, time, datetime ,timedelta | |
import time | |
''' | |
for generating payloads - data to be inserted in APIs - with timestamps | |
- a difference of 15 minutes | |
''' | |
# today = '' | |
exampleDate = '2015-01-07' | |
global leads_Query | |
with open('leads/leadsQuery.txt', 'r') as file: | |
data = file.read() | |
leads_Query = data.rstrip() | |
print("running for leads - query : \n",leads_Query) | |
# exampleDate | |
aDate = datetime(2015,1,7,00) | |
# for example going back in time -- | |
# capital H,M,S - 12 format | |
# | |
def printTimeInterval2(dateTimeObject): | |
print("waiting for 10 minutes") | |
time.sleep(10) | |
# after 10 seconds later change it to 60*10 -- 10 minutes | |
''' | |
pass the datetime object | |
wait for 10 minutes | |
query all records in the last 10 minutes | |
''' | |
# now = datetime.now() | |
dt_string = dateTimeObject.strftime('%Y-%m-%d %H:%M:%S ') | |
print("now \t:", dt_string) | |
diff = dateTimeObject + timedelta(minutes=10) | |
dateTimeObject = diff | |
dt_string_diff = diff.strftime('%Y-%m-%d %H:%M:%S ') | |
print("10 minutes ago : ",dt_string_diff) | |
print("--> timestamp for difference = {} and {} ".format(dt_string,dt_string_diff)) | |
print("run this query :\n\n ") | |
print(leads_Query+" WHERE added_on between '{}' and '{}' ".format(dt_string,dt_string_diff)) | |
print("\n\n\n\n") | |
return diff | |
print("- first - waiting for 10 minutes for data to be written in db\n- second - an infinite loop which runs query for last 10 minutes of records") | |
time.sleep(10) | |
# change to 10*60 -- 10 minute wait | |
while True: | |
# dd/mm/YY H:M:S | |
# this keeps running forever | |
aDate = printTimeInterval2(aDate) | |
print('\\/'*25) | |
# try: | |
# connection = mysql.connector.connect(host='localhost', | |
# database='db-name', | |
# user='username', | |
# password='myPassword') | |
# print('\n\n--connection successful--\n\n') | |
# if connection.is_connected(): | |
# db_Info = connection.get_server_info() | |
# print("Connected to MySQL Server version ", db_Info) | |
# cursor = connection.cursor() | |
# cursor.execute("select database();") | |
# record = cursor.fetchone() | |
# print("You're connected to database: ", record) | |
# print('-'*50) | |
# for_date = leads_Query + " WHERE added_on between '2015-01-08 11:20:00' and '2015-01-08 11:30:00' " | |
# # for_date = mysql_SELECT_JSON_Querys | |
# print("run this query : \n\n\n",for_date) | |
# cursor = connection.cursor() | |
# result = cursor.execute(for_date) | |
# clients_table = cursor.fetchall() | |
# except Error as e: | |
# print("\n\nError while connecting to MySQL", e) | |
# finally: | |
# if connection.is_connected(): | |
# cursor.close() | |
# connection.close() | |
# print("MySQL connection is closed") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment