107 行
3.9 KiB
Java
107 行
3.9 KiB
Java
package com.xuqm.update.service;
|
|
|
|
import com.xuqm.update.repository.DeviceRecordRepository;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.time.LocalDateTime;
|
|
import java.util.ArrayList;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
@Service
|
|
public class UpdateStatisticsService {
|
|
|
|
private final DeviceRecordRepository deviceRecordRepository;
|
|
|
|
public UpdateStatisticsService(DeviceRecordRepository deviceRecordRepository) {
|
|
this.deviceRecordRepository = deviceRecordRepository;
|
|
}
|
|
|
|
/**
|
|
* 版本分布:各版本的设备数占比
|
|
* 返回 [{ versionCode, versionName, deviceCount }]
|
|
*/
|
|
public List<Map<String, Object>> getVersionDistribution(String appKey, String platform, int days) {
|
|
LocalDateTime since = LocalDateTime.now().minusDays(days);
|
|
List<Object[]> rows = deviceRecordRepository.findVersionDistribution(appKey, platform, since);
|
|
List<Map<String, Object>> result = new ArrayList<>();
|
|
for (Object[] row : rows) {
|
|
Map<String, Object> item = new LinkedHashMap<>();
|
|
item.put("versionCode", row[0]);
|
|
item.put("versionName", row[1]);
|
|
item.put("deviceCount", row[2]);
|
|
result.add(item);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* 设备型号分布 Top N
|
|
* 返回 [{ model, deviceCount }]
|
|
*/
|
|
public List<Map<String, Object>> getDeviceDistribution(String appKey, String platform, int days) {
|
|
LocalDateTime since = LocalDateTime.now().minusDays(days);
|
|
List<Object[]> models = deviceRecordRepository.findModelDistribution(appKey, platform, since, 10);
|
|
List<Map<String, Object>> result = new ArrayList<>();
|
|
for (Object[] row : models) {
|
|
Map<String, Object> item = new LinkedHashMap<>();
|
|
item.put("model", row[0]);
|
|
item.put("deviceCount", row[1]);
|
|
result.add(item);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* 系统版本分布
|
|
* 返回 [{ osVersion, deviceCount }]
|
|
*/
|
|
public List<Map<String, Object>> getOsVersionDistribution(String appKey, String platform, int days) {
|
|
LocalDateTime since = LocalDateTime.now().minusDays(days);
|
|
List<Object[]> rows = deviceRecordRepository.findOsVersionDistribution(appKey, platform, since, 10);
|
|
List<Map<String, Object>> result = new ArrayList<>();
|
|
for (Object[] row : rows) {
|
|
Map<String, Object> item = new LinkedHashMap<>();
|
|
item.put("osVersion", row[0]);
|
|
item.put("deviceCount", row[1]);
|
|
result.add(item);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* 厂商分布
|
|
* 返回 [{ vendor, deviceCount }]
|
|
*/
|
|
public List<Map<String, Object>> getVendorDistribution(String appKey, String platform, int days) {
|
|
LocalDateTime since = LocalDateTime.now().minusDays(days);
|
|
List<Object[]> rows = deviceRecordRepository.findVendorDistribution(appKey, platform, since);
|
|
List<Map<String, Object>> result = new ArrayList<>();
|
|
for (Object[] row : rows) {
|
|
Map<String, Object> item = new LinkedHashMap<>();
|
|
item.put("vendor", row[0]);
|
|
item.put("deviceCount", row[1]);
|
|
result.add(item);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* 每日检测趋势
|
|
* 返回 [{ day, deviceCount }]
|
|
*/
|
|
public List<Map<String, Object>> getDailyTrend(String appKey, String platform, int days) {
|
|
LocalDateTime since = LocalDateTime.now().minusDays(days);
|
|
List<Object[]> rows = deviceRecordRepository.findDailyTrend(appKey, platform, since);
|
|
List<Map<String, Object>> result = new ArrayList<>();
|
|
for (Object[] row : rows) {
|
|
Map<String, Object> item = new LinkedHashMap<>();
|
|
item.put("day", row[0] != null ? row[0].toString() : "");
|
|
item.put("deviceCount", row[1]);
|
|
result.add(item);
|
|
}
|
|
return result;
|
|
}
|
|
}
|