"Fossies" - the Fresh Open Source Software Archive

Member "cloudkitty-9.0.0/cloudkitty/tests/samples.py" (10 Apr 2019, 11434 Bytes) of package /linux/misc/openstack/cloudkitty-9.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "samples.py": 8.0.0_vs_9.0.0.

    1 # -*- coding: utf-8 -*-
    2 # Copyright 2015 Objectif Libre
    3 #
    4 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
    5 #    not use this file except in compliance with the License. You may obtain
    6 #    a copy of the License at
    7 #
    8 #         http://www.apache.org/licenses/LICENSE-2.0
    9 #
   10 #    Unless required by applicable law or agreed to in writing, software
   11 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   12 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   13 #    License for the specific language governing permissions and limitations
   14 #    under the License.
   15 #
   16 # @author: St├ęphane Albert
   17 #
   18 import copy
   19 import decimal
   20 
   21 from oslo_utils import uuidutils
   22 
   23 from cloudkitty import utils as ck_utils
   24 
   25 # These have a different format in order to check that both forms are supported
   26 TENANT = 'f266f30b11f246b589fd266f85eeec39'
   27 OTHER_TENANT = '8d3ae500-89ea-4142-9c6e-1269db6a0b64'
   28 
   29 INITIAL_TIMESTAMP = 1420070400
   30 FIRST_PERIOD_BEGIN = INITIAL_TIMESTAMP
   31 FIRST_PERIOD_BEGIN_ISO = ck_utils.ts2iso(FIRST_PERIOD_BEGIN)
   32 FIRST_PERIOD_END = FIRST_PERIOD_BEGIN + 3600
   33 FIRST_PERIOD_END_ISO = ck_utils.ts2iso(FIRST_PERIOD_END)
   34 SECOND_PERIOD_BEGIN = FIRST_PERIOD_END
   35 SECOND_PERIOD_BEGIN_ISO = ck_utils.ts2iso(SECOND_PERIOD_BEGIN)
   36 SECOND_PERIOD_END = SECOND_PERIOD_BEGIN + 3600
   37 SECOND_PERIOD_END_ISO = ck_utils.ts2iso(SECOND_PERIOD_END)
   38 
   39 COMPUTE_METADATA = {
   40     'availability_zone': 'nova',
   41     'flavor': 'm1.nano',
   42     'image_id': 'f5600101-8fa2-4864-899e-ebcb7ed6b568',
   43     'instance_id': '26c084e1-b8f1-4cbc-a7ec-e8b356788a17',
   44     'id': '1558f911-b55a-4fd2-9173-c8f1f23e5639',
   45     'resource_id': '1558f911-b55a-4fd2-9173-c8f1f23e5639',
   46     'memory': '64',
   47     'metadata': {
   48         'farm': 'prod'
   49     },
   50     'name': 'prod1',
   51     'project_id': 'f266f30b11f246b589fd266f85eeec39',
   52     'user_id': '55b3379b949243009ee96972fbf51ed1',
   53     'vcpus': '1'}
   54 
   55 IMAGE_METADATA = {
   56     'checksum': '836c69cbcd1dc4f225daedbab6edc7c7',
   57     'resource_id': '7b5b73f2-9181-4307-a710-b1aa6472526d',
   58     'id': '7b5b73f2-9181-4307-a710-b1aa6472526d',
   59     'container_format': 'aki',
   60     'created_at': '2014-06-04T16:26:01',
   61     'deleted': 'False',
   62     'deleted_at': 'None',
   63     'disk_format': 'aki',
   64     'is_public': 'True',
   65     'min_disk': '0',
   66     'min_ram': '0',
   67     'name': 'cirros-0.3.2-x86_64-uec-kernel',
   68     'protected': 'False',
   69     'size': '4969360',
   70     'status': 'active',
   71     'updated_at': '2014-06-04T16:26:02'}
   72 
   73 FIRST_PERIOD = {
   74     'begin': FIRST_PERIOD_BEGIN,
   75     'end': FIRST_PERIOD_END}
   76 
   77 SECOND_PERIOD = {
   78     'begin': SECOND_PERIOD_BEGIN,
   79     'end': SECOND_PERIOD_END}
   80 
   81 COLLECTED_DATA = [{
   82     'period': FIRST_PERIOD,
   83     'usage': {
   84         'instance': [{
   85             'desc': COMPUTE_METADATA,
   86             'vol': {
   87                 'qty': decimal.Decimal(1.0),
   88                 'unit': 'instance'}}],
   89         'image.size': [{
   90             'desc': IMAGE_METADATA,
   91             'vol': {
   92                 'qty': decimal.Decimal(1.0),
   93                 'unit': 'image'}}]
   94     }}, {
   95     'period': SECOND_PERIOD,
   96     'usage': {
   97         'instance': [{
   98             'desc': COMPUTE_METADATA,
   99             'vol': {
  100                 'qty': decimal.Decimal(1.0),
  101                 'unit': 'instance'}}]
  102     },
  103 }]
  104 
  105 RATED_DATA = copy.deepcopy(COLLECTED_DATA)
  106 RATED_DATA[0]['usage']['instance'][0]['rating'] = {
  107     'price': decimal.Decimal('0.42')}
  108 RATED_DATA[0]['usage']['image.size'][0]['rating'] = {
  109     'price': decimal.Decimal('0.1337')}
  110 RATED_DATA[1]['usage']['instance'][0]['rating'] = {
  111     'price': decimal.Decimal('0.42')}
  112 
  113 
  114 DEFAULT_METRICS_CONF = {
  115     "metrics": {
  116         "cpu": {
  117             "unit": "instance",
  118             "alt_name": "instance",
  119             "groupby": [
  120                 "id",
  121                 "project_id"
  122             ],
  123             "metadata": [
  124                 "flavor",
  125                 "flavor_id",
  126                 "vcpus"
  127             ],
  128             "mutate": "NUMBOOL",
  129             "extra_args": {
  130                 "aggregation_method": "max",
  131                 "resource_type": "instance"
  132             }
  133         },
  134         "image.size": {
  135             "unit": "MiB",
  136             "factor": "1/1048576",
  137             "groupby": [
  138                 "id",
  139                 "project_id"
  140             ],
  141             "metadata": [
  142                 "container_format",
  143                 "disk_format"
  144             ],
  145             "extra_args": {
  146                 "aggregation_method": "max",
  147                 "resource_type": "image"
  148             }
  149         },
  150         "volume.size": {
  151             "unit": "GiB",
  152             "groupby": [
  153                 "id",
  154                 "project_id"
  155             ],
  156             "metadata": [
  157                 "volume_type"
  158             ],
  159             "extra_args": {
  160                 "aggregation_method": "max",
  161                 "resource_type": "volume"
  162             }
  163         },
  164         "network.outgoing.bytes": {
  165             "unit": "MB",
  166             "groupby": [
  167                 "id",
  168                 "project_id"
  169             ],
  170             "factor": "1/1000000",
  171             "metadata": [
  172                 "instance_id"
  173             ],
  174             "extra_args": {
  175                 "aggregation_method": "max",
  176                 "resource_type": "instance_network_interface"
  177             }
  178         },
  179         "network.incoming.bytes": {
  180             "unit": "MB",
  181             "groupby": [
  182                 "id",
  183                 "project_id"
  184             ],
  185             "factor": "1/1000000",
  186             "metadata": [
  187                 "instance_id"
  188             ],
  189             "extra_args": {
  190                 "aggregation_method": "max",
  191                 "resource_type": "instance_network_interface"
  192             }
  193         },
  194         "ip.floating": {
  195             "unit": "ip",
  196             "groupby": [
  197                 "id",
  198                 "project_id"
  199             ],
  200             "metadata": [
  201                 "state"
  202             ],
  203             "mutate": "NUMBOOL",
  204             "extra_args": {
  205                 "aggregation_method": "max",
  206                 "resource_type": "network"
  207             }
  208         },
  209         "radosgw.objects.size": {
  210             "unit": "GiB",
  211             "groupby": [
  212                 "id",
  213                 "project_id"
  214             ],
  215             "factor": "1/1073741824",
  216             "extra_args": {
  217                 "aggregation_method": "max",
  218                 "resource_type": "ceph_account"
  219             }
  220         }
  221     }
  222 }
  223 
  224 
  225 def split_storage_data(raw_data):
  226     final_data = []
  227     for frame in raw_data:
  228         frame['period']['begin'] = ck_utils.ts2iso(frame['period']['begin'])
  229         frame['period']['end'] = ck_utils.ts2iso(frame['period']['end'])
  230         usage_buffer = frame.pop('usage')
  231         # Sort to have a consistent result as we are converting it to a list
  232         for service, data in sorted(usage_buffer.items()):
  233             new_frame = copy.deepcopy(frame)
  234             new_frame['usage'] = {service: data}
  235             new_frame['usage'][service][0]['tenant_id'] = TENANT
  236             final_data.append(new_frame)
  237     return final_data
  238 
  239 
  240 # FIXME(sheeprine): storage is not using decimal for rates, we need to
  241 # transition to decimal.
  242 STORED_DATA = copy.deepcopy(COLLECTED_DATA)
  243 STORED_DATA[0]['usage']['instance'][0]['rating'] = {
  244     'price': 0.42}
  245 STORED_DATA[0]['usage']['image.size'][0]['rating'] = {
  246     'price': 0.1337}
  247 STORED_DATA[1]['usage']['instance'][0]['rating'] = {
  248     'price': 0.42}
  249 
  250 STORED_DATA = split_storage_data(STORED_DATA)
  251 
  252 METRICS_CONF = DEFAULT_METRICS_CONF
  253 
  254 
  255 PROMETHEUS_RESP_INSTANT_QUERY = {
  256     "status": "success",
  257     "data": {
  258         "resultType": "vector",
  259         "result": [
  260             {
  261                 "metric": {
  262                     "code": "200",
  263                     "method": "get",
  264                     "group": "prometheus_group",
  265                     "instance": "localhost:9090",
  266                     "job": "prometheus",
  267                 },
  268                 "value": [
  269                     FIRST_PERIOD_END,
  270                     "7",
  271                 ]
  272             },
  273             {
  274                 "metric": {
  275                     "code": "200",
  276                     "method": "post",
  277                     "group": "prometheus_group",
  278                     "instance": "localhost:9090",
  279                     "job": "prometheus",
  280                 },
  281                 "value": [
  282                     FIRST_PERIOD_END,
  283                     "42",
  284                 ]
  285             },
  286 
  287         ]
  288     }
  289 }
  290 
  291 PROMETHEUS_EMPTY_RESP_INSTANT_QUERY = {
  292     "status": "success",
  293     "data": {
  294         "resultType": "vector",
  295         "result": [],
  296     }
  297 }
  298 
  299 V2_STORAGE_SAMPLE = {
  300     "instance": {
  301         "vol": {
  302             "unit": "instance",
  303             "qty": 1.0,
  304         },
  305         "rating": {
  306             "price": decimal.Decimal(2.5),
  307         },
  308         "groupby": {
  309             "id": uuidutils.generate_uuid(),
  310             "project_id": COMPUTE_METADATA['project_id'],
  311         },
  312         "metadata": {
  313             "flavor": "m1.nano",
  314             "flavor_id": "42",
  315         },
  316     },
  317     "image.size": {
  318         "vol": {
  319             "unit": "MiB",
  320             "qty": 152.0,
  321         },
  322         "rating": {
  323             "price": decimal.Decimal(0.152),
  324         },
  325         "groupby": {
  326             "id": uuidutils.generate_uuid(),
  327             "project_id": COMPUTE_METADATA['project_id'],
  328         },
  329         "metadata": {
  330             "disk_format": "qcow2",
  331         },
  332     },
  333     "volume.size": {
  334         "vol": {
  335             "unit": "GiB",
  336             "qty": 20.0,
  337         },
  338         "rating": {
  339             "price": decimal.Decimal(1.2),
  340         },
  341         "groupby": {
  342             "id": uuidutils.generate_uuid(),
  343             "project_id": COMPUTE_METADATA['project_id'],
  344         },
  345         "metadata": {
  346             "volume_type": "ceph-region1"
  347         },
  348     },
  349     "network.outgoing.bytes": {
  350         "vol": {
  351             "unit": "MB",
  352             "qty": 12345.6,
  353         },
  354         "rating": {
  355             "price": decimal.Decimal(0.00123456),
  356         },
  357         "groupby": {
  358             "id": uuidutils.generate_uuid(),
  359             "project_id": COMPUTE_METADATA['project_id'],
  360         },
  361         "metadata": {
  362             "instance_id": uuidutils.generate_uuid(),
  363         },
  364     },
  365     "network.incoming.bytes": {
  366         "vol": {
  367             "unit": "MB",
  368             "qty": 34567.8,
  369         },
  370         "rating": {
  371             "price": decimal.Decimal(0.00345678),
  372         },
  373         "groupby": {
  374             "id": uuidutils.generate_uuid(),
  375             "project_id": COMPUTE_METADATA['project_id'],
  376         },
  377         "metadata": {
  378             "instance_id": uuidutils.generate_uuid(),
  379         },
  380     },
  381     "ip.floating": {
  382         "vol": {
  383             "unit": "ip",
  384             "qty": 1.0,
  385         },
  386         "rating": {
  387             "price": decimal.Decimal(0.01),
  388         },
  389         "groupby": {
  390             "id": uuidutils.generate_uuid(),
  391             "project_id": COMPUTE_METADATA['project_id'],
  392         },
  393         "metadata": {
  394             "state": "attached",
  395         },
  396     },
  397     "radosgw.objects.size": {
  398         "vol": {
  399             "unit": "GiB",
  400             "qty": 3.0,
  401         },
  402         "rating": {
  403             "price": decimal.Decimal(0.30),
  404         },
  405         "groupby": {
  406             "id": uuidutils.generate_uuid(),
  407             "project_id": COMPUTE_METADATA['project_id'],
  408         },
  409         "metadata": {
  410             "object_id": uuidutils.generate_uuid(),
  411         },
  412     }
  413 }