diff --git a/components/stats/SymptomOccurance.js b/components/stats/SymptomOccurance.js index 30dcf89..73388fd 100644 --- a/components/stats/SymptomOccurance.js +++ b/components/stats/SymptomOccurance.js @@ -12,15 +12,23 @@ import { Spacing, Typography, Colors } from '../../styles' // const { t } = useTranslation(null, { keyPrefix: 'stats' }) const SymptomOccurance = ({ onClose }) => { - const data = symOccModule().getCycleStartsOfLastYear() - if (!data || data.length === 0) return false - console.log('cycle starts:', data) + const cycleDays = symOccModule().getCycleStartsOfLastYear() + if (!cycleDays || cycleDays.length === 0) return false + 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 ( diff --git a/db/index.js b/db/index.js index f6f2fd0..7d45718 100644 --- a/db/index.js +++ b/db/index.js @@ -73,6 +73,9 @@ export function getTemperatureDaysSortedByDate() { .filtered('temperature != null') .sorted('date', true) } +export function getPainDaysSortedByDate() { + return db.objects('CycleDay').filtered('pain != null').sorted('date', true) +} export function getCycleDaysSortedByDate() { const cycleDays = db.objects('CycleDay').sorted('date', true) diff --git a/lib/sympto-occurance.js b/lib/sympto-occurance.js index 1716677..2ffd44e 100644 --- a/lib/sympto-occurance.js +++ b/lib/sympto-occurance.js @@ -1,44 +1,70 @@ import * as joda from '@js-joda/core' const LocalDate = joda.LocalDate -// const DAYS = joda.ChronoUnit.DAYS +const DAYS = joda.ChronoUnit.DAYS export default function config(opts) { let cycleStartsSortedByDate + let painSortedByDate if (!opts) { // we only want to require (and run) the db module // when not running the tests cycleStartsSortedByDate = require('../db').getCycleStartsSortedByDate() + painSortedByDate = require('../db').getPainDaysSortedByDate() // maxCycleLength = 45 } else { cycleStartsSortedByDate = opts.cycleStartsSortedByDate || [] + painSortedByDate = opts.painSortedByDate || [] // maxCycleLength = opts.maxCycleLength || 99 } function getCycleStartsOfLastYear() { 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 }) => - LocalDate.parse(date).isAfter(firstRelevantCycleStart) + LocalDate.parse(date).isAfter(firstRelevantDay) ) return relevantCycles.map(({ date }) => date) } - function getTodayDate() { - return new Date().toISOString().slice(0, 10) + function getPainDaysOfLastYear() { + 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 = () => - cycleStartsSortedByDate.map((day, i) => { - const today = getTodayDate() - return { - date: today, - k: i, + function getCycleDayForPainDays(cycleStarts, painDays) { + let i = 0 + const cycleStartsAsc = cycleStarts.sort().reverse() + const painDaysAsc = painDays.sort().reverse() + const painCycleDays = painDaysAsc.map((pdate) => { + 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 { getCycleStartsOfLastYear, - getStats, + getPainDaysOfLastYear, + getCycleDayForPainDays, } }