strategy.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 rrd import app
  17. from flask import request, jsonify
  18. from rrd.model.portal.strategy import Strategy
  19. @app.route('/portal/strategy/update', methods=['POST'])
  20. def strategy_update_post():
  21. sid = request.form['sid'].strip()
  22. metric = request.form['metric'].strip()
  23. tags = request.form['tags'].strip()
  24. max_step = request.form['max_step'].strip()
  25. priority = request.form['priority'].strip()
  26. note = request.form['note'].strip()
  27. func = request.form['func'].strip()
  28. op = request.form['op'].strip()
  29. right_value = request.form['right_value'].strip()
  30. run_begin = request.form['run_begin'].strip()
  31. run_end = request.form['run_end'].strip()
  32. tpl_id = request.form['tpl_id'].strip()
  33. if not metric:
  34. return jsonify(msg='metric is blank')
  35. if metric == 'net.port.listen' and '=' not in tags:
  36. return jsonify(msg='if metric is net.port.listen, tags should like port=22')
  37. if sid:
  38. # update
  39. Strategy.update_dict(
  40. {
  41. 'metric': metric,
  42. 'tags': tags,
  43. 'max_step': max_step,
  44. 'priority': priority,
  45. 'func': func,
  46. 'op': op,
  47. 'right_value': right_value,
  48. 'note': note,
  49. 'run_begin': run_begin,
  50. 'run_end': run_end
  51. },
  52. 'id=%s',
  53. [sid]
  54. )
  55. return jsonify(msg='')
  56. # insert
  57. Strategy.insert(
  58. {
  59. 'metric': metric,
  60. 'tags': tags,
  61. 'max_step': max_step,
  62. 'priority': priority,
  63. 'func': func,
  64. 'op': op,
  65. 'right_value': right_value,
  66. 'note': note,
  67. 'run_begin': run_begin,
  68. 'run_end': run_end,
  69. 'tpl_id': tpl_id
  70. }
  71. )
  72. return jsonify(msg='')
  73. @app.route('/portal/strategy/<sid>')
  74. def strategy_get(sid):
  75. sid = int(sid)
  76. s = Strategy.get(sid)
  77. if not s:
  78. return jsonify(msg='no such strategy')
  79. return jsonify(msg='', data=s.to_json())
  80. @app.route('/portal/strategy/delete/<sid>')
  81. def strategy_delete_get(sid):
  82. sid = int(sid)
  83. s = Strategy.get(sid)
  84. if not s:
  85. return jsonify(msg='no such strategy')
  86. Strategy.delete_one(sid)
  87. return jsonify(msg='')