Search in sources :

Example 1 with ObjectUtils.firstNonNull

use of org.apache.commons.lang3.ObjectUtils.firstNonNull in project dhis2-core by dhis2.

the class DefaultExpressionService method generateExpression.

/**
     * Generates an expression based on the given data maps.
     * 
     * @param expression the expression.
     * @param valueMap the value map.
     * @param constantMap the constant map.
     * @param orgUnitCountMap the organisation unit count map.
     * @param days the number of days.
     * @param missingValueStrategy the missing value strategy.
     * @param aggregateMap the aggregate map.
     * @return an expression.
     */
private String generateExpression(String expression, Map<? extends DimensionalItemObject, Double> valueMap, Map<String, Double> constantMap, Map<String, Integer> orgUnitCountMap, Integer days, MissingValueStrategy missingValueStrategy, Map<String, List<Double>> aggregateMap) {
    if (expression == null || expression.isEmpty()) {
        return null;
    }
    expression = ExpressionUtils.normalizeExpression(expression);
    Map<String, Double> dimensionItemValueMap = valueMap.entrySet().stream().filter(e -> e.getValue() != null).collect(Collectors.toMap(e -> e.getKey().getDimensionItem(), e -> e.getValue()));
    missingValueStrategy = ObjectUtils.firstNonNull(missingValueStrategy, NEVER_SKIP);
    // ---------------------------------------------------------------------
    // Aggregates
    // ---------------------------------------------------------------------
    StringBuffer sb = new StringBuffer();
    Pattern prefix = CustomFunctions.AGGREGATE_PATTERN_PREFIX;
    Matcher matcher = prefix.matcher(expression);
    int scan = 0, len = expression.length(), tail = 0;
    while (scan < len && matcher.find(scan)) {
        int start = matcher.end();
        int end = Expression.matchExpression(expression, start);
        if (end < 0) {
            sb.append(expression.substring(scan, start));
            scan = start + 1;
            tail = start;
        } else if (aggregateMap == null || expression.charAt(start) == '<') {
            sb.append(expression.substring(scan, end));
            scan = end + 1;
            tail = end;
        } else {
            String subExpression = expression.substring(start, end);
            List<Double> samples = aggregateMap.get(subExpression);
            if (samples == null) {
                if (SKIP_IF_ANY_VALUE_MISSING.equals(missingValueStrategy)) {
                    return null;
                }
            } else {
                String literal = (samples == null) ? ("[]") : (samples.toString());
                sb.append(expression.substring(scan, start));
                sb.append(literal);
            }
            scan = end;
            tail = end;
        }
    }
    sb.append(expression.substring(tail));
    expression = sb.toString();
    // ---------------------------------------------------------------------
    // DimensionalItemObjects
    // ---------------------------------------------------------------------
    sb = new StringBuffer();
    matcher = VARIABLE_PATTERN.matcher(expression);
    int matchCount = 0;
    int valueCount = 0;
    while (matcher.find()) {
        matchCount++;
        String dimItem = matcher.group(GROUP_ID);
        final Double value = dimensionItemValueMap.get(dimItem);
        boolean missingValue = value == null;
        if (missingValue && SKIP_IF_ANY_VALUE_MISSING.equals(missingValueStrategy)) {
            return null;
        }
        if (!missingValue) {
            valueCount++;
        }
        String replacement = value != null ? String.valueOf(value) : NULL_REPLACEMENT;
        matcher.appendReplacement(sb, Matcher.quoteReplacement(replacement));
    }
    if (SKIP_IF_ALL_VALUES_MISSING.equals(missingValueStrategy) && matchCount > 0 && valueCount == 0) {
        return null;
    }
    expression = TextUtils.appendTail(matcher, sb);
    // ---------------------------------------------------------------------
    // Constants
    // ---------------------------------------------------------------------
    sb = new StringBuffer();
    matcher = CONSTANT_PATTERN.matcher(expression);
    while (matcher.find()) {
        final Double constant = constantMap != null ? constantMap.get(matcher.group(GROUP_ID)) : null;
        String replacement = constant != null ? String.valueOf(constant) : NULL_REPLACEMENT;
        matcher.appendReplacement(sb, replacement);
    }
    expression = TextUtils.appendTail(matcher, sb);
    // ---------------------------------------------------------------------
    // Org unit groups
    // ---------------------------------------------------------------------
    sb = new StringBuffer();
    matcher = OU_GROUP_PATTERN.matcher(expression);
    while (matcher.find()) {
        final Integer count = orgUnitCountMap != null ? orgUnitCountMap.get(matcher.group(GROUP_ID)) : null;
        String replacement = count != null ? String.valueOf(count) : NULL_REPLACEMENT;
        matcher.appendReplacement(sb, replacement);
    }
    expression = TextUtils.appendTail(matcher, sb);
    // ---------------------------------------------------------------------
    // Days
    // ---------------------------------------------------------------------
    sb = new StringBuffer();
    matcher = DAYS_PATTERN.matcher(expression);
    while (matcher.find()) {
        String replacement = days != null ? String.valueOf(days) : NULL_REPLACEMENT;
        matcher.appendReplacement(sb, replacement);
    }
    return TextUtils.appendTail(matcher, sb);
}
Also used : ListMap(org.hisp.dhis.common.ListMap) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) DataElementService(org.hisp.dhis.dataelement.DataElementService) DimensionService(org.hisp.dhis.common.DimensionService) CustomFunctions(org.hisp.dhis.system.jep.CustomFunctions) StringUtils(org.apache.commons.lang3.StringUtils) Function(java.util.function.Function) DataElement(org.hisp.dhis.dataelement.DataElement) HashSet(java.util.HashSet) GenericStore(org.hisp.dhis.common.GenericStore) Matcher(java.util.regex.Matcher) ExpressionUtils(org.hisp.dhis.system.util.ExpressionUtils) MissingValueStrategy(org.hisp.dhis.expression.MissingValueStrategy) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) ObjectUtils(org.apache.commons.lang3.ObjectUtils) Map(java.util.Map) IndicatorValue(org.hisp.dhis.indicator.IndicatorValue) Indicator(org.hisp.dhis.indicator.Indicator) Constant(org.hisp.dhis.constant.Constant) DataElementCategoryService(org.hisp.dhis.dataelement.DataElementCategoryService) Period(org.hisp.dhis.period.Period) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) OrganisationUnitGroupService(org.hisp.dhis.organisationunit.OrganisationUnitGroupService) OrganisationUnitGroup(org.hisp.dhis.organisationunit.OrganisationUnitGroup) Collection(java.util.Collection) Set(java.util.Set) ConstantService(org.hisp.dhis.constant.ConstantService) DateUtils(org.hisp.dhis.system.util.DateUtils) InvalidIdentifierReferenceException(org.hisp.dhis.common.exception.InvalidIdentifierReferenceException) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) SetMap(org.hisp.dhis.common.SetMap) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) List(java.util.List) CachingMap(org.hisp.dhis.commons.collection.CachingMap) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) Pattern(java.util.regex.Pattern) MathUtils(org.hisp.dhis.system.util.MathUtils) TextUtils(org.hisp.dhis.commons.util.TextUtils) Transactional(org.springframework.transaction.annotation.Transactional) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) List(java.util.List)

Example 2 with ObjectUtils.firstNonNull

use of org.apache.commons.lang3.ObjectUtils.firstNonNull in project hmftools by hartwigmedical.

the class BreakPointInspectorApplication method main.

