<!doctype html>
<html lang="zh-cn">
<head>
    <title>Sers Dashboard</title>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">


    <script src="Scripts/jquery/jquery.min.js"></script>

    <!-- Main CSS -->
    <link rel="stylesheet" href="Scripts/blog/css/style.css">

    <!-- Font Awesome -->
    <link href="Scripts/blog/css/font-awesome.min.css" rel="stylesheet">
</head>

<body>
    <style>
        .container {
            max-width: 1920px !important;
        }
    </style>

    <!-- Header -->
    <div class="container">
        <div class="header-wrap d-none d-md-block">
            <div class="row">

                <!-- Left header box -->
                <header class="col-6 text-left">
                    <h1><span> Sers </span> Dashboard </h1>
                </header>

            </div>
        </div>
    </div>


    <!-- Main navigation -->
    <div class="container navbar-container">
        <nav class="navbar navbar-expand-md navbar-light">

            <!-- Company name shown on mobile -->
            <a class="navbar-brand d-md-none d-lg-none d-xl-none" href="#"><span> Sers </span> Dashboard </a>

            <!-- Mobile menu toggle -->
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#mainNavbar" aria-controls="mainNavbar" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>

            <!-- Main navigation items -->
            <div class="collapse navbar-collapse" id="mainNavbar">
                <ul class="navbar-nav mr-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="/">Home <span class="sr-only">(current)</span></a>
                    </li>

                    <li class="nav-item">
                        <a class="nav-link" href="/_gover_/ActiveApiDoc.html?r=_gover_">ActiveApiDoc</a>
                    </li>

                    <li class="nav-item">
                        <a class="nav-link" href="/_gover_/ApiDoc.html?r=_gover_">ApiDoc</a>
                    </li>

                    <li class="nav-item">
                        <a class="nav-link" href="/_gover_/ApiStation.html">ApiStation</a>
                    </li>

                    <li class="nav-item active">
                        <a class="nav-link" href="/_gover_/ServiceStation.html">ServiceStation</a>
                    </li>

                    <li class="nav-item">
                        <a class="nav-link" href="/_gover_/RateLimit.html">RateLimit</a>
                    </li>

                    <li class="nav-item">
                        <a class="nav-link" href="/_gover_/Health.html">Health</a>
                    </li>


                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Ext</a>
                        <div class="dropdown-menu navbar-dark bg-primary">
                            <a class="dropdown-item" href="/JsStation/JsStation.html">JsStation</a>
                            <a class="dropdown-item" href="/MicroControl/mc.html">MicroControl</a>
                            <a class="dropdown-item" href="/_robot_/TaskMng.html">Robot</a>
                        </div>
                    </li>

                </ul>


            </div>
        </nav>
    </div>

    <!-- Jumbtron / Slider -->
    <!--<div class="jumbotron-wrap">
        <div class="container">
            <div class="jumbotron jumbotron-narrow static-slider"> 
            </div>
        </div>
    </div>-->

    <!-- Main content area -->
    <main class="container">
        <div class="row">

            <!-- Main content -->
            <div class="col">
                <!--内容区-->
                <article>
                    <h1 style="text-align:center;">服务站点管理</h1>

                    <style>
                        .line {
                            border: 1px solid #cccccc;
                            border-collapse: collapse;
                        }
                    </style>
                    <script src="Scripts/vue/vue.js"></script>
                    <!--<script src="Scripts/jquery/jquery.min.js"></script>-->

                    <script src="Scripts/Sers/sers.apiClient.js"></script>
                    <div id="stations">

                        <div v-for="station in stations" style="width:300px;height:520px;line-height:1.4;float:left;margin:2px;padding:4px;word-wrap:break-word;" class="line">
                            <font style="font-weight:bold;color:#e84855;">{{ (station.serviceStationInfo||{}).serviceStationName }} </font><br />
                            {{station.apiStationNames}} <br />
                            操作 : <a @click="start(station)">打开</a>   <a @click="pause(station)">暂停</a>  <a @click="stop(station)">强制关闭</a>
                            <br />
                            <table>
                                <tr>
                                    <td> 状态(正常|暂停) </td>
                                    <td> : {{station.status}}  </td>
                                </tr>
                                <tr>
                                    <td> stationVersion </td>
                                    <td> : {{ (station.serviceStationInfo||{}).stationVersion }} </td>
                                </tr>
                                <tr>
                                    <td> startTime </td>
                                    <td> : {{station.startTime}} </td>
                                </tr>
                            </table>
                            -----------------------<br />
                            <table>
                                <tr>
                                    <td> qps </td>
                                    <td> : {{station.qps}} </td>
                                </tr>
                                <tr>
                                    <td> apiNode </td>
                                    <td> : {{station.apiNodeCount}} </td>
                                </tr>
                                <tr>
                                    <td> activeApiNode </td>
                                    <td> : {{station.activeApiNodeCount}}    </td>
                                </tr>
                                <tr>
                                    <td> called(err/sum) </td>
                                    <td> : {{(station.counter||{}).errorCount}}/{{(station.counter||{}).sumCount}} </td>
                                </tr>
                            </table>
                            -----------------------<br />
                            <table>
                                <tr>
                                    <td> Machine </td>
                                    <td> : {{ getStringByPath(station,'deviceInfo.MachineName')}} </td>
                                </tr>
                                <tr>
                                    <td> cpu </td>
                                    <td> : {{ getFloatByPath(station,'usageStatus.cpuUsage') }} %  </td>
                                </tr>
                                <tr>
                                    <td> memory </td>
                                    <td> : {{ getFloatByPath(station,'usageStatus.memoryUsage') }} %  </td>
                                </tr>
                            </table>
                            -----------------------<br />
                            <table>
                                <tr>
                                    <td> Thread(running/all) </td>
                                    <td> : {{ getStringByPath(station,'Process.RunningThreadCount') }}/{{ getStringByPath(station,'Process.ThreadCount')}} </td>
                                </tr>
                                <tr>
                                    <td> WorkingSet(MB) </td>
                                    <td> : {{ getFloatByPath(station,'Process.WorkingSet') }} </td>
                                </tr>
                            </table>
                            -----------------------<br />
                            <table>
                                <tr>
                                    <td> deviceKey </td>
                                    <td> : <font :title="(station.deviceInfo||{}).deviceKey">{{((station.deviceInfo||{}).deviceKey||'').substr(0,4) }} </font></td>
                                </tr>
                                <tr>
                                    <td> serviceStationKey </td>
                                    <td> : <font :title="(station.serviceStationInfo||{}).serviceStationKey">{{((station.serviceStationInfo||{}).serviceStationKey||'').substr(0,4) }} </font></td>
                                </tr>
                                <tr>
                                    <td> connKey </td>
                                    <td> : <font :title="station.connKey">{{(station.connKey||'').substr(0,4) }} </font></td>
                                </tr>
                                <tr>
                                    <td> connectionIp </td>
                                    <td> : <font :title="station.connectionIp">{{ station.connectionIp }} </font></td>
                                </tr>
                            </table>
                            <br /> <!--{{station.deviceInfo}}-->
                        </div>


                        <div style=" clear: both;"></div>
                    </div>
                    <script>

                        // getFloatByPath({a:{b:'12.525'}},'a.b');
                        function getFloatByPath(obj, path, fixed) {
                            var items = path.split('.');
                            for (var i in items) {
                                if (!obj) return '';
                                obj = obj[items[i]];
                            }
                            obj = parseFloat(obj);
                            return isNaN(obj) ? '' : obj.toFixed(isNaN(fixed) ? 2 : fixed);
                        }

                        function getStringByPath(obj, path) {
                            var items = path.split('.');
                            for (var i in items) {
                                if (!obj) return '';
                                obj = obj[items[i]];
                            }
                            return '' + obj;
                        }


                        var stations = new Vue({
                            el: '#stations',
                            data: {
                                stations: [
                                    {
                                    }
                                ]
                            }, methods: {
                                getFloatByPath: getFloatByPath,
                                getStringByPath: getStringByPath,

                                start: function (station) {
                                    sers.apiClient.serviceStation_start(station.connKey, function (data) {
                                        rendStations();
                                    });
                                }, pause: function (station) {
                                    sers.apiClient.serviceStation_pause(station.connKey, function (data) {
                                        rendStations();
                                    });
                                }, stop: function (station) {
                                    if (!confirm('手动关闭服务站点后，服务站点不一定会自动重新连接，确定继续吗？')) return;
                                    sers.apiClient.serviceStation_stop(station.connKey, function (data) {
                                        rendStations();
                                    });
                                }
                            }
                        });


                        function rendStations() {
                            sers.apiClient.serviceStation_getAll(function (data) {
                                stations.stations = data.data;
                            });
                        }

                        rendStations();


                        var intervals = [];
                        function startRefreshTask() {
                            var interval = setInterval(rendStations, 2000);
                            intervals.push(interval);
                        }
                        function stopRefreshTask() {
                            for (var t in intervals) {
                                clearInterval(intervals[t]);
                            }
                            intervals = [];
                        }

                        startRefreshTask();
                    </script>
                </article>
            </div>
        </div>
    </main>


    <!-- Footer -->
    <div class="container footer-container">
        <footer class="footer">
            <div class="footer-bottom">
                <p class="text-center"> <a target="_blank" href="https://github.com/serset/Sers">github</a> &nbsp;&nbsp; <a target="_blank" href="https://serset.github.io">online doc</a>  &nbsp;&nbsp; email: serset@yeah.net </p>
                <p class="text-center"><a href="#">Back to top</a></p>
            </div>
        </footer>
    </div>


    <!-- Bootcamp JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <!--<script src="Scripts/blog/js/jquery-3.2.1.slim.min.js"></script>-->
    <script src="Scripts/blog/js/popper.min.js"></script>
    <script src="Scripts/blog/js/bootstrap.min.js"></script>

</body>
</html>