<commit_before>import datetime
import client
from itertools import imap
from sendresult import SendResult
import dynamodb

import logging
logger = logging.getLogger()

def yesterday():
    return datetime.datetime.now() - datetime.timedelta(days=1)


def daysAgo(days):
    return datetime.datetime.now() - datetime.timedelta(days=days)


"""
Simple Operators
"""


def valueEquals(prop, value):
    return {
        'Property': prop,
        'SimpleOperator': 'equals',
        'Value': value
    }


def dateValueGreaterThan(prop, value):
    return {
        'Property': prop,
        'SimpleOperator': 'greaterThan',
        'DateValue': value
    }


"""
Complex Logic ("complex")
"""


def exactTargetOr(leftOperand, rightOperand):
    return {
        'LeftOperand': leftOperand,
        'RightOperand': rightOperand,
        'LogicalOperator': 'OR'
    }


def exactTargetAnd(leftOperand, rightOperand):
    return {
        'LeftOperand': leftOperand,
        'RightOperand': rightOperand,
        'LogicalOperator': 'AND'
    }


"""
Specific values to query for
"""


def sendWithSendDefinition(customerKey):
    """
    :param customerKey: This is ExternalKey in ExactTarget under; Interactions -> Programs
    :return: A filter to be used when querying for an ExactTarget Send
    """
    return valueEquals('EmailSendDefinition.CustomerKey', customerKey)


def sendDateAfterDateFilter(after):
    """
    :param after: A datetime representing a date in which results will appear after
    :return: A filter to be used when querying ExactTarget Send
    """
    return dateValueGreaterThan('SendDate', after)


def createdDateAfterDateFilter(after):
    return dateValueGreaterThan('CreatedDate', after.isoformat())


def getLastNDaySends(days=1):
    """
    :param days: Last number of days to get
    :return: [SendResult]
    """
    searchFilter = exactTargetAnd(
        exactTargetOr(
            exactTargetOr(
                exactTargetOr(
                    sendWithSendDefinition(111),
                    sendWithSendDefinition(16216)),
                exactTargetOr(
                    sendWithSendDefinition(1933),
                    sendWithSendDefinition(2014))),
            sendWithSendDefinition(16125)),
        sendDateAfterDateFilter(<commit_msg>Add methods for pulling email definitions out of config and creating correct nested tree structure as a complex query (exact target term)<commit_after>import datetime
import client
from sendresult import SendResult
from itertools import izip_longest, imap
import config

import logging
logger = logging.getLogger()

def yesterday():
    return datetime.datetime.now() - datetime.timedelta(days=1)


def daysAgo(days):
    return datetime.datetime.now() - datetime.timedelta(days=days)


"""
Simple Operators
"""


def valueEquals(prop, value):
    return {
        'Property': prop,
        'SimpleOperator': 'equals',
        'Value': value
    }


def dateValueGreaterThan(prop, value):
    return {
        'Property': prop,
        'SimpleOperator': 'greaterThan',
        'DateValue': value
    }


"""
Complex Logic ("complex")
"""


def exactTargetOr(leftOperand, rightOperand):
    return {
        'LeftOperand': leftOperand,
        'RightOperand': rightOperand,
        'LogicalOperator': 'OR'
    }


def exactTargetAnd(leftOperand, rightOperand):
    return {
        'LeftOperand': leftOperand,
        'RightOperand': rightOperand,
        'LogicalOperator': 'AND'
    }


"""
Specific values to query for
"""


def sendWithSendDefinition(customerKey):
    """
    :param customerKey: This is ExternalKey in ExactTarget under; Interactions -> Programs
    :return: A filter to be used when querying for an ExactTarget Send
    """
    return valueEquals('EmailSendDefinition.CustomerKey', customerKey)


def sendDateAfterDateFilter(after):
    """
    :param after: A datetime representing a date in which results will appear after
    :return: A filter to be used when querying ExactTarget Send
    """
    return dateValueGreaterThan('SendDate', after)


def createdDateAfterDateFilter(after):
    return dateValueGreaterThan('CreatedDate', after.isoformat())


def grouper(iterable, n, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)


def buildOrsFromList(orList):
    """
    Build a nested OR structure for exact target
    :param orList: list of items to get OR'd
    :return: exactTargetOr structure
    """
    if len(orList) == 2:
        return exactTargetOr(orList[0], orList[1])
    else:
        return buildOrsFromList(
            [exactTargetOr(first, second) if second else first for first, second in grouper(orList, 2)])


def getLastNDaySends(days=1):
    """
    :param days: Last number of days to get
    :return: [SendResult]
    """
    orLists = buildOrsFromList(list(imap(lambda id: sendWithSendDefinition(id), config.SEND_DEFINITIONS)))

    searchFilter = exactTargetAnd(
        orLists,
        sendDateAfterDateFilter(
{'fraction_non_alphanumeric': 0.09929494712103408, 'fraction_numerical': 0.005875440658049354, 'mean_word_length': 3.2772194304857623, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, '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': '3781527', 'n_tokens_mistral': 1474, 'n_tokens_neox': 1429, 'n_words': 407}