add fetch timeout

This commit is contained in:
liugq 2021-11-23 17:17:24 +08:00
parent 7a0fb883f4
commit 9cfabef80b
3 changed files with 99 additions and 74 deletions

10
ui.go
View File

@ -32,12 +32,14 @@ func (h UI) InitUI() {
uiapi.Init(h.Config) uiapi.Init(h.Config)
var apiEndpoint = h.Config.UI.APIEndpoint var apiEndpoint = h.Config.UI.APIEndpoint
if strings.TrimSpace(apiEndpoint) == "" { apiConfig := &global.Env().SystemConfig.APIConfig
apiConfig := &global.Env().SystemConfig.APIConfig
apiEndpoint = fmt.Sprintf("%s://%s", apiConfig.GetSchema(), apiConfig.NetworkConfig.GetPublishAddr())
}
ui.HandleUIFunc("/config", func(w http.ResponseWriter, req *http.Request){ ui.HandleUIFunc("/config", func(w http.ResponseWriter, req *http.Request){
if(strings.TrimSpace(apiEndpoint) == ""){
hostParts := strings.Split(req.Host, ":")
apiEndpoint = fmt.Sprintf("%s//%s:%s", apiConfig.GetSchema(), hostParts[0], apiConfig.NetworkConfig.GetBindingPort())
}
buf, _ := json.Marshal(util.MapStr{ buf, _ := json.Marshal(util.MapStr{
"api_endpoint": apiEndpoint, "api_endpoint": apiEndpoint,
}) })

View File

@ -1,70 +1,74 @@
import request from '@/utils/request'; import request from "@/utils/request";
import {buildQueryArgs, ESPrefix} from './common'; import { buildQueryArgs, ESPrefix, fetchWithTimeout } from "./common";
export async function getClusterVersion(params) { export async function getClusterVersion(params) {
return request(`${ESPrefix}/${params.cluster}/version`, { return request(`${ESPrefix}/${params.cluster}/version`, {
method: 'GET' method: "GET",
}); });
} }
export async function getClusterMetrics(params) { export async function getClusterMetrics(params) {
let id = params.cluster_id; let id = params.cluster_id;
delete(params['cluster_id']); delete params["cluster_id"];
return request(`${ESPrefix}/${id}/metrics?min=${params.timeRange.min}&max=${params.timeRange.max}`, { return request(
method: 'GET' `${ESPrefix}/${id}/metrics?min=${params.timeRange.min}&max=${params.timeRange.max}`,
}); {
method: "GET",
}
);
} }
export async function createClusterConfig(params) { export async function createClusterConfig(params) {
return request(`${ESPrefix}/`, { return request(`${ESPrefix}/`, {
method: 'POST', method: "POST",
body: params, body: params,
}); });
} }
export async function updateClusterConfig(params) { export async function updateClusterConfig(params) {
let id = params.id; let id = params.id;
delete(params['id']); delete params["id"];
return request(`${ESPrefix}/${id}`, { return request(`${ESPrefix}/${id}`, {
method: 'PUT', method: "PUT",
body: params, body: params,
}); });
} }
export async function deleteClusterConfig(params) { export async function deleteClusterConfig(params) {
return request(`${ESPrefix}/${params.id}`, { return request(`${ESPrefix}/${params.id}`, {
method: 'DELETE', method: "DELETE",
body: params, body: params,
}); });
} }
export async function searchClusterConfig(params) { export async function searchClusterConfig(params) {
let url = `${ESPrefix}/_search`; let url = `${ESPrefix}/_search`;
let args = buildQueryArgs({ let args = buildQueryArgs({
name: params.name, name: params.name,
enabled: params.enabled, enabled: params.enabled,
from: params.from, from: params.from,
size: params.size, size: params.size,
}); });
if(args.length > 0){ if (args.length > 0) {
url += args; url += args;
} }
return request(url, { return request(url, {
method: 'GET', method: "GET",
}); });
} }
export async function getClusterStatus(params) { export async function getClusterStatus(params) {
let url = `${ESPrefix}/status`; let url = `${ESPrefix}/status`;
return request(url, { return request(url, {
method: 'GET', method: "GET",
}); });
} }
export async function tryConnect(params) { export async function tryConnect(params) {
let url = `${ESPrefix}/try_connect`; let url = `${ESPrefix}/try_connect`;
return request(url, {
method: 'POST', return request(url, {
body: params, method: "POST",
}); body: params,
} });
}

View File

@ -1,46 +1,65 @@
import $ from 'jquery'; import $ from "jquery";
function getConfig(){ function getConfig() {
const options = { const options = {
url: "/config", url: "/config",
cache: false, cache: false,
type: 'GET', type: "GET",
dataType: 'json', // disable automatic guessing dataType: "json", // disable automatic guessing
async: false, async: false,
}; };
let result = {} let result = {};
try{ try {
const text = $.ajax(options).responseText; const text = $.ajax(options).responseText;
result = JSON.parse(text); result = JSON.parse(text);
}catch(e){ } catch (e) {
console.warn('failed get config data') console.warn("failed get config data");
} }
return result; return result;
} }
const {api_endpoint} = getConfig(); const { api_endpoint } = getConfig();
let apiEndpoint = api_endpoint; let apiEndpoint = api_endpoint;
if(!apiEndpoint){ if (!apiEndpoint) {
apiEndpoint = API_ENDPOINT; apiEndpoint = API_ENDPOINT;
if(!API_ENDPOINT){ if (!API_ENDPOINT) {
apiEndpoint = `${location.protocol}//${location.hostname}:2900` apiEndpoint = `${location.protocol}//${location.hostname}:2900`;
} }
} }
export const pathPrefix = (apiEndpoint || '') + '/_search-center'; export const pathPrefix = (apiEndpoint || "") + "/_search-center";
export function buildQueryArgs(params){ export function buildQueryArgs(params) {
let argsStr = ''; let argsStr = "";
for(let key in params){ for (let key in params) {
if(typeof params[key] !== 'undefined') { if (typeof params[key] !== "undefined") {
argsStr += `${key}=${params[key]}&` argsStr += `${key}=${params[key]}&`;
} }
} }
if(argsStr.length > 0){ if (argsStr.length > 0) {
argsStr = '?' + argsStr argsStr = "?" + argsStr;
argsStr = argsStr.slice(0, argsStr.length -1) argsStr = argsStr.slice(0, argsStr.length - 1);
} }
return argsStr; return argsStr;
} }
export const ESPrefix = (apiEndpoint || "") + "/elasticsearch";
export const ESPrefix = (apiEndpoint || '') + '/elasticsearch'; export async function fetchWithTimeout(resource, options = {}) {
const { timeout = 5000 } = options;
const controller = new AbortController();
const id = setTimeout(() => {
controller.abort();
}, timeout);
try {
const response = await fetch(resource, {
...options,
signal: controller.signal,
});
clearTimeout(id);
return response.json();
} catch (err) {
return {
error: "timeout",
};
}
}