redis-live.js 16.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
import $ from 'jquery';

import echarts from 'echarts/lib/echarts';
import dataZoom from 'echarts/lib/component/dataZoom';
import bar from 'echarts/lib/chart/bar';
import line from 'echarts/lib/chart/line';

import Page from './common/page';

import srvData from '../data/srv';
import mcData from '../data/mc';
import cpData from '../data/cp';

import topSelectHbs from 'redis/top-select.hbs';
import topSrvHbs from 'redis/top-srv.hbs';
import srvGraphSelectHbs from 'redis/srv-graph-select.hbs';
import rosTbody from 'redis/ros-tbody.hbs';
import tcTBodyHbs from 'redis/tc-tbody.hbs';
import tmTbodyHbs from 'redis/tm-tbody.hbs';

class RedisLive extends Page {
    constructor() {
        super();

        /* 图表实例 */
        this.srv;
        this.cp; // 命令总数
        this.mc; // 全集群读写并发
        this.tc; // Top CMD
        this.tm; // Top Key
     

        /* 数据存储 */
        this.cloud; // 云
        this.redisCluster; // 从服务器

        this.selector = {
            srvList: $('#srvList'),
            srv: $('#srv')[0],
            mc: $('#mc')[0],
            cp: $('#cp')[0],
            tc: $('#tc')[0],
            tm: $('#tm')[0],
            cloudSelect: $('#cloudSelect'),
            clusterSelect: $('#clusterSelect'),
            srvGraphSelect: $('#srvGraphSelect'),
            srvTimeSelect: $('#srvTimeSelect'),
            cpSelect: $('#cpSelect'),
            mcSelect: $('#mcSelect'),
            tcSelect: $('#tcSelect'),
            tmSelect: $('#tmSelect'),
            rosSelect: $('#rosSelect'),
            rosFilterBtn: $('#rosFilterBtn'),
            keyTemplateInput: $('#keyTemplateInput'),
            contextInput: $('#contextInput'),
            tcTbody: $('#tcTbody'),
            tmTbody: $('#tmTbody'),
            rosTbody: $('#rosTbody'),
            pageSlipt: $('#pageSlipt'),
            totalPage: $('#totalPage')
        };

        this.hasRedisCluster = false;
        this.srvLastTime = 5;
        this.cpLastTime;
        this.mcLastTime;
        this.tcLastTime = 5;
        this.tmLastTime = 5;
        this.rosSelectVal = 5;
        this.showWhichSrvGraph = {};
        this.mpMetric;
        this.srvGraphSelectVal;
        this.rosFilters = {};
        this.rosPage = {
            first: 1,
            current: 1,
            prev: 0,
            next: 2,
            last: null
        }

        this.selector.cloudSelect.on('change', this.cloudSelectChange.bind(this));
        this.selector.clusterSelect.on('change', this.clusterSelectChange.bind(this));
        this.selector.srvGraphSelect.on('change', this.srvSelectChange.bind(this));
        this.selector.srvTimeSelect.on('change', this.srvSelectChange.bind(this));
        this.selector.cpSelect.on('change', this.cpRender.bind(this));
        this.selector.mcSelect.on('change', this.mcRender.bind(this));
        this.selector.tcSelect.on('change', this.tcRender.bind(this, true));
        this.selector.tmSelect.on('change', this.tmRender.bind(this, true));
        this.selector.rosSelect.on('change', this.rosRender.bind(this, true));
        this.selector.rosFilterBtn.on('click', this.rosFilter.bind(this));
        this.selector.pageSlipt.find('button').on('click', this.rosPageChange.bind(this));

        this.init();
        // setInterval(() => {
        //     this.refreshGraph();
        // }, 1000 * 20);
    }

    async init() {
        await this.navRender();

        this.srv = echarts.init(this.selector.srv);
        this.srv.setOption(srvData());
        this.cp = echarts.init(this.selector.cp);
        this.cp.setOption(cpData());
        this.mc = echarts.init(this.selector.mc);
        this.mc.setOption(mcData());

        if (this.hasRedisCluster) {
            this.srvListRender();
            this.cpRender();
            this.mcRender();
            this.tcRender();
            this.tmRender();
            this.rosRender();
        }
    }

