-
Notifications
You must be signed in to change notification settings - Fork 658
export fetched data into a csv file #777
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package nodomain.freeyourgadget.gadgetbridge.util; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
|
||
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider; | ||
import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample; | ||
|
||
/** | ||
* Created by Vebryn on 29/05/17. | ||
*/ | ||
|
||
public class CSVExport { | ||
private static final Logger LOG = LoggerFactory.getLogger(SampleProvider.class); | ||
|
||
public static void exportToCSV(AbstractActivitySample[] activitySamples, File outFile) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's sufficient to use |
||
String separator = ","; | ||
|
||
LOG.info("Exporting samples into csv file: " + outFile.getName()); | ||
try (BufferedWriter bw = new BufferedWriter(new FileWriter(outFile))) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I've spent too much of my lifetime on encoding bugs already (I actually have a T-Shirt with the text "Schei? Encoding". For the record, Excel' CSV import does CP-1252 (similar to ISO-8859-1), LibreOffice can handle all kinds of encodings including UTF-8. |
||
bw.write("TIMESTAMP" + separator + "DEVICE_ID" + separator + "USER_ID" + separator + "RAW_INTENSITY" + separator + "STEPS" + separator + "RAW_KIND" + separator + "HEART_RATE"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we also export the normalized values? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Normalized values are deduced from a simple matching rule. I think correlation have to be done after exportation. |
||
bw.newLine(); | ||
|
||
for (AbstractActivitySample sample : activitySamples){ | ||
String line = sample.getTimestamp() + separator + sample.getDeviceId() + separator + sample.getUserId() + separator + sample.getRawIntensity() + separator + sample.getSteps() + separator + sample.getRawKind() + separator + sample.getHeartRate(); | ||
|
||
//LOG.debug("Adding line into buffer: " + line); | ||
bw.write(line); | ||
bw.newLine(); | ||
} | ||
|
||
bw.flush(); | ||
} catch (IOException e) { | ||
LOG.error("Error related to " + outFile.getName() + " file: " + e.getMessage(), e); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add a
gadgetbridgedb_
prefix (or something like that, maybe shorter)?But really, this does not belong here. It should be implemented in one place for all devices, not in the Mi Band 2 FetchActivityOperation. And I guess we should also have an option to turn this on and off.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, does data from other devices differs ? Where would you place on/off feature ? I would probably add into Mi Band menu since it's the only device that's supporting it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Data from other devices may differ, yes. So either we use normalized values (= the same data ranges for all devices) or device specific raw values, or even both.
But it would be good to have this for all devices, not just one.
Since some devices provide the data in a different way, where we have no access to all samples at once, we might call a method like
samplesAdded(device, start, end)
which would then check if CSV export is enabled, then fetch the samples from the given date range from the db (probably cached in the session already) and perform the export.Alternatively we could integrate with the new DB export functionality and perform the CSV export repeatedly. We would just need to remember the start and end dates so that we only export the incremental changes.