# Path: opennode/knot/model/user.py
# class IUserStatisticsProvider(Interface):
# 
#     def update(self, username):
#         """ Collect statistics """
# 
#     def get_user_statistics(self, username):
#         """ Get stored user statistics """
# 
# Path: opennode/knot/model/user.py
# class IUserStatisticsLogger(Interface):
# 
#     def log(self, data):
#         """ Log statistics collected using IUserStatisticsProvider """
# 
# Path: opennode/knot/model/compute.py
# class IVirtualCompute(Interface):
#     """A virtual compute."""
#     template = Path(title=u"Template", base_path='../templates/by-name/', relative_to=Path.PARENT)
#     # 0 <= cpu_limit <= 1
#     cpu_limit = schema.Float(title=u"CPU Limit", description=u"CPU usage limit", required=False)
#     ctid = schema.Int(title=u'OpenVZ CTID', description=u'OpenVZ CTID (applies only to OpenVZ VMs)',
#                       required=False, default=101)
# 
#     license_activated = schema.Bool(title=u'License activated', required=False, default=True,
#                                     readonly=True)
# 
#     notify_admin = schema.Bool(title=u'Notify admin on deploy', required=False, default=False,
#                                readonly=True)
# 
# class IDeployed(Interface):
#     """Marker interface implemented when the compute has been deployed."""


from datetime import datetime
from grokcore.component import implements, GlobalUtility
from zope.authentication.interfaces import IAuthentication
from zope.component import getAllUtilitiesRegisteredFor
from zope.component import getUtility
from opennode.knot.model.user import IUserStatisticsProvider
from opennode.knot.model.user import IUserStatisticsLogger
from opennode.knot.model.compute import IVirtualCompute, IDeployed
from opennode.oms.config import get_config
from opennode.oms.model.model.symlink import follow_symlinks
from opennode.oms.zodb import db
import logging
import sys



log = logging.getLogger(__name__)


class UserComputeStatisticsAggregator(GlobalUtility):
    implements(IUserStatisticsProvider)

    @db.assert_transact
    def get_computes(self, username):
        computes = db.get_root()['oms_root']['computes']
        user_computes = []
        for compute in map(follow_symlinks, computes.listcontent()):
            if not IVirtualCompute.providedBy(compute):
                continue

            if not compute.license_activated:
                continue

            if compute.__owner__ == username and IDeployed.providedBy(compute):
                user_computes.append(compute)

        return user_computes

    @db.assert_transact
    def get_credit(self, username):
        profile = db.get_root()['oms_root']['home'][username]
        if profile:
            return profile.credit

# Path: opennode/knot/model/user.py
# class IUserStatisticsProvider(Interface):
# 
#     def update(self, username):
#         """ Collect statistics """
# 
#     def get_user_statistics(self, username):
#         """ Get stored user statistics """
# 
# Path: opennode/knot/model/user.py
# class IUserStatisticsLogger(Interface):
# 
#     def log(self, data):
#         """ Log statistics collected using IUserStatisticsProvider """
# 
# Path: opennode/knot/model/compute.py
# class IVirtualCompute(Interface):
#     """A virtual compute."""
#     template = Path(title=u"Template", base_path='../templates/by-name/', relative_to=Path.PARENT)
#     # 0 <= cpu_limit <= 1
#     cpu_limit = schema.Float(title=u"CPU Limit", description=u"CPU usage limit", required=False)
#     ctid = schema.Int(title=u'OpenVZ CTID', description=u'OpenVZ CTID (applies only to OpenVZ VMs)',
#                       required=False, default=101)
# 
#     license_activated = schema.Bool(title=u'License activated', required=False, default=True,
#                                     readonly=True)
# 
#     notify_admin = schema.Bool(title=u'Notify admin on deploy', required=False, default=False,
#                                readonly=True)
# 
# class IDeployed(Interface):
#     """Marker interface implemented when the compute has been deployed."""


# Path: opennode/knot/backend/stats.py
from datetime import datetime
from grokcore.component import implements, GlobalUtility
from zope.authentication.interfaces import IAuthentication
from zope.component import getAllUtilitiesRegisteredFor
from zope.component import getUtility
from opennode.knot.model.user import IUserStatisticsProvider
from opennode.knot.model.user import IUserStatisticsLogger
from opennode.knot.model.compute import IVirtualCompute, IDeployed
from opennode.oms.config import get_config
from opennode.oms.model.model.symlink import follow_symlinks
from opennode.oms.zodb import db
import logging
import sys




log = logging.getLogger(__name__)


class UserComputeStatisticsAggregator(GlobalUtility):
    implements(IUserStatisticsProvider)

    @db.assert_transact
    def get_computes(self, username):
        computes = db.get_root()['oms_root']['computes']
        user_computes = []
        for compute in map(follow_symlinks, computes.listcontent()):
            if not IVirtualCompute.providedBy(compute):
                continue

            if not compute.license_activated:
                continue

            if compute.__owner__ == username and IDeployed.providedBy(compute):
                user_computes.append(compute)

        return user_computes

    @db.assert_transact
    def get_credit(self, username):
        profile = db.get_root()['oms_root']['home'][username]
        if profile:
            return profile.credit

        else: