cluster.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 flask import jsonify, render_template, request, g
  17. from flask_babel import gettext
  18. from rrd import app
  19. from rrd.model.portal.host_group import HostGroup
  20. from rrd.model.portal.cluster import Cluster
  21. from rrd.utils.params import required_chk
  22. @app.route('/portal/group/<group_id>/cluster')
  23. def cluster_list_get(group_id):
  24. group_id = int(group_id)
  25. group = HostGroup.read(where='id = %s', params=[group_id])
  26. if not group:
  27. return jsonify(msg='no such group %s' % group_id)
  28. clusters = Cluster.select_vs(where='grp_id = %s', params=[group_id])
  29. return render_template('portal/cluster/list.html', **locals())
  30. @app.route('/portal/group/<group_id>/cluster/creator', methods=['GET'])
  31. def cluster_creator_get(group_id):
  32. group_id = int(group_id)
  33. group = HostGroup.read(where='id = %s', params=[group_id])
  34. if not group:
  35. return jsonify(msg='no such group %s' % group_id)
  36. return render_template('portal/cluster/creator.html', **locals())
  37. @app.route('/portal/group/<group_id>/cluster/creator', methods=['POST'])
  38. def cluster_node_post(group_id):
  39. group_id = int(group_id)
  40. group = HostGroup.read(where='id = %s', params=[group_id])
  41. if not group:
  42. return jsonify(msg='no such group %s' % group_id)
  43. numerator = request.form['numerator'].strip()
  44. denominator = request.form['denominator'].strip()
  45. endpoint = request.form['endpoint'].strip()
  46. metric = request.form['metric'].strip()
  47. tags = request.form['tags'].strip()
  48. ds_type = 'GAUGE'
  49. step = request.form['step'].strip()
  50. msg = required_chk({
  51. 'numerator': numerator,
  52. 'denominator': denominator,
  53. 'endpoint': endpoint,
  54. 'metric': metric,
  55. 'ds_type': ds_type,
  56. 'step': step,
  57. })
  58. if msg:
  59. return jsonify(msg=msg)
  60. if Cluster.exists('endpoint=%s and metric=%s and tags=%s', [endpoint, metric, tags]):
  61. return jsonify(msg='%s/%s/%s is already existent' % (endpoint, metric, tags))
  62. last_id = Cluster.insert({
  63. 'grp_id': group_id,
  64. 'numerator': numerator,
  65. 'denominator': denominator,
  66. 'endpoint': endpoint,
  67. 'metric': metric,
  68. 'tags': tags,
  69. 'ds_type': ds_type,
  70. 'step': step,
  71. 'creator': g.user.name,
  72. })
  73. if last_id > 0:
  74. return jsonify(msg='')
  75. else:
  76. return jsonify(msg='occur error')
  77. @app.route('/portal/cluster/edit/<cluster_id>', methods=['GET'])
  78. def cluster_edit_get(cluster_id):
  79. cluster_id = int(cluster_id)
  80. cluster = Cluster.get(cluster_id)
  81. op = gettext('edit')
  82. return render_template('portal/cluster/edit.html', **locals())
  83. @app.route('/portal/cluster/clone/<cluster_id>', methods=['GET'])
  84. def cluster_clone_get(cluster_id):
  85. cluster_id = int(cluster_id)
  86. cluster = Cluster.get(cluster_id)
  87. # for clone
  88. cluster_id = 0
  89. op = gettext('clone')
  90. return render_template('portal/cluster/edit.html', **locals())
  91. @app.route('/portal/cluster/delete/<cluster_id>', methods=['POST'])
  92. def cluster_delete_post(cluster_id):
  93. cluster_id = int(cluster_id)
  94. Cluster.delete_one(cluster_id)
  95. return jsonify(msg='')
  96. @app.route('/portal/cluster/edit/<cluster_id>', methods=['POST'])
  97. def cluster_edit_post(cluster_id):
  98. cluster_id = int(cluster_id)
  99. numerator = request.form['numerator'].strip()
  100. denominator = request.form['denominator'].strip()
  101. endpoint = request.form['endpoint'].strip()
  102. metric = request.form['metric'].strip()
  103. tags = request.form['tags'].strip()
  104. ds_type = 'GAUGE'
  105. step = request.form['step'].strip()
  106. grp_id = request.form['grp_id'].strip()
  107. if cluster_id:
  108. # edit
  109. if Cluster.exists('endpoint=%s and metric=%s and tags=%s and id<>%s', [endpoint, metric, tags, cluster_id]):
  110. return jsonify(msg='%s/%s/%s has already existent' % (endpoint, metric, tags))
  111. Cluster.update_dict({
  112. 'numerator': numerator,
  113. 'denominator': denominator,
  114. 'endpoint': endpoint,
  115. 'metric': metric,
  116. 'tags': tags,
  117. 'ds_type': ds_type,
  118. 'step': step,
  119. }, 'id=%s', [cluster_id])
  120. else:
  121. # clone
  122. last_id = Cluster.insert({
  123. 'numerator': numerator,
  124. 'denominator': denominator,
  125. 'endpoint': endpoint,
  126. 'metric': metric,
  127. 'tags': tags,
  128. 'ds_type': ds_type,
  129. 'step': step,
  130. 'creator': g.user.name,
  131. 'grp_id': grp_id,
  132. })
  133. if last_id <= 0:
  134. return jsonify(msg='occur error')
  135. return jsonify(msg='')