nodata.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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__ = 'niean'
  16. from rrd import app
  17. from flask import request, g, render_template, jsonify
  18. from rrd.model.portal.nodata import Nodata
  19. from rrd.utils.params import required_chk
  20. @app.route('/portal/nodata')
  21. def nodatas_get():
  22. page = int(request.args.get('p', 1))
  23. limit = int(request.args.get('limit', 5))
  24. query = request.args.get('q', '').strip()
  25. mine = request.args.get('mine', '1')
  26. me = g.user.name if mine == '1' else None
  27. vs, total = Nodata.query(page, limit, query, me)
  28. return render_template(
  29. 'portal/nodata/list.html',
  30. data={
  31. 'vs': vs,
  32. 'total': total,
  33. 'query': query,
  34. 'limit': limit,
  35. 'page': page,
  36. 'mine': mine,
  37. }
  38. )
  39. @app.route('/portal/nodata/add')
  40. def nodata_add_get():
  41. o = Nodata.get(int(request.args.get('id', '0').strip()))
  42. return render_template('portal/nodata/add.html', data={'nodata': o})
  43. @app.route('/portal/nodata/update', methods=['POST'])
  44. def nodata_update_post():
  45. nodata_id = request.form['nodata_id'].strip()
  46. name = request.form['name'].strip()
  47. obj = request.form['obj'].strip()
  48. obj_type = request.form['obj_type'].strip()
  49. metric = request.form['metric'].strip()
  50. tags = request.form['tags'].strip()
  51. dstype = request.form['dstype'].strip()
  52. step = request.form['step'].strip()
  53. mock = request.form['mock'].strip()
  54. msg = required_chk({
  55. 'name' : name,
  56. 'endpoint' : obj,
  57. 'endpoint_type' : obj_type,
  58. 'metric' : metric,
  59. 'type' : dstype,
  60. 'step' : step,
  61. 'mock_value': mock,
  62. })
  63. if msg:
  64. return jsonify(msg=msg)
  65. return jsonify(msg=Nodata.save_or_update(
  66. nodata_id,
  67. name,
  68. obj,
  69. obj_type,
  70. metric,
  71. tags,
  72. dstype,
  73. step,
  74. mock,
  75. g.user.name,
  76. ))
  77. @app.route('/portal/nodata/delete/<nodata_id>')
  78. def nodata_delete_get(nodata_id):
  79. nodata_id = int(nodata_id)
  80. Nodata.delete_one(nodata_id)
  81. return jsonify(msg='')