public static void main(final String... args) throws IOException {
    final AnalysisBuilder analysisBuilder = new AnalysisBuilder();
    final Options options = createOptions();
    try {
        final CommandLine cmd = createCommandLine(options, args);
        final String refPath = cmd.getOptionValue(REF_PATH);
        final String refSlicePath = cmd.getOptionValue(REF_SLICE);
        final String tumorPath = cmd.getOptionValue(TUMOR_PATH);
        final String tumorSlicePath = cmd.getOptionValue(TUMOR_SLICE);
        final String vcfPath = cmd.getOptionValue(VCF);
        if (cmd.hasOption(PROXIMITY)) {
            analysisBuilder.setRange(Integer.parseInt(cmd.getOptionValue(PROXIMITY, "500")));
        }
        if (cmd.hasOption(CONTAMINATION)) {
            analysisBuilder.setContaminationFraction(Float.parseFloat(cmd.getOptionValue(CONTAMINATION, "0")));
        }
        if (refPath == null || tumorPath == null || vcfPath == null) {
            printHelpAndExit(options);
            return;
        }
        final File tumorBAM = new File(tumorPath);
        final SamReader tumorReader = SamReaderFactory.makeDefault().open(tumorBAM);
        final File refBAM = new File(refPath);
        final SamReader refReader = SamReaderFactory.makeDefault().open(refBAM);
        final File vcfFile = new File(vcfPath);
        final VCFFileReader vcfReader = new VCFFileReader(vcfFile, false);
        final List<String> samples = vcfReader.getFileHeader().getGenotypeSamples();
        if (samples.size() != 2) {
            System.err.println("could not determine tumor and sample from VCF");
            System.exit(1);
            return;
        }
        TSVOutput.PrintHeaders();
        final Analysis analysis = analysisBuilder.setRefReader(refReader).setTumorReader(tumorReader).createAnalysis();
        final List<QueryInterval> combinedQueryIntervals = Lists.newArrayList();
        final Map<String, VariantContext> variantMap = new HashMap<>();
        final List<VariantContext> variants = Lists.newArrayList();
        for (VariantContext variant : vcfReader) {
            variantMap.put(variant.getID(), variant);
            final VariantContext mateVariant = variant;
            if (variant.hasAttribute("MATEID")) {
                variant = variantMap.get(variant.getAttributeAsString("MATEID", ""));
                if (variant == null) {
                    continue;
                }
            }
            final String location = variant.getContig() + ":" + Integer.toString(variant.getStart());
            final Location location1 = Location.parseLocationString(location, tumorReader.getFileHeader().getSequenceDictionary());
            final Range uncertainty1 = extractCIPOS(variant);
            final List<Integer> CIEND = variant.getAttributeAsIntList("CIEND", 0);
            Range uncertainty2 = CIEND.size() == 2 ? new Range(CIEND.get(0), CIEND.get(1)) : null;
            final boolean IMPRECISE = variant.hasAttribute("IMPRECISE");
            HMFVariantType svType;
            final Location location2;
            switch(variant.getStructuralVariantType()) {
                case INS:
                    svType = HMFVariantType.INS;
                    location2 = location1.set(variant.getAttributeAsInt("END", 0));
                    break;
                case INV:
                    if (variant.hasAttribute("INV3")) {
                        svType = HMFVariantType.INV3;
                    } else if (variant.hasAttribute("INV5")) {
                        svType = HMFVariantType.INV5;
                    } else {
                        System.err.println(variant.getID() + " : expected either INV3 or INV5 flag");
                        continue;
                    }
                    location2 = location1.add(Math.abs(variant.getAttributeAsInt("SVLEN", 0)));
                    break;
                case DEL:
                    svType = HMFVariantType.DEL;
                    location2 = location1.add(Math.abs(variant.getAttributeAsInt("SVLEN", 0)));
                    break;
                case DUP:
                    svType = HMFVariantType.DUP;
                    location2 = location1.add(Math.abs(variant.getAttributeAsInt("SVLEN", 0)));
                    break;
                case BND:
                    // process the breakend string
                    final String call = variant.getAlternateAllele(0).getDisplayString();
                    final String[] leftSplit = call.split("\\]");
                    final String[] rightSplit = call.split("\\[");
                    if (leftSplit.length >= 2) {
                        location2 = Location.parseLocationString(leftSplit[1], tumorReader.getFileHeader().getSequenceDictionary());
                        if (leftSplit[0].length() > 0) {
                            svType = HMFVariantType.INV3;
                            uncertainty2 = Range.invert(uncertainty1);
                        } else {
                            svType = HMFVariantType.DUP;
                            uncertainty2 = uncertainty1;
                        }
                    } else if (rightSplit.length >= 2) {
                        location2 = Location.parseLocationString(rightSplit[1], tumorReader.getFileHeader().getSequenceDictionary());
                        if (rightSplit[0].length() > 0) {
                            svType = HMFVariantType.DEL;
                            uncertainty2 = uncertainty1;
                        } else {
                            svType = HMFVariantType.INV5;
                            uncertainty2 = Range.invert(uncertainty1);
                        }
                    } else {
                        System.err.println(variant.getID() + " : could not parse breakpoint");
                        continue;
                    }
                    if (IMPRECISE) {
                        uncertainty2 = extractCIPOS(mateVariant);
                    }
                    break;
                default:
                    System.err.println(variant.getID() + " : UNEXPECTED SVTYPE=" + variant.getStructuralVariantType());
                    continue;
            }
            final HMFVariantContext ctx = new HMFVariantContext(variant.getID(), location1, location2, svType, IMPRECISE);
            ctx.Filter.addAll(variant.getFilters().stream().filter(s -> !s.startsWith("BPI")).collect(Collectors.toSet()));
            ctx.Uncertainty1 = uncertainty1;
            ctx.Uncertainty2 = ObjectUtils.firstNonNull(uncertainty2, fixup(uncertainty1, IMPRECISE, svType == HMFVariantType.INV3 || svType == HMFVariantType.INV5));
            ctx.HomologySequence = variant.getAttributeAsString("HOMSEQ", "");
            if (variant.hasAttribute("LEFT_SVINSSEQ") && variant.hasAttribute("RIGHT_SVINSSEQ")) {
                ctx.InsertSequence = variant.getAttributeAsString("LEFT_SVINSSEQ", "") + "..." + variant.getAttributeAsString("RIGHT_SVINSSEQ", "");
            } else {
                ctx.InsertSequence = variant.getAttributeAsString("SVINSSEQ", "");
            }
            ctx.BND = variant.getStructuralVariantType() == StructuralVariantType.BND;
            switch(ctx.Type) {
                case INS:
                case DEL:
                    ctx.OrientationBP1 = 1;
                    ctx.OrientationBP2 = -1;
                    break;
                case INV3:
                    ctx.OrientationBP1 = 1;
                    ctx.OrientationBP2 = 1;
                    break;
                case INV5:
                    ctx.OrientationBP1 = -1;
                    ctx.OrientationBP2 = -1;
                    break;
                case DUP:
                    ctx.OrientationBP1 = -1;
                    ctx.OrientationBP2 = 1;
                    break;
            }
            final StructuralVariantResult result = analysis.processStructuralVariant(ctx);
            combinedQueryIntervals.addAll(asList(result.QueryIntervals));
            TSVOutput.print(variant, ctx, result);
            final BiConsumer<VariantContext, Boolean> vcfUpdater = (v, swap) -> {
                final Set<String> filters = v.getCommonInfo().getFiltersMaybeNull();
                if (filters != null) {
                    filters.clear();
                }
                // we will map BreakpointError to a flag
                if (result.Filters.contains(Filter.Filters.BreakpointError.toString())) {
                    v.getCommonInfo().putAttribute("BPI_AMBIGUOUS", true, true);
                } else {
                    v.getCommonInfo().addFilters(result.Filters);
                }
                if (result.Filters.isEmpty()) {
                    final List<Double> af = asList(result.AlleleFrequency.getLeft(), result.AlleleFrequency.getRight());
                    v.getCommonInfo().putAttribute(AlleleFrequency.VCF_INFO_TAG, swap ? Lists.reverse(af) : af, true);
                }
                if (result.Breakpoints.getLeft() != null) {
                    v.getCommonInfo().putAttribute(swap ? "BPI_END" : "BPI_START", result.Breakpoints.getLeft().Position, true);
                }
                if (result.Breakpoints.getRight() != null) {
                    v.getCommonInfo().putAttribute(swap ? "BPI_START" : "BPI_END", result.Breakpoints.getRight().Position, true);
                }
                // remove CIPOS / CIEND when we have an insert sequence
                if (!v.hasAttribute("IMPRECISE") && v.hasAttribute("SVINSSEQ")) {
                    v.getCommonInfo().removeAttribute("CIPOS");
                    v.getCommonInfo().removeAttribute("CIEND");
                }
                variants.add(v);
            };
            vcfUpdater.accept(variant, false);
            if (mateVariant != variant) {
                vcfUpdater.accept(mateVariant, true);
            }
        }
        // TODO: update START, END with BPI values and save Manta values in new attributes
        final String vcfOutputPath = cmd.getOptionValue(VCF_OUT);
        if (vcfOutputPath != null) {
            final VCFHeader header = vcfReader.getFileHeader();
            header.addMetaDataLine(new VCFInfoHeaderLine("BPI_START", 1, VCFHeaderLineType.Integer, "BPI adjusted breakend location"));
            header.addMetaDataLine(new VCFInfoHeaderLine("BPI_END", 1, VCFHeaderLineType.Integer, "BPI adjusted breakend location"));
            header.addMetaDataLine(new VCFInfoHeaderLine("BPI_AMBIGUOUS", 0, VCFHeaderLineType.Flag, "BPI could not determine the breakpoints, inspect manually"));
            header.addMetaDataLine(new VCFHeaderLine("bpiVersion", BreakPointInspectorApplication.class.getPackage().getImplementationVersion()));
            Filter.UpdateVCFHeader(header);
            AlleleFrequency.UpdateVCFHeader(header);
            // setup VCF
            final VariantContextWriter writer = new VariantContextWriterBuilder().setReferenceDictionary(header.getSequenceDictionary()).setOutputFile(vcfOutputPath).build();
            writer.writeHeader(header);
            // write variants
            variants.sort(new VariantContextComparator(header.getSequenceDictionary()));
            variants.forEach(writer::add);
            writer.close();
        }
        final QueryInterval[] optimizedIntervals = QueryInterval.optimizeIntervals(combinedQueryIntervals.toArray(new QueryInterval[combinedQueryIntervals.size()]));
        if (tumorSlicePath != null) {
            writeToSlice(tumorSlicePath, tumorReader, optimizedIntervals);
        }
        if (refSlicePath != null) {
            writeToSlice(refSlicePath, refReader, optimizedIntervals);
        }
        refReader.close();
        tumorReader.close();
    } catch (ParseException e) {
        printHelpAndExit(options);
        System.exit(1);
    }
}
Also used : VCFHeaderLine(htsjdk.variant.vcf.VCFHeaderLine) VCFFileReader(htsjdk.variant.vcf.VCFFileReader) VCFHeader(htsjdk.variant.vcf.VCFHeader) Options(org.apache.commons.cli.Options) HashMap(java.util.HashMap) VariantContextWriterBuilder(htsjdk.variant.variantcontext.writer.VariantContextWriterBuilder) HelpFormatter(org.apache.commons.cli.HelpFormatter) DefaultParser(org.apache.commons.cli.DefaultParser) Lists(com.google.common.collect.Lists) Arrays.asList(java.util.Arrays.asList) ObjectUtils(org.apache.commons.lang3.ObjectUtils) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) CommandLine(org.apache.commons.cli.CommandLine) Option(org.apache.commons.cli.Option) VCFHeaderLineType(htsjdk.variant.vcf.VCFHeaderLineType) SAMFileWriterFactory(htsjdk.samtools.SAMFileWriterFactory) CommandLineParser(org.apache.commons.cli.CommandLineParser) SAMRecordIterator(htsjdk.samtools.SAMRecordIterator) Set(java.util.Set) IOException(java.io.IOException) SAMFileWriter(htsjdk.samtools.SAMFileWriter) SamReader(htsjdk.samtools.SamReader) Collectors(java.util.stream.Collectors) File(java.io.File) VariantContextComparator(htsjdk.variant.variantcontext.VariantContextComparator) List(java.util.List) QueryInterval(htsjdk.samtools.QueryInterval) ParseException(org.apache.commons.cli.ParseException) StructuralVariantType(htsjdk.variant.variantcontext.StructuralVariantType) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) VCFInfoHeaderLine(htsjdk.variant.vcf.VCFInfoHeaderLine) VariantContext(htsjdk.variant.variantcontext.VariantContext) NotNull(org.jetbrains.annotations.NotNull) SamReaderFactory(htsjdk.samtools.SamReaderFactory) Options(org.apache.commons.cli.Options) VCFHeaderLine(htsjdk.variant.vcf.VCFHeaderLine) Set(java.util.Set) HashMap(java.util.HashMap) VCFFileReader(htsjdk.variant.vcf.VCFFileReader) VariantContext(htsjdk.variant.variantcontext.VariantContext) QueryInterval(htsjdk.samtools.QueryInterval) VariantContextComparator(htsjdk.variant.variantcontext.VariantContextComparator) SamReader(htsjdk.samtools.SamReader) Arrays.asList(java.util.Arrays.asList) List(java.util.List) VariantContextWriter(htsjdk.variant.variantcontext.writer.VariantContextWriter) VCFHeader(htsjdk.variant.vcf.VCFHeader) VCFInfoHeaderLine(htsjdk.variant.vcf.VCFInfoHeaderLine) CommandLine(org.apache.commons.cli.CommandLine) VariantContextWriterBuilder(htsjdk.variant.variantcontext.writer.VariantContextWriterBuilder) ParseException(org.apache.commons.cli.ParseException) File(java.io.File)

