Search in sources :

Example 1 with StrSubstitutor

use of org.apache.commons.lang3.text.StrSubstitutor in project pinot by linkedin.

the class AnomalyResource method getExternalDashboardUrlForMergedAnomaly.

@GET
@Path("/external-dashboard-url/{mergedAnomalyId}")
public String getExternalDashboardUrlForMergedAnomaly(@NotNull @PathParam("mergedAnomalyId") Long mergedAnomalyId) throws Exception {
    MergedAnomalyResultDTO mergedAnomalyResultDTO = mergedAnomalyResultDAO.findById(mergedAnomalyId);
    String metric = mergedAnomalyResultDTO.getMetric();
    String dataset = mergedAnomalyResultDTO.getCollection();
    Long startTime = mergedAnomalyResultDTO.getStartTime();
    Long endTime = mergedAnomalyResultDTO.getEndTime();
    MetricConfigDTO metricConfigDTO = metricConfigDAO.findByMetricAndDataset(metric, dataset);
    Map<String, String> context = new HashMap<>();
    context.put(MetricConfigBean.URL_TEMPLATE_START_TIME, String.valueOf(startTime));
    context.put(MetricConfigBean.URL_TEMPLATE_END_TIME, String.valueOf(endTime));
    StrSubstitutor strSubstitutor = new StrSubstitutor(context);
    Map<String, String> urlTemplates = metricConfigDTO.getExtSourceLinkInfo();
    for (Entry<String, String> entry : urlTemplates.entrySet()) {
        String sourceName = entry.getKey();
        String urlTemplate = entry.getValue();
        String extSourceUrl = strSubstitutor.replace(urlTemplate);
        urlTemplates.put(sourceName, extSourceUrl);
    }
    return new JSONObject(urlTemplates).toString();
}
Also used : MetricConfigDTO(com.linkedin.thirdeye.datalayer.dto.MetricConfigDTO) StrSubstitutor(org.apache.commons.lang3.text.StrSubstitutor) JSONObject(org.json.JSONObject) MergedAnomalyResultDTO(com.linkedin.thirdeye.datalayer.dto.MergedAnomalyResultDTO) HashMap(java.util.HashMap) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 2 with StrSubstitutor

use of org.apache.commons.lang3.text.StrSubstitutor in project Gaffer by gchq.

the class WalkthroughStrSubstitutor method substitute.

public static String substitute(final String text, final Map<String, String> paramMap) {
    final String formattedDescription = new StrSubstitutor(paramMap).replace(text);
    final int startIndex = formattedDescription.indexOf("${");
    if (startIndex > -1) {
        final String tmp = formattedDescription.substring(startIndex + 2);
        final int endIndex = tmp.indexOf("}");
        if (endIndex > -1) {
            throw new RuntimeException("Parameter was not substituted: " + tmp.substring(0, endIndex));
        }
    }
    return formattedDescription;
}
Also used : StrSubstitutor(org.apache.commons.lang3.text.StrSubstitutor)

Example 3 with StrSubstitutor

use of org.apache.commons.lang3.text.StrSubstitutor in project raml-module-builder by folio-org.

the class PostgresClient method join.

/**
 * run simple join queries between two tables
 *
 * for example, to generate the following query:
 * SELECT  c1.* , c2.* FROM univeristy.config_data c1
 *  INNER JOIN univeristy.config_data c2 ON ((c1.jsonb->>'code') = (c2.jsonb->'scope'->>'library_id'))
 *    WHERE (c2.jsonb->>'default')::boolean IS TRUE  AND (c2.jsonb->>'default')::boolean IS TRUE
 *
 * Create a criteria representing a join column for each of the tables
 * Create two JoinBy objects containing the:
 *   1. The table to join from and to,
 *   2. An alias for the tables,
 *   3. The Fields to return
 *   4. The column to join on (using the criteria object)
 *
 * @param  JoinBy jb1= new JoinBy("config_data","c1",
 *   new Criteria().addField("'code'"), new String[]{"count(c1._id)"});
 *
 * @param  JoinBy jb2= new JoinBy("config_data","c2",
 *   new Criteria().addField("'scope'").addField("'library_id'"),  new String[]{"avg(length(c2.description))"});
 *
 * Passing "*" to the fields to return may be used as well (or a list of aliased column names)
 *
 * JoinBy jb1= new JoinBy("config_data","c1",
 *  new Criteria().addField("'code'"), new String[]{"*"});
 *
 * @param operation what operation to use when comparing the two columns. For example:
 * setting this to equals would yeild something like:
 *
 * ((c1.jsonb->>'code') = (c2.jsonb->'scope'->>'library_id'))
 *
 * @param joinType - for example: INNER JOIN, LEFT JOIN,
 * some prepared COnsts can be found: JoinBy.RIGHT_JOIN
 *
 * @param cr criterion to use to further filter results per table. can be used to also sort results
 * new Criterion().setOrder(new Order("c2._id", ORDER.DESC))
 * But can be used to create a more complex where clause if needed
 *
 * For example:
 * GroupedCriterias gc = new GroupedCriterias();
 *  gc.addCriteria(new Criteria().setAlias("c2").addField("'default'")
 *    .setOperation(Criteria.OP_IS_TRUE));
 *  gc.addCriteria(new Criteria().setAlias("c2").addField("'enabled'")
 *    .setOperation(Criteria.OP_IS_TRUE) , "OR");
 *  gc.setGroupOp("AND");
 *
 *  NOTE that to use sorting with a combination of functions - group by is needed - not currently implemented
 *
 *  Criterion cr =
 *      new Criterion().addGroupOfCriterias(gc).addGroupOfCriterias(gc1).setOrder(new Order("c1._id", ORDER.DESC));
 */
