kezhenxu94
2 years ago
committed by
GitHub
47 changed files with 1377 additions and 1635 deletions
@ -0,0 +1,84 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.apache.dolphinscheduler.api.configuration; |
||||
|
||||
import static java.util.stream.Collectors.toList; |
||||
import static java.util.stream.Collectors.toSet; |
||||
import java.util.Collection; |
||||
import java.util.Locale; |
||||
import java.util.Set; |
||||
import org.springframework.context.MessageSource; |
||||
import org.springframework.context.i18n.LocaleContextHolder; |
||||
import org.springframework.core.Ordered; |
||||
import org.springframework.core.annotation.Order; |
||||
import org.springframework.stereotype.Component; |
||||
import lombok.RequiredArgsConstructor; |
||||
import springfox.documentation.service.Operation; |
||||
import springfox.documentation.service.RequestParameter; |
||||
import springfox.documentation.spi.DocumentationType; |
||||
import springfox.documentation.spi.service.OperationBuilderPlugin; |
||||
import springfox.documentation.spi.service.contexts.OperationContext; |
||||
|
||||
@Component |
||||
public class OpenAPITranslationConfiguration { |
||||
|
||||
@Component |
||||
@RequiredArgsConstructor |
||||
@Order(Ordered.LOWEST_PRECEDENCE) |
||||
public static class TranslationOperationBuilderPlugin implements OperationBuilderPlugin { |
||||
|
||||
private final MessageSource messageSource; |
||||
|
||||
@Override |
||||
public boolean supports(DocumentationType delimiter) { |
||||
return true; |
||||
} |
||||
|
||||
@Override |
||||
public void apply(OperationContext context) { |
||||
Locale locale = LocaleContextHolder.getLocale(); |
||||
Operation operation = context.operationBuilder().build(); |
||||
String notes = operation.getNotes(); |
||||
notes = messageSource.getMessage(notes, null, notes, locale); |
||||
|
||||
Set<String> tags = operation.getTags().stream() |
||||
.map(tag -> messageSource.getMessage(tag, null, tag, locale)) |
||||
.collect(toSet()); |
||||
|
||||
Collection<RequestParameter> parameters = operation.getRequestParameters().stream() |
||||
.map(it -> new RequestParameter( |
||||
it.getName(), |
||||
it.getIn(), |
||||
messageSource.getMessage(it.getDescription(), null, it.getDescription(), locale), |
||||
it.getRequired(), |
||||
it.getDeprecated(), |
||||
it.getHidden(), |
||||
it.getParameterSpecification(), |
||||
it.getScalarExample(), |
||||
it.getExamples(), |
||||
it.getPrecedence(), |
||||
it.getExtensions(), |
||||
it.getParameterIndex())) |
||||
.collect(toList()); |
||||
|
||||
context.operationBuilder() |
||||
.notes(notes) |
||||
.requestParameters(parameters) |
||||
.tags(tags); |
||||
} |
||||
} |
||||
} |
@ -1,300 +0,0 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.apache.dolphinscheduler.api.configuration; |
||||
|
||||
import static com.google.common.collect.Maps.newTreeMap; |
||||
|
||||
import org.apache.commons.lang3.StringUtils; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.HashSet; |
||||
import java.util.Iterator; |
||||
import java.util.List; |
||||
import java.util.Locale; |
||||
import java.util.Map; |
||||
import java.util.Set; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; |
||||
import org.springframework.context.MessageSource; |
||||
import org.springframework.context.annotation.Primary; |
||||
import org.springframework.context.i18n.LocaleContextHolder; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
import com.google.common.collect.Multimap; |
||||
|
||||
import io.swagger.models.Contact; |
||||
import io.swagger.models.Info; |
||||
import io.swagger.models.Model; |
||||
import io.swagger.models.Scheme; |
||||
import io.swagger.models.Swagger; |
||||
import io.swagger.models.Tag; |
||||
import io.swagger.models.parameters.Parameter; |
||||
import springfox.documentation.service.ApiInfo; |
||||
import springfox.documentation.service.ApiListing; |
||||
import springfox.documentation.service.Documentation; |
||||
import springfox.documentation.service.ResourceListing; |
||||
import springfox.documentation.swagger2.mappers.LicenseMapper; |
||||
import springfox.documentation.swagger2.mappers.ModelMapper; |
||||
import springfox.documentation.swagger2.mappers.ParameterMapper; |
||||
import springfox.documentation.swagger2.mappers.SecurityMapper; |
||||
import springfox.documentation.swagger2.mappers.ServiceModelToSwagger2Mapper; |
||||
import springfox.documentation.swagger2.mappers.VendorExtensionsMapper; |
||||
|
||||
/** |
||||
* application configuration |
||||
*/ |
||||
@Component(value = "ServiceModelToSwagger2Mapper") |
||||
@Primary |
||||
@ConditionalOnWebApplication |
||||
public class ServiceModelToSwagger2MapperImpl extends ServiceModelToSwagger2Mapper { |
||||
|
||||
|
||||
@Autowired |
||||
private ModelMapper modelMapper; |
||||
@Autowired |
||||
private ParameterMapper parameterMapper; |
||||
@Autowired |
||||
private SecurityMapper securityMapper; |
||||
@Autowired |
||||
private LicenseMapper licenseMapper; |
||||
@Autowired |
||||
private VendorExtensionsMapper vendorExtensionsMapper; |
||||
|
||||
@Autowired |
||||
private MessageSource messageSource; |
||||
|
||||
@Override |
||||
public Swagger mapDocumentation(Documentation from) { |
||||
|
||||
if (from == null) { |
||||
return null; |
||||
} |
||||
|
||||
Swagger swagger = new Swagger(); |
||||
|
||||
swagger.setVendorExtensions(vendorExtensionsMapper.mapExtensions(from.getVendorExtensions())); |
||||
swagger.setSchemes(mapSchemes(from.getSchemes())); |
||||
swagger.setPaths(mapApiListings(from.getApiListings())); |
||||
swagger.setHost(from.getHost()); |
||||
swagger.setDefinitions(modelsFromApiListings( from.getApiListings() ) ); |
||||
swagger.setSecurityDefinitions(securityMapper.toSecuritySchemeDefinitions(from.getResourceListing())); |
||||
ApiInfo info = fromResourceListingInfo(from); |
||||
if (info != null) { |
||||
swagger.setInfo(mapApiInfo(info)); |
||||
} |
||||
swagger.setBasePath(from.getBasePath()); |
||||
swagger.setTags(tagSetToTagList(from.getTags())); |
||||
List<String> list2 = from.getConsumes(); |
||||
if (list2 != null) { |
||||
swagger.setConsumes(new ArrayList<String>(list2)); |
||||
} else { |
||||
swagger.setConsumes(null); |
||||
} |
||||
List<String> list3 = from.getProduces(); |
||||
if (list3 != null) { |
||||
swagger.setProduces(new ArrayList<String>(list3)); |
||||
} else { |
||||
swagger.setProduces(null); |
||||
} |
||||
|
||||
return swagger; |
||||
} |
||||
|
||||
|
||||
@Override |
||||
protected Info mapApiInfo(ApiInfo from) { |
||||
|
||||
if (from == null) { |
||||
return null; |
||||
} |
||||
|
||||
Info info = new Info(); |
||||
|
||||
info.setLicense(licenseMapper.apiInfoToLicense(from)); |
||||
info.setVendorExtensions(vendorExtensionsMapper.mapExtensions(from.getVendorExtensions())); |
||||
info.setTermsOfService(from.getTermsOfServiceUrl()); |
||||
info.setContact(map(from.getContact())); |
||||
info.setDescription(from.getDescription()); |
||||
info.setVersion(from.getVersion()); |
||||
info.setTitle(from.getTitle()); |
||||
|
||||
return info; |
||||
} |
||||
|
||||
@Override |
||||
protected Contact map(springfox.documentation.service.Contact from) { |
||||
|
||||
if (from == null) { |
||||
return null; |
||||
} |
||||
|
||||
Contact contact = new Contact(); |
||||
|
||||
contact.setName(from.getName()); |
||||
contact.setUrl(from.getUrl()); |
||||
contact.setEmail(from.getEmail()); |
||||
|
||||
return contact; |
||||
} |
||||
|
||||
@Override |
||||
protected io.swagger.models.Operation mapOperation(springfox.documentation.service.Operation from) { |
||||
|
||||
if (from == null) { |
||||
return null; |
||||
} |
||||
|
||||
Locale locale = LocaleContextHolder.getLocale(); |
||||
|
||||
io.swagger.models.Operation operation = new io.swagger.models.Operation(); |
||||
|
||||
operation.setSecurity(mapAuthorizations(from.getSecurityReferences())); |
||||
operation.setVendorExtensions(vendorExtensionsMapper.mapExtensions(from.getVendorExtensions())); |
||||
operation.setDescription(messageSource.getMessage(from.getNotes(), null, from.getNotes(), locale)); |
||||
operation.setOperationId(from.getUniqueId()); |
||||
operation.setResponses(mapResponseMessages(from.getResponseMessages())); |
||||
operation.setSchemes(stringSetToSchemeList(from.getProtocol())); |
||||
Set<String> tagsSet = new HashSet<>(1); |
||||
|
||||
if(from.getTags() != null && from.getTags().size() > 0){ |
||||
|
||||
List<String> list = new ArrayList<String>(tagsSet.size()); |
||||
|
||||
Iterator<String> it = from.getTags().iterator(); |
||||
while(it.hasNext()){ |
||||
String tag = it.next(); |
||||
list.add( |
||||
StringUtils.isNotBlank(tag) ? messageSource.getMessage(tag, null, tag, locale) : " "); |
||||
} |
||||
|
||||
operation.setTags(list); |
||||
}else { |
||||
operation.setTags(null); |
||||
} |
||||
|
||||
operation.setSummary(from.getSummary()); |
||||
Set<String> set1 = from.getConsumes(); |
||||
if (set1 != null) { |
||||
operation.setConsumes(new ArrayList<String>(set1)); |
||||
} else { |
||||
operation.setConsumes(null); |
||||
} |
||||
|
||||
Set<String> set2 = from.getProduces(); |
||||
if (set2 != null) { |
||||
operation.setProduces(new ArrayList<String>(set2)); |
||||
} else { |
||||
operation.setProduces(null); |
||||
} |
||||
|
||||
|
||||
operation.setParameters(parameterListToParameterList(from.getParameters())); |
||||
if (from.getDeprecated() != null) { |
||||
operation.setDeprecated(Boolean.parseBoolean(from.getDeprecated())); |
||||
} |
||||
|
||||
return operation; |
||||
} |
||||
|
||||
@Override |
||||
protected Tag mapTag(springfox.documentation.service.Tag from) { |
||||
|
||||
if (from == null) { |
||||
return null; |
||||
} |
||||
|
||||
Locale locale = LocaleContextHolder.getLocale(); |
||||
|
||||
Tag tag = new Tag(); |
||||
|
||||
tag.setVendorExtensions(vendorExtensionsMapper.mapExtensions(from.getVendorExtensions())); |
||||
tag.setName(messageSource.getMessage(from.getName(), null, from.getName(), locale)); |
||||
tag.setDescription(from.getDescription()); |
||||
|
||||
return tag; |
||||
} |
||||
|
||||
|
||||
private ApiInfo fromResourceListingInfo(Documentation documentation) { |
||||
|
||||
if (documentation == null) { |
||||
return null; |
||||
} |
||||
ResourceListing resourceListing = documentation.getResourceListing(); |
||||
if (resourceListing == null) { |
||||
return null; |
||||
} |
||||
return resourceListing.getInfo(); |
||||
} |
||||
|
||||
protected List<Tag> tagSetToTagList(Set<springfox.documentation.service.Tag> set) { |
||||
|
||||
if (set == null) { |
||||
return null; |
||||
} |
||||
|
||||
List<Tag> list = new ArrayList<Tag>(set.size()); |
||||
for (springfox.documentation.service.Tag tag : set) { |
||||
list.add(mapTag(tag)); |
||||
} |
||||
|
||||
return list; |
||||
} |
||||
|
||||
protected List<Scheme> stringSetToSchemeList(Set<String> set) { |
||||
if (set == null) { |
||||
return null; |
||||
} |
||||
|
||||
List<Scheme> list = new ArrayList<Scheme>(set.size()); |
||||
for (String string : set) { |
||||
list.add(Enum.valueOf(Scheme.class, string)); |
||||
} |
||||
|
||||
return list; |
||||
} |
||||
|
||||
protected List<Parameter> parameterListToParameterList(List<springfox.documentation.service.Parameter> list) { |
||||
if (list == null) { |
||||
return null; |
||||
} |
||||
|
||||
List<Parameter> list1 = new ArrayList<Parameter>(list.size()); |
||||
|
||||
Locale locale = LocaleContextHolder.getLocale(); |
||||
|
||||
for (springfox.documentation.service.Parameter param : list) { |
||||
String description = messageSource.getMessage(param.getDescription(), null, param.getDescription(), locale); |
||||
|
||||
springfox.documentation.service.Parameter parameter = new springfox.documentation.service.Parameter(param.getName(),description,param.getDefaultValue(),param.isRequired(),param.isAllowMultiple(),param.isAllowEmptyValue(),param.getModelRef(),param.getType(),param.getAllowableValues(),param.getParamType(),param.getParamAccess(),param.isHidden(),param.getPattern(),param.getCollectionFormat(),param.getOrder(),param.getScalarExample(),param.getExamples() ,param.getVendorExtentions()); |
||||
list1.add(parameterMapper.mapParameter(parameter)); |
||||
} |
||||
|
||||
return list1; |
||||
} |
||||
|
||||
|
||||
Map<String, Model> modelsFromApiListings(Multimap<String, ApiListing> apiListings) { |
||||
Map<String, springfox.documentation.schema.Model> definitions = newTreeMap(); |
||||
for (ApiListing each : apiListings.values()) { |
||||
definitions.putAll(each.getModels()); |
||||
} |
||||
return modelMapper.mapModels(definitions); |
||||
} |
||||
|
||||
} |
@ -1,117 +0,0 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.apache.dolphinscheduler.common.os; |
||||
|
||||
|
||||
import org.junit.Test; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
import oshi.SystemInfo; |
||||
import oshi.hardware.CentralProcessor; |
||||
import oshi.hardware.CentralProcessor.TickType; |
||||
import oshi.hardware.GlobalMemory; |
||||
import oshi.hardware.HardwareAbstractionLayer; |
||||
import oshi.util.FormatUtil; |
||||
import oshi.util.Util; |
||||
|
||||
import java.util.Arrays; |
||||
|
||||
|
||||
/** |
||||
* os information test |
||||
*/ |
||||
public class OshiTest { |
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(OshiTest.class); |
||||
|
||||
|
||||
@Test |
||||
public void test() { |
||||
|
||||
SystemInfo si = new SystemInfo(); |
||||
|
||||
HardwareAbstractionLayer hal = si.getHardware(); |
||||
|
||||
logger.info("Checking Memory..."); |
||||
printMemory(hal.getMemory()); |
||||
|
||||
|
||||
logger.info("Checking CPU..."); |
||||
printCpu(si); |
||||
|
||||
} |
||||
|
||||
|
||||
|
||||
private static void printMemory(GlobalMemory memory) { |
||||
|
||||
logger.info("memory avail:{} MB" , memory.getAvailable() / 1024 / 1024 );//memory avail:6863 MB
|
||||
logger.info("memory total:{} MB" , memory.getTotal() / 1024 / 1024 );//memory total:16384 MB
|
||||
} |
||||
|
||||
|
||||
private static void printCpu(SystemInfo si) { |
||||
CentralProcessor processor = si.getHardware().getProcessor(); |
||||
long[] systemCpuLoadTicks = processor.getSystemCpuLoadTicks(); |
||||
Util.sleep(1000); |
||||
logger.info(String.format("CPU load: %.1f%% (OS MXBean)%n", processor.getSystemCpuLoadBetweenTicks(systemCpuLoadTicks) * 100));//CPU load: 24.9% (OS MXBean)
|
||||
logger.info("CPU load averages : {}", processor.getSystemLoadAverage(1)[0]);//CPU load averages : 1.5234375
|
||||
|
||||
|
||||
logger.info("Uptime: " + FormatUtil.formatElapsedSecs(si.getOperatingSystem().getSystemUptime())); |
||||
logger.info("Context Switches/Interrupts: " + processor.getContextSwitches() + " / " + processor.getInterrupts()); |
||||
|
||||
|
||||
long[] prevTicks = processor.getSystemCpuLoadTicks(); |
||||
logger.info("CPU, IOWait, and IRQ ticks @ 0 sec:" + Arrays.toString(prevTicks)); |
||||
//Wait a second...
|
||||
Util.sleep(1000); |
||||
long[] ticks = processor.getSystemCpuLoadTicks(); |
||||
logger.info("CPU, IOWait, and IRQ ticks @ 1 sec:" + Arrays.toString(ticks)); |
||||
long user = ticks[TickType.USER.getIndex()] - prevTicks[TickType.USER.getIndex()]; |
||||
long nice = ticks[TickType.NICE.getIndex()] - prevTicks[TickType.NICE.getIndex()]; |
||||
long sys = ticks[TickType.SYSTEM.getIndex()] - prevTicks[TickType.SYSTEM.getIndex()]; |
||||
long idle = ticks[TickType.IDLE.getIndex()] - prevTicks[TickType.IDLE.getIndex()]; |
||||
long iowait = ticks[TickType.IOWAIT.getIndex()] - prevTicks[TickType.IOWAIT.getIndex()]; |
||||
long irq = ticks[TickType.IRQ.getIndex()] - prevTicks[TickType.IRQ.getIndex()]; |
||||
long softirq = ticks[TickType.SOFTIRQ.getIndex()] - prevTicks[TickType.SOFTIRQ.getIndex()]; |
||||
long steal = ticks[TickType.STEAL.getIndex()] - prevTicks[TickType.STEAL.getIndex()]; |
||||
long totalCpu = user + nice + sys + idle + iowait + irq + softirq + steal; |
||||
|
||||
logger.info(String.format( |
||||
"User: %.1f%% Nice: %.1f%% System: %.1f%% Idle: %.1f%% IOwait: %.1f%% IRQ: %.1f%% SoftIRQ: %.1f%% Steal: %.1f%%%n", |
||||
100d * user / totalCpu, 100d * nice / totalCpu, 100d * sys / totalCpu, 100d * idle / totalCpu, |
||||
100d * iowait / totalCpu, 100d * irq / totalCpu, 100d * softirq / totalCpu, 100d * steal / totalCpu)); |
||||
logger.info(String.format("CPU load: %.1f%% (counting ticks)%n", processor.getSystemCpuLoadBetweenTicks(prevTicks) * 100)); |
||||
|
||||
|
||||
|
||||
double[] loadAverage = processor.getSystemLoadAverage(3); |
||||
logger.info("CPU load averages:" + (loadAverage[0] < 0 ? " N/A" : String.format(" %.2f", loadAverage[0])) |
||||
+ (loadAverage[1] < 0 ? " N/A" : String.format(" %.2f", loadAverage[1])) |
||||
+ (loadAverage[2] < 0 ? " N/A" : String.format(" %.2f", loadAverage[2]))); |
||||
// per core CPU
|
||||
StringBuilder procCpu = new StringBuilder("CPU load per processor:"); |
||||
long[][] processorCpuLoadTicks = processor.getProcessorCpuLoadTicks(); |
||||
Util.sleep(1000); |
||||
double[] load = processor.getProcessorCpuLoadBetweenTicks(processorCpuLoadTicks); |
||||
for (double avg : load) { |
||||
procCpu.append(String.format(" %.1f%%", avg * 100)); |
||||
} |
||||
logger.info(procCpu.toString()); |
||||
} |
||||
} |
Loading…
Reference in new issue