list RDS snapshot created today using Boto 3 - python

I am doing a Python Lambda function to describe list of RDS snapshots created today. The challenge is how to convert the datetime.datetime.today() into a format which RDS client understands?
UPDATE: I have implemented some changes suggested, I have added a string variable to convert the date expression into format which Boto3 RDS understands.
'SnapshotCreateTime': datetime(2015, 1, 1),
today = (datetime.today()).date()
rds_client = boto3.client('rds')
snapshots = rds_client.describe_db_snapshots(SnapshotType='automated')
harini = "datetime("+ today.strftime('%Y,%m,%d') + ")"
print harini
print snapshots
for i in snapshots['DBSnapshots']:
if i['SnapshotCreateTime'].date() == harini:
print(i['DBSnapshotIdentifier'])
print (today)
it is still unable to retrieve list of automated snapshots created today

SnapshotCreateTime is a datetime.datetime object. So, you can just do i['SnapshotCreateTime'].date() to get the date.
import boto3
from datetime import datetime, timezone
today = (datetime.today()).date()
rds_client = boto3.client('rds')
snapshots = rds_client.describe_db_snapshots()
for i in snapshots['DBSnapshots']:
if i['SnapshotCreateTime'].date() == today:
print(i['DBSnapshotIdentifier'])
print (today)

Related

How to return all events on a specific date in Google Calendar API

i am using Python and Google Calendar API to build a calendar.
I would like to get all the events that exist on a given date. The date will be given as a string 'yyyy-mm-dd'.
I have created a function that does that but it is still returning the all-day events on the following day.
from datetime import *
import pytz
def get_events_by_date(api_service, event_date):
split_date = event_date.split('-')
event_date = datetime(int(split_date[0]), int(split_date[1]), int(split_date[2]), 00, 00, 00, 0)
event_date = pytz.UTC.localize(event_date).isoformat()
end = datetime(int(split_date[0]), int(split_date[1]), int(split_date[2]), 23, 59, 59, 999999)
end = pytz.UTC.localize(end).isoformat()
events_result = api_service.events().list(calendarId='primary', timeMin=event_date,timeMax=end).execute()
return events_result.get('items', [])
>>all_events = get_events_by_date(api, '2022-09-24')
>>for event in all_events:
print(event['summary'])
using this example, it prints all events on the '2022-09-24' AND any all-day events on the '2022-09-25'. Any ideas why? (Or a better way to implement this feature)? NOTE: i got the idea for the time zone conversion from this post Generate RFC 3339 timestamp in Python
Although I'm not sure whether this is the direct solution to your issue, from your showing script, how about adding timeZone to the request as follows?
From:
events_result = api_service.events().list(calendarId='primary', timeMin=event_date,timeMax=end).execute()
To:
events_result = api_service.events().list(calendarId='primary', timeMin=event_date,timeMax=end, timeZone="UTC").execute()
Reference:
Events: list

Google Cloud Firestore error: order by clause cannot contain duplicate fields

Seeking guidance on the following error:
google.api_core.exceptions.InvalidArgument: 400 order by clause cannot contain duplicate fields end_date
I am trying to create an endpoint that searches for documents that have an end date between two dates and allows pagination (i.e starting from a particular document).
From the error it seems we cannot use the same field twice (i.e to search between two dates in my case) when also starting from a particular document. Although you can search between two dates without issue when not needing to paginate.
The below code reliably reproduces the error:
from datetime import datetime, timezone
from dateutil.relativedelta import relativedelta
from google.cloud import firestore
one_week_ago = datetime.now(timezone.utc) - relativedelta(weeks=1)
one_weeks_time = datetime.now(timezone.utc) + relativedelta(weeks=1)
collection_name = '...'
starting_doc_id = 'FnAFSazMlXwYWfnEzS1x' # used to support pagination
client = firestore.Client()
collection_ref = client.collection(collection_name)
start_at_snapshot = collection_ref.document(starting_doc_id).get()
collection_ref = collection_ref.start_at(start_at_snapshot)
collection_ref = collection_ref.where('end_date', '>=', one_week_ago)].where('end_date', '<=', one_weeks_time)
for item in collection_ref.stream():
print(item.id)
To get it to work, I had to set order_by by including .order_by('end_date').
The documentation was not clear about this, so I hope this helps someone in the future.
Final working code is:
from datetime import datetime, timezone
from dateutil.relativedelta import relativedelta
from google.cloud import firestore
one_week_ago = datetime.now(timezone.utc) - relativedelta(weeks=1)
one_weeks_time = datetime.now(timezone.utc) + relativedelta(weeks=1)
collection_name = '...'
starting_doc_id = 'FnAFSazMlXwYWfnEzS1x' # used to support pagination
client = firestore.Client()
collection_ref = client.collection(collection_name)
start_at_snapshot = collection_ref.document(starting_doc_id).get()
collection_ref = collection_ref.start_at(start_at_snapshot)
# note only this line changed
collection_ref = collection_ref.order_by('end_date').where('end_date', '>=', one_week_ago)].where('end_date', '<=', one_weeks_time)
for item in collection_ref.stream():
print(item.id)

