<!--测试类-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!--测试类-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<scope>test</scope>
</dependency>
<!--测试类-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
import com.rjgf.influxdb.RjgfInfluxdbApplication;
import com.rjgf.influxdb.domain.OdsElectricConsume;
import com.rjgf.influxdb.service.IOdsElectricConsumeService;
import com.rjgf.influxdb.service.influxdbService;
import com.rjgf.influxdb.utils.DateUtil;
import com.rjgf.influxdb.utils.InfluxDBConnection;
import com.rjgf.influxdb.utils.SnowFlake;
import com.rjgf.influxdb.utils.factory.InfluxdbFactory;
import org.influxdb.dto.QueryResult;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = RjgfInfluxdbApplication.class)
public class TestTime {
@Autowired
private influxdbService infludbService;
private final static String electricConsumeSql = "SELECT MAX(kwh) FROM \"electric_amount\" \n" +
"WHERE time >= '%s' \n" +
"AND time <= '%s' \n" +
"GROUP BY buildId,deptId,energyCategory";
@Test
public void testhourTime(){
infludbService.hourElectricityConsumption();
}
@Autowired
private IOdsElectricConsumeService odsHourElectricConsumeService;
@Test
public void testdayTime(){
Date d = new Date();
String beforeOneDay = "2022-09-17 00:00:00";
String beforeTwoDay = "2022-09-16 00:00:00";
String nowDay = "2022-09-18 00:00:00";
String nowDaySql = String.format(electricConsumeSql, beforeOneDay, nowDay);
String beforeDaySql = String.format(electricConsumeSql, beforeTwoDay, beforeOneDay);
System.out.println("nowDaySql"+nowDaySql);
System.out.println("beforeDaySql"+beforeDaySql);
List<OdsElectricConsume> odsElectricConsumeList = searchElectricityConsumption(
nowDaySql, beforeDaySql, beforeOneDay);
odsHourElectricConsumeService.insertOdsHourElectricConsumeList(odsElectricConsumeList, "ods_day_electric_consume");
}
@Test
public void testdayTimetow(){
infludbService.dayElectricityConsumption();
}
public List<OdsElectricConsume> searchElectricityConsumption(String nowSql, String beforeSql, String influxdbTime) {
InfluxDBConnection influxdbconnection = InfluxdbFactory.getInfluxdb("energy_manager_dev");
Map<Map<String, String>, List<Object>> nowMap = influxdbconnection.query(nowSql)
.getResults().get(0).getSeries()
.stream().collect(Collectors.toMap(QueryResult.Series::getTags, o -> o.getValues().get(0)));
Map<Map<String, String>, List<Object>> beforeMap = influxdbconnection.query(beforeSql)
.getResults().get(0).getSeries()
.stream().collect(Collectors.toMap(QueryResult.Series::getTags, o -> o.getValues().get(0)));
List<OdsElectricConsume> odsElectricConsumeList = new ArrayList<>();
for (Map.Entry<Map<String, String>, List<Object>> mapListEntry : nowMap.entrySet()) {
if (beforeMap.containsKey(mapListEntry.getKey())) {
double nowConsume = (double) nowMap.get(mapListEntry.getKey()).get(1);
double beforeConsume = (double) beforeMap.get(mapListEntry.getKey()).get(1);
DecimalFormat df = new DecimalFormat("#0.00");
double consume = Double.parseDouble(df.format(nowConsume - beforeConsume));
Map<String, String> tagsMap = mapListEntry.getKey();
OdsElectricConsume odsHourElectricConsume = new OdsElectricConsume(
SnowFlake.nextId(),
Integer.parseInt(tagsMap.get("buildId")),
Long.parseLong(tagsMap.get("deptId")),
tagsMap.get("energyCategory"),
DateUtil.StrToDate(influxdbTime),
consume);
odsElectricConsumeList.add(odsHourElectricConsume);
}
}
return odsElectricConsumeList;
}
}