add unit column for metric

Change-Id: I9cdf83f1ec51670e74a11125ac9f048bf9d70d84
Closes-Bug: #1530967
This commit is contained in:
zhangguoqing 2016-05-04 12:26:20 +00:00 committed by Julien Danjou
parent 45150036bb
commit d74ea92ebe
7 changed files with 69 additions and 3 deletions

View File

@ -306,7 +306,8 @@ class IndexerDriver(object):
@staticmethod
def create_metric(id, created_by_user_id, created_by_project_id,
archive_policy_name, name=None, resource_id=None):
archive_policy_name, name=None, unit=None,
resource_id=None):
raise exceptions.NotImplementedError
@staticmethod

View File

@ -0,0 +1,38 @@
# Copyright 2016 OpenStack Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
"""add unit column for metric
Revision ID: c62df18bf4ee
Revises: 2e0b912062d1
Create Date: 2016-05-04 12:31:25.350190
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'c62df18bf4ee'
down_revision = '2e0b912062d1'
branch_labels = None
depends_on = None
def upgrade():
op.add_column('metric', sa.Column('unit',
sa.String(length=31),
nullable=True))

View File

@ -370,12 +370,13 @@ class SQLAlchemyIndexer(indexer.IndexerDriver):
def create_metric(self, id, created_by_user_id, created_by_project_id,
archive_policy_name,
name=None, resource_id=None):
name=None, unit=None, resource_id=None):
m = Metric(id=id,
created_by_user_id=created_by_user_id,
created_by_project_id=created_by_project_id,
archive_policy_name=archive_policy_name,
name=name,
unit=unit,
resource_id=resource_id)
try:
with self.facade.writer() as session:
@ -535,12 +536,14 @@ class SQLAlchemyIndexer(indexer.IndexerDriver):
if update == 0:
raise indexer.NoSuchMetric(value)
else:
unit = value.get('unit')
ap_name = value['archive_policy_name']
m = Metric(id=uuid.uuid4(),
created_by_user_id=r.created_by_user_id,
created_by_project_id=r.created_by_project_id,
archive_policy_name=ap_name,
name=name,
unit=unit,
resource_id=r.id)
session.add(m)
try:

View File

@ -161,6 +161,7 @@ class Metric(Base, GnocchiBase, storage.Metric):
ondelete="SET NULL",
name="fk_metric_resource_id_resource_id"))
name = sqlalchemy.Column(sqlalchemy.String(255))
unit = sqlalchemy.Column(sqlalchemy.String(31))
status = sqlalchemy.Column(sqlalchemy.Enum('active', 'delete',
name="metric_status_enum"),
nullable=False,
@ -172,6 +173,7 @@ class Metric(Base, GnocchiBase, storage.Metric):
"created_by_user_id": self.created_by_user_id,
"created_by_project_id": self.created_by_project_id,
"name": self.name,
"unit": self.unit,
}
unloaded = sqlalchemy.inspect(self).unloaded
if 'resource' in unloaded:
@ -195,6 +197,7 @@ class Metric(Base, GnocchiBase, storage.Metric):
and self.created_by_user_id == other.created_by_user_id
and self.created_by_project_id == other.created_by_project_id
and self.name == other.name
and self.unit == other.unit
and self.resource_id == other.resource_id)
or (storage.Metric.__eq__(self, other)))

View File

@ -586,6 +586,8 @@ class MetricsController(rest.RestController):
"project_id": six.text_type,
"archive_policy_name": six.text_type,
"name": six.text_type,
voluptuous.Optional("unit"):
voluptuous.All(six.text_type, voluptuous.Length(max=31)),
})
# NOTE(jd) Define this method as it was a voluptuous schema it's just a
@ -621,6 +623,7 @@ class MetricsController(rest.RestController):
"project_id": definition.get('project_id'),
"archive_policy_name": archive_policy_name,
"name": name,
"unit": definition.get('unit'),
})
return definition
@ -634,6 +637,7 @@ class MetricsController(rest.RestController):
uuid.uuid4(),
user, project,
name=body.get('name'),
unit=body.get('unit'),
archive_policy_name=body['archive_policy_name'])
except indexer.NoSuchArchivePolicy as e:
abort(400, e)

View File

@ -63,17 +63,33 @@ tests:
response_strings:
- "[]"
- name: create metric with name
- name: create metric with name and unit
url: /v1/metric
request_headers:
content-type: application/json
method: post
data:
name: "disk.io.rate"
unit: "B/s"
status: 201
response_json_paths:
$.archive_policy_name: cookies
$.name: disk.io.rate
$.unit: B/s
- name: create metric with name and over length unit
url: /v1/metric
request_headers:
content-type: application/json
method: post
data:
name: "disk.io.rate"
unit: "over_length_unit_over_length_unit"
status: 400
response_strings:
# split to not match the u' in py2
- "Invalid input: length of value must be at most 31 for dictionary value @ data["
- "'unit']"
- name: create metric with name no rule
url: /v1/metric

View File

@ -102,6 +102,7 @@ class TestIndexerDriver(tests_base.TestCase):
self.assertEqual(m.created_by_user_id, user)
self.assertEqual(m.created_by_project_id, project)
self.assertIsNone(m.name)
self.assertIsNone(m.unit)
self.assertIsNone(m.resource_id)
m2 = self.index.list_metrics(id=r1)
self.assertEqual([m], m2)