Dynamodb query/scan using python boto3

my dynamodb table has timestamp(in YYYY-MM-DD HH:MN:SS) as PrimaryKey column and temperature as sortkey while in data {"humidity" : 42 ,"location":"room" , "temperature":,"thermostat":}
in boto3 python i need to scan based on timestamp (now and 15min ago) with condition if difference(temperature - thermostat) > 5 for more than 10 times then return thermostat-5 and if (temperature - thermostat) < 5 for more than 10 times then returns thermostat+5... following is the code
import boto3
import math
import json
import time
import dateutil.tz
from datetime import datetime,timedelta
from dateutil import tz
from dateutil.tz import tzlocal
from boto3.dynamodb.conditions import Key, Attr
client = boto3.client('dynamodb')
dynamodb = boto3.resource('dynamodb')
def lambda_handler(event, context):
#table_name= "thermostat_dynamo"
table_name= "TableDynamo"
Primary_Column_Name = 'timestamp'
table = dynamodb.Table(table_name)
#key_param = "thermostat"
#thermostatVal = table.get_item(Key={key_param:event[key_param]}) ## get record from dynamodb for this sensor
thermostatVal= 77
south = dateutil.tz.gettz('Asia/Kolkata')
now = datetime.now(tz=south)
fifteen_min_ago = now - timedelta(seconds=900)
now = now.strftime('%F %T')
fifteen_min_ago = fifteen_min_ago.strftime('%F %T')
fe = Key('timeStamp').between(fifteen_min_ago,now);
response = table.scan(FilterExpression=fe & Attr('temperature').lt(thermostatVal))
if response['Count'] == 10:
#return thermostatVal+5
thermonew = thermostatVal + 5
tosensor = '{"thermostat":'+ str(thermonew) + '}'
print(tosensor)
#response = client.publish(topic="updatehomesensor", qos=1, payload=tosensor)
return
elif response['Count'] < 10:
print('{"thermostat":' + str(thermostatVal) + '}')
return
If timestamp was a sort key, you could have used a Query request to scan through all the items with timestamp > now-15min.
However, unfortunately, timestamp is your hash key. The only way you can find the items with timestamp > now-15min is to Scan through all your items. This will cost you a lot of money: You pay Amazon for each item scanned, not each item returned after the filtering.
Another problem is that the DynamoDB filtering syntax (look at the FilterExpression documentation) doesn't actually allow to do addition and subtractions as part of the test. If you always want to do "temperature - thermostat", you can use that as one of the attributes (so you can do a FilterExpression on it), and the second attribute would be "thermostat", and later you can add the two up to get the "temperature".

MongoDB Query by DateTime with Python

