Search in sources :

Example 6 with VaultEntry

use of de.opendiabetes.vault.container.VaultEntry in project BachelorPraktikum by lucasbuschlinger.

the class SlidingWindow method updateValue1WindowElevation.

/**
 * Updates the internal buffer and calculates the value 1 elevation (absolute).
 *
 * @param entry The VaultEntry to update.
 * @return Absolute elevation within the window or 0 if wrong type or buffer
 * not statuated.
 */
public double updateValue1WindowElevation(final VaultEntry entry) {
    if (entry.getType() != typeToReactOn) {
        return 0.0;
    }
    buffer.add(entry);
    // Clean buffer according to window size
    Date bufferStartTime = TimestampUtils.addMinutesToTimestamp(entry.getTimestamp(), -windowSizeInMinutes);
    ArrayList<VaultEntry> itemsToKill = new ArrayList<>();
    for (VaultEntry item : buffer) {
        if (item.getTimestamp().before(bufferStartTime)) {
            // Remove items which are to old for the window
            itemsToKill.add(item);
        } else {
            if (itemsToKill.isEmpty()) {
                // Buffer is not saturated --> no calculation
                return 0.0;
            } else {
                // Reached values in window
                break;
            }
        }
    }
    buffer.removeAll(itemsToKill);
    // Calculate elevation
    double elevation = 0.0;
    double lastValue = 0.0;
    for (VaultEntry item : buffer) {
        if (lastValue == 0.0) {
            // Warm up
            lastValue = item.getValue();
        } else {
            // Calculate elevation
            elevation += item.getValue() - lastValue;
            lastValue = item.getValue();
        }
    }
    // Return with small filtering
    if (Math.abs(elevation) > outputFilterSize) {
        return elevation;
    } else {
        return 0.0;
    }
}
Also used : VaultEntry(de.opendiabetes.vault.container.VaultEntry) ArrayList(java.util.ArrayList) Date(java.util.Date)

Example 7 with VaultEntry

use of de.opendiabetes.vault.container.VaultEntry in project BachelorPraktikum by lucasbuschlinger.

the class AbstractFileImporter method importData.

/**
 * Imports the data from a specified file path.
 *
 * @param filePath File path from which the data should be imported to
 * @return List of VaultEntry consisting of the imported data.
 * @throws Exception Thrown if there was an error reading the file
 */
public List<VaultEntry> importData(final String filePath) throws Exception {
    if (filePath == null) {
        LOG.log(Level.SEVERE, "File path cannot be empty.");
        throw new IllegalArgumentException("File path cannot be empty.");
    }
    File file = new File(filePath);
    if (!file.exists()) {
        LOG.log(Level.SEVERE, "File at given path does not exist.");
        throw new FileNotFoundException("File at given path does not exist.");
    }
    preprocessingIfNeeded(filePath);
    this.notifyStatus(0, "Preprocessing done.");
    FileInputStream inputStream = null;
    List<VaultEntry> result = null;
    try {
        inputStream = new FileInputStream(filePath);
        result = processImport(inputStream, filePath);
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
    }
    return result;
}
Also used : VaultEntry(de.opendiabetes.vault.container.VaultEntry) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 8 with VaultEntry

use of de.opendiabetes.vault.container.VaultEntry in project BachelorPraktikum by lucasbuschlinger.

the class VaultDAO method removeDuplicates.

/**
 * Searches the database for duplicate entries and removes them accordingly.
 *
 * @return True if no duplicate entries were found or all duplicate entries were successfully removed from the database.
 *          False if a duplicate entry could not be removed.
 */
public boolean removeDuplicates() {
    // but we need a workaround for the or mapper
    try {
        PreparedQuery<VaultEntry> query = vaultDao.queryBuilder().orderBy("timestamp", true).prepare();
        CloseableIterator<VaultEntry> iterator = vaultDao.iterator(query);
        Date startGenerationTimestamp = null;
        List<VaultEntry> tmpList = new ArrayList<>();
        List<Long> duplicateId = new ArrayList<>();
        while (iterator.hasNext()) {
            VaultEntry entry = iterator.next();
            if (startGenerationTimestamp == null) {
                // start up
                startGenerationTimestamp = entry.getTimestamp();
                tmpList.add(entry);
            } else if (!startGenerationTimestamp.equals(entry.getTimestamp())) {
                // not same timestamp --> new line generation
                startGenerationTimestamp = entry.getTimestamp();
                tmpList.clear();
                tmpList.add(entry);
            } else {
                // same timestamp --> check if it is a duplicate
                for (VaultEntry item : tmpList) {
                    if (item.equals(entry)) {
                        // duplicate --> delete and move on
                        duplicateId.add(entry.getId());
                        break;
                    }
                }
            }
        }
        // delete duplicates
        int lines = vaultDao.deleteIds(duplicateId);
        LOG.log(Level.INFO, "Removed {0} duplicates", lines);
    } catch (SQLException exception) {
        LOG.log(Level.SEVERE, "Error while db query", exception);
        return false;
    }
    return true;
}
Also used : SQLException(java.sql.SQLException) VaultEntry(de.opendiabetes.vault.container.VaultEntry) ArrayList(java.util.ArrayList) Date(java.util.Date)

Aggregations

VaultEntry (de.opendiabetes.vault.container.VaultEntry)8 ArrayList (java.util.ArrayList)6 Date (java.util.Date)5 SQLException (java.sql.SQLException)3 CsvReader (com.csvreader.CsvReader)1 ExportEntry (de.opendiabetes.vault.container.csv.ExportEntry)1 VaultCSVEntry (de.opendiabetes.vault.container.csv.VaultCSVEntry)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1