Example 3 with ObjectUtils.firstNonNull

use of org.apache.commons.lang3.ObjectUtils.firstNonNull in project dhis2-core by dhis2.

the class JdbcRawAnalyticsManager method getSelectStatement.

// -------------------------------------------------------------------------
// Supportive methods
// -------------------------------------------------------------------------
/**
 * Returns a SQL select statement.
 *
 * @param params the data query parameters.
 * @param dimensions the list of dimensions.
 * @return a SQL select statement.
 */
private String getSelectStatement(DataQueryParams params, List<DimensionalObject> dimensions) {
    String idScheme = ObjectUtils.firstNonNull(params.getOutputIdScheme(), IdScheme.UID).getIdentifiableString().toLowerCase();
    List<String> dimensionColumns = dimensions.stream().map(d -> asColumnSelect(d, idScheme)).collect(Collectors.toList());
    SqlHelper sqlHelper = new SqlHelper();
    String sql = "select " + StringUtils.join(dimensionColumns, ", ") + ", " + DIM_NAME_OU + ", value " + "from " + params.getTableName() + " as " + ANALYTICS_TBL_ALIAS + " " + "inner join organisationunit ou on ax.ou = ou.uid " + "inner join _orgunitstructure ous on ax.ou = ous.organisationunituid " + "inner join _periodstructure ps on ax.pe = ps.iso ";
    for (DimensionalObject dim : dimensions) {
        if (!dim.getItems().isEmpty() && !dim.isFixed()) {
            String col = quote(dim.getDimensionName());
            if (DimensionalObject.ORGUNIT_DIM_ID.equals(dim.getDimension())) {
                sql += sqlHelper.whereAnd() + " (";
                for (DimensionalItemObject item : dim.getItems()) {
                    OrganisationUnit unit = (OrganisationUnit) item;
                    sql += DIM_NAME_OU + " like '" + unit.getPath() + "%' or ";
                }
                sql = TextUtils.removeLastOr(sql) + ") ";
            } else {
                sql += sqlHelper.whereAnd() + " " + col + " in (" + getQuotedCommaDelimitedString(getUids(dim.getItems())) + ") ";
            }
        }
    }
    sql += sqlHelper.whereAnd() + " " + "ps.startdate >= '" + DateUtils.getMediumDateString(params.getStartDate()) + "' and " + "ps.enddate <= '" + DateUtils.getMediumDateString(params.getEndDate()) + "' ";
    return sql;
}
Also used : org.hisp.dhis.common(org.hisp.dhis.common) AnalyticsSqlUtils.quote(org.hisp.dhis.analytics.util.AnalyticsSqlUtils.quote) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) RawAnalyticsManager(org.hisp.dhis.analytics.RawAnalyticsManager) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) AnalyticsUtils(org.hisp.dhis.analytics.util.AnalyticsUtils) ArrayList(java.util.ArrayList) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ObjectUtils(org.hisp.dhis.util.ObjectUtils) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) IdentifiableObjectUtils.getUids(org.hisp.dhis.common.IdentifiableObjectUtils.getUids) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) ANALYTICS_TBL_ALIAS(org.hisp.dhis.analytics.util.AnalyticsSqlUtils.ANALYTICS_TBL_ALIAS) Qualifier(org.springframework.beans.factory.annotation.Qualifier) SqlRowSet(org.springframework.jdbc.support.rowset.SqlRowSet) TextUtils.getQuotedCommaDelimitedString(org.hisp.dhis.commons.util.TextUtils.getQuotedCommaDelimitedString) SqlHelper(org.hisp.dhis.commons.util.SqlHelper) DateUtils(org.hisp.dhis.util.DateUtils) TextUtils(org.hisp.dhis.commons.util.TextUtils) Assert(org.springframework.util.Assert) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) SqlHelper(org.hisp.dhis.commons.util.SqlHelper) TextUtils.getQuotedCommaDelimitedString(org.hisp.dhis.commons.util.TextUtils.getQuotedCommaDelimitedString)