    /**
     * 服务器列表筛选
     */
    rosFilter() {
        let keyTemplate = this.selector.keyTemplateInput.val();
        let context = this.selector.contextInput.val();

        if (keyTemplate) {
            this.rosFilters.keyTemplate = keyTemplate;
        } else {
            delete this.rosFilters.keyTemplate;
        }

        if (context) {
            this.rosFilters.context = context;
        } else {
            delete this.rosFilters.context;
        }
        
        if (!$.isEmptyObject(this.rosFilters)) {
            this.rosRender(true);
        }
    }

    /**
     * 服务器单个指标改变
     */
    async srvSelectChange() {
        this.srvGraphSelectVal = this.selector.srvGraphSelect.val();
        this.srvLastTime = this.selector.srvTimeSelect.val();

        let mpResData =  await this.getMetricProcessed();

        this.srv.setOption({
            xAxis: {
                    data: mpResData.x
                },
            series: [
                {
                    data: mpResData.y
                }
            ]
        });
    }

    /**
     * 翻页
     */
    async rosPageChange(event) {
        let $elem = $(event.currentTarget);

        if ($elem.hasClass('pure-button-disabled')) {
            return false;
        }
       
        if ($elem.hasClass('first')) {
            this.rosPage.current = 1;
        } else if ($elem.hasClass('prev')) {
            this.rosPage.current--;
        } else if ($elem.hasClass('next')) {
            this.rosPage.current++;
        } else if ($elem.hasClass('last')) {
            this.rosPage.current = this.rosPage.last;
        }

        this.rosPage.prev = Math.max(this.rosPage.current - 1, 1);
        this.rosPage.next = Math.min(this.rosPage.current + 1, this.rosPage.last);

        if (this.rosPage.current === 1) {
            this.selector.pageSlipt.find('.prev').addClass('pure-button-disabled');
        } else {
            this.selector.pageSlipt.find('.prev').removeClass('pure-button-disabled');
        }

        if (this.rosPage.current === this.rosPage.last) {
            this.selector.pageSlipt.find('.next').addClass('pure-button-disabled');
        } else {
            this.selector.pageSlipt.find('.next').removeClass('pure-button-disabled');
        }

        this.rosRender(true);
    }

    /**
     * 导航筛选渲染
     */
    async navRender() {
        let influxCloudsData = await this.getInfluxClouds();

        if (influxCloudsData && influxCloudsData.length > 0) {
            this.cloud = influxCloudsData[0];
            $('#cloudSelect').html(topSelectHbs(influxCloudsData));

            let redisClusterData = await this.getRedisCluster();

            if (redisClusterData && redisClusterData.length > 0) {
                this.hasRedisCluster = true;
                this.redisCluster = redisClusterData[0];
                $('#clusterSelect').html(topSelectHbs(redisClusterData));
            } else {
                this.hasRedisCluster = false;
            }
        }
    }

    /**
     * 服务器列表渲染
     */
    async srvListRender() {
        let resData = await this.getLastedHealthy();

        if (!resData || resData.length < 1 || resData.code) {
            resData = [];
        }

        this.showWhichSrvGraph = resData[0] || {};

        let topSrvTables = $(topSrvHbs(resData));

        this.selector.srvList.html(topSrvTables);
        this.selector.srvc = $('.srvc');
        this.selector.srvc.on('click', (event) => {
            let $elem = $(event.currentTarget);

            this.showWhichSrvGraph = resData[$elem.data('index')] || {};
            this.srvGraphRender(true);
        });
    }

