# Path: code/api/enums.py
# JSON_TYPES = {
#     INIT: "init",
#     PROPOSE: "propose",
#     PREVOTE: "prevote",
#     VOTE: "vote",
# }
# 
# Path: code/api/utils.py
# def jsonify(data, allow_all_origin=False):
#     from flask import Response, jsonify as json_ify
#     res = json_ify(data)
#     assert isinstance(res, Response)
#     origin = request.environ.get('HTTP_ORIGIN')
#     if not origin:
#         origin = request.environ.get('ORIGIN')
#     # end if
#     if allow_all_origin:
#         res.headers["Access-Control-Allow-Origin"] = '*'
#     elif origin and origin in ORIGIN_LIST:
#         res.headers["Access-Control-Allow-Origin"] = origin
#     # end if
#     return res
# 
# Path: code/api/database.py
# VALUE_TYPE = float
# MSG_TYPE_TYPE = int
# NODE_TYPE = int
# SEQUENCE_TYPE = int
# MSG_TYPE_CLASS_MAP = {
#     INIT: DBInitMessage,
#     PROPOSE: DBProposeMessage,
#     PREVOTE: DBPrevoteMessage,
#     VOTE: DBVoteMessage,
#     # ...
#     ACKNOWLEDGE: DBAcknowledge,
# }
# class DBMessage(db.Entity):
# class DBInitMessage(DBMessage):
# class DBProposeMessage(DBMessage):
# class DBPrevoteMessage(DBMessage):
# class DBVoteMessage(DBMessage):
# class DBAcknowledge(DBMessage):
#     def from_db(self):
#     def to_db(cls, msg):
#     def from_db(self):
#     def to_db(cls, msg):
#     def from_db(self):
#     def to_db(cls, msg):
#     def to_db(cls, msg):
#     def from_db(self):
#     def to_db(cls, msg):
#     def from_db(self):
#     def to_db(cls, msg):
#     def from_db(self):
# def to_db(msg):


from datetime import datetime
from DictObject import DictObject
from flask import Flask, request
from luckydonaldUtils.logger import logging
from pony import orm
from .enums import JSON_TYPES
from .utils import jsonify
from .database import to_db, db, DBVoteMessage, DBMessage, DBInitMessage, DBPrevoteMessage, DBProposeMessage, DBAcknowledge, \
    MSG_TYPE_CLASS_MAP
from node.enums import INIT  # noqa # pylint: disable=unused-import
from node.messages import Message  # noqa # pylint: disable=unused-import
from werkzeug.debug import DebuggedApplication
        raise
# end def


@app.route(API_V1+"/get_value")
@app.route(API_V1+"/get_value/")
@orm.db_session
def get_value():
    """
    Gets latest value they decided on, and the most recent measured value of each node.
    Only considers events in the last 10 seconds.

    > {"summary": 3.456, "1": 2.345, "2": 3.456, "3": 4.567, "4": 5.678}}

    :return:
    """
    latest_vote = orm.select(m for m in DBVoteMessage if m.date > orm.raw_sql("NOW() - '10 seconds'::INTERVAL")).order_by(orm.desc(DBVoteMessage.date)).first()
    if not latest_vote:
        return jsonify({}, allow_all_origin=True)
    # end if
    assert isinstance(latest_vote, DBVoteMessage)
    latest_values = DBMessage.select_by_sql("""
    SELECT DISTINCT ON (m.node) * FROM (
      SELECT * FROM DBmessage
      WHERE type = $INIT
      AND date >= NOW() - '10 seconds'::INTERVAL
    ) as m ORDER BY m.node, m.date DESC
    """)
    data = {"summary": latest_vote.value}
    for msg in latest_values:

# Path: code/api/enums.py
# JSON_TYPES = {
#     INIT: "init",
#     PROPOSE: "propose",
#     PREVOTE: "prevote",
#     VOTE: "vote",
# }
# 
# Path: code/api/utils.py
# def jsonify(data, allow_all_origin=False):
#     from flask import Response, jsonify as json_ify
#     res = json_ify(data)
#     assert isinstance(res, Response)
#     origin = request.environ.get('HTTP_ORIGIN')
#     if not origin:
#         origin = request.environ.get('ORIGIN')
#     # end if
#     if allow_all_origin:
#         res.headers["Access-Control-Allow-Origin"] = '*'
#     elif origin and origin in ORIGIN_LIST:
#         res.headers["Access-Control-Allow-Origin"] = origin
#     # end if
#     return res
# 
# Path: code/api/database.py
# VALUE_TYPE = float
# MSG_TYPE_TYPE = int
# NODE_TYPE = int
# SEQUENCE_TYPE = int
# MSG_TYPE_CLASS_MAP = {
#     INIT: DBInitMessage,
#     PROPOSE: DBProposeMessage,
#     PREVOTE: DBPrevoteMessage,
#     VOTE: DBVoteMessage,
#     # ...
#     ACKNOWLEDGE: DBAcknowledge,
# }
# class DBMessage(db.Entity):
# class DBInitMessage(DBMessage):
# class DBProposeMessage(DBMessage):
# class DBPrevoteMessage(DBMessage):
# class DBVoteMessage(DBMessage):
# class DBAcknowledge(DBMessage):
#     def from_db(self):
#     def to_db(cls, msg):
#     def from_db(self):
#     def to_db(cls, msg):
#     def from_db(self):
#     def to_db(cls, msg):
#     def to_db(cls, msg):
#     def from_db(self):
#     def to_db(cls, msg):
#     def from_db(self):
#     def to_db(cls, msg):
#     def from_db(self):
# def to_db(msg):


# Path: code/api/main.py
from datetime import datetime
from DictObject import DictObject
from flask import Flask, request
from luckydonaldUtils.logger import logging
from pony import orm
from .enums import JSON_TYPES
from .utils import jsonify
from .database import to_db, db, DBVoteMessage, DBMessage, DBInitMessage, DBPrevoteMessage, DBProposeMessage, DBAcknowledge, \
    MSG_TYPE_CLASS_MAP
from node.enums import INIT  # noqa # pylint: disable=unused-import
from node.messages import Message  # noqa # pylint: disable=unused-import
from werkzeug.debug import DebuggedApplication

        raise
# end def


@app.route(API_V1+"/get_value")
@app.route(API_V1+"/get_value/")
@orm.db_session
def get_value():
    """
    Gets latest value they decided on, and the most recent measured value of each node.
    Only considers events in the last 10 seconds.

    > {"summary": 3.456, "1": 2.345, "2": 3.456, "3": 4.567, "4": 5.678}}

    :return:
    """
    latest_vote = orm.select(m for m in DBVoteMessage if m.date > orm.raw_sql("NOW() - '10 seconds'::INTERVAL")).order_by(orm.desc(DBVoteMessage.date)).first()
    if not latest_vote:
        return jsonify({}, allow_all_origin=True)
    # end if
    assert isinstance(latest_vote, DBVoteMessage)
    latest_values = DBMessage.select_by_sql("""
    SELECT DISTINCT ON (m.node) * FROM (
      SELECT * FROM DBmessage
      WHERE type = $INIT
      AND date >= NOW() - '10 seconds'::INTERVAL
    ) as m ORDER BY m.node, m.date DESC
    """)
    data = {"summary": latest_vote.value}
    for msg in latest_values:

        assert isinstance(msg, DBInitMessage)