Example 4 with ObjectUtils.firstNonNull

use of org.apache.commons.lang3.ObjectUtils.firstNonNull in project alf.io by alfio-event.

the class EventManager method updateEventHeader.

public void updateEventHeader(Event original, EventModification em, String username) {
    IntPredicate ownershipChecker = checkOwnershipByOrgId(username, organizationRepository);
    boolean sameOrganization = original.getOrganizationId() == em.getOrganizationId();
    Validate.isTrue(ownershipChecker.test(original.getOrganizationId()) && (sameOrganization || ownershipChecker.test(em.getOrganizationId())), "Invalid organizationId");
    int eventId = original.getId();
    Validate.isTrue(sameOrganization || groupRepository.countByEventId(eventId) == 0, "Cannot change organization because there is a group linked to this event.");
    Validate.isTrue(sameOrganization || !subscriptionRepository.hasLinkedSubscription(eventId), "Cannot change organization because there are one or more subscriptions linked.");
    boolean formatUpdated = em.getFormat() != original.getFormat();
    if (em.getFormat() == EventFormat.ONLINE && formatUpdated) {
        Validate.isTrue(original.getAllowedPaymentProxies().stream().allMatch(p -> p != PaymentProxy.ON_SITE), ERROR_ONLINE_ON_SITE_NOT_COMPATIBLE);
    }
    String timeZone = ObjectUtils.firstNonNull(em.getZoneId(), em.getGeolocation() != null ? em.getGeolocation().getTimeZone() : null);
    String latitude = ObjectUtils.firstNonNull(em.getLatitude(), em.getGeolocation() != null ? em.getGeolocation().getLatitude() : null);
    String longitude = ObjectUtils.firstNonNull(em.getLongitude(), em.getGeolocation() != null ? em.getGeolocation().getLongitude() : null);
    final ZoneId zoneId = ZoneId.of(timeZone);
    final ZonedDateTime begin = em.getBegin().toZonedDateTime(zoneId);
    final ZonedDateTime end = em.getEnd().toZonedDateTime(zoneId);
    eventRepository.updateHeader(eventId, em.getDisplayName(), em.getWebsiteUrl(), em.getExternalUrl(), em.getTermsAndConditionsUrl(), em.getPrivacyPolicyUrl(), em.getImageUrl(), em.getFileBlobId(), em.getLocation(), latitude, longitude, begin, end, timeZone, em.getOrganizationId(), em.getLocales(), em.getFormat());
    createOrUpdateEventDescription(eventId, em);
    if (!original.getBegin().equals(begin) || !original.getEnd().equals(end)) {
        fixOutOfRangeCategories(em, username, zoneId, end);
    }
    if (formatUpdated) {
        // update ticket access type for categories if the format has been updated
        var ticketAccessType = evaluateTicketAccessType(original.getFormat(), em.getFormat());
        ticketCategoryRepository.updateTicketAccessTypeForEvent(eventId, ticketAccessType);
    }
    extensionManager.handleEventHeaderUpdate(eventRepository.findById(eventId), organizationRepository.findOrganizationForUser(username, em.getOrganizationId()).orElseThrow());
}
Also used : AdditionalField(alfio.model.modification.EventModification.AdditionalField) alfio.repository(alfio.repository) PaymentProxy(alfio.model.transaction.PaymentProxy) AffectedRowCountAndKey(ch.digitalfondue.npjt.AffectedRowCountAndKey) DiscountType(alfio.model.PromoCodeDiscount.DiscountType) ZonedDateTime(java.time.ZonedDateTime) NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) AlfioMetadata(alfio.model.metadata.AlfioMetadata) TicketStatus(alfio.model.Ticket.TicketStatus) StringUtils(org.apache.commons.lang3.StringUtils) IntPredicate(java.util.function.IntPredicate) Collections.singletonList(java.util.Collections.singletonList) BigDecimal(java.math.BigDecimal) Json(alfio.util.Json) ErrorCode(alfio.model.result.ErrorCode) Arrays.asList(java.util.Arrays.asList) Profiles(org.springframework.core.env.Profiles) Triple(org.apache.commons.lang3.tuple.Triple) OrganizationRepository(alfio.repository.user.OrganizationRepository) RequestUtils(alfio.util.RequestUtils) Organization(alfio.model.user.Organization) Predicate(java.util.function.Predicate) CheckInOutputColorConfiguration(alfio.model.support.CheckInOutputColorConfiguration) EventCreationRequest(alfio.model.api.v1.admin.EventCreationRequest) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) String.format(java.lang.String.format) ConfigurationRepository(alfio.repository.system.ConfigurationRepository) SearchOptions(alfio.controller.form.SearchOptions) ColorConfiguration(alfio.model.support.CheckInOutputColorConfiguration.ColorConfiguration) Initializer(alfio.config.Initializer) Principal(java.security.Principal) Stream(java.util.stream.Stream) alfio.model(alfio.model) Environment(org.springframework.core.env.Environment) DateTimeModification.atZone(alfio.model.modification.DateTimeModification.atZone) ClockProvider(alfio.util.ClockProvider) alfio.model.modification(alfio.model.modification) IntStream(java.util.stream.IntStream) java.util(java.util) EventFormat(alfio.model.Event.EventFormat) ONCE_PER_EVENT(alfio.model.TicketCategory.TicketCheckInStrategy.ONCE_PER_EVENT) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) ConfigurationManager(alfio.manager.system.ConfigurationManager) Function(java.util.function.Function) CollectionUtils(org.apache.commons.collections4.CollectionUtils) Objects.requireNonNullElse(java.util.Objects.requireNonNullElse) IterableUtils(org.apache.commons.collections4.IterableUtils) TicketAccessType(alfio.model.TicketCategory.TicketAccessType) ObjectUtils(org.apache.commons.lang3.ObjectUtils) Context(alfio.model.TicketFieldConfiguration.Context) Objects.requireNonNullElseGet(java.util.Objects.requireNonNullElseGet) Result(alfio.model.result.Result) Consumer(java.util.function.Consumer) Component(org.springframework.stereotype.Component) Validate(org.apache.commons.lang3.Validate) ChronoUnit(java.time.temporal.ChronoUnit) CategoryEvaluator(alfio.manager.support.CategoryEvaluator) MonetaryUtil(alfio.util.MonetaryUtil) ExtensionCapability(alfio.manager.support.extension.ExtensionCapability) UserManager(alfio.manager.user.UserManager) Log4j2(lombok.extern.log4j.Log4j2) EventUtil(alfio.util.EventUtil) Flyway(org.flywaydb.core.Flyway) Wrappers.optionally(alfio.util.Wrappers.optionally) AllArgsConstructor(lombok.AllArgsConstructor) ConfigurationKeys(alfio.model.system.ConfigurationKeys) CHECK_IN_COLOR_CONFIGURATION(alfio.model.system.ConfigurationKeys.CHECK_IN_COLOR_CONFIGURATION) Transactional(org.springframework.transaction.annotation.Transactional) Assert(org.springframework.util.Assert) ZoneId(java.time.ZoneId) IntPredicate(java.util.function.IntPredicate) ZonedDateTime(java.time.ZonedDateTime)

