I have a problem, as I trying to automate the cloudwatch Logs insight query. I found this code
def getInsightLogCounts():
print("starting cloudwatch insight queries")
msg = ''
#STEP 1 - create a dict with query message as key and query as value. This can be stored in the db as well
query_Dict = getQueryDict()
log_group = '/the/loggroup/you/want/to/'
print("starting cloudwatch insight queries for " + str(log_group))
for x in query_Dict.items():
query_key = x[0]
query = x[1]
print("query key : " + str(query_key) + " \n query : " + str(query))
#STEP 2 - Create a query response object using the start_query method of the logs. Here we are fetching data for the last 24 hours
start_query_response = logs_client.start_query(
logGroupName=log_group,
queryString=query,
startTime=int((datetime.today() - timedelta(hours=24)).timestamp()),
endTime=int(datetime.now().timestamp()),
limit=1)
query_id = start_query_response['queryId']
response = None
#STEP3 - run a while loop and waith for the query to complete.
while response == None or response['status'] == 'Running':
time.sleep(1)
response = logs_client.get_query_results(queryId=query_id)
#STEP4 - Extract the result and proceed to the next query
if response and response['results'] and response['results'][0] and response['results'][0][1]:
#response found update msg
msg = msg + str(query_key) + " : " + str(response['results'][0][1].get('value')) + "; \n"
print("query value returned for " + str(query_key) + " is : " + str(response['results'][0][1].get('value')))
else:
msg = msg + str(query_key) + " : 0" + "; \n"
print("no query data returned for " + str(query_key))
return msg
Now I need to fit my query into this code, but I'm new to the Lambda (Python) codes.
My query is (sample one)
fields #timestamp, #message
| filter #message like 'END RequestId'
| sort #timestamp desc
If anyone has some ideas I would be very grateful if you can help me with this or just give me some advice
this is a link for the original post I took the code from.
https://medium.com/xebia-engineering/accessing-cloudwatch-metrics-and-insights-from-aws-lambda-1119c40ff80b#5833
Sorry guys, for the not clear post before. I manage to fit my code here so hopefully, someone with a big heart can help me
Related
So I made a chatbot using Dialogflow. It's supposed to retrieve the number of daily covid-19 cases through webhook call. In my python program, I programmed so that it would save the fulfillment message to MongoDB and then send the message to Dialogflow. Only problem right now is that if I connect to my database, the program won't return anything. But when I don't connect, then it works just fine.
Here's the part of the code that I'm having trouble with:
log = Conversations.Log()
sessionID = req.get('responseId')
result = req.get("queryResult")
intent = result.get("intent").get('displayName')
query_text = result.get("queryText")
parameters = result.get("parameters")
cust_name = parameters.get("cust_name")
cust_contact = parameters.get("cust_contact")
cust_email = parameters.get("cust_email")
db = configureDataBase()
if intent == 'covid_searchcountry':
cust_country = parameters.get("geo-country")
if(cust_country=="United States"):
cust_country = "USA"
fulfillmentText, deaths_data, testsdone_data = makeAPIRequest(cust_country)
webhookresponse = "***Covid Report*** \n\n" + " New cases :" + str(fulfillmentText.get('new')) + \
"\n" + " Active cases : " + str(
fulfillmentText.get('active')) + "\n" + " Critical cases : " + str(fulfillmentText.get('critical')) + \
"\n" + " Recovered cases : " + str(
fulfillmentText.get('recovered')) + "\n" + " Total cases : " + str(fulfillmentText.get('total')) + \
"\n" + " Total Deaths : " + str(deaths_data.get('total')) + "\n" + " New Deaths : " + str(
deaths_data.get('new')) + \
"\n" + " Total Test Done : " + str(deaths_data.get('total')) + "\n\n*******END********* \n "
print(webhookresponse)
log.saveConversations(sessionID, cust_country, webhookresponse, intent, db)
log.saveCases( "country", fulfillmentText, db)
return {
"fulfillmentMessages": [
{
"text": {
"text": [
webhookresponse
]
}
},
{
"text": {
"text": [
"Do you want me to send the detailed report to your e-mail address? Type.. \n 1. Sure \n 2. Not now "
# "We have sent the detailed report of {} Covid-19 to your given mail address.Do you have any other Query?".format(cust_country)
]
}
}
]
}
Whenever I comment out db=configureDataBase(), everything works fine, but when I establish the connection again, the program will stop returning.
Here's the code for configureDataBase():
def configureDataBase():
client = MongoClient("connection URL")
return client.get_database('Covid_19_DB')
If anyone can help me figure out why connecting to my database is causing it to break, I'd really appreciate it.
Getting this error how to fix ? i m trying to create a function to plance an order kucoin api v2 python but give error how to fix
i tried to make this function with full code but still cant find a solution whats wrong
Kucoin Api v2 Python Place a New Order Code - {'code': '400005', 'msg': 'Invalid KC-API-SIGN'}
base_uri = 'https://api.kucoin.com'
def float_to_dic(price):
formatted_float = "{:.9f}".format(price)
return formatted_float
def get_headers(method, endpoint):
now = int(time.time() * 1000)
str_to_sign = str(now) + method + endpoint
signature = base64.b64encode(hmac.new(api_secret.encode(), str_to_sign.encode(), hashlib.sha256).digest()).decode()
passphrase = base64.b64encode(hmac.new(api_secret.encode(), api_passphrase.encode(), hashlib.sha256).digest()).decode()
return {'KC-API-KEY': api_key,
'KC-API-KEY-VERSION': '2',
'KC-API-PASSPHRASE': passphrase,
'KC-API-SIGN': signature,
'KC-API-TIMESTAMP': str(now)
}
#List Accounts
method = 'POST'
endpoint = '/api/v1/orders'
price_buy = float(1900)
quantity = 0.3
price = "{:.9f}".format(price_buy)
symbol = 'ETH-USDT'
body = '{"text":"t-123456","symbol":"' + symbol + '","type":"market","tradeType":"TRADE","side":"buy","time_in_force":"gtc","auto_borrow":false}'
response = requests.request(method, base_uri+endpoint, headers=get_headers(method, endpoint), data=body)
print(response.status_code)
print(response.json())
Here is a snippet of code i have created based on kucoin-python wrapper.
You can test it :
from kucoin.client import Trade, Market
class my_kukoin_handler:
def __init__(self,key,sec,pas):
self.api_key = key
self.api_secret = sec
self.api_passphrase = pas
self.client = Trade(key=self.api_key, secret=self.api_secret, passphrase=self.api_passphrase, is_sandbox=False, url='')
def update_client(self):
self.client = Trade(key=self.api_key, secret=self.api_secret, passphrase=self.api_passphrase, is_sandbox=False,url='')
def buy(self,coin_no_usdt,qty):
try:
order_id = self.client.create_market_order(str(coin_no_usdt).upper().replace('USDT','').strip()+'-USDT', 'buy', size=str(int(qty)))
except Exception as e :
print('Kukoin error while buyin has occured',str(e))
you need to pass your timestamp for your signature encode process
the other thing i did was this:
encode(Config.SECRET_KEY, timestamp + "POST" + "/api/v1/orders" + kucoinOrderRequest.toString()) + "",
KucoinOrderRequest model class toString function is basically a json format of the request, like this:
"{" +
"\n\"clientOid\":\"" + clientOid + '\"' +
",\n\"side\":\"" + side + '\"' +
",\n\"symbol\":\"" + symbol + '\"' +
",\n\"type\":\"" + type + '\"' +
",\n\"size\":\"" + size + '\"' +
"\n}";
The body needs to be converted to json before making the request since this is V2.
We have a working REST API that handles many diff endpoints for FrontEnd. During one specific endpoint where subprocess command should process operation all other endpoints halts and wait for the subprocess to finish. Can anyone help me to understand why it's happening? My thoughts that Python Flask runs asynchronously?
[...]
class withdrawCrypto(Resource):
def get(self):
auth = json.loads('{"ERROR" : "User authentication failed!"}')
wrongAmount = json.loads('{"ERROR" : "Wrong amount"}')
wrongWlt = json.loads('{"ERROR" : "Invalid wallet provided. Please check the wallet addr!"}')
notEnough = json.loads('{"ERROR" : "You don\'t have enough crypto to withdraw this amount"}')
account = str(request.args.get('account'))
token = str(request.args.get('token'))
wallet = str(request.args.get('wlt'))
count = int(request.args.get('count'))
if len(wallet) != 34:
return jsonify(data=wrongWlt)
if wallet[0] != 'B':
return jsonify(data=wrongWlt)
cursorLG.execute("select balance from btc WHERE login=%s;", account)
checkBalance = cursorLG.fetchall()
if checkBalance[0]['balance'] < int(count):
return jsonify(data=notEnough)
cursorLG.execute("select cred from accounts WHERE login=%s;", account)
userCheck = cursorLG.fetchall()
if userCheck[0]['secret'] == token:
if count and int(count) > 0:
host = credentials['rpc']
user = credentials['rpcuser']
passwd= credentials['rpcpassword']
timeout = credentials['rpcclienttimeout']
command = 'bitcoin-cli -rpcconnect=' + host + ' -rpcuser=' + user + ' -rpcpassword=' + passwd + ' -rpcclienttimeout=' + timeout + ' sendtoaddress ' + wallet + ' ' + str(count)
result = subprocess.check_output(command,shell=True).strip()
cursorLG.execute("select balance from btc WHERE login=%s", account)
current = cursorLG.fetchall()
setNew = int(int(current[0]['balance']) - int(count))
cursorLG.execute("replace into btc (login, balance, lastwithdrawalwlt) values (%s, %s, %s) ", (account, setNew, wallet))
return jsonify(data=result.decode("utf-8"))
else:
return jsonify(data=wrongAmount)
else:
print('Failed Crypto withdrawal! Actual passw / user sent: ', userCheck[0]['secret'], token)
return jsonify(data=auth)
[...]
# Serve the high performance http server
if __name__ == '__main__':
http_server = WSGIServer(('', 9000), app)
http_server.serve_forever()
All other endpoints work fast without any delays. Any help appreciated.
The problem was:
result = subprocess.check_output(command,shell=True).strip()
More specific:
shell=True
which waits until the process stops and reads the
STDOUT
As a quick workaround installed gunicorn app and served with a flag --timeout 120 --workers 20
So now 1 worker busy, 19 still returning other requests.
I use a working python script to Check Crypto Coin Price drops, If a Coin drops -3% or more I get a message in Telegram and the same Message is sent to my Email.
Now I want to send a Custom Text for every Market like BTC/ETH/USDT and so on...
I want with python to Check the message and If Message Contains a price drop from BTC Market send text1
ETH Market send text2
USDT Market send text3
The Bot gets the CoinName like this Example: IOTA/BTC < In this case I need the bot to see the word BTC and Send text1. I tryed some things but can't figure it out.
Here the section where the text for the Message is,
`bot.send_message(parse_mode='HTML', chat_id=BINANCE_ID,
text = "<a href = 'https://www.binance.com/tradeDetail.html?symbol {C}_{M}'> {Coin} </a> ".format(C = CoinItself,
M = Market,
Coin = Coin)
+ "▼" + str(RoundPercent) + "% "
+ str(floatDictPrice) + " -> "
+ str(floatDataPrice) + " [Binance]")
binance_prices.update( { CurrentCoin: dataPrice })
send_message("<a href = 'https://www.binance.com/tradeDetail.html?symbol={C}_{M}'> {Coin} </a> ".format(C = CoinItself,
M = Market,
Coin = Coin)
+ "▼" + str(RoundPercent) + "% "
+ str(floatDictPrice) + " -> "
+ str(floatDataPrice) + " [Binance]")`
The bot.send_message section sends the message to telegram & the send_message section sends the same Message text to my Email.
`search_word = input("BTC")
if(search_word == send_message):
print("text1")
else:
print("word not found")`
I tried this one. Any Ideas how to get this working?
What AChampion means is the following:
if search_word in send_message:
print("text1")
else:
print("word not found")
I am trying to use the TransactionSearch API to bring up merchant transaction history. I have been stuck with a "security header is not valid error" and wasn't able to find a solution in the other posts. My code works fine on the sandbox server and I generated live credentials+signature from PayPal. I assume that if they gave me credentials that I have permission to access the API.
My python code is as follows:
import requests
def getTransactionHistory(start_datetime, end_datetime):
headers = {'X-PAYPAL-SECURITY-USERID' : api_username, 'X-PAYPAL-SECURITY-PASSWORD' : api_password,
'X-PAYPAL-SECURITY-SIGNATURE' : api_sig}
data = 'USER=' + api_username + '&PWD=' + api_password + '&SIGNATURE=' + api_sig + '&METHOD=' + \
'TransactionSearch' + '&STARTDATE=' + start_datetime + '&ENDDATE=' + end_datetime + \
'&VERSION=94'
print data
req = requests.post(base+nvp_point, data=data)
return req.text
r = getTransactionHistory('2012-01-01T05:38:48Z', '2012-01-02T05:38:48Z')
The above code is correct. There was a '-' in my signature or password, which was not copy-pasted correctly. Solved.