Commit 2294427d authored by neop's avatar neop
Browse files

Refactor in preparation for influxdb import

parent d146ad77
......@@ -31,21 +31,19 @@ class DayUploader {
Stream<Integer> results = csv_file_uris.flatMap(file_uri -> {
Main.glogger.fine("creating session in thread " + Thread.currentThread().getName());
try (
Session session = Main.driver.session(SessionConfig.forDatabase(Main.database_name));
BufferedReader csv = Util.reader_of_uri(file_uri)
) {
csv.mark(2000);
Map<String, String> one_csv_line = read_one_csv_line(csv);
csv.reset();
save_csv_to_disk(file_uri, csv);
try {
var saved_csv_file = new File(Main.csv_directory, new URL(file_uri).getFile());
download_csv_to_disk(saved_csv_file, file_uri);
// init sensor after saving to get broken csvs also
Sensor sensor = Sensor.fromCSV(file_uri, one_csv_line);
var sensor = make_sensor_from_csv(saved_csv_file, file_uri);
Main.glogger.info("uploading sensor " + file_uri);
sensor.process(session, transaction_config);
try (var session = Main.driver.session(SessionConfig.forDatabase(Main.database_name))) {
sensor.process(session, transaction_config);
}
Main.glogger.fine("uploaded sensor " + file_uri);
} catch(Exception e) {
Main.glogger.log(Level.SEVERE, "sensor " + file_uri + " not uploaded because: " + e.getMessage(), e);
try {
......@@ -70,11 +68,19 @@ class DayUploader {
};
}
private static void save_csv_to_disk(String file_uri, BufferedReader csv) throws MalformedURLException, IOException {
File saved_csv_file = new File(Main.csv_directory, new URL(file_uri).getFile());
private static Sensor make_sensor_from_csv(File saved_csv_file, String file_uri) throws Exception {
try (var csv = new BufferedReader(new FileReader(saved_csv_file))) {
return Sensor.fromCSV(file_uri, read_one_csv_line(csv));
}
}
private static void download_csv_to_disk(File saved_csv_file, String file_uri) throws MalformedURLException, IOException {
Files.createDirectories(saved_csv_file.getParentFile().toPath(), daily_folder_perms);
try (FileWriter csv_file_writer = new FileWriter(saved_csv_file)) {
Main.glogger.info("copying csv to " + saved_csv_file);
Main.glogger.info("copying csv to " + saved_csv_file);
try (
FileWriter csv_file_writer = new FileWriter(saved_csv_file);
BufferedReader csv = Util.reader_of_uri(file_uri)
) {
csv.transferTo(csv_file_writer);
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment