1
# -*- encoding: utf-8 -*-
3
# Copyright © 2013 eNovance <licensing@enovance.com>
5
# Author: Mehdi Abaakouk <mehdi.abaakouk@enovance.com>
7
# Licensed under the Apache License, Version 2.0 (the "License"); you may
8
# not use this file except in compliance with the License. You may obtain
9
# a copy of the License at
11
# http://www.apache.org/licenses/LICENSE-2.0
13
# Unless required by applicable law or agreed to in writing, software
14
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16
# License for the specific language governing permissions and limitations
21
from sqlalchemy import MetaData, Table, Column, Index
22
from sqlalchemy import String, Float, Integer, Text
27
def upgrade(migrate_engine):
28
meta.bind = migrate_engine
29
table = Table('alarm', meta, autoload=True)
31
type = Column('type', String(50), default='threshold')
32
type.create(table, populate_default=True)
34
rule = Column('rule', Text())
37
for row in table.select().execute().fetchall():
39
if row.matching_metadata is not None:
40
matching_metadata = json.loads(row.matching_metadata)
41
for key in matching_metadata:
42
query.append({'field': key,
44
'value': matching_metadata[key]})
46
'meter_name': row.meter_name,
47
'comparison_operator': row.comparison_operator,
48
'threshold': row.threshold,
49
'statistic': row.statistic,
50
'evaluation_periods': row.evaluation_periods,
54
table.update().where(table.c.id == row.id).values(rule=rule).execute()
56
index = Index('ix_alarm_counter_name', table.c.meter_name)
57
index.drop(bind=migrate_engine)
58
table.c.meter_name.drop()
59
table.c.comparison_operator.drop()
60
table.c.threshold.drop()
61
table.c.statistic.drop()
62
table.c.evaluation_periods.drop()
64
table.c.matching_metadata.drop()
67
def downgrade(migrate_engine):
68
meta.bind = migrate_engine
69
table = Table('alarm', meta, autoload=True)
72
Column('meter_name', String(255)),
73
Column('comparison_operator', String(2)),
74
Column('threshold', Float),
75
Column('statistic', String(255)),
76
Column('evaluation_periods', Integer),
77
Column('period', Integer),
78
Column('matching_metadata', Text())
83
for row in table.select().execute().fetchall():
84
if row.type != 'threshold':
85
#note: type insupported in previous version
86
table.delete().where(table.c.id == row.id).execute()
88
rule = json.loads(row.rule)
89
values = {'comparison_operator': rule['comparison_operator'],
90
'threshold': float(rule['threshold']),
91
'statistic': rule['statistic'],
92
'evaluation_periods': int(rule['evaluation_periods']),
93
'period': int(rule['period']),
94
'meter_name': int(rule['mater_name']),
95
'matching_metadata': {}}
97
#note: op are ignored because previous format don't support it
98
for q in rule['query']:
99
values['matching_metadata'][q['field']] = q['value']
100
values['matching_metadata'] = json.dumps(
101
values['matching_metadata'])
102
table.update().where(table.c.id == row.id
103
).values(**values).execute()
105
index = Index('ix_alarm_counter_name', table.c.meter_name)
106
index.create(bind=migrate_engine)