|
@@ -41,20 +41,42 @@ public class NginxLogService {
|
|
|
private final String timeZone = "+08:00";
|
|
private final String timeZone = "+08:00";
|
|
|
private final NginxLogDocument nginxLogDocument;
|
|
private final NginxLogDocument nginxLogDocument;
|
|
|
private final SearchService searchService;
|
|
private final SearchService searchService;
|
|
|
- private final GeoIpTool geoIpTool;
|
|
|
|
|
- private final String geoJson;
|
|
|
|
|
|
|
+ private final AppProperties appProperties;
|
|
|
private final SnowFlake idGenerator;
|
|
private final SnowFlake idGenerator;
|
|
|
|
|
+ private GeoIpTool geoIpTool;
|
|
|
|
|
+ private String geoJson;
|
|
|
|
|
|
|
|
public NginxLogService(NginxLogDocument nginxLogDocument, SearchService searchService,
|
|
public NginxLogService(NginxLogDocument nginxLogDocument, SearchService searchService,
|
|
|
- GeoIpTool geoIpTool, AppProperties appProperties) {
|
|
|
|
|
|
|
+ AppProperties appProperties) {
|
|
|
this.nginxLogDocument = nginxLogDocument;
|
|
this.nginxLogDocument = nginxLogDocument;
|
|
|
this.searchService = searchService;
|
|
this.searchService = searchService;
|
|
|
- this.geoIpTool = geoIpTool;
|
|
|
|
|
- String geojsonPath = String.format("%s/%s", appProperties.getBaseDir(), appProperties.getGeojsonFilename());
|
|
|
|
|
- this.geoJson = new TextFile().readFile(geojsonPath);
|
|
|
|
|
|
|
+ this.appProperties = appProperties;
|
|
|
this.idGenerator = new SnowFlake(1, 1);
|
|
this.idGenerator = new SnowFlake(1, 1);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private GeoIpTool getGeoIpTool() {
|
|
|
|
|
+ if (geoIpTool == null) {
|
|
|
|
|
+ String geoipPath = String.format("%s/%s", appProperties.getBaseDir(), appProperties.getGeoipFilename());
|
|
|
|
|
+ try {
|
|
|
|
|
+ this.geoIpTool = new GeoIpTool(geoipPath);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return geoIpTool;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String getGeoJson() {
|
|
|
|
|
+ if (geoJson == null) {
|
|
|
|
|
+ String geojsonPath = String.format("%s/%s", appProperties.getBaseDir(), appProperties.getGeojsonFilename());
|
|
|
|
|
+ geoJson = new TextFile().readFile(geojsonPath);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return geoJson;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
public void processNginxLogs(List<NginxLog> list) {
|
|
public void processNginxLogs(List<NginxLog> list) {
|
|
|
if (list.isEmpty()) {
|
|
if (list.isEmpty()) {
|
|
|
return;
|
|
return;
|
|
@@ -227,7 +249,7 @@ public class NginxLogService {
|
|
|
Map<String, Map<String, Set<String>>> map = new HashMap<>();
|
|
Map<String, Map<String, Set<String>>> map = new HashMap<>();
|
|
|
groupMap1.forEach((key, value) -> {
|
|
groupMap1.forEach((key, value) -> {
|
|
|
String accessIp = key;
|
|
String accessIp = key;
|
|
|
- Location location = geoIpTool.getLocation(accessIp);
|
|
|
|
|
|
|
+ Location location = getGeoIpTool().getLocation(accessIp);
|
|
|
String country0 = location.getCountry();
|
|
String country0 = location.getCountry();
|
|
|
String[] array = country0.split("–");
|
|
String[] array = country0.split("–");
|
|
|
int len = array.length;
|
|
int len = array.length;
|
|
@@ -258,7 +280,7 @@ public class NginxLogService {
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
Map<Integer, String> geoMap = new HashMap<>();
|
|
Map<Integer, String> geoMap = new HashMap<>();
|
|
|
- JsonObject jsonObject = JsonConverter.jsonToJsonElement(geoJson).getAsJsonObject();
|
|
|
|
|
|
|
+ JsonObject jsonObject = JsonConverter.jsonToJsonElement(getGeoJson()).getAsJsonObject();
|
|
|
for (JsonElement jsonElement : jsonObject.get("features").getAsJsonArray()) {
|
|
for (JsonElement jsonElement : jsonObject.get("features").getAsJsonArray()) {
|
|
|
JsonObject propertiesObject = jsonElement.getAsJsonObject().get("properties").getAsJsonObject();
|
|
JsonObject propertiesObject = jsonElement.getAsJsonObject().get("properties").getAsJsonObject();
|
|
|
int adcode = propertiesObject.get("adcode").getAsInt();
|
|
int adcode = propertiesObject.get("adcode").getAsInt();
|