redis-console.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>redis控制台</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  6. <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
  7. <!-- 所有的 css & js 资源 -->
  8. <link rel="stylesheet" href="https://unpkg.com/element-ui@2.13.0/lib/theme-chalk/index.css">
  9. <link rel="stylesheet" href="../../static/sa.css">
  10. <script src="../../static/kj/vue.min.js"></script>
  11. <script src="../../static/kj/element-ui/index.js"></script>
  12. <script src="../../static/kj/httpVueLoader.js"></script>
  13. <script src="../../static/kj/jquery.min.js"></script>
  14. <script src="../../static/kj/layer/layer.js"></script>
  15. <script src="../../static/sa.js"></script>
  16. <style type="text/css">
  17. .card-box {
  18. min-width: 230px;
  19. margin-right: 10px;
  20. margin-bottom: 10px;
  21. display: inline-block;
  22. background-color: #f5f5f5;
  23. cursor: pointer;
  24. transition: all 0.3s;
  25. }
  26. .card-box:hover{box-shadow: 0 0 20px #999;}
  27. .card-box .prop-name {
  28. padding-left: 14px;
  29. padding-top: 14px;
  30. color: #666;
  31. }
  32. .card-box .prop-value {
  33. /* border: 1px #000 solid; */
  34. padding-left: 14px;
  35. height: 40px;
  36. line-height: 40px;
  37. padding-top: 10px;
  38. padding-bottom: 10px;
  39. font-size: 26px;
  40. color: green;
  41. }
  42. .f5-pre-btn:hover{cursor: pointer; text-decoration: underline;}
  43. .key-div{color: green; cursor: pointer;font-weight: bold;}
  44. .not-show,.is-show{padding: 5px 10px; background-color: #eee; cursor: pointer; }
  45. .is-show{background-color: rgba(0,0,0,0);}
  46. /* .k-input input{font-weight: bold;} */
  47. </style>
  48. </head>
  49. <body>
  50. <div class="vue-box" style="display: none;" :style="'display: block;'">
  51. <!-- ------------- 总览 ------------- -->
  52. <div class="c-panel">
  53. <div class="c-title">
  54. Redis 控制台
  55. <span class="f5-pre-btn" @click="f5_pre(false)">刷新</span>
  56. </div>
  57. <div style="height: 10px;"></div>
  58. <div>
  59. <div class="card-box" @click="sa.msg('表点我 >_<')">
  60. <p class="prop-name">键值总数</p>
  61. <p class="prop-value">{{preData.keys_count}}</p>
  62. </div>
  63. <div class="card-box" @click="sa.msg('表点我 >_<')">
  64. <p class="prop-name">命中次数</p>
  65. <p class="prop-value">{{preData.keyspace_hits}}</p>
  66. </div>
  67. <div class="card-box" @click="sa.msg('表点我 >_<')">
  68. <p class="prop-name">已用内存</p>
  69. <p class="prop-value">{{preData.used_memory_human}}</p>
  70. </div>
  71. <div class="card-box" @click="sa.msg('表点我 >_<')">
  72. <p class="prop-name">内存峰值</p>
  73. <p class="prop-value">{{preData.used_memory_peak_human}}</p>
  74. </div>
  75. <div class="card-box" @click="sa.msg('表点我 >_<')">
  76. <p class="prop-name">启动时间</p>
  77. <p class="prop-value">{{preData.uptime_in_seconds_str}}</p>
  78. </div>
  79. </div>
  80. </div>
  81. <!-- ------------- 检索参数 ------------- -->
  82. <div class="c-panel c-table">
  83. <div class="c-title">搜索键值</div>
  84. <div class="c-item">
  85. <!-- <label class="c-label">搜索键值:</label> -->
  86. <el-input v-model="p.k" class="k-input" :placeholder="isLike ? '当前为模糊搜索' : '当前为精确搜索'" @keyup.native.enter="f5()" style="width: 400px;"></el-input>
  87. </div>
  88. <div class="c-item" style="min-width: 0px;">
  89. <el-button type="primary" icon="el-icon-search" @click="f5()">查询</el-button>
  90. <el-button type="success" icon="el-icon-plus" @click="add()">添加</el-button>
  91. <el-button type="danger" icon="el-icon-delete" @click="deleteByKeys()">删除</el-button>
  92. <el-button type="info" icon="el-icon-sort" @click="isLike = !isLike; sa.ok('切换成功')">
  93. {{isLike ? '切换为精确搜索' : '切换为模糊搜索'}}
  94. </el-button>
  95. </div>
  96. <div style="height: 10px;"></div>
  97. <el-table class="data-table" ref="data-table" :data="dataListShow" size="small">
  98. <!-- <el-table-column label="键"></el-table-column> -->
  99. <el-table-column type="selection" width="45px"></el-table-column>
  100. <el-table-column label="键">
  101. <template slot-scope="s">
  102. <div class="key-div" @click="sa.copyText(s.row.key); sa.msg('复制成功')">{{s.row.key}}</div>
  103. </template>
  104. </el-table-column>
  105. <el-table-column label="值">
  106. <template slot-scope="s">
  107. <div class="not-show" @click="get(s.row)" v-if="s.row.is_show == false">点击加载</div>
  108. <div class="is-show" v-if="s.row.is_show == true" @click="sa.copyText(s.row.value); sa.msg('复制成功')">{{s.row.value}}</div>
  109. </template>
  110. </el-table-column>
  111. <el-table-column label="TTL (秒)" prop="ttl" width="150px"></el-table-column>
  112. <el-table-column label="操作" width="250px">
  113. <template slot-scope="s">
  114. <el-button type="text" @click="get(s.row)">查询</el-button>
  115. <el-button type="text" @click="updateValue(s.row)">修改值</el-button>
  116. <el-button type="text" @click="updateTTL(s.row)">修改TTL</el-button>
  117. <el-button type="text" @click="del(s.row)">删除</el-button>
  118. </template>
  119. </el-table-column>
  120. </el-table>
  121. <div class="page-box">
  122. <el-pagination background
  123. layout="total, prev, pager, next, sizes, jumper"
  124. :current-page.sync="p.pageNo"
  125. :page-size.sync="p.pageSize"
  126. :total="dataCount"
  127. :page-sizes="[1, 10, 20, 50, 100, 1000]"
  128. @current-change="f5ByPage()"
  129. @size-change="f5ByPage()">
  130. </el-pagination>
  131. </div>
  132. </div>
  133. </div>
  134. <script>
  135. var app = new Vue({
  136. el: '.vue-box',
  137. data: {
  138. sa: sa, // 超级对象
  139. p: { // 查询参数
  140. k: '',
  141. pageNo: 1, // 当前页
  142. pageSize: 10, // 页大小
  143. sortType: 0 // 排序方式
  144. },
  145. isLike: true, // 是否为模糊匹配
  146. dataCount: 0,
  147. preData: {
  148. keys_count: 0, // key 总数
  149. keyspace_hits: 0, // 被命中次数
  150. used_memory_human: 0, // 已经占用内存数量
  151. used_memory_peak_human: 0, // 内存消耗峰值
  152. uptime_in_seconds: 0, // redis 已经启动的秒数
  153. uptime_in_seconds_str: '0', // redis 已经启动的时间
  154. },
  155. dataList: [],
  156. dataListShow: [],
  157. },
  158. methods: {
  159. // 根据分页信息显示出来
  160. f5ByPage: function() {
  161. var dataListShow = [];
  162. var start = (this.p.pageNo - 1) * this.p.pageSize;
  163. var end = this.p.pageNo * this.p.pageSize;
  164. for (var i = start; i < end; i++) {
  165. if(i >= this.dataList.length) {
  166. break;
  167. }
  168. dataListShow.push(this.dataList[i]);
  169. }
  170. this.dataListShow = dataListShow;
  171. sa.f5TableHeight(); // 刷新表格高度
  172. },
  173. // 查询key列表
  174. f5: function() {
  175. let k = this.p.k;
  176. if(this.isLike && k != '') {
  177. k = '*' + k + '*';
  178. }
  179. sa.ajax('/sp-admin/RedisConsole/getKeys', {k: k}, function(res) {
  180. var dataList = [];
  181. for (var i = 0; i < res.data.length; i++) {
  182. dataList.push({
  183. key: res.data[i], // key
  184. value: '', // value
  185. is_show: false, // 是否已经显示详情
  186. ttl: '未加载', // 过期时间
  187. })
  188. }
  189. this.dataList = dataList;
  190. this.f5ByPage();
  191. this.dataCount = this.dataList.length;
  192. }.bind(this), {
  193. success501: function(res) {
  194. sa.msg(res.msg);
  195. this.dataList = [];
  196. this.f5ByPage();
  197. this.dataCount = 0;
  198. }.bind(this)
  199. });
  200. },
  201. // 刷新预览
  202. f5_pre: function(is_f5_keys) {
  203. // 基本预览信息
  204. sa.ajax('/sp-admin/RedisConsole/getPreInfo', this.p, function(res) {
  205. res.data.uptime_in_seconds_str = getDuration(parseInt(res.data.uptime_in_seconds) * 1000);
  206. this.preData = res.data;
  207. // 如果指定不查询keys列表
  208. if(is_f5_keys === false) {
  209. return;
  210. }
  211. // 如果超过了最大值,则提示一下
  212. if(res.data.isGtMax) {
  213. var tipStr = 'key值数量已达' + this.preData.keys_count + ',为了避免卡顿已取消返回结果列表(您可以增加筛选条件缩短记录总数)';
  214. tipStr = '<b style="color: red;">' + tipStr + '</b>';
  215. sa.alert(tipStr);
  216. } else {
  217. this.f5();
  218. }
  219. }.bind(this));
  220. },
  221. // 加载详情
  222. get: function(data) {
  223. sa.ajax('/sp-admin/RedisConsole/getByKey?key=' + data.key, function(res) {
  224. data.value = res.data.value;
  225. data.ttl = res.data.ttl;
  226. data.is_show = true;
  227. sa.f5TableHeight(); // 刷新表格高度
  228. }.bind(this));
  229. },
  230. // 删除
  231. del: function(data) {
  232. sa.confirm('是否删除,此操作不可撤销', function() {
  233. sa.ajax('/sp-admin/RedisConsole/del?key=' + data.key, function(res) {
  234. sa.arrayDelete(app.dataListShow, data);
  235. sa.ok('删除成功');
  236. sa.f5TableHeight(); // 刷新表格高度
  237. })
  238. });
  239. },
  240. // 修改键值
  241. updateValue: function(data) {
  242. layer.prompt({
  243. title: '修改键值',
  244. // shadeClose: true, // 点击遮罩关闭
  245. formType: 2, // 多行输入
  246. maxlength: 9999999999, // 最大输入字符长度
  247. area: ['600px', '400px'], // 弹窗尺寸
  248. }, function(pass, index, elem){
  249. layer.close(index); //如果设定了yes回调,需进行手工关闭
  250. sa.ajax('/sp-admin/RedisConsole/updateValue', {key: data.key, value: pass}, function(res){
  251. data.value = pass;
  252. layer.msg('修改成功');
  253. sa.f5TableHeight(); // 刷新表格高度
  254. })
  255. });
  256. },
  257. // 修改ttl
  258. updateTTL: function(data) {
  259. sa.prompt('修改TTL', function(pass, index){
  260. if(isNaN(pass)) {
  261. return sa.error('请输入一个数值');
  262. }
  263. sa.ajax('/sp-admin/RedisConsole/updateTtl', {key: data.key, ttl: pass}, function(res){
  264. data.ttl = pass;
  265. sa.ok('修改成功');
  266. })
  267. });
  268. },
  269. // 添加
  270. add: function() {
  271. sa.showIframe('添加键值', 'redis-key-add.html', '800px', '510px');
  272. },
  273. // 根据id列表删除
  274. deleteByKeys: function() {
  275. // 获取选中元素的id列表
  276. let selection = this.$refs['data-table'].selection;
  277. let keys = sa.getArrayField(selection, 'key');
  278. // 判断
  279. if(keys.length < 1) {
  280. return sa.error('请至少选择一行');
  281. }
  282. // 删除
  283. sa.confirm('是否删除选中记录,此操作不可撤销', function() {
  284. sa.ajax('/sp-admin/RedisConsole/deleteByKeys', {key: keys}, function(res) {
  285. sa.arrayDelete(this.dataListShow, selection);
  286. sa.ok2('删除成功');
  287. sa.f5TableHeight(); // 刷新表格高度
  288. }.bind(this))
  289. }.bind(this));
  290. },
  291. // 刷新秒数
  292. auto_f5_run_time: function() {
  293. setInterval(function() {
  294. if(this.preData.uptime_in_seconds <= 0 || this.preData.uptime_in_seconds > 60 * 60 * 24) {
  295. return;
  296. }
  297. this.preData.uptime_in_seconds++;
  298. this.preData.uptime_in_seconds_str = getDuration(parseInt(this.preData.uptime_in_seconds) * 1000);
  299. }.bind(this), 1000);
  300. }
  301. },
  302. created: function() {
  303. this.f5_pre();
  304. this.auto_f5_run_time();
  305. }
  306. })
  307. function getDuration(my_time) {
  308. var days = my_time / 1000 / 60 / 60 / 24;
  309. var daysRound = Math.floor(days);
  310. var hours = my_time / 1000 / 60 / 60 - (24 * daysRound);
  311. var hoursRound = Math.floor(hours);
  312. var minutes = my_time / 1000 / 60 - (24 * 60 * daysRound) - (60 * hoursRound);
  313. var minutesRound = Math.floor(minutes);
  314. var seconds = my_time / 1000 - (24 * 60 * 60 * daysRound) - (60 * 60 * hoursRound) - (60 * minutesRound);
  315. seconds = parseInt(seconds);
  316. // console.log('转换时间:', daysRound + '天', hoursRound + '时', minutesRound + '分', seconds + '秒');
  317. // var time = hoursRound + ':' + minutesRound + ':' + seconds
  318. // return time;
  319. if(daysRound >= 1) {
  320. return daysRound + '天' + hoursRound + '小时';
  321. } else if(hoursRound >= 1) {
  322. return hoursRound + '小时' + hoursRound + '分';
  323. } else if(minutesRound >= 1) {
  324. return minutesRound + '分' + seconds + '秒';
  325. } else {
  326. return seconds + '秒';
  327. }
  328. }
  329. // console.log(getDuration(200000));;
  330. </script>
  331. </body>
  332. </html>