    /**
     * 服务器单个指标状态曲线图渲染
     */
    async srvGraphRender() {
        let resData = await this.getMetric();

        this.mpMetric = resData;
        
        let srvGraphSelect = srvGraphSelectHbs(resData);
        this.selector.srvGraphSelect.html(srvGraphSelect);

        if (this.srvGraphSelectVal) {
            this.selector.srvGraphSelect.val(this.srvGraphSelectVal);
        } else {
            this.srvGraphSelectVal = this.selector.srvGraphSelect.val();
        }
       
        let mpResData =  (await this.getMetricProcessed()) || {};

        this.srv.setOption({
            xAxis: {
                data: mpResData.x
            },
            series: [
                {
                    data: mpResData.y
                }
            ]
        });
    }

    /**
     * 命令总数
     */
    async cpRender() {
        let option = {};

        this.cpLastTime = this.selector.cpSelect.val();

        let resData = (await this.getCommandProcessed()) || {};

        this.cp.setOption({
            xAxis: {
                data: resData.x
            },
            series: [{
                data: resData.y
            }]
        });
    }

    /**
     * 全集群读写并发
     */
    async mcRender() {
        let option = {};

        this.mcLastTime = this.selector.mcSelect.val();

        let resData = (await this.getWriteReadProcessed()) || {};

        this.mc.setOption({
            xAxis: {
                data: resData.x
            },
            series: [
                {
                    data: resData.yr
                },
                {
                    data: resData.yw
                }
            ]
        });
    }

    /**
     * Top CMD
     */
    async tcRender(refresh) {
        if (refresh) {
            this.tcLastTime = this.selector.tcSelect.val();
        }

        let resData = await this.getTopCommandTotal();
        let tcTbody = tcTBodyHbs(resData);

        this.selector.tcTbody.html(tcTbody);
    }

    /**
     * Top 命中率低
     */
    async tmRender(refresh) {
        if (refresh) {
            this.tmLastTime = this.selector.tmSelect.val();
        }

        let resData = await this.getTopMissRate();
        let tmTbody = tmTbodyHbs(resData);

        this.selector.tmTbody.html(tmTbody);
    }

    /**
     * 服务器数据表渲染
     */
    async rosRender(refresh) {
        if (refresh) {
            this.rosSelectVal = this.selector.rosSelect.val();
        }

        let resData = await this.getRedisOpsStatistics();

        this.rosPage.last = (resData && resData.pageSize) || 1;

        if (this.rosPage.last === 1 || this.rosPage.last === resData.currPage) {
            this.selector.pageSlipt.find('.next').addClass('pure-button-disabled');
            this.selector.pageSlipt.find('.last').addClass('pure-button-disabled');
        } else {
            this.selector.pageSlipt.find('.next').removeClass('pure-button-disabled');
            this.selector.pageSlipt.find('.last').removeClass('pure-button-disabled');
        }

        let tbody = rosTbody(resData.list);

        this.selector.totalPage.text((resData && resData.pageSize) || 0);
        this.selector.rosTbody.html(tbody);
    }

    /**
     * 选择云
     */
    async cloudSelectChange() {
        this.cloud = this.selector.cloudSelect.val();

        let redisClusterData = await this.getRedisCluster();

        if (redisClusterData && redisClusterData.length > 0) {
            this.hasRedisCluster = true;
        } else {
            this.hasRedisCluster = false;
        }

        this.redisCluster = redisClusterData[0];
        $('#clusterSelect').html(topSelectHbs(redisClusterData));

        this.refreshGraph();
    }

    /**
     * 选择从服务器
     */
    clusterSelectChange() {
        this.redisCluster = this.selector.clusterSelect.val();
        this.refreshGraph();
    }

    /**
     * 获取云
     */
    getInfluxClouds() {
        return this.ajax({
            url: '/redismonitor/getInfluxClouds'
        });
    }

    /**
     * 获取从
     */
    getRedisCluster() {
        let resResut = Promise.resolve({});

        if (this.cloud) {
            resResut = this.ajax({
                url: '/redismonitor/getRedisCluster',
                data: {
                    cloud: this.cloud
                }
            });
        }

        return resResut;
    }

