project-koku / masu

This is a READ ONLY repo. See https://github.com/project-koku/koku for current masu implementation
GNU Affero General Public License v3.0
5 stars 6 forks source link

872 cost models api #469

Closed adberglund closed 5 years ago

adberglund commented 5 years ago

Summary

This is a corresponding PR for cost model APIs to use cost models to grab rates when calculating derived cost.

Testing

First created a new cost model using

{
    "name": "Example Cost Model",
    "description": "A super cool example.",
    "rates": [
        {
            "metric": {
                "name": "cpu_core_usage_per_hour"
            },
            "source_type": "OCP",
            "tiered_rates": [
                {
                    "unit": "USD",
                    "value": 0.22,
                    "usage": {
                        "usage_start": null,
                        "usage_end": null,
                        "unit": "USD"
                    }
                }
            ]
        },
        {
            "metric": {
                "name": "cpu_core_request_per_hour"
            },
            "source_type": "OCP",
            "tiered_rates": [
                {
                    "unit": "USD",
                    "value": 0.22,
                    "usage": {
                        "usage_start": null,
                        "usage_end": null,
                        "unit": "USD"
                    }
                }
            ]
        },
        {
            "metric": {
                "name": "memory_gb_usage_per_hour"
            },
            "source_type": "OCP",
            "tiered_rates": [
                {
                    "unit": "USD",
                    "value": 0.22,
                    "usage": {
                        "usage_start": null,
                        "usage_end": null,
                        "unit": "USD"
                    }
                }
            ]
        },
        {
            "metric": {
                "name": "memory_gb_request_per_hour"
            },
            "source_type": "OCP",
            "tiered_rates": [
                {
                    "unit": "USD",
                    "value": 0.22,
                    "usage": {
                        "usage_start": null,
                        "usage_end": null,
                        "unit": "USD"
                    }
                }
            ]
        },
        {
            "metric": {
                "name": "storage_gb_usage_per_month"
            },
            "source_type": "OCP",
            "tiered_rates": [
                {
                    "unit": "USD",
                    "value": 0.22,
                    "usage": {
                        "usage_start": null,
                        "usage_end": null,
                        "unit": "USD"
                    }
                }
            ]
        },
        {
            "metric": {
                "name": "storage_gb_request_per_month"
            },
            "source_type": "OCP",
            "tiered_rates": [
                {
                    "unit": "USD",
                    "value": 0.22,
                    "usage": {
                        "usage_start": null,
                        "usage_end": null,
                        "unit": "USD"
                    }
                }
            ]
        }
    ],
    "provider_uuids": ["3c6e687e-1a09-4a05-970c-2ccf44b0952e"]
}

Then summarized a set of nise generated OpenShift data for the month of June. Derived cost is calculated and the costs API returns the value.

GET /local/v1/reports/openshift/costs/?filter[time_scope_value]=-1
HTTP 200 OK
Allow: GET, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "meta": {
        "count": 1,
        "filter": {
            "time_scope_value": "-1",
            "time_scope_units": "month",
            "resolution": "monthly"
        },
        "total": {
            "infrastructure_cost": {
                "value": 0.0,
                "units": "USD"
            },
            "derived_cost": {
                "value": 18975.132402,
                "units": "USD"
            },
            "cost": {
                "value": 18975.132402,
                "units": "USD"
            }
        }
    },
    "links": {
        "first": "/local/v1/reports/openshift/costs/?filter%5Btime_scope_value%5D=-1&limit=100&offset=0",
        "next": null,
        "previous": null,
        "last": "/local/v1/reports/openshift/costs/?filter%5Btime_scope_value%5D=-1&limit=100&offset=0"
    },
    "data": [
        {
            "date": "2019-06",
            "values": [
                {
                    "date": "2019-06",
                    "infrastructure_cost": {
                        "value": 0.0,
                        "units": "USD"
                    },
                    "derived_cost": {
                        "value": 18975.132402,
                        "units": "USD"
                    },
                    "cost": {
                        "value": 18975.132402,
                        "units": "USD"
                    }
                }
            ]
        }
    ]
}

A manual calculation using the rates gives the same derived cost

SELECT sum(pod_usage_cpu_core_hours) * 0.22 + sum(pod_request_cpu_core_hours) * 0.22 AS cpu_charge,
    sum(pod_usage_memory_gigabyte_hours) * 0.22 + sum(pod_request_memory_gigabyte_hours) * 0.22 AS memory_charge
FROM reporting_ocpusagelineitem_daily_summary
;

 cpu_charge   | memory_charge
---------------+---------------
 1057.98645260 | 2406.40620000

SELECT sum(persistentvolumeclaim_usage_gigabyte_months) * 0.22 + sum(volume_request_storage_gigabyte_months) * 0.22 AS volume_charge
FROM reporting_ocpstoragelineitem_daily_summary
;

volume_charge
----------------
 15510.73975000

SELECT 1057.98645260 + 2406.40620000 + 15510.73975000;
    ?column?
----------------
 18975.13240260
codecov[bot] commented 5 years ago

Codecov Report

:exclamation: No coverage uploaded for pull request base (master@fb9a698). Click here to learn what that means. The diff coverage is 100%.

Impacted file tree graph

@@           Coverage Diff            @@
##             master    #469   +/-   ##
========================================
  Coverage          ?   97.6%           
========================================
  Files             ?      67           
  Lines             ?    3800           
  Branches          ?     405           
========================================
  Hits              ?    3710           
  Misses            ?      50           
  Partials          ?      40
Impacted Files Coverage Δ
masu/database/ocp_rate_db_accessor.py 100% <100%> (ø)
masu/processor/ocp/ocp_report_charge_updater.py 97.5% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update fb9a698...0f62c67. Read the comment docs.