-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
113 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 4 additions & 1 deletion
5
src/main/kotlin/cambio/monitoring/mosim/config/SearchConfiguration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
package cambio.monitoring.mosim.config | ||
|
||
import kotlin.random.Random | ||
|
||
/** | ||
* The configuration of the overall search. | ||
*/ | ||
data class SearchConfiguration( | ||
val searchWindowSize: Double = 5.0, | ||
val searchInterval: Double = 1.0, | ||
val isDebugOn: Boolean = true | ||
val isDebugOn: Boolean = true, | ||
val id: String = Random.nextInt().toString() | ||
) |
91 changes: 91 additions & 0 deletions
91
src/main/kotlin/cambio/monitoring/mosim/export/CSVFileExporter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package cambio.monitoring.mosim.export | ||
|
||
import cambio.monitoring.mosim.api.util.TempFileUtils | ||
import cambio.monitoring.mosim.config.SearchConfiguration | ||
import cambio.tltea.parser.core.temporal.TimeInstance | ||
import java.io.BufferedReader | ||
import java.io.BufferedWriter | ||
import java.io.File | ||
import java.io.FileReader | ||
|
||
class CSVFileExporter(private val monitoringCSVLoc: String, private val config: SearchConfiguration) : Exporter { | ||
private val columnSeparator: String = "," | ||
|
||
private data class MonitoringData( | ||
val headers: String, | ||
val times: MutableList<Double> = mutableListOf(), | ||
val data: MutableList<String> = mutableListOf() | ||
) | ||
|
||
override fun export(occurrences: List<Pair<TimeInstance, TimeInstance>>) { | ||
val monitoringData = readMonitoringData() | ||
for (occurrence in occurrences.withIndex()) { | ||
val startValue = occurrence.value.first.time | ||
val endValue = occurrence.value.second.time | ||
val (startIndex, endIndex) = findIndices(startValue, endValue, monitoringData) | ||
val writer = prepareOutputFile(occurrence.index) | ||
writeMonitoringData(startIndex, endIndex, monitoringData, writer) | ||
} | ||
} | ||
|
||
private fun readMonitoringData(): MonitoringData { | ||
val br = BufferedReader(FileReader(monitoringCSVLoc)) | ||
var line = br.readLine() | ||
val monitoringData = MonitoringData(line) | ||
line = br.readLine() | ||
while (line != null) { | ||
val valueInLine = line.split(columnSeparator) | ||
val time = valueInLine[0].toDouble() | ||
monitoringData.times.add(time) | ||
monitoringData.data.add(line) | ||
line = br.readLine() | ||
} | ||
return monitoringData | ||
} | ||
|
||
private fun findIndices(startValue: Double, endValue: Double, monitoringData: MonitoringData): Pair<Int, Int> { | ||
var startIndex = -1 | ||
var endIndex = 0 | ||
for (time in monitoringData.times.withIndex()) { | ||
if (!isIndexSet(startIndex) && time.value >= startValue) { | ||
startIndex = time.index | ||
} | ||
if (isIndexSet(startIndex)) { | ||
if (time.value <= endValue) { | ||
endIndex = time.index | ||
} else { | ||
break | ||
} | ||
} | ||
} | ||
return Pair(startIndex, endIndex) | ||
} | ||
|
||
private fun isIndexSet(index: Int): Boolean { | ||
return index >= 0 | ||
} | ||
|
||
private fun prepareOutputFile(occurrenceId: Int): BufferedWriter { | ||
val outputDir = TempFileUtils.getOutputDir(TempFileUtils.OUTPUT_DIR, config.id) | ||
val outputFile = File(outputDir.toFile(), "$occurrenceId.csv") | ||
outputFile.createNewFile() | ||
return outputFile.bufferedWriter() | ||
} | ||
|
||
private fun writeMonitoringData( | ||
startIndex: Int, | ||
endIndex: Int, | ||
monitoringData: MonitoringData, | ||
writer: BufferedWriter | ||
) { | ||
writer.write(monitoringData.headers) | ||
writer.newLine() | ||
val endIndexInclusive = (endIndex + 1).coerceAtMost(monitoringData.data.size) | ||
for (line in monitoringData.data.subList(startIndex, endIndexInclusive)) { | ||
writer.write(line) | ||
writer.newLine() | ||
} | ||
writer.close() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters