Autopsy 4.22.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
ExportGeolocation.java
Go to the documentation of this file.
1/*
2 * Autopsy Forensic Browser
3 *
4 * Copyright 2021 Basis Technology Corp.
5 * Contact: carrier <at> sleuthkit <dot> org
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19package org.sleuthkit.autopsy.report.modules.datasourcesummaryexport;
20
21import java.util.ArrayList;
22import java.util.Arrays;
23import java.util.Collections;
24import java.util.List;
25import java.util.stream.Collectors;
26import java.util.stream.Stream;
27import org.apache.commons.lang3.StringUtils;
28import org.apache.commons.lang3.tuple.Pair;
29import org.openide.util.NbBundle.Messages;
30import org.sleuthkit.autopsy.datasourcesummary.datamodel.DataFetcher;
31import org.sleuthkit.autopsy.datasourcesummary.datamodel.GeolocationSummary.CityCountsList;
32import org.sleuthkit.autopsy.datasourcesummary.datamodel.GeolocationSummary.CityData;
33import org.sleuthkit.autopsy.datasourcesummary.datamodel.GeolocationSummary.CityRecordCount;
34import org.sleuthkit.autopsy.datasourcesummary.datamodel.CityRecord;
35import org.sleuthkit.autopsy.datasourcesummary.datamodel.GeolocationSummary;
36import static org.sleuthkit.autopsy.report.modules.datasourcesummaryexport.ExcelExportAction.getFetchResult;
37import static org.sleuthkit.autopsy.report.modules.datasourcesummaryexport.ExcelExportAction.getTableExport;
38import org.sleuthkit.datamodel.DataSource;
39
43@Messages({
44 "ExportGeolocation_cityColumn_title=Closest City",
45 "ExportGeolocation_countColumn_title=Count",
46 "ExportGeolocation_unknownRow_title=Unknown",
47 "ExportGeolocation_mostCommon_tabName=Most Common Cities",
48 "ExportGeolocation_mostRecent_tabName=Most Recent Cities",})
49class ExportGeolocation {
50
51 private final GeolocationSummary geoSummary;
52
56 private static class GeolocationData {
57
58 private final List<Pair<String, Integer>> mostRecentData;
59 private final List<Pair<String, Integer>> mostCommonData;
60
69 GeolocationData(List<Pair<String, Integer>> mostRecentData, List<Pair<String, Integer>> mostCommonData) {
70 this.mostRecentData = mostRecentData;
71 this.mostCommonData = mostCommonData;
72 }
73
79 List<Pair<String, Integer>> getMostRecentData() {
80 return mostRecentData;
81 }
82
88 List<Pair<String, Integer>> getMostCommonData() {
89 return mostCommonData;
90 }
91 }
92
93 private static final int DAYS_COUNT = 30;
94 private static final int MAX_COUNT = 10;
95
96 // The column indicating the city
97 private static final ColumnModel<Pair<String, Integer>, DefaultCellModel<?>> CITY_COL = new ColumnModel<>(
98 Bundle.ExportGeolocation_cityColumn_title(),
99 (pair) -> new DefaultCellModel<>(pair.getLeft()),
100 300
101 );
102
103 // The column indicating the count of points seen close to that city
104 private static final ColumnModel<Pair<String, Integer>, DefaultCellModel<?>> COUNT_COL = new ColumnModel<>(
105 Bundle.ExportGeolocation_countColumn_title(),
106 (pair) -> new DefaultCellModel<>(pair.getRight()),
107 100
108 );
109
110 private static final List<ColumnModel<Pair<String, Integer>, DefaultCellModel<?>>> DEFAULT_TEMPLATE = Arrays.asList(
111 CITY_COL,
112 COUNT_COL
113 );
114
115 ExportGeolocation() {
116 geoSummary = new GeolocationSummary();
117 }
118
126 private static String getCityName(CityRecord record) {
127 if (record == null) {
128 return null;
129 }
130
131 List<String> cityIdentifiers = Stream.of(record.getCityName(), record.getState(), record.getCountry())
132 .filter(StringUtils::isNotBlank)
133 .collect(Collectors.toList());
134
135 if (cityIdentifiers.size() == 1) {
136 return cityIdentifiers.get(0);
137 } else if (cityIdentifiers.size() == 2) {
138 return String.format("%s, %s", cityIdentifiers.get(0), cityIdentifiers.get(1));
139 } else if (cityIdentifiers.size() >= 3) {
140 return String.format("%s, %s; %s", cityIdentifiers.get(0), cityIdentifiers.get(1), cityIdentifiers.get(2));
141 }
142
143 return null;
144 }
145
154 private static Pair<String, Integer> formatRecord(CityRecordCount cityCount) {
155 if (cityCount == null) {
156 return null;
157 }
158
159 String cityName = getCityName(cityCount.getCityRecord());
160 int count = cityCount.getCount();
161 return Pair.of(cityName, count);
162 }
163
174 private static List<Pair<String, Integer>> formatList(CityCountsList countsList) {
175 if (countsList == null) {
176 return Collections.emptyList();
177 }
178
179 Stream<CityRecordCount> countsStream = ((countsList.getCounts() == null)
180 ? new ArrayList<CityRecordCount>()
181 : countsList.getCounts()).stream();
182
183 Stream<Pair<String, Integer>> pairStream = countsStream.map((r) -> formatRecord(r));
184
185 Pair<String, Integer> unknownRecord = Pair.of(Bundle.ExportGeolocation_unknownRow_title(), countsList.getOtherCount());
186
187 return Stream.concat(pairStream, Stream.of(unknownRecord))
188 .filter((p) -> p != null && p.getRight() != null && p.getRight() > 0)
189 .sorted((a, b) -> -Integer.compare(a.getRight(), b.getRight()))
190 .limit(MAX_COUNT)
191 .collect(Collectors.toList());
192 }
193
202 private static GeolocationData convertToViewModel(CityData cityData) {
203 if (cityData == null) {
204 return new GeolocationData(Collections.emptyList(), Collections.emptyList());
205 } else {
206 return new GeolocationData(formatList(cityData.getMostRecent()), formatList(cityData.getMostCommon()));
207 }
208 }
209
210 List<ExcelExport.ExcelSheetExport> getExports(DataSource dataSource) {
211
212 DataFetcher<DataSource, GeolocationData> geolocationFetcher = (ds) -> convertToViewModel(geoSummary.getCityCounts(ds, DAYS_COUNT, MAX_COUNT));
213
214 GeolocationData model
215 = getFetchResult(geolocationFetcher, "Geolocation sheets", dataSource);
216 if (model == null) {
217 return Collections.emptyList();
218 }
219
220 return Arrays.asList(getTableExport(DEFAULT_TEMPLATE,
221 Bundle.ExportGeolocation_mostRecent_tabName(), model.getMostRecentData()),
222 getTableExport(DEFAULT_TEMPLATE,
223 Bundle.ExportGeolocation_mostCommon_tabName(), model.getMostCommonData())
224 );
225 }
226
227}

Copyright © 2012-2024 Sleuth Kit Labs. Generated on:
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.