<commit_before>from boxoffice import app
from boxoffice.models import DiscountPolicy, ItemCollection, LineItem, Order, ORDER_STATUS
from boxoffice.models.line_item import LINE_ITEM_STATUS
import requests
import json
from tabulate import tabulate
from rq.decorators import job

def discount_stats():
    stats = []
    for policy in DiscountPolicy.query.all():
        line_item_count = line_item_count=LineItem.query.filter(LineItem.item_id.in_([li_item.id for li_item in policy.items]), LineItem.status==LINE_ITEM_STATUS.CONFIRMED, LineItem.discount_policy == policy).count()
        stats.append([policy.title, line_item_count])
    return tabulate(stats, headers=["Policy Title", "Count"])

def ticket_stats(item_collection):
    started = [ORDER_STATUS.PURCHASE_ORDER]
    sold = [ORDER_STATUS.SALES_ORDER, ORDER_STATUS.INVOICE]
    cancelled = [ORDER_STATUS.CANCELLED]
    stats = []
    for item in item_collection.items:
        initiated_line_items = LineItem.query.join(Order).filter(LineItem.item == item, Order.status.in_(started)).count()

        sold_line_items = LineItem.query.join(Order).filter(LineItem.item == item, Order.status.in_(sold), LineItem.status==LINE_ITEM_STATUS.CONFIRMED).count()

        cancelled_line_items = LineItem.query.join(Order).filter(LineItem.item == item, Order.status.in_(cancelled)).count()

        stats.append([item.title, initiated_line_items, sold_line_items, cancelled_line_items])
    return tabulate(stats, headers=["Ticket", "Initiated", "Sold", "Cancelled"])

@job('boxoffice')
def post_stats(id, webhook_url):
    with app.test_request_context():
        item_collection = ItemCollection.query.get(id)
        tickets = ticket_stats(item_collection)
        stats = ":moneybag: " + item_collection.title + "\n```" + tickets + "\n\n" + discount_stats() + "```"
        data = {"username": "boxoffice", "text": stats}
        response = requests.post(webhook_url, data=json.dumps(data))

        # TODO: Debug error code, maybe retry?
        if response.status_code != 200:
            pass
<commit_msg>Fix for DiscountPolicy query by item_collection<commit_after>from boxoffice import app
from boxoffice.models import DiscountPolicy, ItemCollection, LineItem, Order, ORDER_STATUS
from boxoffice.models.line_item import LINE_ITEM_STATUS
import requests
import json
from tabulate import tabulate
from rq.decorators import job

def discount_stats(item_collection):
    stats = []
    for policy in DiscountPolicy.query.get(item_collection):
        line_item_count = line_item_count=LineItem.query.filter(LineItem.item_id.in_([li_item.id for li_item in policy.items]), LineItem.status==LINE_ITEM_STATUS.CONFIRMED, LineItem.discount_policy == policy).count()
        stats.append([policy.title, line_item_count])
    return tabulate(stats, headers=["Policy Title", "Count"])

def ticket_stats(item_collection):
    started = [ORDER_STATUS.PURCHASE_ORDER]
    sold = [ORDER_STATUS.SALES_ORDER, ORDER_STATUS.INVOICE]
    cancelled = [ORDER_STATUS.CANCELLED]
    stats = []
    for item in item_collection.items:
        initiated_line_items = LineItem.query.join(Order).filter(LineItem.item == item, Order.status.in_(started)).count()

        sold_line_items = LineItem.query.join(Order).filter(LineItem.item == item, Order.status.in_(sold), LineItem.status==LINE_ITEM_STATUS.CONFIRMED).count()

        cancelled_line_items = LineItem.query.join(Order).filter(LineItem.item == item, Order.status.in_(cancelled)).count()

        stats.append([item.title, initiated_line_items, sold_line_items, cancelled_line_items])
    return tabulate(stats, headers=["Ticket", "Initiated", "Sold", "Cancelled"])

@job('boxoffice')
def post_stats(id, webhook_url):
    with app.test_request_context():
        item_collection = ItemCollection.query.get(id)
        tickets = ticket_stats(item_collection)
        discounts = discount_stats(item_collection)
        stats = ":moneybag: " + item_collection.title + "\n```" + tickets + "\n\n" + discounts + "```"
        data = {"username": "boxoffice", "text": stats}
        response = requests.post(webhook_url, data=json.dumps(data))

        # TODO: Debug error code, maybe retry?
        if response.status_code != 200:
            pass

{'fraction_non_alphanumeric': 0.12083234807283046, 'fraction_numerical': 0.0014187751241428233, 'mean_word_length': 4.529411764705882, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, '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': '207185', 'n_tokens_mistral': 1321, 'n_tokens_neox': 1288, 'n_words': 253}