def match_stats(timespan):
    """
    Return all match victories/losses that match timespan.

    Return all matches that match timespan. Then checks the match's victory conditions in checkModeVictory.

    Parameters:
        timespan(tuple): Three-part tuple. First part is "monthly" or "all".
        Second part is a datetime with month and year as the starting month.

    Returns:
        array: Array of MatchTypeVictory.

    """
    db = LocalProxy(lambda: current_app.db.session)
    q = models.Match.query
    timespan_criteria = None
    count_query = db.query(models.Match.modes_string,
                           func.count(models.Match.id))
    if timespan[0] != "all":
        query_start = timespan[1]
        query_end = add_months(query_start, 1)
        print(query_start, query_end)
        timespan_criteria = and_(
            models.Match.start_datetime is not None,
            models.Match.start_datetime.between(query_start, query_end))
        q = q.filter(timespan_criteria)
        count_query = count_query.filter(timespan_criteria)

    # completion-agnostic playrate first

    counts = count_query.group_by(models.Match.modes_string).all()
    formattedCounts = [list(a) for a in (zip(*counts))]

    q = q.filter(~models.Match.modes_string.contains('|'),
                 ~models.Match.mastermode.contains('mixed'))
    q = q.all()

    matches = []

    for match in q:
        if match.is_mixed():
            continue
        victory = checkModeVictory(match)
        if victory is None:
            continue
        s = True if match.mastermode == "secret" else False
        t = match.modes_string
        m = MatchTypeVictory(victory, s, t)
        matches.append(m)
    return matches, formattedCounts