public void join(JoinBy from, JoinBy to, String operation, String joinType, String cr, Class<?> returnedClass, boolean setId, Handler<AsyncResult<?>> replyHandler) {
    long start = System.nanoTime();
    client.getConnection(res -> {
        if (res.succeeded()) {
            SQLConnection connection = res.result();
            try {
                String select = "SELECT ";
                StringBuffer joinon = new StringBuffer();
                StringBuffer tables = new StringBuffer();
                StringBuffer selectFields = new StringBuffer();
                String filter = "";
                if (cr != null) {
                    filter = cr;
                }
                String selectFromTable = from.getSelectFields();
                String selectToTable = to.getSelectFields();
                boolean addComma = false;
                if (selectFromTable != null && selectFromTable.length() > 0) {
                    selectFields.append(from.getSelectFields());
                    addComma = true;
                }
                if (selectToTable != null && selectToTable.length() > 0) {
                    if (addComma) {
                        selectFields.append(",");
                    }
                    selectFields.append(to.getSelectFields());
                }
                tables.append(convertToPsqlStandard(tenantId) + "." + from.getTableName() + " " + from.getAlias() + " ");
                joinon.append(joinType + " " + convertToPsqlStandard(tenantId) + "." + to.getTableName() + " " + to.getAlias() + " ");
                String[] q = new String[] { select + selectFields.toString() + " FROM " + tables.toString() + joinon.toString() + new Criterion().addCriterion(from.getJoinColumn(), operation, to.getJoinColumn(), " AND ") + filter };
                // TODO optimize query building
                Map<String, String> replaceMapping = new HashMap<>();
                replaceMapping.put("tenantId", convertToPsqlStandard(tenantId));
                replaceMapping.put("query", org.apache.commons.lang.StringEscapeUtils.escapeSql(parseQuery(q[0]).getCountFuncQuery()));
                StrSubstitutor sub = new StrSubstitutor(replaceMapping);
                q[0] = select + sub.replace(countClauseTemplate) + q[0].replaceFirst(select, " ");
                log.debug("query = " + q[0]);
                connection.query(q[0], query -> {
                    connection.close();
                    if (query.failed()) {
                        log.error(query.cause().getMessage(), query.cause());
                        replyHandler.handle(Future.failedFuture(query.cause()));
                    } else {
                        if (returnedClass != null) {
                            replyHandler.handle(Future.succeededFuture(processResult(query.result(), returnedClass, true, setId)));
                        } else {
                            replyHandler.handle(Future.succeededFuture(query.result()));
                        }
                    }
                    long end = System.nanoTime();
                    StatsTracker.addStatElement(STATS_KEY + ".join", (end - start));
                    if (log.isDebugEnabled()) {
                        log.debug("timer: get " + q[0] + " (ns) " + (end - start));
                    }
                });
            } catch (Exception e) {
                if (connection != null) {
                    connection.close();
                }
                log.error(e.getMessage(), e);
                replyHandler.handle(Future.failedFuture(e));
            }
        } else {
            log.error(res.cause().getMessage(), res.cause());
            replyHandler.handle(Future.failedFuture(res.cause()));
        }
    });
}
Also used : StrSubstitutor(org.apache.commons.lang3.text.StrSubstitutor) Criterion(org.folio.rest.persist.Criteria.Criterion) HashMap(java.util.HashMap) SQLConnection(io.vertx.ext.sql.SQLConnection) UnrecognizedPropertyException(com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException) SQLException(java.sql.SQLException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 4 with StrSubstitutor

use of org.apache.commons.lang3.text.StrSubstitutor in project incubator-gobblin by apache.

the class SalesforceSource method getHistogramByDayBucketing.

/**
 * Get a histogram with day granularity buckets.
 */
private Histogram getHistogramByDayBucketing(SalesforceConnector connector, String entity, String watermarkColumn, Partition partition) {
    Histogram histogram = new Histogram();
    Calendar calendar = new GregorianCalendar();
    Date startDate = Utils.toDate(partition.getLowWatermark(), Partitioner.WATERMARKTIMEFORMAT);
    calendar.setTime(startDate);
    int startYear = calendar.get(Calendar.YEAR);
    String lowWatermarkDate = Utils.dateToString(startDate, SalesforceExtractor.SALESFORCE_TIMESTAMP_FORMAT);
    Date endDate = Utils.toDate(partition.getHighWatermark(), Partitioner.WATERMARKTIMEFORMAT);
    calendar.setTime(endDate);
    int endYear = calendar.get(Calendar.YEAR);
    String highWatermarkDate = Utils.dateToString(endDate, SalesforceExtractor.SALESFORCE_TIMESTAMP_FORMAT);
    Map<String, String> values = new HashMap<>();
    values.put("table", entity);
    values.put("column", watermarkColumn);
    StrSubstitutor sub = new StrSubstitutor(values);
    for (int year = startYear; year <= endYear; year++) {
        if (year == startYear) {
            values.put("start", lowWatermarkDate);
            values.put("greater", partition.isLowWatermarkInclusive() ? ">=" : ">");
        } else {
            values.put("start", getDateString(year));
            values.put("greater", ">=");
        }
        if (year == endYear) {
            values.put("end", highWatermarkDate);
            values.put("less", partition.isHighWatermarkInclusive() ? "<=" : "<");
        } else {
            values.put("end", getDateString(year + 1));
            values.put("less", "<");
        }
        String query = sub.replace(DAY_PARTITION_QUERY_TEMPLATE);
        log.info("Histogram query: " + query);
        histogram.add(parseDayBucketingHistogram(getRecordsForQuery(connector, query)));
    }
    return histogram;
}
Also used : StrSubstitutor(org.apache.commons.lang3.text.StrSubstitutor) HashMap(java.util.HashMap) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) Date(java.util.Date)

