|
|
|
@ -38,12 +38,11 @@ open class EntryList {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the entry corresponding to the given timestamp. If no entry with such timestamp
|
|
|
|
|
* has been previously added, returns Entry(timestamp, UNKNOWN). or Entry(timestamp, SKIP) if
|
|
|
|
|
* skip days are enabled and that day is to be skipped
|
|
|
|
|
* has been previously added, returns Entry(timestamp, UNKNOWN).
|
|
|
|
|
*/
|
|
|
|
|
@Synchronized
|
|
|
|
|
open fun get(timestamp: Timestamp, skipDays: SkipDays = SkipDays.NONE): Entry {
|
|
|
|
|
return if (skipDays.isDaySkipped(timestamp)) Entry(timestamp, SKIP) else entriesByTimestamp[timestamp] ?: Entry(timestamp, UNKNOWN)
|
|
|
|
|
open fun get(timestamp: Timestamp): Entry {
|
|
|
|
|
return entriesByTimestamp[timestamp] ?: Entry(timestamp, UNKNOWN)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -52,12 +51,12 @@ open class EntryList {
|
|
|
|
|
* included.
|
|
|
|
|
*/
|
|
|
|
|
@Synchronized
|
|
|
|
|
open fun getByInterval(from: Timestamp, to: Timestamp, skipDays: SkipDays = SkipDays.NONE): List<Entry> {
|
|
|
|
|
open fun getByInterval(from: Timestamp, to: Timestamp): List<Entry> {
|
|
|
|
|
val result = mutableListOf<Entry>()
|
|
|
|
|
if (from.isNewerThan(to)) return result
|
|
|
|
|
var current = to
|
|
|
|
|
while (current >= from) {
|
|
|
|
|
result.add(get(current, skipDays))
|
|
|
|
|
result.add(get(current))
|
|
|
|
|
current = current.minus(1)
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
@ -91,18 +90,16 @@ open class EntryList {
|
|
|
|
|
open fun recomputeFrom(
|
|
|
|
|
originalEntries: EntryList,
|
|
|
|
|
frequency: Frequency,
|
|
|
|
|
isNumerical: Boolean,
|
|
|
|
|
skipDays: SkipDays
|
|
|
|
|
isNumerical: Boolean
|
|
|
|
|
) {
|
|
|
|
|
clear()
|
|
|
|
|
val original = originalEntries.getKnown()
|
|
|
|
|
if (isNumerical) {
|
|
|
|
|
val computed = addEntriesWithSkipDays(original, skipDays)
|
|
|
|
|
computed.forEach { add(it) }
|
|
|
|
|
original.forEach { add(it) }
|
|
|
|
|
} else {
|
|
|
|
|
val intervals = buildIntervals(frequency, original)
|
|
|
|
|
snapIntervalsTogether(intervals)
|
|
|
|
|
val computed = buildEntriesFromInterval(original, intervals, skipDays)
|
|
|
|
|
val computed = buildEntriesFromInterval(original, intervals)
|
|
|
|
|
computed.filter { it.value != UNKNOWN || it.notes.isNotEmpty() }.forEach { add(it) }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -131,10 +128,10 @@ open class EntryList {
|
|
|
|
|
fun computeWeekdayFrequency(isNumerical: Boolean): HashMap<Timestamp, Array<Int>> {
|
|
|
|
|
val entries = getKnown()
|
|
|
|
|
val map = hashMapOf<Timestamp, Array<Int>>()
|
|
|
|
|
for ((computedTimestamp, value) in entries) {
|
|
|
|
|
val weekday = computedTimestamp.weekday
|
|
|
|
|
for ((originalTimestamp, value) in entries) {
|
|
|
|
|
val weekday = originalTimestamp.weekday
|
|
|
|
|
val truncatedTimestamp = Timestamp(
|
|
|
|
|
computedTimestamp.toCalendar().apply {
|
|
|
|
|
originalTimestamp.toCalendar().apply {
|
|
|
|
|
set(Calendar.DAY_OF_MONTH, 1)
|
|
|
|
|
}.timeInMillis
|
|
|
|
|
)
|
|
|
|
@ -145,7 +142,7 @@ open class EntryList {
|
|
|
|
|
map[truncatedTimestamp] = list
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isNumerical && value != SKIP) {
|
|
|
|
|
if (isNumerical) {
|
|
|
|
|
list[weekday] += value
|
|
|
|
|
} else if (value == YES_MANUAL) {
|
|
|
|
|
list[weekday] += 1
|
|
|
|
@ -170,8 +167,7 @@ open class EntryList {
|
|
|
|
|
*/
|
|
|
|
|
fun buildEntriesFromInterval(
|
|
|
|
|
original: List<Entry>,
|
|
|
|
|
intervals: List<Interval>,
|
|
|
|
|
skipDays: SkipDays
|
|
|
|
|
intervals: List<Interval>
|
|
|
|
|
): List<Entry> {
|
|
|
|
|
val result = arrayListOf<Entry>()
|
|
|
|
|
if (original.isEmpty()) return result
|
|
|
|
@ -200,17 +196,15 @@ open class EntryList {
|
|
|
|
|
current = interval.end
|
|
|
|
|
while (current >= interval.begin) {
|
|
|
|
|
val offset = current.daysUntil(to)
|
|
|
|
|
result[offset] = if (skipDays.isDaySkipped(current)) Entry(current, SKIP) else Entry(current, YES_AUTO)
|
|
|
|
|
result[offset] = Entry(current, YES_AUTO)
|
|
|
|
|
current = current.minus(1)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Copy original entries except for the skipped days
|
|
|
|
|
// Copy original entries
|
|
|
|
|
original.forEach { entry ->
|
|
|
|
|
val offset = entry.timestamp.daysUntil(to)
|
|
|
|
|
val value = if (skipDays.isDaySkipped(entry)) {
|
|
|
|
|
SKIP
|
|
|
|
|
} else if (
|
|
|
|
|
val value = if (
|
|
|
|
|
result[offset].value == UNKNOWN ||
|
|
|
|
|
entry.value == SKIP ||
|
|
|
|
|
entry.value == YES_MANUAL
|
|
|
|
@ -277,29 +271,6 @@ open class EntryList {
|
|
|
|
|
}
|
|
|
|
|
return intervals
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun addEntriesWithSkipDays(original: List<Entry>, skipDays: SkipDays): List<Entry> {
|
|
|
|
|
if (original.isEmpty()) return original
|
|
|
|
|
val earliest = original.last().timestamp
|
|
|
|
|
val today = DateUtils.getTodayWithOffset()
|
|
|
|
|
val computed = mutableListOf<Entry>()
|
|
|
|
|
var current = today
|
|
|
|
|
var offset = 0
|
|
|
|
|
while (current >= earliest) {
|
|
|
|
|
if (current == original[offset].timestamp) {
|
|
|
|
|
if (!skipDays.isDaySkipped(current)) {
|
|
|
|
|
computed.add(original[offset])
|
|
|
|
|
} else {
|
|
|
|
|
computed.add(Entry(current, SKIP))
|
|
|
|
|
}
|
|
|
|
|
offset++
|
|
|
|
|
} else if (skipDays.isDaySkipped(current)) {
|
|
|
|
|
computed.add(Entry(current, SKIP))
|
|
|
|
|
}
|
|
|
|
|
current = current.minus(1)
|
|
|
|
|
}
|
|
|
|
|
return computed
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -352,12 +323,10 @@ fun List<Entry>.groupedSum(
|
|
|
|
|
*/
|
|
|
|
|
fun List<Entry>.countSkippedDays(
|
|
|
|
|
truncateField: DateUtils.TruncateField,
|
|
|
|
|
firstWeekday: Int = Calendar.SATURDAY,
|
|
|
|
|
skipDays: SkipDays
|
|
|
|
|
firstWeekday: Int = Calendar.SATURDAY
|
|
|
|
|
): List<Entry> {
|
|
|
|
|
val thisIntervalStart = DateUtils.getTodayWithOffset().truncate(truncateField, firstWeekday)
|
|
|
|
|
return this.map { (timestamp, value) ->
|
|
|
|
|
if (value == SKIP || skipDays.isDaySkipped(timestamp)) {
|
|
|
|
|
if (value == SKIP) {
|
|
|
|
|
Entry(timestamp, 1)
|
|
|
|
|
} else {
|
|
|
|
|
Entry(timestamp, 0)
|
|
|
|
@ -371,5 +340,5 @@ fun List<Entry>.countSkippedDays(
|
|
|
|
|
Entry(timestamp, entries.sumOf { it.value })
|
|
|
|
|
}.sortedBy { (timestamp, _) ->
|
|
|
|
|
-timestamp.unixTime
|
|
|
|
|
}.filter { it.timestamp == thisIntervalStart }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|