def fetch_billing_data_for_day(process_day, service_id=None): start_date = convert_local_timezone_to_utc( datetime.combine(process_day, time.min)) end_date = convert_local_timezone_to_utc( datetime.combine(process_day + timedelta(days=1), time.min)) # use notification_history if process day is older than 7 days # this is useful if we need to rebuild the ft_billing table for a date older than 7 days ago. current_app.logger.info("Populate ft_billing for {} to {}".format( start_date, end_date)) transit_data = [] if not service_id: service_ids = [x.id for x in Service.query.all()] else: service_ids = [service_id] for id_of_service in service_ids: for notification_type in (SMS_TYPE, EMAIL_TYPE, LETTER_TYPE): results = _query_for_billing_data( table=Notification, notification_type=notification_type, start_date=start_date, end_date=end_date, service_id=id_of_service) # If data has been purged from Notification then use NotificationHistory if len(results) == 0: results = _query_for_billing_data( table=NotificationHistory, notification_type=notification_type, start_date=start_date, end_date=end_date, service_id=id_of_service) transit_data = transit_data + results return transit_data
def get_months_for_financial_year(year): return [ convert_local_timezone_to_utc(month) for month in (get_months_for_year(4, 13, year) + get_months_for_year(1, 4, year + 1)) if convert_local_timezone_to_utc(month) < datetime.now() ]
def fetch_notification_status_for_day(process_day, service_id=None): start_date = convert_local_timezone_to_utc( datetime.combine(process_day, time.min)) end_date = convert_local_timezone_to_utc( datetime.combine(process_day + timedelta(days=1), time.min)) # use notification_history if process day is older than 7 days # this is useful if we need to rebuild the ft_billing table for a date older than 7 days ago. current_app.logger.info("Fetch ft_notification_status for {} to {}".format( start_date, end_date)) all_data_for_process_day = [] service_ids = [x.id for x in Service.query.all()] # for each service # for each notification type # query notifications for day # if no rows try notificationHistory for service_id in service_ids: for notification_type in [EMAIL_TYPE, SMS_TYPE, LETTER_TYPE]: data_for_service_and_type = query_for_fact_status_data( table=Notification, start_date=start_date, end_date=end_date, notification_type=notification_type, service_id=service_id) if len(data_for_service_and_type) == 0: data_for_service_and_type = query_for_fact_status_data( table=NotificationHistory, start_date=start_date, end_date=end_date, notification_type=notification_type, service_id=service_id) all_data_for_process_day = all_data_for_process_day + data_for_service_and_type return all_data_for_process_day
def get_month_start_and_end_date_in_utc(month_year): """ This function return the start and date of the month_year as UTC, :param month_year: the datetime to calculate the start and end date for that month :return: start_date, end_date, month """ import calendar _, num_days = calendar.monthrange(month_year.year, month_year.month) first_day = datetime(month_year.year, month_year.month, 1, 0, 0, 0) last_day = datetime(month_year.year, month_year.month, num_days, 23, 59, 59, 99999) return convert_local_timezone_to_utc( first_day), convert_local_timezone_to_utc(last_day)
def test_convert_local_timezone_to_utc(): local_timezone = "2017-05-12 13:15" local_timezone_datetime = datetime.strptime(local_timezone, "%Y-%m-%d %H:%M") utc = convert_local_timezone_to_utc(local_timezone_datetime, pytz.timezone("Pacific/Tongatapu")) assert utc == datetime(2017, 5, 12, 0, 15)
def format_mmg_datetime(date): """ We expect datetimes in format 2017-05-21+11%3A56%3A11 - ie, spaces replaced with pluses, and URI encoded (the same as UTC) """ orig_date = format_mmg_message(date) parsed_datetime = iso8601.parse_date(orig_date).replace(tzinfo=None) return convert_local_timezone_to_utc(parsed_datetime)
def dao_old_letters_with_created_status(): yesterday_bst = convert_utc_to_local_timezone(datetime.utcnow()) - timedelta(days=1) last_processing_deadline = yesterday_bst.replace(hour=17, minute=30, second=0, microsecond=0) notifications = Notification.query.filter( Notification.updated_at < convert_local_timezone_to_utc(last_processing_deadline), Notification.notification_type == LETTER_TYPE, Notification.status == NOTIFICATION_CREATED ).order_by( Notification.updated_at ).all() return notifications
def process_shortnumber_keyword_client_response(service, short_number, from_number, body, received_at, provider_ref, client_name): success = None errors = None parsed_datetime = iso8601.parse_date(received_at).replace(tzinfo=None) parsed_datetime = convert_local_timezone_to_utc(parsed_datetime) inbound = create_inbound_sms_keyword_object(service=service, content=body, from_number=from_number, provider_ref=provider_ref, date_received=parsed_datetime, provider_name=client_name) service_callback_api = get_service_delivery_status_callback_api_for_service(service_id=service.id) # queue callback task only if the service_callback_api exists if service_callback_api: encrypted_data = create_shortnumber_keyword_status_callback_data(inbound, service_callback_api) send_keyword_status_to_service.apply_async([encrypted_data], queue=QueueNames.CALLBACKS) success = "{} callback succeeded. keyword stored".format(client_name) return success, errors
def persist_scheduled_notification(notification_id, scheduled_for): scheduled_datetime = convert_local_timezone_to_utc(datetime.strptime(scheduled_for, "%Y-%m-%d %H:%M")) scheduled_notification = ScheduledNotification(notification_id=notification_id, scheduled_for=scheduled_datetime) dao_created_scheduled_notification(scheduled_notification)