Search in sources :

Example 26 with TreeSet

use of java.util.TreeSet in project pinot by linkedin.

the class DeleteOverlappingSegmentsInPinot method computeNewIdealStateAfterDeletingOverlappingSegments.

private static IdealState computeNewIdealStateAfterDeletingOverlappingSegments(HelixDataAccessor helixDataAccessor, PropertyKey idealStatesKey) {
    IdealState is = helixDataAccessor.getProperty(idealStatesKey);
    // compute existing DAILY segments
    Set<String> daysWithDailySegments = new HashSet<>();
    for (String segmentName : is.getPartitionSet()) {
        LOG.info("Segment Name : {}", segmentName);
        if (segmentName.indexOf("DAILY") > -1) {
            String[] splits = segmentName.split("_");
            String endDay = splits[splits.length - 2].substring(0, "yyyy-mm-dd".length());
            String startDay = splits[splits.length - 3].substring(0, "yyyy-mm-dd".length());
            LOG.info("Start : {} End : {}", startDay, endDay);
            daysWithDailySegments.add(startDay);
        }
    }
    // compute list of HOURLY segments to be deleted
    Set<String> hourlySegmentsToDelete = new TreeSet<>();
    for (String segmentName : is.getPartitionSet()) {
        LOG.info("Segment name {}", segmentName);
        if (segmentName.indexOf("HOURLY") > -1) {
            String[] splits = segmentName.split("_");
            String endDay = splits[splits.length - 2].substring(0, "yyyy-mm-dd".length());
            String startDay = splits[splits.length - 3].substring(0, "yyyy-mm-dd".length());
            LOG.info("Start : {} End : {}", startDay, endDay);
            if (daysWithDailySegments.contains(startDay)) {
                hourlySegmentsToDelete.add(segmentName);
            }
        }
    }
    LOG.info("HOURLY segments that can be deleted: {}", hourlySegmentsToDelete.size());
    LOG.info("Hourly segments to delete {}", hourlySegmentsToDelete.toString().replaceAll(",", "\n"));
    IdealState newIdealState = new IdealState(is.getRecord());
    for (String hourlySegmentToDelete : hourlySegmentsToDelete) {
        newIdealState.getRecord().getMapFields().remove(hourlySegmentToDelete);
    }
    return newIdealState;
}
Also used : TreeSet(java.util.TreeSet) IdealState(org.apache.helix.model.IdealState) HashSet(java.util.HashSet)

Example 27 with TreeSet

use of java.util.TreeSet in project pinot by linkedin.

the class MetricExpression method computeMetricFunctions.

public List<MetricFunction> computeMetricFunctions() {
    try {
        Scope scope = Scope.create();
        Set<String> metricNames = new TreeSet<>();
        // expression parser errors out on variables starting with _
        // we're replacing the __COUNT default metric, with an escaped string
        // after evaluating, we replace the escaped string back with the original
        String modifiedExpressions = expression.replace(COUNT_METRIC, COUNT_METRIC_ESCAPED);
        Parser.parse(modifiedExpressions, scope);
        metricNames = scope.getLocalNames();
        ArrayList<MetricFunction> metricFunctions = new ArrayList<>();
        for (String metricName : metricNames) {
            if (metricName.equals(COUNT_METRIC_ESCAPED)) {
                metricName = COUNT_METRIC;
            }
            metricFunctions.add(new MetricFunction(aggFunction, metricName));
        }
        return metricFunctions;
    } catch (ParseException e) {
        throw new RuntimeException("Exception parsing expressionString:" + expression, e);
    }
}
Also used : Scope(parsii.eval.Scope) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) ParseException(parsii.tokenizer.ParseException)

Example 28 with TreeSet

use of java.util.TreeSet in project pinot by linkedin.

the class AnomaliesResource method getTimeSeriesData.

/**
   * Get timeseries for metric
   * @param collection
   * @param filters
   * @param start
   * @param end
   * @param aggTimeGranularity
   * @param metric
   * @return
   * @throws Exception
   */
