1
0

nodata.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 rrd.model.user import User
  19. import time
  20. class Nodata(Bean):
  21. _tbl = 'mockcfg'
  22. _cols = 'id, name, obj, obj_type, metric, tags, dstype, step, mock, creator, t_create, t_modify'
  23. _max_obj_items = 5
  24. _max_obj_len = 1024
  25. def __init__(self, _id, name, obj, obj_type, metric, tags, dstype, step, mock, creator,
  26. t_create, t_modify):
  27. self.id = _id
  28. self.name = name
  29. self.obj = obj
  30. self.obj_type = obj_type
  31. self.metric = metric
  32. self.tags = tags
  33. self.dstype = dstype
  34. self.step = step
  35. self.mock = mock
  36. self.creator = creator
  37. self.t_create = t_create
  38. self.t_modify = t_modify
  39. @classmethod
  40. def query(cls, page, limit, query, me=None):
  41. where = ''
  42. params = []
  43. if me is not None:
  44. where = 'creator = %s'
  45. params.append(me)
  46. if query:
  47. where += ' and ' if where else ''
  48. where += ' name like %s'
  49. params.append('%' + query + '%')
  50. vs = cls.select_vs(where=where, params=params, page=page, limit=limit)
  51. total = cls.total(where=where, params=params)
  52. return vs, total
  53. @classmethod
  54. def save_or_update(cls, nodata_id, name, obj, obj_type, metric, tags, dstype, step, mock, login_user):
  55. if len(obj) > cls._max_obj_len:
  56. return 'endpoint too long'
  57. obj_len = len(obj.strip().split('\n'))
  58. if obj_len > cls._max_obj_items:
  59. return 'endpoint too many items'
  60. if nodata_id:
  61. return cls.update_nodata(nodata_id, name, obj, obj_type, metric, tags, dstype, step, mock)
  62. else:
  63. return cls.insert_nodata(name, obj, obj_type, metric, tags, dstype, step, mock, login_user)
  64. @classmethod
  65. def insert_nodata(cls, name, obj, obj_type, metric, tags, dstype, step, mock, login_user):
  66. nodata_id = Nodata.insert({
  67. 'name' : name,
  68. 'obj' : obj,
  69. 'obj_type' : obj_type,
  70. 'metric' : metric,
  71. 'tags' : tags,
  72. 'dstype' : dstype,
  73. 'step' : step,
  74. 'mock': mock,
  75. 'creator': login_user,
  76. 't_create': time.strftime('%Y-%m-%d %H:%M:%S')
  77. })
  78. if nodata_id:
  79. return ''
  80. return 'save nodata fail'
  81. @classmethod
  82. def update_nodata(cls, nodata_id, name, obj, obj_type, metric, tags, dstype, step, mock):
  83. e = Nodata.get(nodata_id)
  84. if not e:
  85. return 'no such nodata config %s' % nodata_id
  86. Nodata.update_dict(
  87. {
  88. 'name' : name,
  89. 'obj' : obj,
  90. 'obj_type' : obj_type,
  91. 'metric' : metric,
  92. 'tags' : tags,
  93. 'dstype' : dstype,
  94. 'step' : step,
  95. 'mock': mock,
  96. },
  97. 'id=%s',
  98. [e.id]
  99. )
  100. return ''
  101. def writable(self, login_user):
  102. #login_user can be str or User obj
  103. if isinstance(login_user, str):
  104. login_user = User.get_by_name(login_user)
  105. if not login_user:
  106. return False
  107. if login_user.is_admin() or login_user.is_root():
  108. return True
  109. if self.creator == login_user.name:
  110. return True
  111. if login_user.name in MAINTAINERS:
  112. return True
  113. return False