<?xml version="1.0"?>
<ZopeData>
  <record id="1" aka="AAAAAAAAAAE=">
    <pickle>
      <global name="PythonScript" module="Products.PythonScripts.PythonScript"/>
    </pickle>
    <pickle>
      <dictionary>
        <item>
            <key> <string>Script_magic</string> </key>
            <value> <int>3</int> </value>
        </item>
        <item>
            <key> <string>_bind_names</string> </key>
            <value>
              <object>
                <klass>
                  <global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
                </klass>
                <tuple/>
                <state>
                  <dictionary>
                    <item>
                        <key> <string>_asgns</string> </key>
                        <value>
                          <dictionary>
                            <item>
                                <key> <string>name_container</string> </key>
                                <value> <string>container</string> </value>
                            </item>
                            <item>
                                <key> <string>name_context</string> </key>
                                <value> <string>context</string> </value>
                            </item>
                            <item>
                                <key> <string>name_m_self</string> </key>
                                <value> <string>script</string> </value>
                            </item>
                            <item>
                                <key> <string>name_subpath</string> </key>
                                <value> <string>traverse_subpath</string> </value>
                            </item>
                          </dictionary>
                        </value>
                    </item>
                  </dictionary>
                </state>
              </object>
            </value>
        </item>
        <item>
            <key> <string>_body</string> </key>
            <value> <string>from DateTime import DateTime\n