I want to query MongoDB and return records that have the date of 12/6/2017. The date is in this format:
u'Date': datetime.datetime(2017, 12, 6, 15, 9, 21, 303000)
So, how do I query just the year, month, and day of that entry? I have tried:
date = db.data.find({ 'Date' : {'2017, 12, 6'} },{'Date' : '1'})
for document in date:
print(date)
Which returns: "InvalidDocument: Cannot encode object: set(['2017, 12, 6'])".
I also tried:
date = db.data.find({ 'Date' : {datetime('2017, 12, 6')} },{'Date' : '1'})
for document in date:
print(date)
Which returns: "NameError: name 'datetime' is not defined".
UPDATE...SOLUTION
I was putting the date into Mongo incorrectly. I'm now putting the date into Mongo with Python like this:
import datetime
import dateutil.parser
# the date
now = datetime.datetime.now()
year = now.year
month = now.month
day = now.day
theDate = str(year) + "-" + str(month) + "-" + str(day)
dateStr = theDate
date = dateutil.parser.parse(dateStr)
# Then put that date into your Mongo insert
This is how I'm querying by date. This pulls documents inserted after yersterday (today).
import dateutil.parser, datetime
now = datetime.datetime.now()
year = now.year
month = now.month
yesterday = now.day - 1
dateStr = str(year) + "-" + str(month) + "-" + str(yesterday)
date = dateutil.parser.parse(dateStr)
cursor = db.data.find({'Date' : { '$gt' : date}},{'Date':'1'})
for document in cursor:
print(document)
When you say the object is stored as datetime.datetime, to what are you referring? A custom object?
Per the Mongo docs, this is the only date object they support explicity:
https://docs.mongodb.com/v3.4/reference/method/Date/
From the Docs:
Date() Returns a date either as a string or as a Date object.
Date() returns the current date as a string in the mongo shell. new
Date() returns the current date as a Date object. The mongo shell
wraps the Date object with the ISODate helper. The ISODate is in UTC.
You can specify a particular date by passing to the Date() method a
datetime string. For example:
new Date("") which returns the ISODate with the specified
date. new Date("") which specifies the datetime
in local datetime and returns the ISODate with the specified datetime
in UTC. new Date("") which specifies the
datetime in UTC and returns the ISODate with the specified datetime in
UTC.
To create a query to search on a Date field in mongo, you would instatiate an ISODate like this
db.collection.find({"datefield" : ISODate("YYYY-MM-DDTHH:MM:SS")})
Generally, this will be of somewhat limited use, since the time is measured in milliseconds, so you'll likely need to do a range query similar to:
db.collection.find({"datefield" : {"$gte" : <beginning_of_day_as_ISODate>, "$lte" : <end_of_day_as_ISODate>})
For example:
{createdDate : {$gte : ISODate("2017-12-06T00:00:00"), $lte : ISODate("2017-12-06T23:59:59")}}
If you are using a custom date object, or storing the date in some non-standard format, your query will need to be tailored to that object.
You can try out something like this
date = db.data.find({ 'Date' : {new ISODate("2017-12-6T23:59:59Z") } },{'Date' : '1'})
for document in date:
print(date)
Or you could try out this,
date = db.data.find({ 'Date' : {new Date(2017,12,6) } },{'Date' : '1'})
for document in date:
print(date)

python boto issue with displaying latest snapshot for each instance

I am trying to print Instance name, volume id , instance ID, instance private IP, snapshot start time snapshot ID in one tabular form.
Code :-
#!/usr/bin/python
import os
import boto.ec2
import time
import datetime
conn = boto.ec2.connect_to_region("eu-west-1")
reservations = conn.get_all_instances()
tags = conn.get_all_tags()
snapshots = conn.get_all_snapshots(owner="self")
instances = [i for r in reservations for i in r.instances]
import datetime
today = datetime.datetime.today()
one_day = datetime.timedelta(days=1)
yesterday = today - one_day
from datetime import datetime
for inst in instances:
for snap in snapshots:
start_time = datetime.strptime (snap.start_time.split('.')[0],'%Y-%m-%dT%H:%M:%S')
if start_time > yesterday and 'Name' in inst.tags:
print "%s (%s) (%s) (%s) [%s] " % (inst.tags['Name'], start_time, snap.volume_id
It's looping through all instances again & again. E.g-> I have 17 instances then it loops through all instances 17*17 times and displays the result.
Any suggestions..
It looks like your snapshots don't come from your instances so you could use something like this
for inst, snap in zip(instances, snapshots):
do stuff
That way it will loop through both at the same time and not all of your snapshots per instance

Categories