use of java.util.TreeMap in project pinot by linkedin.
the class AlertTaskRunner method runTask.
private void runTask() throws Exception {
LOG.info("Starting email report {}", alertConfig.getId());
final String collection = alertConfig.getCollection();
// Get the anomalies in that range
final List<MergedAnomalyResultDTO> allResults = anomalyMergedResultDAO.getAllByTimeEmailIdAndNotifiedFalse(windowStart.getMillis(), windowEnd.getMillis(), alertConfig.getId());
// apply filtration rule
List<MergedAnomalyResultDTO> results = AlertFilterHelper.applyFiltrationRule(allResults, alertFilterFactory);
if (results.isEmpty() && !alertConfig.isSendZeroAnomalyEmail()) {
LOG.info("Zero anomalies found, skipping sending email");
return;
}
// Group by dimension key, then sort according to anomaly result compareTo method.
Map<DimensionMap, List<MergedAnomalyResultDTO>> groupedResults = new TreeMap<>();
for (MergedAnomalyResultDTO result : results) {
DimensionMap dimensions = result.getDimensions();
if (!groupedResults.containsKey(dimensions)) {
groupedResults.put(dimensions, new ArrayList<>());
}
groupedResults.get(dimensions).add(result);
}
// sort each list of anomaly results afterwards
for (List<MergedAnomalyResultDTO> resultsByExploredDimensions : groupedResults.values()) {
Collections.sort(resultsByExploredDimensions);
}
sendAlertForAnomalies(collection, results, groupedResults);
updateNotifiedStatus(results);
}
use of java.util.TreeMap in project pinot by linkedin.
the class DataReportHelper method convertToStringKeyBasedMap.
/**
* Convert a map of "dimension map to merged anomalies" to a map of "human readable dimension string to merged
* anomalies".
*
* The dimension map is converted as follows. Assume that we have a dimension map (in Json string):
* {"country"="US","page_name"="front_page'}, then it is converted to this String: "country=US, page_name=front_page".
*
* @param groupedResults a map of dimensionMap to a group of merged anomaly results
* @return a map of "human readable dimension string to merged anomalies"
*/
public static Map<String, List<MergedAnomalyResultDTO>> convertToStringKeyBasedMap(Map<DimensionMap, List<MergedAnomalyResultDTO>> groupedResults) {
// Sorted by dimension name and value pairs
Map<String, List<MergedAnomalyResultDTO>> freemarkerGroupedResults = new TreeMap<>();
if (MapUtils.isNotEmpty(groupedResults)) {
for (Map.Entry<DimensionMap, List<MergedAnomalyResultDTO>> entry : groupedResults.entrySet()) {
DimensionMap dimensionMap = entry.getKey();
String dimensionMapString;
if (MapUtils.isNotEmpty(dimensionMap)) {
StringBuilder sb = new StringBuilder();
String dimensionValueSeparator = "";
for (Map.Entry<String, String> dimensionMapEntry : dimensionMap.entrySet()) {
sb.append(dimensionValueSeparator).append(dimensionMapEntry.getKey());
sb.append(EQUALS).append(dimensionMapEntry.getValue());
dimensionValueSeparator = DIMENSION_VALUE_SEPARATOR;
}
dimensionMapString = sb.toString();
} else {
dimensionMapString = "ALL";
}
freemarkerGroupedResults.put(dimensionMapString, entry.getValue());
}
}
return freemarkerGroupedResults;
}
use of java.util.TreeMap in project pinot by linkedin.
the class ContributorViewHandler method process.
@Override
public ContributorViewResponse process(ContributorViewRequest request) throws Exception {
TimeOnTimeComparisonRequest comparisonRequest = generateTimeOnTimeComparisonRequest(request);
TimeOnTimeComparisonHandler handler = new TimeOnTimeComparisonHandler(queryCache);
TimeOnTimeComparisonResponse response = handler.handle(comparisonRequest);
List<String> metricNames = new ArrayList<>(response.getMetrics());
List<String> expressionNames = new ArrayList<>();
for (MetricExpression expression : request.getMetricExpressions()) {
expressionNames.add(expression.getExpressionName());
}
List<String> dimensions = new ArrayList<>(response.getDimensions());
List<TimeBucket> timeBuckets = getTimeBuckets(response);
Map<String, SortedSet<Row>> rows = getRowsSortedByTime(response);
ContributorViewResponse contributorViewResponse = new ContributorViewResponse();
contributorViewResponse.setMetrics(expressionNames);
contributorViewResponse.setDimensions(dimensions);
contributorViewResponse.setTimeBuckets(timeBuckets);
GenericResponse genericResponse = new GenericResponse();
Map<String, Double[]> runningTotalMap = new HashMap<>();
// one view per <metric,dimensionName> combination
Map<String, ContributionViewTableBuilder> contributionViewTableMap = new LinkedHashMap<>();
Map<String, List<String>> dimensionValuesMap = new HashMap<>();
for (Map.Entry<String, SortedSet<Row>> entry : rows.entrySet()) {
for (Row row : entry.getValue()) {
String dimensionName = row.getDimensionName();
String dimensionValue = row.getDimensionValue();
for (Metric metric : row.getMetrics()) {
String metricName = metric.getMetricName();
if (!expressionNames.contains(metricName)) {
continue;
}
Double baselineValue = metric.getBaselineValue();
Double currentValue = metric.getCurrentValue();
Double cumulativeBaselineValue;
Double cumulativeCurrentValue;
String metricDimensionNameString = metricName + "." + dimensionName;
ContributionViewTableBuilder contributionViewTable = contributionViewTableMap.get(metricDimensionNameString);
if (contributionViewTable == null) {
contributionViewTable = new ContributionViewTableBuilder(metricName, dimensionName);
contributionViewTableMap.put(metricDimensionNameString, contributionViewTable);
}
String rowKey = metricName + "." + dimensionName + "." + dimensionValue;
if (runningTotalMap.containsKey(rowKey)) {
Double[] totalValues = runningTotalMap.get(rowKey);
cumulativeBaselineValue = totalValues[0] + baselineValue;
cumulativeCurrentValue = totalValues[1] + currentValue;
} else {
cumulativeBaselineValue = baselineValue;
cumulativeCurrentValue = currentValue;
}
TimeBucket timeBucket = TimeBucket.fromRow(row);
contributionViewTable.addEntry(dimensionValue, timeBucket, baselineValue, currentValue, cumulativeBaselineValue, cumulativeCurrentValue);
List<String> dimensionValues = dimensionValuesMap.get(dimensionName);
if (dimensionValues == null) {
dimensionValues = new ArrayList<>();
dimensionValuesMap.put(dimensionName, dimensionValues);
}
if (!dimensionValues.contains(dimensionValue)) {
dimensionValues.add(dimensionValue);
}
Double[] runningTotalPerMetric = new Double[] { cumulativeBaselineValue, cumulativeCurrentValue };
runningTotalMap.put(rowKey, runningTotalPerMetric);
}
}
}
Map<String, List<Integer>> keyToRowIdListMapping = new TreeMap<>();
List<String[]> rowData = new ArrayList<>();
// for each metric, dimension pair compute the total value for each dimension. This will be used
// to sort the dimension values
Map<String, Map<String, Map<String, Double>>> baselineTotalMapPerDimensionValue = new HashMap<>();
Map<String, Map<String, Map<String, Double>>> currentTotalMapPerDimensionValue = new HashMap<>();
for (String metricDimensionNameString : contributionViewTableMap.keySet()) {
ContributionViewTableBuilder contributionViewTable = contributionViewTableMap.get(metricDimensionNameString);
ContributionViewTable table = contributionViewTable.build();
List<ContributionCell> cells = table.getCells();
for (ContributionCell cell : cells) {
String metricName = table.getMetricName();
String dimName = table.getDimensionName();
String dimValue = cell.getDimensionValue();
String key = metricName + "|" + dimName + "|" + dimValue;
List<Integer> rowIdList = keyToRowIdListMapping.get(key);
if (rowIdList == null) {
rowIdList = new ArrayList<>();
keyToRowIdListMapping.put(key, rowIdList);
}
rowIdList.add(rowData.size());
rowData.add(cell.toArray());
// update baseline
updateTotalForDimensionValue(baselineTotalMapPerDimensionValue, metricName, dimName, dimValue, cell.getBaselineValue());
// update current
updateTotalForDimensionValue(currentTotalMapPerDimensionValue, metricName, dimName, dimValue, cell.getCurrentValue());
}
}
genericResponse.setResponseData(rowData);
genericResponse.setSchema(new ResponseSchema(ContributionCell.columns()));
genericResponse.setKeyToRowIdMapping(keyToRowIdListMapping);
Info summary = new Info();
genericResponse.setSummary(summary);
for (String dimensionName : dimensionValuesMap.keySet()) {
List<String> dimensionValues = dimensionValuesMap.get(dimensionName);
sort(expressionNames, dimensionName, dimensionValues, baselineTotalMapPerDimensionValue, currentTotalMapPerDimensionValue);
}
contributorViewResponse.setDimensionValuesMap(dimensionValuesMap);
contributorViewResponse.setResponseData(genericResponse);
contributorViewResponse.setCurrentTotalMapPerDimensionValue(currentTotalMapPerDimensionValue);
contributorViewResponse.setBaselineTotalMapPerDimensionValue(baselineTotalMapPerDimensionValue);
return contributorViewResponse;
}
use of java.util.TreeMap in project Smack by igniterealtime.
the class DNSUtil method sortSRVRecords.
/**
* Sort a given list of SRVRecords as described in RFC 2782
* Note that we follow the RFC with one exception. In a group of the same priority, only the first entry
* is calculated by random. The others are ore simply ordered by their priority.
*
* @param records
* @return the list of resolved HostAddresses
*/
private static List<HostAddress> sortSRVRecords(List<SRVRecord> records) {
// (the root domain), abort."
if (records.size() == 1 && records.get(0).getFQDN().equals("."))
return Collections.emptyList();
// sorting the records improves the performance of the bisection later
Collections.sort(records);
// create the priority buckets
SortedMap<Integer, List<SRVRecord>> buckets = new TreeMap<Integer, List<SRVRecord>>();
for (SRVRecord r : records) {
Integer priority = r.getPriority();
List<SRVRecord> bucket = buckets.get(priority);
// create the list of SRVRecords if it doesn't exist
if (bucket == null) {
bucket = new LinkedList<SRVRecord>();
buckets.put(priority, bucket);
}
bucket.add(r);
}
List<HostAddress> res = new ArrayList<HostAddress>(records.size());
for (Integer priority : buckets.keySet()) {
List<SRVRecord> bucket = buckets.get(priority);
int bucketSize;
while ((bucketSize = bucket.size()) > 0) {
int[] totals = new int[bucketSize];
int running_total = 0;
int count = 0;
int zeroWeight = 1;
for (SRVRecord r : bucket) {
if (r.getWeight() > 0) {
zeroWeight = 0;
break;
}
}
for (SRVRecord r : bucket) {
running_total += (r.getWeight() + zeroWeight);
totals[count] = running_total;
count++;
}
int selectedPos;
if (running_total == 0) {
// If running total is 0, then all weights in this priority
// group are 0. So we simply select one of the weights randomly
// as the other 'normal' algorithm is unable to handle this case
selectedPos = (int) (Math.random() * bucketSize);
} else {
double rnd = Math.random() * running_total;
selectedPos = bisect(totals, rnd);
}
// add the SRVRecord that was randomly chosen on it's weight
// to the start of the result list
SRVRecord chosenSRVRecord = bucket.remove(selectedPos);
res.add(chosenSRVRecord);
}
}
return res;
}
use of java.util.TreeMap in project Openfire by igniterealtime.
the class EmailTranscriptEvent method chatSupportFinished.
public void chatSupportFinished(Workgroup workgroup, String sessionID) {
Log.debug("Chat Support Finished, sending transcripts");
final EmailService emailService = EmailService.getInstance();
String property = JiveGlobals.getProperty("mail.configured");
if (!ModelUtil.hasLength(property)) {
Log.debug("Mail settings are not configured, transcripts will not be sent.");
return;
}
final ChatSession chatSession = ChatTranscriptManager.getChatSession(sessionID);
if (chatSession == null || chatSession.getFirstSession() == null) {
return;
}
final StringBuilder builder = new StringBuilder();
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyy hh:mm a");
// Get duration of conversation
Date date = new Date(chatSession.getFirstSession().getStartTime());
int duration = getChatDuration(date, chatSession);
TreeMap<String, List<String>> map = new TreeMap<String, List<String>>(chatSession.getMetadata());
String body = JiveGlobals.getProperty("chat.transcript.body");
if (ModelUtil.hasLength(body)) {
builder.append(body).append("\n\n");
}
builder.append("formname=chat transcript\n");
extractAndDisplay(builder, "question", map);
display(builder, "fullname", chatSession.getCustomerName());
extractAndDisplay(builder, "email", map);
extractAndDisplay(builder, "Location", map);
extractAndDisplay(builder, "userID", map);
extractAndDisplay(builder, "username", map);
extractAndDisplay(builder, "workgroup", map);
display(builder, "chatduration", String.valueOf(duration));
display(builder, "chatdate", formatter.format(date));
if (chatSession.getFirstSession() != null && chatSession.getFirstSession().getAgentJID() != null) {
try {
display(builder, "agent", new JID(chatSession.getFirstSession().getAgentJID()).toBareJID());
} catch (Exception e) {
Log.debug("Could not display agent in transcript.", e);
}
}
for (Iterator<Map.Entry<String, List<String>>> iterator = map.entrySet().iterator(); iterator.hasNext(); ) {
Map.Entry<String, List<String>> entry = iterator.next();
display(builder, entry.getKey(), getListItem(entry.getValue()));
}
builder.append("ctranscript=\n");
builder.append(ChatTranscriptManager.getTextTranscriptFromSessionID(sessionID));
String subject = JiveGlobals.getProperty("chat.transcript.subject");
String from = JiveGlobals.getProperty("chat.transcript.from");
String to = JiveGlobals.getProperty("chat.transcript.to");
if (!ModelUtil.hasLength(subject) || !ModelUtil.hasLength(from)) {
Log.debug("Transcript settings (chat.transcript.subject, chat.transcript.from) are not configured, " + "transcripts will not be sent.");
return;
}
if (ModelUtil.hasLength(to)) {
emailService.sendMessage("Chat Transcript", to, "Chat Transcript", from, subject, builder.toString(), null);
Log.debug("Transcript sent to " + to);
}
// NOTE: Do not sent to the customer. They will receive a prompt for a seperate chat transcript
// that does not contain agent information.
// Send to Agents
UserManager um = UserManager.getInstance();
for (Iterator<AgentChatSession> iterator = chatSession.getAgents(); iterator.hasNext(); ) {
AgentChatSession agentSession = iterator.next();
try {
User user = um.getUser(new JID(agentSession.getAgentJID()).getNode());
emailService.sendMessage("Chat Transcript", user.getEmail(), "Chat Transcript", from, subject, builder.toString(), null);
Log.debug("Transcript sent to agent " + agentSession.getAgentJID());
} catch (UserNotFoundException e) {
Log.error("Email Transcript Not Sent:" + "Could not load agent user object for jid " + agentSession.getAgentJID());
}
}
}
Aggregations