from Products.ZSQLCatalog.SQLCatalog import Query\n
from Products.ERP5Type.Document import newTempDocument\n
\n
portal = context.getPortalObject()\n
\n
start_date = query_kw.pop(\'start_date\', None)\n
stop_date = query_kw.pop(\'stop_date\', None)\n
software_instance_uid = query_kw.pop(\'software_instance\', None)\n
hosting_subscription_uid = query_kw.pop(\'hosting_subscription_uid\', None)\n
resource_uid = query_kw.pop(\'resource_service\', None)\n
comparison_operator = query_kw.pop(\'resource_operator\', None)\n
resource_value = query_kw.pop(\'resource_value\', None)\n
\n
if not software_instance_uid and not hosting_subscription_uid:\n
  return []\n
\n
if start_date:\n
  query_kw[\'movement.start_date\'] = dict(range=\'min\', query=start_date)\n
if stop_date:\n
  query_kw[\'movement.stop_date\'] = dict(range=\'ngt\', \n
                                     query=stop_date.latestTime())\n
\n
if software_instance_uid and software_instance_uid != \'all\':\n
  query_kw[\'aggregate_uid\'] = software_instance_uid\n
elif hosting_subscription_uid and hosting_subscription_uid != \'all\':\n
  query_kw[\'aggregate_uid\'] = hosting_subscription_uid\n
elif context.getPortalType() == \'Person\':\n
  validation_state = query_kw.pop(\'hosting_validation_state\', None)\n
  hosting_uid_list = []\n
  for subscription in portal.portal_catalog(\n
                          portal_type=\'Hosting Subscription\',\n
                          validation_state=validation_state,\n
                          default_destination_section_uid=context.getUid()):\n
    if validation_state == \'validated\' and subscription.getSlapState() == \'destroy_requested\':\n
      continue\n
    if validation_state == \'archived\' and subscription.getSlapState() != \'destroy_requested\':\n
      continue\n
    hosting_uid_list.append(subscription.getUid())\n
  if hosting_uid_list:\n
    query_kw[\'aggregate_uid\'] = hosting_uid_list\n
  else:\n
    return []\n
elif context.getPortalType() in [\'Software Instance\', \'Hosting Subscription\',\n
                                  \'Computer\']:\n
  query_kw[\'aggregate_uid\'] = context.getUid()\n
else:\n
  return []\n
\n
cpu_resource_uid = context.service_module.cpu_load_percent.getUid()\n
memory_resource_uid = context.service_module.memory_used.getUid()\n
disk_resource_uid = context.service_module.disk_used.getUid()\n
resource_uid_list = [cpu_resource_uid, memory_resource_uid, disk_resource_uid]\n
if resource_uid and comparison_operator and resource_value:\n
  resource_uid_list = [resource_uid]\n
  query_kw[\'quantity\'] = dict(quantity=resource_value, range=comparison_operator)\n
\n
consumption_dict = {}\n
\n
def getPackingListLineForResource(resource_uid_list):\n
  return portal.portal_catalog(\n
    portal_type="Sale Packing List Line",\n
    default_resource_uid = resource_uid_list,\n
    **query_kw\n
  )\n
\n
def setDetailLine(packing_list_line):\n
  start_date = DateTime(packing_list_line.getStartDate()).strftime(\'%Y/%m/%d\')\n
  hosting_s = packing_list_line.getAggregateValue(\n
                                            portal_type=\'Hosting Subscription\')\n
  software_instance = packing_list_line.getAggregateValue(\n
                                            portal_type=\'Software Instance\')\n
  computer_partition = packing_list_line.getAggregateValue(\n
                                            portal_type=\'Computer Partition\')\n
  if software_instance is None:\n
    # In case we found SPL line not aggregated to instance and hosting\n
    return\n
  hosting_reference = hosting_s.getReference()\n
  instance_reference = software_instance.getReference()\n
  computer_title = ""\n
  if computer_partition is not None:\n
    computer = computer_partition.getParent()\n
    computer_title = computer.getTitle() if computer.getCpuCore() is None else \'%s (%s CPU Cores)\' % (computer.getTitle(), computer.getCpuCore())\n
  #default_line = {\'date\': {\'hosting_ref\': [\'hs_title\', {\'instance_ref\': [\'inst_title\', [\'res1\', \'res2\', \'resN\'] ] } ] } }\n
  if not start_date in consumption_dict:\n
    # Add new date line\n
    consumption_dict[start_date] = {hosting_reference: \n
                                      [hosting_s.getTitle(), \n
                                        {instance_reference: \n
                                          [software_instance.getTitle(), \n
                                            [0.0, 0.0, 0.0],\n
                                            software_instance.getRelativeUrl(),\n
                                            computer_title\n
                                          ]\n
                                        },\n
                                        hosting_s.getRelativeUrl()\n
                                      ]\n
                                    }\n
  # Add new Hosting line\n
  if not hosting_reference in consumption_dict[start_date]:\n
    consumption_dict[start_date][hosting_reference] = [hosting_s.getTitle(),\n
                                                        {instance_reference: \n
                                                          [software_instance.getTitle(), \n
                                                            [0.0, 0.0, 0.0],\n
                                                            software_instance.getRelativeUrl(),\n
                                                            computer_title\n
                                                          ]\n
                                                        },\n
                                                        hosting_s.getRelativeUrl()\n
                                                      ]\n
  # Add new instance line\n
  if not instance_reference in consumption_dict[start_date][hosting_reference][1]:\n
    consumption_dict[start_date][hosting_reference][1][instance_reference] = [\n
        software_instance.getTitle(),  [0.0, 0.0, 0.0], software_instance.getRelativeUrl(),\n
        computer_title\n
      ]\n
  if packing_list_line.getResourceUid() == cpu_resource_uid:\n
    quantity = round(float(packing_list_line.getQuantity()), 3)\n
    consumption_dict[start_date][hosting_reference][1][instance_reference][1][0] = quantity\n
  elif packing_list_line.getResourceUid() == memory_resource_uid:\n
    quantity = round( float(packing_list_line.getQuantity()), 3)\n
    consumption_dict[start_date][hosting_reference][1][instance_reference][1][1] = quantity\n
  elif packing_list_line.getResourceUid() == disk_resource_uid:\n
    quantity = round( float(packing_list_line.getQuantity()), 3)\n
    consumption_dict[start_date][hosting_reference][1][instance_reference][1][2] = quantity\n
\n
# Add CPU_LOAD consumption details\n
for packing_list_line in getPackingListLineForResource(resource_uid_list):\n
  setDetailLine(packing_list_line)\n
\n
consumption_list = []\n
i = 1\n
# Sort on movement.start_date in catalog doesn\'t work !\n
for date in sorted(consumption_dict, reverse=True):\n
  for hosting_key in sorted(consumption_dict[date]):\n
    hosting_title, instance_dict, hs_url = consumption_dict[date][hosting_key]\n
    for instance_value_list in instance_dict.values():\n
      instance_title, values, instance_url, computer_title = instance_value_list\n
      line = newTempDocument(portal, instance_url, uid="%s_%s" % (context.getUid(), i))\n
      line.edit(\n
        title=hosting_title,\n
        start_date=date,\n
        instance_title=instance_title,\n
        cpu_load=values[0],\n
        memory_used=values[1],\n
        disk_used=values[2],\n
        computer_title=computer_title,\n
        hosting_url=hs_url,\n
        instance_url=instance_url\n
      )\n
      consumption_list.append(line)\n
      i += 1\n
\n
return consumption_list\n
</string> </value>
        </item>
        <item>
            <key> <string>_params</string> </key>
            <value> <string>**query_kw</string> </value>
        </item>
        <item>
            <key> <string>id</string> </key>
            <value> <string>HostingSubscription_getResourceConsumptionDetailList</string> </value>
        </item>
      </dictionary>
    </pickle>
  </record>
</ZopeData>
