<commit_before>import os
import json
import boto3
import datetime
import urllib.request
import re

sns = boto3.client('sns')

SLACK_ICON = ':adzerk:'
SLACK_USERNAME = 'Adzerk'
SLACK_CHANNEL = '#adzerk-flights'
SLACK_CHANNEL_PROBLEMS = '#adzerk-flights-problems'

ADZERK_REQUEST_HEADERS = {
    'X-Adzerk-ApiKey': os.environ['ADZERK_API_KEY']
}


def slack_attachments(flights):
    attachments = []

    for flight in flights:
        fields = []

        fields.append({
            'title': 'StartDate',
            'value': flight['StartDate'],
            'short': True
        })

        if 'LifetimeCapAmount' in flight and flight['LifetimeCapAmount']:
            fields.append({
                'title': 'LifetimeCapAmount',
                'value': flight['LifetimeCapAmount'],
                'short': True
            })

        if 'EndDate' in flight and flight['EndDate']:
            fields.append({
                'title': 'EndDate',
                'value': flight['EndDate'],
                'short': True
            })

        attachments.append({
            'title': flight['Name'],
            'fields': fields
        })

    return attachments


# Finds all active flights starting tomorrow, and sends a message to Slack
# with their details
def upcoming_flight_report():
    flights = []
    # problem_flights = []

    t1 = datetime.datetime.utcnow() + datetime.timedelta(days=0)
    t2 = datetime.datetime.utcnow() + datetime.timedelta(days=2)

    s1 = t1.strftime("%Y-%m-%d%%2023:59:59")
    s2 = t2.strftime("%Y-%m-%d%%2000:00:00")

    api_url = f"https://api.adzerk.net/v1/fast/flight?isActive=true&afterStartDate={s1}&beforeStartDate={s2}"

    print(f"Filter flights for {s1} – {s2}")

    flight_filter_req = urllib.request.Request(
        api_url,
        headers=ADZERK_REQUEST_HEADERS)
    with urllib.request.urlopen(flight_filter_req) as response:
        response_data = response.read()

    # The response is a line-delimited set of JSON flight objects
    for json_str in response_data.decode("utf-8").split("\n"):
        if len(json_str) > 1:
            flight_data = json.loads(json_str)
            flights.append(flight_data)

            # if 'EndDate' in flight_data and flight_data['EndDate']:
            #     if not flight_data['EndDate'].endswith('59:00Z'):
            #         problem_flights.append(flight_data)

    print(f"Found {len(flights)} flights for this period")
    # print(f"Found {len(problem_flights)} problem flights for this period")

    if len(flights) > 0:
        sns.publish(
            TopicArn=os.environ['SLACK_MESSAGE_RELAY_SNS_TOPIC_ARN'],
            Message=json.dumps({
                'channel': SLACK_CHANNEL,
                'username': SLACK_USERNAME,
                'icon_emoji': SLACK_ICON,
                'attachments': slack_attachments(flights)
            })
        )

def problem_flight_report():
    flights = []
    problem_flights = []

    t1 = datetime.datetime.utcnow() + datetime.timedelta(days=0)
    s1 = t1.strftime("%Y-%m-%d%%2023:59:59")

    api_url = f"https://api.adzerk.net/v1/fast/flight?isActive=true&afterStartDate={s1}"

    flight_filter_req = urllib.request.Request(
        api_url,
        headers=ADZERK_REQUEST_HEADERS)
    with urllib.request.urlopen(flight_filter_req) as response:
        response_data = response.read()

    # The response is a line-delimited set of JSON flight objects
    for json_str in response_data.decode("utf-8").split("\n"):
        if len(json_str) > 1:
            flight_data = json.loads(json_str)
            flights.append(flight_data)<commit_msg>Update Adzerk channel names<commit_after>import os
import json
import boto3
import datetime
import urllib.request
import re

sns = boto3.client('sns')