    /**
     * 获取服务器列表
     */
    getLastedHealthy() {
        let resResut = Promise.resolve({});

        if (this.cloud && this.redisCluster) {
            resResut =  this.ajax({
                url: '/redismonitor/getLastedHealthy',
                data: {
                    cloud: this.cloud,
                    redisCluster: this.redisCluster && this.redisCluster.split(':')[0] || ''
                }
            });
        }

        return resResut;
    }

    /**
     * 获取服务器指标筛选项
     */
    getMetric() {
        let resResut = Promise.resolve({});

        if (this.cloud && this.redisCluster) {
            resResut =  this.ajax({
                url: '/redismonitor/getMetric',
                data: {
                    cloud: this.cloud,
                    redisCluster: (this.showWhichSrvGraph && `${this.showWhichSrvGraph.ip}:${this.showWhichSrvGraph.port}`)
                        || this.redisCluster
                }
            });
        }

        return resResut;
    }

    /**
     * 获取服务器单个指标图表数据
     */
    getMetricProcessed() {
        let resResut = Promise.resolve({});

        if (this.showWhichSrvGraph.ip && this.showWhichSrvGraph.port && this.srvGraphSelectVal) {
            resResut = this.ajax({
                url: '/redismonitor/getMetricProcessed',
                data: {
                    ip: this.showWhichSrvGraph.ip,
                    port: this.showWhichSrvGraph.port,
                    metric: this.srvGraphSelectVal,
                    lastTime: this.srvLastTime
                }
            });
        }

        return resResut;
    }

    /**
     * 获取命令总数
     */
    getCommandProcessed() {
        let resResut = Promise.resolve({});

        if (this.cloud && this.redisCluster) {
            resResut = this.ajax({
                url: '/redismonitor/getCommandProcessed',
                data: {
                    cloud: this.cloud,
                    redisCluster: this.redisCluster,
                    lastTime: this.cpLastTime
                }
            });
        }

        return resResut;
    }

    /**
     * 获取全集群读写并发
     */
    getWriteReadProcessed() {
        let resResut = Promise.resolve({});

        if (this.cloud && this.redisCluster) {
            resResut = this.ajax({
                url: '/redismonitor/getWriteReadProcessed',
                data: {
                    cloud: this.cloud,
                    redisCluster: this.redisCluster,
                    lastTime: this.mcLastTime
                }
            });
        }

        return resResut;
    }

    /**
     * 获取 Top CMD
     */
    getTopCommandTotal() {
        let resResut = Promise.resolve({});

        if (this.cloud && this.redisCluster) {
            resResut = this.ajax({
                url: '/redismonitor/getTopCommand',
                data: {
                    cloud: this.cloud,
                    redisCluster: this.redisCluster,
                    lastTime: this.tcLastTime
                }
            });
        }

        return resResut;
    }

    /**
     * 获取 Top 命中率低
     */
    getTopMissRate() {
        let resResut = Promise.resolve({});

        if (this.cloud && this.redisCluster) {
            resResut = this.ajax({
                url: '/redismonitor/getTopMissRate',
                data: {
                    cloud: this.cloud,
                    redisCluster: this.redisCluster,
                    lastTime: this.tmLastTime
                }
            });
        }

        return resResut;
    }

    /**
     * 获取服务器数据表
     */
    getRedisOpsStatistics() {
        let resResut = Promise.resolve({});
        let resParams = {
            page: this.rosPage.current,
            cloud: this.cloud,
            redisCluster: this.redisCluster,
            lastTime: this.rosSelectVal
        };

        if (!$.isEmptyObject(this.rosFilters)) {
            $.extend(resParams, this.rosFilters);
        }

        if (this.cloud && this.redisCluster) {
            resResut = this.ajax({
                url: '/redismonitor/getRedisOpsStatistics',
                data: resParams
            });
        }

        return resResut;
    }

    /**
     * 集中刷新数据
     */
    async refreshGraph() {
        await this.srvListRender();
        await this.srvGraphRender();
        this.cpRender();
        this.mcRender();
        this.tcRender(true);
        this.tmRender(true);
        this.rosRender(true);
    }
}

export default new RedisLive();