Autopsy  4.8.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
CommonAttributeSearchResults.java
Go to the documentation of this file.
1 /*
2  *
3  * Autopsy Forensic Browser
4  *
5  * Copyright 2018 Basis Technology Corp.
6  * Contact: carrier <at> sleuthkit <dot> org
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 package org.sleuthkit.autopsy.commonfilesearch;
21 
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Map.Entry;
28 import java.util.logging.Level;
34 
39 final public class CommonAttributeSearchResults {
40 
41  private static final Logger LOGGER = Logger.getLogger(CommonAttributeSearchResults.class.getName());
42 
43  // maps instance count to list of attribute values.
44  private final Map<Integer, CommonAttributeValueList> instanceCountToAttributeValues;
45 
46  private final int percentageThreshold;
47  private final int resultTypeId;
48 
55  CommonAttributeSearchResults(Map<Integer, CommonAttributeValueList> metadata, int percentageThreshold, CorrelationAttributeInstance.Type resultType) {
56  //wrap in a new object in case any client code has used an unmodifiable collection
57  this.instanceCountToAttributeValues = new HashMap<>(metadata);
58  this.percentageThreshold = percentageThreshold;
59  this.resultTypeId = resultType.getId();
60  }
61 
68  CommonAttributeSearchResults(Map<Integer, CommonAttributeValueList> metadata, int percentageThreshold) {
69  //wrap in a new object in case any client code has used an unmodifiable collection
70  this.instanceCountToAttributeValues = new HashMap<>(metadata);
71  this.percentageThreshold = percentageThreshold;
72  this.resultTypeId = CorrelationAttributeInstance.FILES_TYPE_ID;
73  }
74 
84  CommonAttributeValueList getAttributeValuesForInstanceCount(Integer instanceCount) {
85  return this.instanceCountToAttributeValues.get(instanceCount);
86  }
87 
95  public Map<Integer, CommonAttributeValueList> getMetadata() throws EamDbException {
96  if(this.percentageThreshold == 0){
97  return Collections.unmodifiableMap(this.instanceCountToAttributeValues);
98  } else {
99  return this.getMetadata(this.percentageThreshold);
100  }
101  }
102 
113  private Map<Integer, CommonAttributeValueList> getMetadata(int maximumPercentageThreshold) throws EamDbException {
114 
115  if(maximumPercentageThreshold == 0){
116  return Collections.unmodifiableMap(this.instanceCountToAttributeValues);
117  }
118 
121  .stream()
122  .filter(filterType -> filterType.getId() == this.resultTypeId)
123  .findFirst().get();
124 
125  EamDb eamDb = EamDb.getInstance();
126 
127  Map<Integer, List<CommonAttributeValue>> itemsToRemove = new HashMap<>();
128  //Call countUniqueDataSources once to reduce the number of DB queries needed to get
129  //the frequencyPercentage
130  Double uniqueCaseDataSourceTuples = eamDb.getCountUniqueDataSources().doubleValue();
131 
132  for(Entry<Integer, CommonAttributeValueList> listOfValues : Collections.unmodifiableMap(this.instanceCountToAttributeValues).entrySet()){
133 
134  final Integer key = listOfValues.getKey();
135  final CommonAttributeValueList values = listOfValues.getValue();
136 
137  for(CommonAttributeValue value : values.getDelayedMetadataList()){ // Need the real metadata
138 
139  try {
140  Double uniqueTypeValueTuples = eamDb.getCountUniqueCaseDataSourceTuplesHavingTypeValue(
141  attributeType, value.getValue()).doubleValue();
142  Double commonalityPercentage = uniqueTypeValueTuples / uniqueCaseDataSourceTuples * 100;
143  int frequencyPercentage = commonalityPercentage.intValue();
144 
145  if(frequencyPercentage > maximumPercentageThreshold){
146  if(itemsToRemove.containsKey(key)){
147  itemsToRemove.get(key).add(value);
148  } else {
149  List<CommonAttributeValue> toRemove = new ArrayList<>();
150  toRemove.add(value);
151  itemsToRemove.put(key, toRemove);
152  }
153  }
155  LOGGER.log(Level.WARNING, "Unable to determine frequency percentage attribute - frequency filter may not be accurate for these results.", ex);
156  }
157  }
158  }
159 
160  for(Entry<Integer, List<CommonAttributeValue>> valuesToRemove : itemsToRemove.entrySet()){
161 
162  final Integer key = valuesToRemove.getKey();
163  final List<CommonAttributeValue> values = valuesToRemove.getValue();
164 
165  for (CommonAttributeValue value : values){
166  final CommonAttributeValueList instanceCountValue = this.instanceCountToAttributeValues.get(key);
167  instanceCountValue.removeMetaData(value);
168 
169  if(instanceCountValue.getDelayedMetadataList().isEmpty()){ // Check the real metadata
170  this.instanceCountToAttributeValues.remove(key);
171  }
172  }
173  }
174 
175  return Collections.unmodifiableMap(this.instanceCountToAttributeValues);
176  }
177 
183  public int size() {
184 
185  int count = 0;
186  for (CommonAttributeValueList data : this.instanceCountToAttributeValues.values()) {
187  for(CommonAttributeValue md5 : data.getDelayedMetadataList()){
188  count += md5.getInstanceCount();
189  }
190  }
191  return count;
192  }
193 }
Long getCountUniqueCaseDataSourceTuplesHavingTypeValue(CorrelationAttributeInstance.Type aType, String value)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
Map< Integer, CommonAttributeValueList > getMetadata(int maximumPercentageThreshold)

Copyright © 2012-2018 Basis Technology. Generated on: Thu Oct 4 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.