Exemple #1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.HttpMethod != "POST")
            {
                return;
            }

            String area = "";

            if (!String.IsNullOrWhiteSpace((String)RouteData.Values["area"]))
            {
                area = (String)RouteData.Values["area"];
            }

            Int64 enterpriseId = 0;

            if ((Session["enterprise_data"]) != null && (Session["enterprise_data"] is EnterpriseData))
            {
                enterpriseId = ((EnterpriseData)Session["enterprise_data"]).Id;
            }

            Boolean newItem = false;

            if ((RouteData.Values["new"] != null) && (RouteData.Values["new"] == "1"))
            {
                newItem = true;
            }

            String ApplicationVirtualPath = Session["ApplicationVirtualPath"].ToString();

            LMenu menu1 = new LMenu("Dashboard", ApplicationVirtualPath + "admin/");
            LMenu menu2 = new LMenu("Proxy", ApplicationVirtualPath + "admin/proxy/" + (Request.Form["hashtag"] != null ? "#" + Request.Form["hashtag"].ToString() : ""));
            LMenu menu3 = new LMenu("Proxy", ApplicationVirtualPath + "admin/proxy/" + (Request.Form["hashtag"] != null ? "#" + Request.Form["hashtag"].ToString() : ""));

            WebJsonResponse contentRet = null;

            String html  = "";
            String eHtml = "";
            String js    = null;

            String errorTemplate = "<span class=\"empty-results\">{0}</span>";

            //Verifica se está sendo selecionada uma role
            Int64 proxyId = 0;

            try
            {
                proxyId = Int64.Parse((String)RouteData.Values["id"]);

                if (proxyId < 0)
                {
                    proxyId = 0;
                }
            }
            catch { }

            String         error    = "";
            ProxyGetResult retProxy = null;
            String         filter   = "";
            HashData       hashData = new HashData(this);


            if (!String.IsNullOrWhiteSpace((String)RouteData.Values["filter"]))
            {
                filter = (String)RouteData.Values["filter"];
            }

            if ((proxyId > 0) && (area.ToLower() != "search"))
            {
                try
                {
                    String rData = SafeTrend.Json.JSON.Serialize2(new
                    {
                        jsonrpc    = "1.0",
                        method     = "proxy.get",
                        parameters = new
                        {
                            proxyid = proxyId
                        },
                        id = 1
                    });
                    String jData = "";
                    using (IAMDatabase database = new IAMDatabase(IAMDatabase.GetWebConnectionString()))
                        jData = WebPageAPI.ExecuteLocal(database, this, rData);


                    if (String.IsNullOrWhiteSpace(jData))
                    {
                        throw new Exception("");
                    }

                    retProxy = JSON.Deserialize <ProxyGetResult>(jData);
                    if (retProxy == null)
                    {
                        error = MessageResource.GetMessage("proxy_not_found");
                        //ret = new WebJsonResponse("", MessageResource.GetMessage("user_not_found"), 3000, true);
                    }
                    else if (retProxy.error != null)
                    {
                        error    = retProxy.error.data;
                        retProxy = null;
                    }
                    else if (retProxy.result == null || retProxy.result.info == null)
                    {
                        error    = MessageResource.GetMessage("proxy_not_found");
                        retProxy = null;
                    }
                    else
                    {
                        menu3.Name = retProxy.result.info.name;
                    }
                }
                catch (Exception ex)
                {
                    error = MessageResource.GetMessage("api_error");
                    Tools.Tool.notifyException(ex, this);
                    retProxy = null;
                    //ret = new WebJsonResponse("", MessageResource.GetMessage("api_error"), 3000, true);
                }
            }

            switch (area)
            {
            case "":
            case "search":
            case "content":
                if (newItem)
                {
                    html  = "<h3>Adição de proxy</h3>";
                    html += "<form id=\"form_add_proxy\" method=\"post\" action=\"" + ApplicationVirtualPath + "admin/proxy/action/add_proxy/\"><div class=\"no-tabs pb10\">";
                    html += "<div class=\"form-group\"><label>Nome</label><input id=\"proxy_name\" name=\"proxy_name\" placeholder=\"Digite o nome do proxy\" type=\"text\"\"></div>";
                    html += "<div class=\"clear-block\"></div></div>";
                    html += "<button type=\"submit\" id=\"user-profile-password-save\" class=\"button secondary floatleft\">Adicionar</button>    <a href=\"" + ApplicationVirtualPath + "admin/proxy/" + (Request.Form["hashtag"] != null ? "#" + Request.Form["hashtag"].ToString() : "") + "\" class=\"button link floatleft\">Cancelar</a></form>";

                    contentRet = new WebJsonResponse("#content-wrapper", (eHtml != "" ? eHtml : html));
                }
                else
                {
                    if (retProxy == null)
                    {
                        Int32   page     = 1;
                        Int32   pageSize = 20;
                        Boolean hasNext  = true;

                        Int32.TryParse(Request.Form["page"], out page);

                        if (page < 1)
                        {
                            page = 1;
                        }

                        String proxyTemplate = "<div id=\"proxy-list-{0}\" data-id=\"{0}\" data-name=\"{1}\" data-total=\"{2}\" class=\"app-list-item\">";
                        proxyTemplate += "<table>";
                        proxyTemplate += "   <tbody>";
                        proxyTemplate += "       <tr>";
                        proxyTemplate += "           <td class=\"col1\">";
                        proxyTemplate += "               <span id=\"total_{0}\" class=\"total \">{2}</span>";
                        proxyTemplate += "               <a href=\"" + ApplicationVirtualPath + "admin/resource/#proxy/{0}\">";
                        proxyTemplate += "                   <div class=\"app-btn a-btn\"><span class=\"a-btn-inner\">Ver recursos</span></div>";
                        proxyTemplate += "               </a>";
                        proxyTemplate += "           </td>";
                        proxyTemplate += "           <td class=\"col2\">";
                        proxyTemplate += "               <div class=\"title\"><span class=\"name field-editor\" id=\"proxy_name_{0}\" data-id=\"{0}\" data-function=\"iamadmin.editTextField('#proxy_name_{0}',null,proxyNameEdit);\">{1}</span><span class=\"date\">{3}</span><div class=\"clear-block\"></div></div>";
                        proxyTemplate += "               <div class=\"description\">";
                        proxyTemplate += "                   <div class=\"first\">{4}</div>";
                        proxyTemplate += "               </div>";
                        proxyTemplate += "               <div class=\"links\">";
                        proxyTemplate += "                   <div class=\"last\">{5}</div>";
                        proxyTemplate += "               </div>";
                        proxyTemplate += "           </td>";
                        proxyTemplate += "       </tr>";
                        proxyTemplate += "   </tbody>";
                        proxyTemplate += "</table></div>";

                        js += "proxyNameEdit = function(thisId, changedText) { iamadmin.changeName(thisId,changedText); };";

                        html += "<div id=\"box-container\" class=\"box-container\">";

                        String query = "";
                        try
                        {
                            String rData = "";

                            if (!String.IsNullOrWhiteSpace((String)RouteData.Values["query"]))
                            {
                                query = (String)RouteData.Values["query"];
                            }

                            if (String.IsNullOrWhiteSpace(query) && !String.IsNullOrWhiteSpace(hashData.GetValue("query")))
                            {
                                query = hashData.GetValue("query");
                            }

                            if (String.IsNullOrWhiteSpace(query))
                            {
                                var tmpReq = new
                                {
                                    jsonrpc    = "1.0",
                                    method     = "proxy.list",
                                    parameters = new
                                    {
                                        page_size = pageSize,
                                        page      = page
                                    },
                                    id = 1
                                };

                                rData = SafeTrend.Json.JSON.Serialize2(tmpReq);
                            }
                            else
                            {
                                var tmpReq = new
                                {
                                    jsonrpc    = "1.0",
                                    method     = "proxy.search",
                                    parameters = new
                                    {
                                        text      = query,
                                        page_size = pageSize,
                                        page      = page
                                    },
                                    id = 1
                                };

                                rData = SafeTrend.Json.JSON.Serialize2(tmpReq);
                            }

                            String jData = "";
                            using (IAMDatabase database = new IAMDatabase(IAMDatabase.GetWebConnectionString()))
                                jData = WebPageAPI.ExecuteLocal(database, this, rData);


                            if (String.IsNullOrWhiteSpace(jData))
                            {
                                throw new Exception("");
                            }

                            ProxyListResult ret2 = JSON.Deserialize <ProxyListResult>(jData);
                            if (ret2 == null)
                            {
                                eHtml  += String.Format(errorTemplate, MessageResource.GetMessage("proxy_not_found"));
                                hasNext = false;
                            }
                            else if (ret2.error != null)
                            {
#if DEBUG
                                eHtml += String.Format(errorTemplate, ret2.error.data + ret2.error.debug);
#else
                                eHtml += String.Format(errorTemplate, ret2.error.data);
#endif
                                hasNext = false;
                            }
                            else if (ret2.result == null || (ret2.result.Count == 0 && page == 1))
                            {
                                eHtml  += String.Format(errorTemplate, MessageResource.GetMessage("proxy_not_found"));
                                hasNext = false;
                            }
                            else
                            {
                                foreach (ProxyData proxy in ret2.result)
                                {
                                    String text = "";
                                    if (proxy.last_sync > 0)
                                    {
                                        DateTime lastSync = new DateTime(1970, 1, 1).AddSeconds(proxy.last_sync);
                                        TimeSpan ts       = DateTime.Now - lastSync;
                                        if (ts.TotalSeconds > 60)
                                        {
                                            text = "<span class=\"red-text\">Última conexão a " + MessageResource.FormatTs(ts) + " através do endereço " + proxy.last_sync_address + ". Versão: " + proxy.last_sync_version + "</span>";
                                        }
                                        else
                                        {
                                            text = "On-line através do endereço " + proxy.last_sync_address + ". Versão: " + proxy.last_sync_version + ". Pid: " + proxy.last_sync_pid;
                                        }
                                    }
                                    else
                                    {
                                        text = "<span class=\"red-text\">Nunca se conectou no servidor</span>";
                                    }

                                    String links = "";
                                    links += (proxy.resource_qty > 0 ? "" : "<a class=\"confirm-action\" href=\"" + ApplicationVirtualPath + "admin/proxy/" + proxy.proxy_id + "/action/delete/" + (Request.Form["hashtag"] != null ? "#" + Request.Form["hashtag"].ToString() : "") + "\" confirm-title=\"Exclusão\" confirm-text=\"Deseja excluir definitivamente o proxy '" + proxy.name + "'?\" ok=\"Excluir\" cancel=\"Cancelar\"><div class=\"ico icon-close\">Apagar</div></a>");
                                    links += "<a class=\"confirm-action\" href=\"" + ApplicationVirtualPath + "admin/proxy/" + proxy.proxy_id + "/action/restart/" + (Request.Form["hashtag"] != null ? "#" + Request.Form["hashtag"].ToString() : "") + "\" confirm-title=\"Reset\" confirm-text=\"Deseja reiniciar o proxy '" + proxy.name + "' no servidor remoto?\" ok=\"Reiniciar\" cancel=\"Cancelar\"><div class=\"ico icon-loop\">Reiniciar</div></a>";

                                    links += "<a href=\"" + ApplicationVirtualPath + "admin/proxy/" + proxy.proxy_id + "/direct/download/" + (Request.Form["hashtag"] != null ? "#" + Request.Form["hashtag"].ToString() : "") + "\"><div class=\"ico icon-download-alt\">Download (instalador e configuração)</div></a>";

                                    html += String.Format(proxyTemplate, proxy.proxy_id, proxy.name, proxy.resource_qty, (proxy.create_date > 0 ? "Criado em " + MessageResource.FormatDate(new DateTime(1970, 1, 1).AddSeconds(proxy.create_date), true) : ""), text, links);
                                }

                                if (ret2.result.Count < pageSize)
                                {
                                    hasNext = false;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            eHtml += String.Format(errorTemplate, MessageResource.GetMessage("api_error"));
                        }

                        if (page == 1)
                        {
                            html += "</div>";

                            html += "<span class=\"empty-results content-loading proxy-list-loader hide\"></span>";

                            contentRet = new WebJsonResponse("#content-wrapper", (eHtml != "" ? eHtml : html));
                        }
                        else
                        {
                            contentRet = new WebJsonResponse("#content-wrapper #box-container", (eHtml != "" ? eHtml : html), true);
                        }

                        contentRet.js = js + "$( document ).unbind('end_of_scroll');";

                        if (hasNext)
                        {
                            contentRet.js += "$( document ).bind( 'end_of_scroll.loader_role', function() { $( document ).unbind('end_of_scroll.loader_role'); $('.proxy-list-loader').removeClass('hide'); iamadmin.getPageContent2( { page: " + ++page + ", search:'" + (!String.IsNullOrWhiteSpace(query) ? query : "") + "' }, function(){ $('.proxy-list-loader').addClass('hide'); } ); });";
                        }
                    }
                    else    //Esta sendo selecionado o proxy
                    {
                        if (error != "")
                        {
                            contentRet = new WebJsonResponse("#content-wrapper", String.Format(errorTemplate, error));
                        }
                        else
                        {
                        }
                    }
                }

                break;

            case "sidebar":
                if (menu1 != null)
                {
                    html += "<div class=\"sep\"><div class=\"section-nav-header\">";
                    html += "    <div class=\"crumbs\">";
                    html += "        <div class=\"subject subject-color\">";
                    html += "            <a href=\"" + menu1.HRef + "\">" + menu1.Name + "</a>";
                    html += "        </div>";
                    if (menu2 != null)
                    {
                        html += "        <div class=\"topic topic-color\">";
                        html += "            <a href=\"" + menu2.HRef + "\">" + menu2.Name + "</a>";
                        html += "        </div>";
                    }
                    html += "    </div>";
                    if (menu3 != null)
                    {
                        html += "    <div class=\"crumbs tutorial-title\">";
                        html += "        <h2 class=\"title tutorial-color\">" + menu3.Name + "</h2>";
                        html += "    </div>";
                    }
                    html += "</div></div>";
                }

                if (!newItem)
                {
                    html += "<div class=\"sep\"><button class=\"a-btn-big a-btn\" type=\"button\" onclick=\"window.location='" + ApplicationVirtualPath + "admin/proxy/new/" + (Request.Form["hashtag"] != null ? "#" + Request.Form["hashtag"].ToString() : "") + "'\">Novo proxy</button></div>";
                }

                contentRet = new WebJsonResponse("#main aside", html);
                break;

            case "mobilebar":
                break;


            case "buttonbox":
                break;
            }

            if (contentRet != null)
            {
                if (!String.IsNullOrWhiteSpace((String)Request["cid"]))
                {
                    contentRet.callId = (String)Request["cid"];
                }

                Retorno.Controls.Add(new LiteralControl(contentRet.ToJSON()));
            }
        }
Exemple #2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.HttpMethod != "POST")
            {
                return;
            }

            String area = "";

            if (!String.IsNullOrWhiteSpace((String)RouteData.Values["area"]))
            {
                area = (String)RouteData.Values["area"];
            }

            Int64 enterpriseId = 0;

            if ((Session["enterprise_data"]) != null && (Session["enterprise_data"] is EnterpriseData))
            {
                enterpriseId = ((EnterpriseData)Session["enterprise_data"]).Id;
            }


            String ApplicationVirtualPath = Session["ApplicationVirtualPath"].ToString();

            LMenu menu1 = null;
            LMenu menu2 = null;
            LMenu menu3 = null;

            WebJsonResponse contentRet = null;

            String html  = "";
            String eHtml = "";
            String js    = null;



            String errorTemplate = "<span class=\"empty-results\">{0}</span>";


            switch (area)
            {
            case "":
            case "content":
                using (IAMDatabase db = new IAMDatabase(IAMDatabase.GetWebConnectionString()))
                {
                    DataTable dtServices = db.ExecuteDataTable("select * from service_status order by service_name", CommandType.Text, null);
                    if ((dtServices != null) && (dtServices.Rows.Count > 0))
                    {
                        html += "<table id=\"users-table\" class=\"sorter\"><thead>";
                        html += "    <tr>";
                        html += "        <th class=\"pointer header headerSortDown\" data-column=\"name\">Serviço <div class=\"icomoon\"></div></th>";
                        html += "        <th class=\"pointer tHide header\" data-column=\"login\">Status do serviço <div class=\"icomoon\"></div></th>";
                        html += "        <th class=\"pointer tHide mHide header\" data-column=\"login\">Data de inicio <div class=\"icomoon\"></div></th>";
                        html += "        <th class=\"pointer tHide mHide header\" data-column=\"login\">Status da execução <div class=\"icomoon\"></div></th>";
                        html += "        <th class=\"pointer tHide mHide header\" data-column=\"login\">Data do processamento <div class=\"icomoon\"></div></th>";
                        html += "        <th class=\"pointer w150 tHide mHide header\" data-column=\"login\">% <div class=\"icomoon\"></div></th>";
                        html += "        <th class=\"pointer tHide mHide header\" data-column=\"login\">Informações adicionais <div class=\"icomoon\"></div></th>";
                        html += "        <th class=\"pointer w80 tHide mHide header\" data-column=\"login\">Ações <div class=\"icomoon\"></div></th>";
                        html += "    </tr>";
                        html += "</thead>";

                        html += "<tbody>";

                        String trTemplate = "    <tr class=\"user\">";
                        trTemplate += "            <td class=\"pointer ident10\">{0}</td>";
                        trTemplate += "            <td class=\"pointer tHide\">{1}</td>";
                        trTemplate += "            <td class=\"pointer tHide mHide\">{2}</td>";
                        trTemplate += "            <td class=\"pointer tHide mHide\">{3}</td>";
                        trTemplate += "            <td class=\"pointer tHide mHide\">{4}</td>";
                        trTemplate += "            <td class=\"pointer tHide mHide\">{5}</td>";
                        trTemplate += "            <td class=\"pointer tHide mHide\" style=\"line-height: 17px;\">{6}</td>";
                        trTemplate += "            <td class=\"pointer tHide mHide\"><button href=\"/admin/service_status/{0}/action/restart/\" class=\"a-btn btn-service confirm-action\" confirm-title=\"Restart\" confirm-text=\"Deseja reiniciar o serviço '{0}'?\" ok=\"Sim\" cancel=\"Não\"><div class=\"ico icon-loop\"></div></button></td>";
                        trTemplate += "    </tr>";

                        foreach (DataRow drS in dtServices.Rows)
                        {
                            String eStatus  = "";
                            String eDate    = "";
                            String sDate    = "";
                            String ePercent = "";
                            String eInfo    = "";

                            try
                            {
                                sDate = MessageResource.FormatDate((DateTime)drS["started_at"], false);
                            }
                            catch { }

                            String   sStatus  = "";
                            DateTime lastSync = (DateTime)drS["last_status"];
                            TimeSpan ts       = DateTime.Now - lastSync;
                            if (ts.TotalSeconds > 60)
                            {
                                sStatus += "<div class=\"red-text level-icon level-300\" style=\"margin: 0;\"></div>";
                                sStatus += "<span class=\"red-text\">Último status a " + MessageResource.FormatTs(ts) + "</span>";
                            }
                            else
                            {
                                sStatus += "<div class=\"green-text level-icon level-200\" style=\"margin: 0;\"></div>";
                                sStatus += "<span class=\"green-text\">Ativo</span>";

                                try
                                {
                                    switch (drS["service_name"].ToString().ToLower())
                                    {
                                    case "engine":

                                        ServiceStatusEngine stE = JSON.Deserialize <ServiceStatusEngine>(drS["additional_data"].ToString());
                                        if (stE.executing)
                                        {
                                            eStatus = "Processando registros";

                                            if (!String.IsNullOrEmpty(stE.last_status))
                                            {
                                                eStatus = stE.last_status;
                                            }

                                            //ePercent = stE.percent + "%";

                                            ePercent += "<div class=\"center\"><canvas id=\"usrLockChart\" width=\"40\" height=\"40\"></canvas></div>";
                                            js       += "iamadmin.buildPercentChart('#usrLockChart'," + stE.percent + ",{strokeColor:'#e5e5e5',textColor:'#333',color:'#76c558',showText:true});";

                                            eInfo  = "Total de registros: " + stE.total_registers;
                                            eInfo += "<br />Processado: " + stE.atual_register;
                                            eInfo += "<br />Erros: " + stE.errors;
                                            eInfo += "<br />Ignorados: " + stE.ignored;
                                            eInfo += "<br />Novas entidades: " + stE.new_users;
                                            eInfo += "<br />Atualizados: " + (stE.atual_register - stE.errors - stE.ignored - stE.new_users);
                                            eInfo += "<br />Threads: " + stE.thread_count;
                                            eInfo += "<br />Fila nas threads: " + stE.queue_count;

                                            try
                                            {
                                                DateTime dt1 = DateTime.Parse(stE.start_time);
                                                eDate = MessageResource.FormatDate(dt1, false);

                                                //Tempo estimado para conclusão
                                                TimeSpan ts2  = DateTime.Now - dt1;
                                                Double   calc = (ts2.TotalSeconds / (Double)stE.atual_register) * (Double)(stE.total_registers - stE.atual_register);

                                                eInfo += "<br />Conclusão estimada: " + MessageResource.FormatTime(DateTime.Now.AddSeconds(calc));
                                            }
                                            catch { }
                                        }

                                        break;

                                    case "report":
                                        ServiceStatusBase stR = JSON.Deserialize <ServiceStatusBase>(drS["additional_data"].ToString());
                                        break;

                                    case "inbound":
                                        ServiceStatusInbound stI = JSON.Deserialize <ServiceStatusInbound>(drS["additional_data"].ToString());
                                        if (stI.executing)
                                        {
                                            eStatus = "Processando importação";

                                            if (!String.IsNullOrEmpty(stI.last_status))
                                            {
                                                eStatus = stI.last_status;
                                            }


                                            //ePercent = stE.percent + "%";

                                            ePercent += "<div class=\"center\"><canvas id=\"inboundLockChart\" width=\"40\" height=\"40\"></canvas></div>";
                                            js       += "iamadmin.buildPercentChart('#inboundLockChart'," + stI.percent + ",{strokeColor:'#e5e5e5',textColor:'#333',color:'#76c558',showText:true});";

                                            eInfo  = "Total de arquivos: " + stI.total_files;
                                            eInfo += "<br />Processado: " + stI.processed_files;

                                            try
                                            {
                                                DateTime dt1 = DateTime.Parse(stI.start_time);
                                                eDate = MessageResource.FormatDate(dt1, false);

                                                //Tempo estimado para conclusão
                                                TimeSpan ts2  = DateTime.Now - dt1;
                                                Double   calc = (ts2.TotalSeconds / (Double)stI.processed_files) * (Double)(stI.total_files - stI.processed_files);

                                                eInfo += "<br />Conclusão estimada: " + MessageResource.FormatTime(DateTime.Now.AddSeconds(calc));
                                            }
                                            catch { }
                                        }
                                        break;

                                    case "dispatcher":
                                        ServiceStatusDispatcher stD = JSON.Deserialize <ServiceStatusDispatcher>(drS["additional_data"].ToString());
                                        if (stD.executing)
                                        {
                                            eStatus += "Executando publicação";
                                        }

                                        if (stD.executing_deploy_now)
                                        {
                                            if (eStatus != "")
                                            {
                                                eStatus += "/";
                                            }

                                            eStatus += "Executando publicação sob demanda";
                                        }

                                        break;
                                    }
                                }
                                catch { }
                            }

                            html += String.Format(trTemplate, drS["service_name"], sStatus, sDate, eStatus, eDate, ePercent, eInfo);
                        }

                        html += "</tbody></table>";

                        html += "<span class=\"empty-results content-loading user-list-loader hide\"></span>";

                        contentRet    = new WebJsonResponse("#content-wrapper", (eHtml != "" ? eHtml : html));
                        contentRet.js = "iamadmin.doReload(10000);" + js;
                    }
                    else
                    {
                        eHtml     += String.Format(errorTemplate, MessageResource.GetMessage("api_error"));
                        contentRet = new WebJsonResponse("#content-wrapper", (eHtml != "" ? eHtml : html));
                    }
                }
                break;

            case "sidebar":
                break;

            case "mobilebar":
                break;


            case "buttonbox":
                break;
            }

            if (contentRet != null)
            {
                if (!String.IsNullOrWhiteSpace((String)Request["cid"]))
                {
                    contentRet.callId = (String)Request["cid"];
                }

                Retorno.Controls.Add(new LiteralControl(contentRet.ToJSON()));
            }
        }