"Fossies" - the Fresh Open Source Software Archive

Member "monasca-api-3.1.0/monasca_api/common/repositories/sqla/models.py" (27 Sep 2019, 9565 Bytes) of package /linux/misc/openstack/monasca-api-3.1.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. For more information about "models.py" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 4.0.0_vs_5.0.0.

    1 # Copyright 2015 Robin Hood
    2 # Copyright 2016 FUJITSU LIMITED
    3 # (C) Copyright 2016 Hewlett Packard Enterprise Development LP
    4 # Copyright 2018 SUSE Linux GmbH
    5 #
    6 # Licensed under the Apache License, Version 2.0 (the "License");
    7 # you may not use this file except in compliance with the License.
    8 # You may obtain a copy of the License at
    9 #
   10 #     http://www.apache.org/licenses/LICENSE-2.0
   11 #
   12 # Unless required by applicable law or agreed to in writing, software
   13 # distributed under the License is distributed on an "AS IS" BASIS,
   14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   15 # See the License for the specific language governing permissions and
   16 # limitations under the License.
   17 
   18 from __future__ import absolute_import
   19 from __future__ import division
   20 from __future__ import print_function
   21 from __future__ import unicode_literals
   22 
   23 from sqlalchemy import Column
   24 from sqlalchemy.ext import compiler
   25 from sqlalchemy.sql import expression
   26 from sqlalchemy import String, DateTime, Boolean, Integer, Binary, Float
   27 from sqlalchemy import MetaData
   28 from sqlalchemy import Table
   29 
   30 
   31 def get_all_metadata():
   32     """Return metadata for full data model
   33 
   34     This metadata is used for autogenerating a complete (works on an empty
   35     database) schema migration. To ensure this mechanism keeps working please
   36     invoke any new model creation methods you add from this function as well.
   37 
   38     """
   39     metadata = MetaData()
   40     create_a_model(metadata)
   41     create_aa_model(metadata)
   42     create_ad_model(metadata)
   43     create_am_model(metadata)
   44     create_nm_model(metadata)
   45     create_md_model(metadata)
   46     create_mde_model(metadata)
   47     create_mdd_model(metadata)
   48     create_sa_model(metadata)
   49     create_sad_model(metadata)
   50     create_sadd_model(metadata)
   51     create_nmt_model(metadata)
   52 
   53     return metadata
   54 
   55 
   56 def create_a_model(metadata=None):
   57     return Table('alarm', metadata,
   58                  Column('id', String(36)),
   59                  Column('alarm_definition_id', String(36)),
   60                  Column('state', String(20)),
   61                  Column('lifecycle_state', String(50)),
   62                  Column('link', String(512)),
   63                  Column('created_at', DateTime),
   64                  Column('state_updated_at', DateTime),
   65                  Column('updated_at', DateTime))
   66 
   67 
   68 def create_sadd_model(metadata=None):
   69     return Table('sub_alarm_definition_dimension', metadata,
   70                  Column('sub_alarm_definition_id', String(36)),
   71                  Column('dimension_name', String(255)),
   72                  Column('value', String(255)))
   73 
   74 
   75 def create_aa_model(metadata=None):
   76     return Table('alarm_action', metadata,
   77                  Column('alarm_definition_id', String(36)),
   78                  Column('alarm_state', String(20)),
   79                  Column('action_id', String(36)))
   80 
   81 
   82 def create_md_model(metadata=None):
   83     return Table('metric_dimension', metadata,
   84                  Column('dimension_set_id', Binary),
   85                  Column('name', String(255)),
   86                  Column('value', String(255)))
   87 
   88 
   89 def create_mde_model(metadata=None):
   90     return Table('metric_definition', metadata,
   91                  Column('id', Binary),
   92                  Column('name', String(255)),
   93                  Column('tenant_id', String(255)),
   94                  Column('region', String(255)))
   95 
   96 
   97 def create_nm_model(metadata=None):
   98     return Table('notification_method', metadata,
   99                  Column('id', String(36)),
  100                  Column('tenant_id', String(36)),
  101                  Column('name', String(250)),
  102                  Column('type', String(20)),
  103                  Column('address', String(512)),
  104                  Column('period', Integer),
  105                  Column('created_at', DateTime),
  106                  Column('updated_at', DateTime))
  107 
  108 
  109 def create_nmt_model(metadata=None):
  110     return Table('notification_method_type', metadata,
  111                  Column('name', String(20), primary_key=True))
  112 
  113 
  114 def create_mdd_model(metadata=None):
  115     return Table('metric_definition_dimensions', metadata,
  116                  Column('id', Binary),
  117                  Column('metric_definition_id', Binary),
  118                  Column('metric_dimension_set_id', Binary))
  119 
  120 
  121 def create_am_model(metadata=None):
  122     return Table('alarm_metric', metadata,
  123                  Column('alarm_id', String(36)),
  124                  Column('metric_definition_dimensions_id', Binary))
  125 
  126 
  127 def create_ad_model(metadata=None):
  128     return Table('alarm_definition', metadata,
  129                  Column('id', String(36)),
  130                  Column('tenant_id', String(36)),
  131                  Column('name', String(255)),
  132                  Column('description', String(255)),
  133                  Column('expression', String),
  134                  Column('severity', String(20)),
  135                  Column('match_by', String(255)),
  136                  Column('actions_enabled', Boolean),
  137                  Column('created_at', DateTime),
  138                  Column('updated_at', DateTime),
  139                  Column('deleted_at', DateTime))
  140 
  141 
  142 def create_sa_model(metadata=None):
  143     return Table('sub_alarm', metadata,
  144                  Column('id', String(36)),
  145                  Column('alarm_id', String(36)),
  146                  Column('sub_expression_id', String(36)),
  147                  Column('expression', String),
  148                  Column('created_at', DateTime),
  149                  Column('updated_at', DateTime))
  150 
  151 
  152 def create_sad_model(metadata=None):
  153     return Table('sub_alarm_definition', metadata,
  154                  Column('id', String(36)),
  155                  Column('alarm_definition_id', String(36)),
  156                  Column('function', String(10)),
  157                  Column('metric_name', String(100)),
  158                  Column('operator', String(5)),
  159                  Column('threshold', Float),
  160                  Column('period', Integer),
  161                  Column('periods', Integer),
  162                  Column('is_deterministic', Boolean),
  163                  Column('created_at', DateTime),
  164                  Column('updated_at', DateTime))
  165 
  166 
  167 class group_concat(expression.ColumnElement):
  168     name = "group_concat"
  169     order_by = None
  170     separator = ','
  171     columns = []
  172 
  173     def __init__(self, columns, separator=',', order_by=None):
  174         self.order_by = order_by
  175         self.separator = separator
  176         self.columns = columns
  177 
  178 
  179 @compiler.compiles(group_concat, 'oracle')
  180 def _group_concat_oracle(element, compiler_, **kw):
  181     str_order_by = ''
  182     if element.order_by is not None and len(element.order_by) > 0:
  183         str_order_by = "ORDER BY {0}".format(
  184             ", ".join([compiler_.process(x) for x in element.order_by]))
  185     else:
  186         str_order_by = "ORDER BY {0}".format(
  187             ", ".join([compiler_.process(x) for x in element.columns]))
  188     return "LISTAGG({0}, '{2}') WITHIN GROUP ({1})".format(
  189         ", ".join([compiler_.process(x) for x in element.columns]),
  190         str_order_by,
  191         element.separator,
  192     )
  193 
  194 
  195 @compiler.compiles(group_concat, 'postgresql')
  196 def _group_concat_postgresql(element, compiler_, **kw):
  197     str_order_by = ''
  198     if element.order_by is not None and len(element.order_by) > 0:
  199         str_order_by = "ORDER BY {0}".format(
  200             ", ".join([compiler_.process(x) for x in element.order_by]))
  201 
  202     return "STRING_AGG({0}, '{2}' {1})".format(
  203         ", ".join([compiler_.process(x) for x in element.columns]),
  204         str_order_by,
  205         element.separator,
  206     )
  207 
  208 
  209 @compiler.compiles(group_concat, 'sybase')
  210 def _group_concat_sybase(element, compiler_, **kw):
  211     return "LIST({0}, '{1}')".format(
  212         ", ".join([compiler_.process(x) for x in element.columns]),
  213         element.separator,
  214     )
  215 
  216 
  217 @compiler.compiles(group_concat, 'mysql')
  218 def _group_concat_mysql(element, compiler_, **kw):
  219     str_order_by = ''
  220     if element.order_by is not None and len(element.order_by) > 0:
  221         str_order_by = "ORDER BY {0}".format(
  222             ",".join([compiler_.process(x) for x in element.order_by]))
  223     return "GROUP_CONCAT({0} {1} SEPARATOR '{2}')".format(
  224         ", ".join([compiler_.process(x) for x in element.columns]),
  225         str_order_by,
  226         element.separator,
  227     )
  228 
  229 
  230 @compiler.compiles(group_concat)
  231 def _group_concat_default(element, compiler_, **kw):
  232     return "GROUP_CONCAT({0}, '{1}')".format(
  233         ", ".join([compiler_.process(x) for x in element.columns]),
  234         element.separator,
  235     )
  236 
  237 
  238 class field_sort(expression.ColumnElement):
  239     name = "field_sort"
  240     column = None
  241     fields = []
  242 
  243     def __init__(self, column, fields):
  244         self.column = column
  245         self.fields = fields
  246 
  247 
  248 @compiler.compiles(field_sort, "mysql")
  249 def _field_sort_mysql(element, compiler_, **kw):
  250     if element.fields:
  251         return "FIELD({0}, {1})".format(compiler_.process(element.column),
  252                                         ", ".join(map(compiler_.process,
  253                                                       element.fields)))
  254     else:
  255         return str(compiler_.process(element.column))
  256 
  257 
  258 @compiler.compiles(field_sort)
  259 def _field_sort_general(element, compiler_, **kw):
  260     fields_list = []
  261     fields = list(element.fields)
  262     if fields:
  263         fields_list.append("CASE")
  264         for idx, field in enumerate(fields):
  265             fields_list.append("WHEN {0}={1} THEN {2}".format(compiler_.process(element.column),
  266                                                               compiler_.process(field),
  267                                                               idx))
  268         fields_list.append("ELSE {0}".format(len(fields)))
  269         fields_list.append("END")
  270     return " ".join(fields_list)