Example 5 with ObjectUtils.firstNonNull

use of org.apache.commons.lang3.ObjectUtils.firstNonNull in project java by wavefrontHQ.

the class ProxyConfig method verifyAndInit.

@Override
public void verifyAndInit() {
    if (unparsed_params != null) {
        logger.info("Unparsed arguments: " + Joiner.on(", ").join(unparsed_params));
    }
    ReportableConfig config;
    // If they've specified a push configuration file, override the command line values
    try {
        if (pushConfigFile != null) {
            config = new ReportableConfig(pushConfigFile);
        } else {
            // dummy config
            config = new ReportableConfig();
        }
        prefix = Strings.emptyToNull(config.getString("prefix", prefix));
        // don't track token in proxy config metrics
        token = ObjectUtils.firstNonNull(config.getRawProperty("token", token), "undefined").trim();
        server = config.getString("server", server);
        hostname = config.getString("hostname", hostname);
        idFile = config.getString("idFile", idFile);
        pushRateLimit = config.getInteger("pushRateLimit", pushRateLimit);
        pushRateLimitHistograms = config.getInteger("pushRateLimitHistograms", pushRateLimitHistograms);
        pushRateLimitSourceTags = config.getDouble("pushRateLimitSourceTags", pushRateLimitSourceTags);
        pushRateLimitSpans = config.getInteger("pushRateLimitSpans", pushRateLimitSpans);
        pushRateLimitSpanLogs = config.getInteger("pushRateLimitSpanLogs", pushRateLimitSpanLogs);
        pushRateLimitEvents = config.getDouble("pushRateLimitEvents", pushRateLimitEvents);
        pushRateLimitMaxBurstSeconds = config.getInteger("pushRateLimitMaxBurstSeconds", pushRateLimitMaxBurstSeconds);
        pushBlockedSamples = config.getInteger("pushBlockedSamples", pushBlockedSamples);
        blockedPointsLoggerName = config.getString("blockedPointsLoggerName", blockedPointsLoggerName);
        blockedHistogramsLoggerName = config.getString("blockedHistogramsLoggerName", blockedHistogramsLoggerName);
        blockedSpansLoggerName = config.getString("blockedSpansLoggerName", blockedSpansLoggerName);
        pushListenerPorts = config.getString("pushListenerPorts", pushListenerPorts);
        pushListenerMaxReceivedLength = config.getInteger("pushListenerMaxReceivedLength", pushListenerMaxReceivedLength);
        pushListenerHttpBufferSize = config.getInteger("pushListenerHttpBufferSize", pushListenerHttpBufferSize);
        traceListenerMaxReceivedLength = config.getInteger("traceListenerMaxReceivedLength", traceListenerMaxReceivedLength);
        traceListenerHttpBufferSize = config.getInteger("traceListenerHttpBufferSize", traceListenerHttpBufferSize);
        listenerIdleConnectionTimeout = config.getInteger("listenerIdleConnectionTimeout", listenerIdleConnectionTimeout);
        memGuardFlushThreshold = config.getInteger("memGuardFlushThreshold", memGuardFlushThreshold);
        // Histogram: global settings
        histogramPassthroughRecompression = config.getBoolean("histogramPassthroughRecompression", histogramPassthroughRecompression);
        histogramStateDirectory = config.getString("histogramStateDirectory", histogramStateDirectory);
        histogramAccumulatorResolveInterval = config.getLong("histogramAccumulatorResolveInterval", histogramAccumulatorResolveInterval);
        histogramAccumulatorFlushInterval = config.getLong("histogramAccumulatorFlushInterval", histogramAccumulatorFlushInterval);
        histogramAccumulatorFlushMaxBatchSize = config.getInteger("histogramAccumulatorFlushMaxBatchSize", histogramAccumulatorFlushMaxBatchSize);
        histogramMaxReceivedLength = config.getInteger("histogramMaxReceivedLength", histogramMaxReceivedLength);
        histogramHttpBufferSize = config.getInteger("histogramHttpBufferSize", histogramHttpBufferSize);
        deltaCountersAggregationListenerPorts = config.getString("deltaCountersAggregationListenerPorts", deltaCountersAggregationListenerPorts);
        deltaCountersAggregationIntervalSeconds = config.getLong("deltaCountersAggregationIntervalSeconds", deltaCountersAggregationIntervalSeconds);
        customTracingListenerPorts = config.getString("customTracingListenerPorts", customTracingListenerPorts);
        // Histogram: deprecated settings - fall back for backwards compatibility
        if (config.isDefined("avgHistogramKeyBytes")) {
            histogramMinuteAvgKeyBytes = histogramHourAvgKeyBytes = histogramDayAvgKeyBytes = histogramDistAvgKeyBytes = config.getInteger("avgHistogramKeyBytes", 150);
        }
        if (config.isDefined("avgHistogramDigestBytes")) {
            histogramMinuteAvgDigestBytes = histogramHourAvgDigestBytes = histogramDayAvgDigestBytes = histogramDistAvgDigestBytes = config.getInteger("avgHistogramDigestBytes", 500);
        }
        if (config.isDefined("histogramAccumulatorSize")) {
            histogramMinuteAccumulatorSize = histogramHourAccumulatorSize = histogramDayAccumulatorSize = histogramDistAccumulatorSize = config.getLong("histogramAccumulatorSize", 100000);
        }
        if (config.isDefined("histogramCompression")) {
            histogramMinuteCompression = histogramHourCompression = histogramDayCompression = histogramDistCompression = config.getNumber("histogramCompression", null, 20, 1000).shortValue();
        }
        if (config.isDefined("persistAccumulator")) {
            histogramMinuteAccumulatorPersisted = histogramHourAccumulatorPersisted = histogramDayAccumulatorPersisted = histogramDistAccumulatorPersisted = config.getBoolean("persistAccumulator", false);
        }
        // Histogram: minute accumulator settings
        histogramMinuteListenerPorts = config.getString("histogramMinuteListenerPorts", histogramMinuteListenerPorts);
        histogramMinuteFlushSecs = config.getInteger("histogramMinuteFlushSecs", histogramMinuteFlushSecs);
        histogramMinuteCompression = config.getNumber("histogramMinuteCompression", histogramMinuteCompression, 20, 1000).shortValue();
        histogramMinuteAvgKeyBytes = config.getInteger("histogramMinuteAvgKeyBytes", histogramMinuteAvgKeyBytes);
        histogramMinuteAvgDigestBytes = 32 + histogramMinuteCompression * 7;
        histogramMinuteAvgDigestBytes = config.getInteger("histogramMinuteAvgDigestBytes", histogramMinuteAvgDigestBytes);
        histogramMinuteAccumulatorSize = config.getLong("histogramMinuteAccumulatorSize", histogramMinuteAccumulatorSize);
        histogramMinuteAccumulatorPersisted = config.getBoolean("histogramMinuteAccumulatorPersisted", histogramMinuteAccumulatorPersisted);
        histogramMinuteMemoryCache = config.getBoolean("histogramMinuteMemoryCache", histogramMinuteMemoryCache);
        // Histogram: hour accumulator settings
        histogramHourListenerPorts = config.getString("histogramHourListenerPorts", histogramHourListenerPorts);
        histogramHourFlushSecs = config.getInteger("histogramHourFlushSecs", histogramHourFlushSecs);
        histogramHourCompression = config.getNumber("histogramHourCompression", histogramHourCompression, 20, 1000).shortValue();
        histogramHourAvgKeyBytes = config.getInteger("histogramHourAvgKeyBytes", histogramHourAvgKeyBytes);
        histogramHourAvgDigestBytes = 32 + histogramHourCompression * 7;
        histogramHourAvgDigestBytes = config.getInteger("histogramHourAvgDigestBytes", histogramHourAvgDigestBytes);
        histogramHourAccumulatorSize = config.getLong("histogramHourAccumulatorSize", histogramHourAccumulatorSize);
        histogramHourAccumulatorPersisted = config.getBoolean("histogramHourAccumulatorPersisted", histogramHourAccumulatorPersisted);
        histogramHourMemoryCache = config.getBoolean("histogramHourMemoryCache", histogramHourMemoryCache);
        // Histogram: day accumulator settings
        histogramDayListenerPorts = config.getString("histogramDayListenerPorts", histogramDayListenerPorts);
        histogramDayFlushSecs = config.getInteger("histogramDayFlushSecs", histogramDayFlushSecs);
        histogramDayCompression = config.getNumber("histogramDayCompression", histogramDayCompression, 20, 1000).shortValue();
        histogramDayAvgKeyBytes = config.getInteger("histogramDayAvgKeyBytes", histogramDayAvgKeyBytes);
        histogramDayAvgDigestBytes = 32 + histogramDayCompression * 7;
        histogramDayAvgDigestBytes = config.getInteger("histogramDayAvgDigestBytes", histogramDayAvgDigestBytes);
        histogramDayAccumulatorSize = config.getLong("histogramDayAccumulatorSize", histogramDayAccumulatorSize);
        histogramDayAccumulatorPersisted = config.getBoolean("histogramDayAccumulatorPersisted", histogramDayAccumulatorPersisted);
        histogramDayMemoryCache = config.getBoolean("histogramDayMemoryCache", histogramDayMemoryCache);
        // Histogram: dist accumulator settings
        histogramDistListenerPorts = config.getString("histogramDistListenerPorts", histogramDistListenerPorts);
        histogramDistFlushSecs = config.getInteger("histogramDistFlushSecs", histogramDistFlushSecs);
        histogramDistCompression = config.getNumber("histogramDistCompression", histogramDistCompression, 20, 1000).shortValue();
        histogramDistAvgKeyBytes = config.getInteger("histogramDistAvgKeyBytes", histogramDistAvgKeyBytes);
        histogramDistAvgDigestBytes = 32 + histogramDistCompression * 7;
        histogramDistAvgDigestBytes = config.getInteger("histogramDistAvgDigestBytes", histogramDistAvgDigestBytes);
        histogramDistAccumulatorSize = config.getLong("histogramDistAccumulatorSize", histogramDistAccumulatorSize);
        histogramDistAccumulatorPersisted = config.getBoolean("histogramDistAccumulatorPersisted", histogramDistAccumulatorPersisted);
        histogramDistMemoryCache = config.getBoolean("histogramDistMemoryCache", histogramDistMemoryCache);
        exportQueuePorts = config.getString("exportQueuePorts", exportQueuePorts);
        exportQueueOutputFile = config.getString("exportQueueOutputFile", exportQueueOutputFile);
        exportQueueRetainData = config.getBoolean("exportQueueRetainData", exportQueueRetainData);
        useNoopSender = config.getBoolean("useNoopSender", useNoopSender);
        flushThreads = config.getInteger("flushThreads", flushThreads);
        flushThreadsEvents = config.getInteger("flushThreadsEvents", flushThreadsEvents);
        flushThreadsSourceTags = config.getInteger("flushThreadsSourceTags", flushThreadsSourceTags);
        jsonListenerPorts = config.getString("jsonListenerPorts", jsonListenerPorts);
        writeHttpJsonListenerPorts = config.getString("writeHttpJsonListenerPorts", writeHttpJsonListenerPorts);
        dataDogJsonPorts = config.getString("dataDogJsonPorts", dataDogJsonPorts);
        dataDogRequestRelayTarget = config.getString("dataDogRequestRelayTarget", dataDogRequestRelayTarget);
        dataDogRequestRelayAsyncThreads = config.getInteger("dataDogRequestRelayAsyncThreads", dataDogRequestRelayAsyncThreads);
        dataDogRequestRelaySyncMode = config.getBoolean("dataDogRequestRelaySyncMode", dataDogRequestRelaySyncMode);
        dataDogProcessSystemMetrics = config.getBoolean("dataDogProcessSystemMetrics", dataDogProcessSystemMetrics);
        dataDogProcessServiceChecks = config.getBoolean("dataDogProcessServiceChecks", dataDogProcessServiceChecks);
        graphitePorts = config.getString("graphitePorts", graphitePorts);
        graphiteFormat = config.getString("graphiteFormat", graphiteFormat);
        graphiteFieldsToRemove = config.getString("graphiteFieldsToRemove", graphiteFieldsToRemove);
        graphiteDelimiters = config.getString("graphiteDelimiters", graphiteDelimiters);
        allowRegex = config.getString("allowRegex", config.getString("whitelistRegex", allowRegex));
        blockRegex = config.getString("blockRegex", config.getString("blacklistRegex", blockRegex));
        opentsdbPorts = config.getString("opentsdbPorts", opentsdbPorts);
        opentsdbAllowRegex = config.getString("opentsdbAllowRegex", config.getString("opentsdbWhitelistRegex", opentsdbAllowRegex));
        opentsdbBlockRegex = config.getString("opentsdbBlockRegex", config.getString("opentsdbBlacklistRegex", opentsdbBlockRegex));
        proxyHost = config.getString("proxyHost", proxyHost);
        proxyPort = config.getInteger("proxyPort", proxyPort);
        proxyPassword = config.getString("proxyPassword", proxyPassword, s -> "<removed>");
        proxyUser = config.getString("proxyUser", proxyUser);
        httpUserAgent = config.getString("httpUserAgent", httpUserAgent);
        httpConnectTimeout = config.getInteger("httpConnectTimeout", httpConnectTimeout);
        httpRequestTimeout = config.getInteger("httpRequestTimeout", httpRequestTimeout);
        httpMaxConnTotal = Math.min(200, config.getInteger("httpMaxConnTotal", httpMaxConnTotal));
        httpMaxConnPerRoute = Math.min(100, config.getInteger("httpMaxConnPerRoute", httpMaxConnPerRoute));
        httpAutoRetries = config.getInteger("httpAutoRetries", httpAutoRetries);
        gzipCompression = config.getBoolean("gzipCompression", gzipCompression);
        gzipCompressionLevel = config.getNumber("gzipCompressionLevel", gzipCompressionLevel, 1, 9).intValue();
        soLingerTime = config.getInteger("soLingerTime", soLingerTime);
        splitPushWhenRateLimited = config.getBoolean("splitPushWhenRateLimited", splitPushWhenRateLimited);
        customSourceTags = config.getString("customSourceTags", customSourceTags);
        agentMetricsPointTags = config.getString("agentMetricsPointTags", agentMetricsPointTags);
        ephemeral = config.getBoolean("ephemeral", ephemeral);
        disableRdnsLookup = config.getBoolean("disableRdnsLookup", disableRdnsLookup);
        picklePorts = config.getString("picklePorts", picklePorts);
        traceListenerPorts = config.getString("traceListenerPorts", traceListenerPorts);
        traceJaegerListenerPorts = config.getString("traceJaegerListenerPorts", traceJaegerListenerPorts);
        traceJaegerHttpListenerPorts = config.getString("traceJaegerHttpListenerPorts", traceJaegerHttpListenerPorts);
        traceJaegerGrpcListenerPorts = config.getString("traceJaegerGrpcListenerPorts", traceJaegerGrpcListenerPorts);
        traceJaegerApplicationName = config.getString("traceJaegerApplicationName", traceJaegerApplicationName);
        traceZipkinListenerPorts = config.getString("traceZipkinListenerPorts", traceZipkinListenerPorts);
        traceZipkinApplicationName = config.getString("traceZipkinApplicationName", traceZipkinApplicationName);
        customTracingListenerPorts = config.getString("customTracingListenerPorts", customTracingListenerPorts);
        customTracingApplicationName = config.getString("customTracingApplicationName", customTracingApplicationName);
        customTracingServiceName = config.getString("customTracingServiceName", customTracingServiceName);
        traceSamplingRate = config.getDouble("traceSamplingRate", traceSamplingRate);
        traceSamplingDuration = config.getInteger("traceSamplingDuration", traceSamplingDuration);
        traceDerivedCustomTagKeys = config.getString("traceDerivedCustomTagKeys", traceDerivedCustomTagKeys);
        backendSpanHeadSamplingPercentIgnored = config.getBoolean("backendSpanHeadSamplingPercentIgnored", backendSpanHeadSamplingPercentIgnored);
        pushRelayListenerPorts = config.getString("pushRelayListenerPorts", pushRelayListenerPorts);
        pushRelayHistogramAggregator = config.getBoolean("pushRelayHistogramAggregator", pushRelayHistogramAggregator);
        pushRelayHistogramAggregatorAccumulatorSize = config.getLong("pushRelayHistogramAggregatorAccumulatorSize", pushRelayHistogramAggregatorAccumulatorSize);
        pushRelayHistogramAggregatorFlushSecs = config.getInteger("pushRelayHistogramAggregatorFlushSecs", pushRelayHistogramAggregatorFlushSecs);
        pushRelayHistogramAggregatorCompression = config.getNumber("pushRelayHistogramAggregatorCompression", pushRelayHistogramAggregatorCompression).shortValue();
        bufferFile = config.getString("buffer", bufferFile);
        bufferShardSize = config.getInteger("bufferShardSize", bufferShardSize);
        disableBufferSharding = config.getBoolean("disableBufferSharding", disableBufferSharding);
        taskQueueLevel = TaskQueueLevel.fromString(config.getString("taskQueueStrategy", taskQueueLevel.toString()));
        purgeBuffer = config.getBoolean("purgeBuffer", purgeBuffer);
        preprocessorConfigFile = config.getString("preprocessorConfigFile", preprocessorConfigFile);
        dataBackfillCutoffHours = config.getInteger("dataBackfillCutoffHours", dataBackfillCutoffHours);
        dataPrefillCutoffHours = config.getInteger("dataPrefillCutoffHours", dataPrefillCutoffHours);
        filebeatPort = config.getInteger("filebeatPort", filebeatPort);
        rawLogsPort = config.getInteger("rawLogsPort", rawLogsPort);
        rawLogsMaxReceivedLength = config.getInteger("rawLogsMaxReceivedLength", rawLogsMaxReceivedLength);
        rawLogsHttpBufferSize = config.getInteger("rawLogsHttpBufferSize", rawLogsHttpBufferSize);
        logsIngestionConfigFile = config.getString("logsIngestionConfigFile", logsIngestionConfigFile);
        sqsQueueBuffer = config.getBoolean("sqsBuffer", sqsQueueBuffer);
        sqsQueueNameTemplate = config.getString("sqsQueueNameTemplate", sqsQueueNameTemplate);
        sqsQueueRegion = config.getString("sqsQueueRegion", sqsQueueRegion);
        sqsQueueIdentifier = config.getString("sqsQueueIdentifier", sqsQueueIdentifier);
        // auth settings
        authMethod = TokenValidationMethod.fromString(config.getString("authMethod", authMethod.toString()));
        authTokenIntrospectionServiceUrl = config.getString("authTokenIntrospectionServiceUrl", authTokenIntrospectionServiceUrl);
        authTokenIntrospectionAuthorizationHeader = config.getString("authTokenIntrospectionAuthorizationHeader", authTokenIntrospectionAuthorizationHeader);
        authResponseRefreshInterval = config.getInteger("authResponseRefreshInterval", authResponseRefreshInterval);
        authResponseMaxTtl = config.getInteger("authResponseMaxTtl", authResponseMaxTtl);
        authStaticToken = config.getString("authStaticToken", authStaticToken);
        // health check / admin API settings
        adminApiListenerPort = config.getInteger("adminApiListenerPort", adminApiListenerPort);
        adminApiRemoteIpAllowRegex = config.getString("adminApiRemoteIpWhitelistRegex", adminApiRemoteIpAllowRegex);
        httpHealthCheckPorts = config.getString("httpHealthCheckPorts", httpHealthCheckPorts);
        httpHealthCheckAllPorts = config.getBoolean("httpHealthCheckAllPorts", false);
        httpHealthCheckPath = config.getString("httpHealthCheckPath", httpHealthCheckPath);
        httpHealthCheckResponseContentType = config.getString("httpHealthCheckResponseContentType", httpHealthCheckResponseContentType);
        httpHealthCheckPassStatusCode = config.getInteger("httpHealthCheckPassStatusCode", httpHealthCheckPassStatusCode);
        httpHealthCheckPassResponseBody = config.getString("httpHealthCheckPassResponseBody", httpHealthCheckPassResponseBody);
        httpHealthCheckFailStatusCode = config.getInteger("httpHealthCheckFailStatusCode", httpHealthCheckFailStatusCode);
        httpHealthCheckFailResponseBody = config.getString("httpHealthCheckFailResponseBody", httpHealthCheckFailResponseBody);
        // TLS configurations
        privateCertPath = config.getString("privateCertPath", privateCertPath);
        privateKeyPath = config.getString("privateKeyPath", privateKeyPath);
        tlsPorts = config.getString("tlsPorts", tlsPorts);
        // Traffic shaping config
        trafficShaping = config.getBoolean("trafficShaping", trafficShaping);
        trafficShapingWindowSeconds = config.getInteger("trafficShapingWindowSeconds", trafficShapingWindowSeconds);
        trafficShapingHeadroom = config.getDouble("trafficShapingHeadroom", trafficShapingHeadroom);
        // CORS configuration
        corsEnabledPorts = config.getString("corsEnabledPorts", corsEnabledPorts);
        corsOrigin = config.getString("corsOrigin", corsOrigin);
        corsAllowNullOrigin = config.getBoolean("corsAllowNullOrigin", corsAllowNullOrigin);
        // clamp values for pushFlushMaxPoints/etc between min split size
        // (or 1 in case of source tags and events) and default batch size.
        // also make sure it is never higher than the configured rate limit.
        pushFlushMaxPoints = Math.max(Math.min(Math.min(config.getInteger("pushFlushMaxPoints", pushFlushMaxPoints), DEFAULT_BATCH_SIZE), (int) pushRateLimit), DEFAULT_MIN_SPLIT_BATCH_SIZE);
        pushFlushMaxHistograms = Math.max(Math.min(Math.min(config.getInteger("pushFlushMaxHistograms", pushFlushMaxHistograms), DEFAULT_BATCH_SIZE_HISTOGRAMS), (int) pushRateLimitHistograms), DEFAULT_MIN_SPLIT_BATCH_SIZE);
        pushFlushMaxSourceTags = Math.max(Math.min(Math.min(config.getInteger("pushFlushMaxSourceTags", pushFlushMaxSourceTags), DEFAULT_BATCH_SIZE_SOURCE_TAGS), (int) pushRateLimitSourceTags), 1);
        pushFlushMaxSpans = Math.max(Math.min(Math.min(config.getInteger("pushFlushMaxSpans", pushFlushMaxSpans), DEFAULT_BATCH_SIZE_SPANS), (int) pushRateLimitSpans), DEFAULT_MIN_SPLIT_BATCH_SIZE);
        pushFlushMaxSpanLogs = Math.max(Math.min(Math.min(config.getInteger("pushFlushMaxSpanLogs", pushFlushMaxSpanLogs), DEFAULT_BATCH_SIZE_SPAN_LOGS), (int) pushRateLimitSpanLogs), DEFAULT_MIN_SPLIT_BATCH_SIZE);
        pushFlushMaxEvents = Math.min(Math.min(Math.max(config.getInteger("pushFlushMaxEvents", pushFlushMaxEvents), 1), DEFAULT_BATCH_SIZE_EVENTS), (int) (pushRateLimitEvents + 1));
        /*
        default value for pushMemoryBufferLimit is 16 * pushFlushMaxPoints, but no more than 25% of
        available heap memory. 25% is chosen heuristically as a safe number for scenarios with
        limited system resources (4 CPU cores or less, heap size less than 4GB) to prevent OOM.
        this is a conservative estimate, budgeting 200 characters (400 bytes) per per point line.
        Also, it shouldn't be less than 1 batch size (pushFlushMaxPoints).
       */
        int listeningPorts = Iterables.size(Splitter.on(",").omitEmptyStrings().trimResults().split(pushListenerPorts));
        long calculatedMemoryBufferLimit = Math.max(Math.min(16 * pushFlushMaxPoints, Runtime.getRuntime().maxMemory() / Math.max(0, listeningPorts) / 4 / flushThreads / 400), pushFlushMaxPoints);
        logger.fine("Calculated pushMemoryBufferLimit: " + calculatedMemoryBufferLimit);
        pushMemoryBufferLimit = Math.max(config.getInteger("pushMemoryBufferLimit", pushMemoryBufferLimit), pushFlushMaxPoints);
        logger.fine("Configured pushMemoryBufferLimit: " + pushMemoryBufferLimit);
        pushFlushInterval = config.getInteger("pushFlushInterval", pushFlushInterval);
        retryBackoffBaseSeconds = Math.max(Math.min(config.getDouble("retryBackoffBaseSeconds", retryBackoffBaseSeconds), MAX_RETRY_BACKOFF_BASE_SECONDS), 1.0);
    } catch (Throwable exception) {
        logger.severe("Could not load configuration file " + pushConfigFile);
        throw new RuntimeException(exception.getMessage());
    }
    if (httpUserAgent == null) {
        httpUserAgent = "Wavefront-Proxy/" + getBuildVersion();
    }
    if (pushConfigFile != null) {
        logger.info("Loaded configuration file " + pushConfigFile);
    }
}
Also used : Iterables(com.google.common.collect.Iterables) Configuration(com.wavefront.agent.config.Configuration) ParameterException(com.beust.jcommander.ParameterException) Utils.getLocalHostName(com.wavefront.common.Utils.getLocalHostName) Parameter(com.beust.jcommander.Parameter) SPAN_KIND(io.opentracing.tag.Tags.SPAN_KIND) DEFAULT_FLUSH_THREADS_SOURCE_TAGS(com.wavefront.agent.data.EntityProperties.DEFAULT_FLUSH_THREADS_SOURCE_TAGS) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) DEFAULT_BATCH_SIZE_EVENTS(com.wavefront.agent.data.EntityProperties.DEFAULT_BATCH_SIZE_EVENTS) TaskQueueLevel(com.wavefront.agent.data.TaskQueueLevel) ObjectUtils(org.apache.commons.lang3.ObjectUtils) IStringConverter(com.beust.jcommander.IStringConverter) Map(java.util.Map) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) DEFAULT_MIN_SPLIT_BATCH_SIZE(com.wavefront.agent.data.EntityProperties.DEFAULT_MIN_SPLIT_BATCH_SIZE) Splitter(com.google.common.base.Splitter) DEFAULT_SPLIT_PUSH_WHEN_RATE_LIMITED(com.wavefront.agent.data.EntityProperties.DEFAULT_SPLIT_PUSH_WHEN_RATE_LIMITED) LinkedHashSet(java.util.LinkedHashSet) ReportableConfig(com.wavefront.agent.config.ReportableConfig) DEFAULT_BATCH_SIZE_SPAN_LOGS(com.wavefront.agent.data.EntityProperties.DEFAULT_BATCH_SIZE_SPAN_LOGS) DEFAULT_FLUSH_INTERVAL(com.wavefront.agent.data.EntityProperties.DEFAULT_FLUSH_INTERVAL) DEFAULT_BATCH_SIZE(com.wavefront.agent.data.EntityProperties.DEFAULT_BATCH_SIZE) DEFAULT_BATCH_SIZE_SOURCE_TAGS(com.wavefront.agent.data.EntityProperties.DEFAULT_BATCH_SIZE_SOURCE_TAGS) JCommander(com.beust.jcommander.JCommander) Set(java.util.Set) DEFAULT_RETRY_BACKOFF_BASE_SECONDS(com.wavefront.agent.data.EntityProperties.DEFAULT_RETRY_BACKOFF_BASE_SECONDS) NO_RATE_LIMIT(com.wavefront.agent.data.EntityProperties.NO_RATE_LIMIT) TokenValidationMethod(com.wavefront.agent.auth.TokenValidationMethod) Logger(java.util.logging.Logger) DEFAULT_BATCH_SIZE_SPANS(com.wavefront.agent.data.EntityProperties.DEFAULT_BATCH_SIZE_SPANS) DEFAULT_BATCH_SIZE_HISTOGRAMS(com.wavefront.agent.data.EntityProperties.DEFAULT_BATCH_SIZE_HISTOGRAMS) List(java.util.List) TimeProvider(com.wavefront.common.TimeProvider) Utils.getBuildVersion(com.wavefront.common.Utils.getBuildVersion) Collections(java.util.Collections) DEFAULT_FLUSH_THREADS_EVENTS(com.wavefront.agent.data.EntityProperties.DEFAULT_FLUSH_THREADS_EVENTS) Joiner(com.google.common.base.Joiner) ReportableConfig(com.wavefront.agent.config.ReportableConfig)

Aggregations

Collectors (java.util.stream.Collectors)9 List (java.util.List)8 ObjectUtils (org.apache.commons.lang3.ObjectUtils)8 Set (java.util.Set)6 StringUtils (org.apache.commons.lang3.StringUtils)6 HashSet (java.util.HashSet)5 Map (java.util.Map)5 Function (java.util.function.Function)5 Transactional (org.springframework.transaction.annotation.Transactional)5 ArrayList (java.util.ArrayList)4 Date (java.util.Date)4 TextUtils (org.hisp.dhis.commons.util.TextUtils)4 OrganisationUnitSelectionMode (org.hisp.dhis.common.OrganisationUnitSelectionMode)3 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)3 CurrentUserService (org.hisp.dhis.user.CurrentUserService)3 User (org.hisp.dhis.user.User)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Joiner (com.google.common.base.Joiner)2 Lists (com.google.common.collect.Lists)2 IOException (java.io.IOException)2