Example 5 with StrSubstitutor

use of org.apache.commons.lang3.text.StrSubstitutor in project incubator-gobblin by apache.

the class SalesforceSource method getHistogramByProbing.

/**
 * Get a histogram for the time range by probing to break down large buckets. Use count instead of
 * querying if it is non-negative.
 */
private Histogram getHistogramByProbing(TableCountProbingContext probingContext, int count, long startEpoch, long endEpoch) {
    Histogram histogram = new Histogram();
    Map<String, String> values = new HashMap<>();
    values.put("table", probingContext.entity);
    values.put("column", probingContext.watermarkColumn);
    values.put("greater", ">=");
    values.put("less", "<");
    StrSubstitutor sub = new StrSubstitutor(values);
    getHistogramRecursively(probingContext, histogram, sub, values, count, startEpoch, endEpoch);
    return histogram;
}
Also used : StrSubstitutor(org.apache.commons.lang3.text.StrSubstitutor) HashMap(java.util.HashMap)

Aggregations

StrSubstitutor (org.apache.commons.lang3.text.StrSubstitutor)29 HashMap (java.util.HashMap)16 File (java.io.File)5 Map (java.util.Map)5 IOException (java.io.IOException)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 UnrecognizedPropertyException (com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException)2 MetricConfigDTO (com.linkedin.thirdeye.datalayer.dto.MetricConfigDTO)2 Field (de.tblsoft.solr.pipeline.bean.Field)2 FileWriter (java.io.FileWriter)2 SQLException (java.sql.SQLException)2 List (java.util.List)2 Properties (java.util.Properties)2 StringUtils (org.apache.commons.lang3.StringUtils)2 StrLookup (org.apache.commons.lang3.text.StrLookup)2 Test (org.junit.Test)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 DomainVO (com.cloud.domain.DomainVO)1 AccountVO (com.cloud.user.AccountVO)1 UserVO (com.cloud.user.UserVO)1