SLACK_ICON = ':adzerk:'
SLACK_USERNAME = 'Adzerk'
SLACK_CHANNEL = '#ad-ops-adzerk-flights'
SLACK_CHANNEL_PROBLEMS = '#ad-ops-flight-problems'

ADZERK_REQUEST_HEADERS = {
    'X-Adzerk-ApiKey': os.environ['ADZERK_API_KEY']
}


def slack_attachments(flights):
    attachments = []

    for flight in flights:
        fields = []

        fields.append({
            'title': 'StartDate',
            'value': flight['StartDate'],
            'short': True
        })

        if 'LifetimeCapAmount' in flight and flight['LifetimeCapAmount']:
            fields.append({
                'title': 'LifetimeCapAmount',
                'value': flight['LifetimeCapAmount'],
                'short': True
            })

        if 'EndDate' in flight and flight['EndDate']:
            fields.append({
                'title': 'EndDate',
                'value': flight['EndDate'],
                'short': True
            })

        attachments.append({
            'title': flight['Name'],
            'fields': fields
        })

    return attachments


# Finds all active flights starting tomorrow, and sends a message to Slack
# with their details
def upcoming_flight_report():
    flights = []
    # problem_flights = []

    t1 = datetime.datetime.utcnow() + datetime.timedelta(days=0)
    t2 = datetime.datetime.utcnow() + datetime.timedelta(days=2)

    s1 = t1.strftime("%Y-%m-%d%%2023:59:59")
    s2 = t2.strftime("%Y-%m-%d%%2000:00:00")

    api_url = f"https://api.adzerk.net/v1/fast/flight?isActive=true&afterStartDate={s1}&beforeStartDate={s2}"

    print(f"Filter flights for {s1} – {s2}")

    flight_filter_req = urllib.request.Request(
        api_url,
        headers=ADZERK_REQUEST_HEADERS)
    with urllib.request.urlopen(flight_filter_req) as response:
        response_data = response.read()

    # The response is a line-delimited set of JSON flight objects
    for json_str in response_data.decode("utf-8").split("\n"):
        if len(json_str) > 1:
            flight_data = json.loads(json_str)
            flights.append(flight_data)

            # if 'EndDate' in flight_data and flight_data['EndDate']:
            #     if not flight_data['EndDate'].endswith('59:00Z'):
            #         problem_flights.append(flight_data)

    print(f"Found {len(flights)} flights for this period")
    # print(f"Found {len(problem_flights)} problem flights for this period")

    if len(flights) > 0:
        sns.publish(
            TopicArn=os.environ['SLACK_MESSAGE_RELAY_SNS_TOPIC_ARN'],
            Message=json.dumps({
                'channel': SLACK_CHANNEL,
                'username': SLACK_USERNAME,
                'icon_emoji': SLACK_ICON,
                'attachments': slack_attachments(flights)
            })
        )

def problem_flight_report():
    flights = []
    problem_flights = []

    t1 = datetime.datetime.utcnow() + datetime.timedelta(days=-28)
    s1 = t1.strftime("%Y-%m-%d%%2023:59:59")

    api_url = f"https://api.adzerk.net/v1/fast/flight?isActive=true&afterStartDate={s1}"

    flight_filter_req = urllib.request.Request(
        api_url,
        headers=ADZERK_REQUEST_HEADERS)
    with urllib.request.urlopen(flight_filter_req) as response:
        response_data = response.read()

    # The response is a line-delimited set of JSON flight objects
    for json_str in response_data.decode("utf-8").split("\n"):
        if len(json_str) > 1:
            flight_data = json.loads(json_str)
            flights.append(flight_data)
{'fraction_non_alphanumeric': 0.12572373862696443, 'fraction_numerical': 0.015026192445547284, 'mean_word_length': 2.54248046875, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4642903', 'n_tokens_mistral': 2435, 'n_tokens_neox': 2305, 'n_words': 438}