private JSONObject getTimeSeriesData(String collection, Multimap<String, String> filters, Long start, Long end, String aggTimeGranularity, String metric) throws Exception {
    TimeSeriesRequest request = new TimeSeriesRequest();
    request.setCollectionName(collection);
    DateTimeZone timeZoneForCollection = Utils.getDataTimeZone(collection);
    request.setStart(new DateTime(start, timeZoneForCollection));
    request.setEnd(new DateTime(end, timeZoneForCollection));
    request.setFilterSet(filters);
    List<MetricExpression> metricExpressions = Utils.convertToMetricExpressions(metric, MetricAggFunction.SUM, collection);
    request.setMetricExpressions(metricExpressions);
    request.setAggregationTimeGranularity(Utils.getAggregationTimeGranularity(aggTimeGranularity, collection));
    DatasetConfigDTO datasetConfig = CACHE_REGISTRY.getDatasetConfigCache().get(collection);
    TimeSpec timespec = ThirdEyeUtils.getTimeSpecFromDatasetConfig(datasetConfig);
    if (!request.getAggregationTimeGranularity().getUnit().equals(TimeUnit.DAYS) || !StringUtils.isBlank(timespec.getFormat())) {
        request.setEndDateInclusive(true);
    }
    TimeSeriesHandler handler = new TimeSeriesHandler(CACHE_REGISTRY.getQueryCache());
    JSONObject jsonResponseObject = new JSONObject();
    try {
        TimeSeriesResponse response = handler.handle(request);
        JSONObject timeseriesMap = new JSONObject();
        JSONArray timeValueArray = new JSONArray();
        TreeSet<String> keys = new TreeSet<>();
        TreeSet<Long> times = new TreeSet<>();
        for (int i = 0; i < response.getNumRows(); i++) {
            TimeSeriesRow timeSeriesRow = response.getRow(i);
            times.add(timeSeriesRow.getStart());
        }
        for (Long time : times) {
            timeValueArray.put(time);
        }
        timeseriesMap.put("time", timeValueArray);
        for (int i = 0; i < response.getNumRows(); i++) {
            TimeSeriesRow timeSeriesRow = response.getRow(i);
            for (TimeSeriesMetric metricTimeSeries : timeSeriesRow.getMetrics()) {
                String key = metricTimeSeries.getMetricName();
                JSONArray valueArray;
                if (!timeseriesMap.has(key)) {
                    valueArray = new JSONArray();
                    timeseriesMap.put(key, valueArray);
                    keys.add(key);
                } else {
                    valueArray = timeseriesMap.getJSONArray(key);
                }
                valueArray.put(metricTimeSeries.getValue());
            }
        }
        JSONObject summaryMap = new JSONObject();
        summaryMap.put("currentStart", start);
        summaryMap.put("currentEnd", end);
        jsonResponseObject.put("timeSeriesData", timeseriesMap);
        jsonResponseObject.put("keys", new JSONArray(keys));
        jsonResponseObject.put("summary", summaryMap);
    } catch (Exception e) {
        throw e;
    }
    LOG.info("Response:{}", jsonResponseObject);
    return jsonResponseObject;
}
Also used : TimeSeriesRow(com.linkedin.thirdeye.client.timeseries.TimeSeriesRow) TimeSeriesResponse(com.linkedin.thirdeye.client.timeseries.TimeSeriesResponse) JSONArray(org.json.JSONArray) MetricExpression(com.linkedin.thirdeye.client.MetricExpression) DateTimeZone(org.joda.time.DateTimeZone) DateTime(org.joda.time.DateTime) TimeoutException(java.util.concurrent.TimeoutException) JSONException(org.json.JSONException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TimeSpec(com.linkedin.thirdeye.api.TimeSpec) DatasetConfigDTO(com.linkedin.thirdeye.datalayer.dto.DatasetConfigDTO) JSONObject(org.json.JSONObject) TimeSeriesHandler(com.linkedin.thirdeye.client.timeseries.TimeSeriesHandler) TreeSet(java.util.TreeSet) TimeSeriesMetric(com.linkedin.thirdeye.client.timeseries.TimeSeriesRow.TimeSeriesMetric) TimeSeriesRequest(com.linkedin.thirdeye.client.timeseries.TimeSeriesRequest)

Example 29 with TreeSet

use of java.util.TreeSet in project pinot by linkedin.

the class ContributorViewHandler method getRowsSortedByTime.

private Map<String, SortedSet<Row>> getRowsSortedByTime(TimeOnTimeComparisonResponse response) {
    Map<String, SortedSet<Row>> result = new HashMap<>();
    int numRows = response.getNumRows();
    for (int i = 0; i < numRows; i++) {
        Row row = response.getRow(i);
        String dimensionName = row.getDimensionName();
        String dimensionValue = row.getDimensionValue();
        String rowGroupKey = dimensionName + "." + dimensionValue;
        if (result.containsKey(rowGroupKey)) {
            result.get(rowGroupKey).add(row);
        } else {
            SortedSet<Row> rows = new TreeSet<>(rowComparator);
            rows.add(row);
            result.put(rowGroupKey, rows);
        }
    }
    return result;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeSet(java.util.TreeSet) Row(com.linkedin.thirdeye.client.comparison.Row) SortedSet(java.util.SortedSet)

Example 30 with TreeSet

use of java.util.TreeSet in project Smack by igniterealtime.

the class EntityCapsManager method generateVerificationString.

/**
     * Generates a XEP-115 Verification String
     * 
     * @see <a href="http://xmpp.org/extensions/xep-0115.html#ver">XEP-115
     *      Verification String</a>
     * 
     * @param discoverInfo
     * @param hash
     *            the used hash function, if null, default hash will be used
     * @return The generated verification String or null if the hash is not
     *         supported
     */
protected static CapsVersionAndHash generateVerificationString(DiscoverInfo discoverInfo, String hash) {
    if (hash == null) {
        hash = DEFAULT_HASH;
    }
    // SUPPORTED_HASHES uses the format of MessageDigest, which is uppercase, e.g. "SHA-1" instead of "sha-1"
    MessageDigest md = SUPPORTED_HASHES.get(hash.toUpperCase(Locale.US));
    if (md == null)
        return null;
    // Then transform the hash to lowercase, as this value will be put on the wire within the caps element's hash
    // attribute. I'm not sure if the standard is case insensitive here, but let's assume that even it is, there could
    // be "broken" implementation in the wild, so we *always* transform to lowercase.
    hash = hash.toLowerCase(Locale.US);
    DataForm extendedInfo = DataForm.from(discoverInfo);
    // 1. Initialize an empty string S ('sb' in this method).
    // Use StringBuilder as we don't
    StringBuilder sb = new StringBuilder();
    // need thread-safe StringBuffer
    // 2. Sort the service discovery identities by category and then by
    // type and then by xml:lang
    // (if it exists), formatted as CATEGORY '/' [TYPE] '/' [LANG] '/'
    // [NAME]. Note that each slash is included even if the LANG or
    // NAME is not included (in accordance with XEP-0030, the category and
    // type MUST be included.
    SortedSet<DiscoverInfo.Identity> sortedIdentities = new TreeSet<DiscoverInfo.Identity>();
    for (DiscoverInfo.Identity i : discoverInfo.getIdentities()) sortedIdentities.add(i);
    // followed by the '<' character.
    for (DiscoverInfo.Identity identity : sortedIdentities) {
        sb.append(identity.getCategory());
        sb.append('/');
        sb.append(identity.getType());
        sb.append('/');
        sb.append(identity.getLanguage() == null ? "" : identity.getLanguage());
        sb.append('/');
        sb.append(identity.getName() == null ? "" : identity.getName());
        sb.append('<');
    }
    // 4. Sort the supported service discovery features.
    SortedSet<String> features = new TreeSet<String>();
    for (Feature f : discoverInfo.getFeatures()) features.add(f.getVar());
    // character
    for (String f : features) {
        sb.append(f);
        sb.append('<');
    }
    // see XEP-0115 5.4 step 3.6
    if (extendedInfo != null && extendedInfo.hasHiddenFormTypeField()) {
        synchronized (extendedInfo) {
            // 6. If the service discovery information response includes
            // XEP-0128 data forms, sort the forms by the FORM_TYPE (i.e.,
            // by the XML character data of the <value/> element).
            SortedSet<FormField> fs = new TreeSet<FormField>(new Comparator<FormField>() {

                @Override
                public int compare(FormField f1, FormField f2) {
                    return f1.getVariable().compareTo(f2.getVariable());
                }
            });
            FormField ft = null;
            for (FormField f : extendedInfo.getFields()) {
                if (!f.getVariable().equals("FORM_TYPE")) {
                    fs.add(f);
                } else {
                    ft = f;
                }
            }
            // Add FORM_TYPE values
            if (ft != null) {
                formFieldValuesToCaps(ft.getValues(), sb);
            }
            // followed by the '<' character.
            for (FormField f : fs) {
                sb.append(f.getVariable());
                sb.append('<');
                formFieldValuesToCaps(f.getValues(), sb);
            }
        }
    }
    // 8. Ensure that S is encoded according to the UTF-8 encoding (RFC
    // 3269).
    // 9. Compute the verification string by hashing S using the algorithm
    // specified in the 'hash' attribute (e.g., SHA-1 as defined in RFC
    // 3174).
    // The hashed data MUST be generated with binary output and
    // encoded using Base64 as specified in Section 4 of RFC 4648
    // (note: the Base64 output MUST NOT include whitespace and MUST set
    // padding bits to zero).
    byte[] bytes;
    try {
        bytes = sb.toString().getBytes(StringUtils.UTF8);
    } catch (UnsupportedEncodingException e) {
        throw new AssertionError(e);
    }
    byte[] digest;
    synchronized (md) {
        digest = md.digest(bytes);
    }
    String version = Base64.encodeToString(digest);
    return new CapsVersionAndHash(version, hash);
}
Also used : DiscoverInfo(org.jivesoftware.smackx.disco.packet.DiscoverInfo) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Feature(org.jivesoftware.smackx.disco.packet.DiscoverInfo.Feature) Identity(org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity) TreeSet(java.util.TreeSet) DataForm(org.jivesoftware.smackx.xdata.packet.DataForm) MessageDigest(java.security.MessageDigest) Identity(org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity) FormField(org.jivesoftware.smackx.xdata.FormField)

Aggregations

TreeSet (java.util.TreeSet)3795 ArrayList (java.util.ArrayList)835 Test (org.junit.Test)544 HashMap (java.util.HashMap)502 HashSet (java.util.HashSet)430 Set (java.util.Set)424 Map (java.util.Map)405 IOException (java.io.IOException)378 File (java.io.File)341 List (java.util.List)323 TreeMap (java.util.TreeMap)229 Iterator (java.util.Iterator)189 SortedSet (java.util.SortedSet)186 LinkedList (java.util.LinkedList)110 LinkedHashSet (java.util.LinkedHashSet)106 Date (java.util.Date)94 Collection (java.util.Collection)92 Comparator (java.util.Comparator)85 Test (org.testng.annotations.Test)81 Text (org.apache.hadoop.io.Text)79