# Path: planb/models.py
# class HostGroup(models.Model):
#     name = models.CharField(max_length=63, unique=True)
#     notify_email = MultiEmailField(
#         blank=True, null=True,
#         help_text=_('Use a newline per emailaddress'))
#     last_monthly_report = models.DateTimeField(blank=True, null=True)
#     notes = models.TextField(blank=True, help_text=_(
#         'Description, guidelines and agreements for the hostgroup.'))
# 
#     blacklist_hours = models.CharField(
#         _('Blacklist hours'), max_length=31, blank=True,
#         validators=[validate_blacklist_hours], help_text=_(
#             'Specify hours during which backups are disabled using notation '
#             'h,h-h or none to disable blacklist hours. When left empty the '
#             'system blacklist hours are used.'))
#     retention = models.CharField(
#         max_length=31, blank=True, validators=[validate_retention],
#         help_text=_(
#             'The backup retention period using notation <n><period> separated '
#             'by comma: 1y,6m,3w,15d. When left empty the system retention '
#             'periods are used.'))
# 
#     def get_blacklist_hours(self):
#         if self.blacklist_hours:
#             return self.blacklist_hours
#         return settings.PLANB_BLACKLIST_HOURS
#     get_blacklist_hours.short_description = _('Blacklist hours')
# 
#     def get_retention(self):
#         if self.retention:
#             return self.retention
#         return settings.PLANB_RETENTION
#     get_retention.short_description = _('Retention')
# 
#     def __str__(self):
#         return self.name
# 
#     class Meta:
#         ordering = ('name',)


import logging
import requests
from datetime import date
from urllib.error import HTTPError
from urllib.parse import urlencode
from urllib.request import Request, urlopen
from planb.models import HostGroup
                    self.handle_error(d, e)
            # Post the items that did not fail validation.
            if remaining:
                self.post(remaining)
        else:
            self.handle_error(data, error)

    def handle_error(self, data, error):
        if error.get('non_field_errors', []) == self.UNIQUE_ERROR:
            logger.warning('%r returned error %r', data, error)
        else:
            logger.error('%r returned error %r', data, error)

    def format_data(self, hostgroup, fileset, report_date):
        return {
            'relation_code': hostgroup.name,
            'item_code': fileset.friendly_name,
            'service_code': 'backup-size-gibibyte',
            'date': report_date.strftime('%Y-%m-%d'),
            'value': round(fileset.total_size_mb / 1024, 5),  # MiB to GiB
            'unit': 'GiB',
        }


def daily_hostgroup_report(data_poster, hostgroup_qs=None):
    """
    This could be run daily to report to REMOTE how many data each
    hostgroup has backed up.
    """
    if hostgroup_qs is None:

# Path: planb/models.py
# class HostGroup(models.Model):
#     name = models.CharField(max_length=63, unique=True)
#     notify_email = MultiEmailField(
#         blank=True, null=True,
#         help_text=_('Use a newline per emailaddress'))
#     last_monthly_report = models.DateTimeField(blank=True, null=True)
#     notes = models.TextField(blank=True, help_text=_(
#         'Description, guidelines and agreements for the hostgroup.'))
# 
#     blacklist_hours = models.CharField(
#         _('Blacklist hours'), max_length=31, blank=True,
#         validators=[validate_blacklist_hours], help_text=_(
#             'Specify hours during which backups are disabled using notation '
#             'h,h-h or none to disable blacklist hours. When left empty the '
#             'system blacklist hours are used.'))
#     retention = models.CharField(
#         max_length=31, blank=True, validators=[validate_retention],
#         help_text=_(
#             'The backup retention period using notation <n><period> separated '
#             'by comma: 1y,6m,3w,15d. When left empty the system retention '
#             'periods are used.'))
# 
#     def get_blacklist_hours(self):
#         if self.blacklist_hours:
#             return self.blacklist_hours
#         return settings.PLANB_BLACKLIST_HOURS
#     get_blacklist_hours.short_description = _('Blacklist hours')
# 
#     def get_retention(self):
#         if self.retention:
#             return self.retention
#         return settings.PLANB_RETENTION
#     get_retention.short_description = _('Retention')
# 
#     def __str__(self):
#         return self.name
# 
#     class Meta:
#         ordering = ('name',)


# Path: planb/contrib/billing.py
import logging
import requests
from datetime import date
from urllib.error import HTTPError
from urllib.parse import urlencode
from urllib.request import Request, urlopen
from planb.models import HostGroup

                    self.handle_error(d, e)
            # Post the items that did not fail validation.
            if remaining:
                self.post(remaining)
        else:
            self.handle_error(data, error)

    def handle_error(self, data, error):
        if error.get('non_field_errors', []) == self.UNIQUE_ERROR:
            logger.warning('%r returned error %r', data, error)
        else:
            logger.error('%r returned error %r', data, error)

    def format_data(self, hostgroup, fileset, report_date):
        return {
            'relation_code': hostgroup.name,
            'item_code': fileset.friendly_name,
            'service_code': 'backup-size-gibibyte',
            'date': report_date.strftime('%Y-%m-%d'),
            'value': round(fileset.total_size_mb / 1024, 5),  # MiB to GiB
            'unit': 'GiB',
        }


def daily_hostgroup_report(data_poster, hostgroup_qs=None):
    """
    This could be run daily to report to REMOTE how many data each
    hostgroup has backed up.
    """
    if hostgroup_qs is None:

        hostgroup_qs = HostGroup.objects.all()