calculates cycle days for headaches
This commit is contained in:
@@ -12,15 +12,23 @@ import { Spacing, Typography, Colors } from '../../styles'
|
|||||||
// const { t } = useTranslation(null, { keyPrefix: 'stats' })
|
// const { t } = useTranslation(null, { keyPrefix: 'stats' })
|
||||||
|
|
||||||
const SymptomOccurance = ({ onClose }) => {
|
const SymptomOccurance = ({ onClose }) => {
|
||||||
const data = symOccModule().getCycleStartsOfLastYear()
|
const cycleDays = symOccModule().getCycleStartsOfLastYear()
|
||||||
if (!data || data.length === 0) return false
|
if (!cycleDays || cycleDays.length === 0) return false
|
||||||
console.log('cycle starts:', data)
|
console.log('cycle starts:', cycleDays)
|
||||||
|
|
||||||
|
const headacheDays = symOccModule().getPainDaysOfLastYear()
|
||||||
|
console.log('pain', headacheDays)
|
||||||
|
|
||||||
|
const cycleDaysOfPain = symOccModule().getCycleDayForPainDays(
|
||||||
|
cycleDays,
|
||||||
|
headacheDays
|
||||||
|
)
|
||||||
|
console.log('cycle days of pain', cycleDaysOfPain)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AppModal onClose={onClose}>
|
<AppModal onClose={onClose}>
|
||||||
<View>
|
<View>
|
||||||
<FlatList
|
<FlatList
|
||||||
data={data}
|
|
||||||
ListHeaderComponent={FlatListHeader}
|
ListHeaderComponent={FlatListHeader}
|
||||||
contentContainerStyle={styles.container}
|
contentContainerStyle={styles.container}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -73,6 +73,9 @@ export function getTemperatureDaysSortedByDate() {
|
|||||||
.filtered('temperature != null')
|
.filtered('temperature != null')
|
||||||
.sorted('date', true)
|
.sorted('date', true)
|
||||||
}
|
}
|
||||||
|
export function getPainDaysSortedByDate() {
|
||||||
|
return db.objects('CycleDay').filtered('pain != null').sorted('date', true)
|
||||||
|
}
|
||||||
|
|
||||||
export function getCycleDaysSortedByDate() {
|
export function getCycleDaysSortedByDate() {
|
||||||
const cycleDays = db.objects('CycleDay').sorted('date', true)
|
const cycleDays = db.objects('CycleDay').sorted('date', true)
|
||||||
|
|||||||
+38
-12
@@ -1,44 +1,70 @@
|
|||||||
import * as joda from '@js-joda/core'
|
import * as joda from '@js-joda/core'
|
||||||
const LocalDate = joda.LocalDate
|
const LocalDate = joda.LocalDate
|
||||||
// const DAYS = joda.ChronoUnit.DAYS
|
const DAYS = joda.ChronoUnit.DAYS
|
||||||
|
|
||||||
export default function config(opts) {
|
export default function config(opts) {
|
||||||
let cycleStartsSortedByDate
|
let cycleStartsSortedByDate
|
||||||
|
let painSortedByDate
|
||||||
|
|
||||||
if (!opts) {
|
if (!opts) {
|
||||||
// we only want to require (and run) the db module
|
// we only want to require (and run) the db module
|
||||||
// when not running the tests
|
// when not running the tests
|
||||||
cycleStartsSortedByDate = require('../db').getCycleStartsSortedByDate()
|
cycleStartsSortedByDate = require('../db').getCycleStartsSortedByDate()
|
||||||
|
painSortedByDate = require('../db').getPainDaysSortedByDate()
|
||||||
// maxCycleLength = 45
|
// maxCycleLength = 45
|
||||||
} else {
|
} else {
|
||||||
cycleStartsSortedByDate = opts.cycleStartsSortedByDate || []
|
cycleStartsSortedByDate = opts.cycleStartsSortedByDate || []
|
||||||
|
painSortedByDate = opts.painSortedByDate || []
|
||||||
// maxCycleLength = opts.maxCycleLength || 99
|
// maxCycleLength = opts.maxCycleLength || 99
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCycleStartsOfLastYear() {
|
function getCycleStartsOfLastYear() {
|
||||||
const today = LocalDate.parse(new Date().toISOString().slice(0, 10))
|
const today = LocalDate.parse(new Date().toISOString().slice(0, 10))
|
||||||
const firstRelevantCycleStart = today.minusYears(1)
|
const firstRelevantDay = today.minusYears(1)
|
||||||
const relevantCycles = cycleStartsSortedByDate.filter(({ date }) =>
|
const relevantCycles = cycleStartsSortedByDate.filter(({ date }) =>
|
||||||
LocalDate.parse(date).isAfter(firstRelevantCycleStart)
|
LocalDate.parse(date).isAfter(firstRelevantDay)
|
||||||
)
|
)
|
||||||
return relevantCycles.map(({ date }) => date)
|
return relevantCycles.map(({ date }) => date)
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTodayDate() {
|
function getPainDaysOfLastYear() {
|
||||||
return new Date().toISOString().slice(0, 10)
|
const today = LocalDate.parse(new Date().toISOString().slice(0, 10))
|
||||||
|
const firstRelevantDay = today.minusYears(1)
|
||||||
|
const relevantPainDays = painSortedByDate.filter(
|
||||||
|
({ date, pain }) =>
|
||||||
|
LocalDate.parse(date).isAfter(firstRelevantDay) && pain.headache
|
||||||
|
)
|
||||||
|
return relevantPainDays.map(({ date }) => date)
|
||||||
}
|
}
|
||||||
|
|
||||||
const getStats = () =>
|
function getCycleDayForPainDays(cycleStarts, painDays) {
|
||||||
cycleStartsSortedByDate.map((day, i) => {
|
let i = 0
|
||||||
const today = getTodayDate()
|
const cycleStartsAsc = cycleStarts.sort().reverse()
|
||||||
return {
|
const painDaysAsc = painDays.sort().reverse()
|
||||||
date: today,
|
const painCycleDays = painDaysAsc.map((pdate) => {
|
||||||
k: i,
|
if (LocalDate.parse(pdate).isBefore(LocalDate.parse(cycleStartsAsc[i]))) {
|
||||||
|
// increase index i until cycleStart of this painDay is found
|
||||||
|
for (let j = i + 1; j < cycleStartsAsc.length; j++) {
|
||||||
|
i = j
|
||||||
|
if (
|
||||||
|
!LocalDate.parse(cycleStartsAsc[i]).isAfter(LocalDate.parse(pdate))
|
||||||
|
) {
|
||||||
|
// not(C > P) === C ≤ P
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return LocalDate.parse(cycleStartsAsc[i]).until(
|
||||||
|
LocalDate.parse(pdate),
|
||||||
|
DAYS
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
return painCycleDays
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
getCycleStartsOfLastYear,
|
getCycleStartsOfLastYear,
|
||||||
getStats,
|
getPainDaysOfLastYear,
|
||||||
|
getCycleDayForPainDays,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user