# Path: zaifbot/errors.py
# class InvalidRequest(ZaifBotError):
#     status_code = 400
# 
#     def __init__(self, message, status_code=None, payload=None):
#         super().__init__(message)
# 
#         if status_code is not None:
#             self.status_code = status_code
#         self.payload = payload
# 
#     def to_dict(self):
#         rv = dict(self.payload or ())
#         rv['Error'] = {'message': self.message}
#         return rv


from collections import OrderedDict
from flask import Blueprint, jsonify, Response
from flask import current_app as app
from zaifbot.errors import InvalidRequest


@resource.route('/', methods=['GET'])
def index():
    strategy_list = list()
    trade_count = 0
    total_profit = 0
    rv = OrderedDict()

    strategies = app.portfolio.collect_strategies()
    for strategy in strategies:
        info = strategy.get_info()
        strategy_list.append(info)
        trade_count += info['trade_count']
        total_profit += info['profit']

    rv['strategies'] = strategy_list
    rv['total_trades'] = trade_count
    rv['total_profit'] = total_profit

    res = jsonify(rv)
    return res


@resource.route('/<id_>', methods=['GET'])
def show(id_):
    strategy = app.portfolio.find_strategy(id_)
    if strategy:
        res = jsonify(strategy.get_info())
        return res

# Path: zaifbot/errors.py
# class InvalidRequest(ZaifBotError):
#     status_code = 400
# 
#     def __init__(self, message, status_code=None, payload=None):
#         super().__init__(message)
# 
#         if status_code is not None:
#             self.status_code = status_code
#         self.payload = payload
# 
#     def to_dict(self):
#         rv = dict(self.payload or ())
#         rv['Error'] = {'message': self.message}
#         return rv


# Path: zaifbot/web/resources/strategies.py
from collections import OrderedDict
from flask import Blueprint, jsonify, Response
from flask import current_app as app
from zaifbot.errors import InvalidRequest



@resource.route('/', methods=['GET'])
def index():
    strategy_list = list()
    trade_count = 0
    total_profit = 0
    rv = OrderedDict()

    strategies = app.portfolio.collect_strategies()
    for strategy in strategies:
        info = strategy.get_info()
        strategy_list.append(info)
        trade_count += info['trade_count']
        total_profit += info['profit']

    rv['strategies'] = strategy_list
    rv['total_trades'] = trade_count
    rv['total_profit'] = total_profit

    res = jsonify(rv)
    return res


@resource.route('/<id_>', methods=['GET'])
def show(id_):
    strategy = app.portfolio.find_strategy(id_)
    if strategy:
        res = jsonify(strategy.get_info())
        return res

    raise InvalidRequest('strategy not found', status_code=404)