1
0

template.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # -*- coding:utf-8 -*-
  2. # Copyright 2017 Xiaomi, Inc.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain 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,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. __author__ = 'Ulric Qin'
  16. from .bean import Bean
  17. from rrd.config import MAINTAINERS
  18. from .strategy import Strategy
  19. from .action import Action
  20. from rrd.model.user import User
  21. class Template(Bean):
  22. _tbl = 'tpl'
  23. _cols = 'id, tpl_name, parent_id, action_id, create_user'
  24. def __init__(self, _id, tpl_name, parent_id, action_id, create_user):
  25. self.id = _id
  26. self.tpl_name = tpl_name
  27. self.parent_id = parent_id
  28. self.action_id = action_id
  29. self.create_user = create_user
  30. self.parent = None
  31. self.action = None
  32. def to_json(self):
  33. return {
  34. 'id': self.id,
  35. 'name': self.tpl_name,
  36. 'parent_id': self.parent_id,
  37. 'action_id': self.action_id,
  38. 'create_user': self.create_user,
  39. }
  40. @classmethod
  41. def query(cls, page, limit, query, me=None):
  42. where = ''
  43. params = []
  44. if me is not None:
  45. where = 'create_user = %s'
  46. params = [me]
  47. if query:
  48. where += ' and ' if where else ''
  49. where += 'tpl_name like %s'
  50. params.append('%' + query + '%')
  51. vs = cls.select_vs(where=where, params=params, page=page, limit=limit, order='tpl_name')
  52. total = cls.total(where, params)
  53. return vs, total
  54. def writable(self, login_user):
  55. #login_user can be str or User obj
  56. if isinstance(login_user, str):
  57. login_user = User.get_by_name(login_user)
  58. if not login_user:
  59. return False
  60. if login_user.is_admin() or login_user.is_root():
  61. return True
  62. if self.create_user == login_user.name:
  63. return True
  64. if login_user.name in MAINTAINERS:
  65. return True
  66. a = self.action
  67. if not a:
  68. return False
  69. if not a.uic:
  70. return False
  71. return login_user.in_teams(a.uic)
  72. def fork(self, login_user):
  73. new_name = 'copy_of_' + self.tpl_name
  74. if self.__class__.read('tpl_name=%s', [new_name]):
  75. return -1
  76. # fork action
  77. action_id = self.action_id
  78. if action_id:
  79. action = Action.get(action_id)
  80. if action:
  81. action_id = Action.insert(
  82. {
  83. 'uic': action.uic,
  84. 'url': action.url,
  85. 'callback': action.callback,
  86. 'before_callback_sms': action.before_callback_sms,
  87. 'before_callback_mail': action.before_callback_mail,
  88. 'after_callback_sms': action.after_callback_sms,
  89. 'after_callback_mail': action.after_callback_mail,
  90. }
  91. )
  92. # fork tpl
  93. tpl_id = self.__class__.insert({
  94. 'tpl_name': new_name,
  95. 'parent_id': self.parent_id,
  96. 'action_id': action_id,
  97. 'create_user': login_user,
  98. })
  99. # fork strategy
  100. ss = Strategy.select_vs(where='tpl_id = %s', params=[self.id])
  101. for s in ss:
  102. Strategy.insert({
  103. 'metric': s.metric,
  104. 'tags': s.tags,
  105. 'max_step': s.max_step,
  106. 'priority': s.priority,
  107. 'func': s.func,
  108. 'op': s.op,
  109. 'right_value': s.right_value,
  110. 'note': s.note,
  111. 'run_begin': s.run_begin,
  112. 'run_end': s.run_end,
  113. 'tpl_id': tpl_id,
  114. })
  115. return tpl_id