Merge branch '235-add-cycle-start-flag-in-db' into 'master'

Resolve "add cycle start flag in db"

Closes #235

See merge request bloodyhealth/drip!100
This commit is contained in:
Julia Friesel
2018-11-02 12:55:48 +00:00
8 changed files with 992 additions and 273 deletions
+3 -2
View File
@@ -9,16 +9,17 @@ import cycleModule from '../lib/cycle'
import {getCycleLengthStats as getCycleInfo} from '../lib/cycle-length' import {getCycleLengthStats as getCycleInfo} from '../lib/cycle-length'
import {stats as labels} from './labels' import {stats as labels} from './labels'
import AppText from './app-text' import AppText from './app-text'
import { getCycleStartsSortedByDate } from '../db'
export default class Stats extends Component { export default class Stats extends Component {
render() { render() {
const allMensesStarts = cycleModule().getAllMensesStarts() const allMensesStarts = getCycleStartsSortedByDate()
const atLeastOneCycle = allMensesStarts.length > 1 const atLeastOneCycle = allMensesStarts.length > 1
let cycleLengths let cycleLengths
let numberOfCycles let numberOfCycles
let cycleInfo let cycleInfo
if (atLeastOneCycle) { if (atLeastOneCycle) {
cycleLengths = cycleModule().getCycleLength(allMensesStarts) cycleLengths = cycleModule().getAllCycleLengths()
numberOfCycles = cycleLengths.length numberOfCycles = cycleLengths.length
if (numberOfCycles > 1) { if (numberOfCycles > 1) {
cycleInfo = getCycleInfo(cycleLengths) cycleInfo = getCycleInfo(cycleLengths)
+65 -5
View File
@@ -4,8 +4,11 @@ import nodejs from 'nodejs-mobile-react-native'
import fs from 'react-native-fs' import fs from 'react-native-fs'
import restart from 'react-native-restart' import restart from 'react-native-restart'
import schemas from './schemas' import schemas from './schemas'
import cycleModule from '../lib/cycle'
let db let db
let isMensesStart
let getMensesDaysRightAfter
export async function openDb ({ hash, persistConnection }) { export async function openDb ({ hash, persistConnection }) {
const realmConfig = {} const realmConfig = {}
@@ -32,9 +35,11 @@ export async function openDb ({ hash, persistConnection }) {
)) ))
if (persistConnection) db = connection if (persistConnection) db = connection
const cycle = cycleModule()
isMensesStart = cycle.isMensesStart
getMensesDaysRightAfter = cycle.getMensesDaysRightAfter
} }
export function getBleedingDaysSortedByDate() { export function getBleedingDaysSortedByDate() {
return db.objects('CycleDay').filtered('bleeding != null').sorted('date', true) return db.objects('CycleDay').filtered('bleeding != null').sorted('date', true)
} }
@@ -45,9 +50,61 @@ export function getCycleDaysSortedByDate() {
return db.objects('CycleDay').sorted('date', true) return db.objects('CycleDay').sorted('date', true)
} }
export function getCycleStartsSortedByDate() {
return db.objects('CycleDay').filtered('isCycleStart = true').sorted('date', true)
}
export function saveSymptom(symptom, cycleDay, val) { export function saveSymptom(symptom, cycleDay, val) {
db.write(() => { db.write(() => {
if (bleedingValueDeleted(symptom, val)) {
cycleDay.bleeding = val
cycleDay.isCycleStart = false
maybeSetNewCycleStart(cycleDay, val)
} else if (bleedingValueAddedOrChanged(symptom, val)) {
cycleDay.bleeding = val
cycleDay.isCycleStart = isMensesStart(cycleDay)
maybeClearOldCycleStarts(cycleDay)
} else {
cycleDay[symptom] = val cycleDay[symptom] = val
}
})
function bleedingValueDeleted(symptom, val) {
return symptom === 'bleeding' && !val
}
function bleedingValueAddedOrChanged(symptom, val) {
return symptom === 'bleeding' && val
}
function maybeSetNewCycleStart(dayWithDeletedBleeding) {
// if a bleeding value is deleted, we need to check if
// there are any following bleeding days and if the
// next one of them is now a cycle start
const mensesDaysAfter = getMensesDaysRightAfter(dayWithDeletedBleeding)
if (!mensesDaysAfter.length) return
const nextOne = mensesDaysAfter[mensesDaysAfter.length - 1]
if (isMensesStart(nextOne)) {
nextOne.isCycleStart = true
}
}
function maybeClearOldCycleStarts(cycleDay) {
// if we have a new bleeding day, we need to clear the
// menses start marker from all following days of this
// menses that may have been marked as start before
const mensesDaysAfter = getMensesDaysRightAfter(cycleDay)
mensesDaysAfter.forEach(day => day.isCycleStart = false)
}
}
export function updateCycleStartsForAllCycleDays() {
db.write(() => {
getBleedingDaysSortedByDate().forEach(day => {
if (isMensesStart(day)) {
day.isCycleStart = true
}
})
}) })
} }
@@ -56,7 +113,8 @@ export function getOrCreateCycleDay(localDate) {
if (!result) { if (!result) {
db.write(() => { db.write(() => {
result = db.create('CycleDay', { result = db.create('CycleDay', {
date: localDate date: localDate,
isCycleStart: false
}) })
}) })
} }
@@ -77,8 +135,10 @@ export function getPreviousTemperature(cycleDay) {
return winner.temperature.value return winner.temperature.value
} }
export function tryToCreateCycleDay(day, i) { function tryToCreateCycleDayFromImport(day, i) {
try { try {
// we cannot know this yet, gets detected afterwards
day.isCycleStart = false
db.create('CycleDay', day) db.create('CycleDay', day)
} catch (err) { } catch (err) {
const msg = `Line ${i + 1}(${day.date}): ${err.message}` const msg = `Line ${i + 1}(${day.date}): ${err.message}`
@@ -106,7 +166,7 @@ export function getSchema() {
export function tryToImportWithDelete(cycleDays) { export function tryToImportWithDelete(cycleDays) {
db.write(() => { db.write(() => {
db.delete(db.objects('CycleDay')) db.delete(db.objects('CycleDay'))
cycleDays.forEach(tryToCreateCycleDay) cycleDays.forEach(tryToCreateCycleDayFromImport)
}) })
} }
@@ -115,7 +175,7 @@ export function tryToImportWithoutDelete(cycleDays) {
cycleDays.forEach((day, i) => { cycleDays.forEach((day, i) => {
const existing = getCycleDay(day.date) const existing = getCycleDay(day.date)
if (existing) db.delete(existing) if (existing) db.delete(existing)
tryToCreateCycleDay(day, i) tryToCreateCycleDayFromImport(day, i)
}) })
}) })
} }
+166
View File
@@ -0,0 +1,166 @@
import cycleModule from '../../lib/cycle'
const TemperatureSchema = {
name: 'Temperature',
properties: {
value: 'double',
exclude: 'bool',
time: {
type: 'string',
optional: true
},
note: {
type: 'string',
optional: true
}
}
}
const BleedingSchema = {
name: 'Bleeding',
properties: {
value: 'int',
exclude: 'bool'
}
}
const MucusSchema = {
name: 'Mucus',
properties: {
feeling: 'int',
texture: 'int',
value: 'int',
exclude: 'bool'
}
}
const CervixSchema = {
name: 'Cervix',
properties: {
opening: 'int',
firmness: 'int',
position: {type: 'int', optional: true },
exclude: 'bool'
}
}
const NoteSchema = {
name: 'Note',
properties: {
value: 'string'
}
}
const DesireSchema = {
name: 'Desire',
properties: {
value: 'int'
}
}
const SexSchema = {
name: 'Sex',
properties: {
solo: { type: 'bool', optional: true },
partner: { type: 'bool', optional: true },
condom: { type: 'bool', optional: true },
pill: { type: 'bool', optional: true },
iud: { type: 'bool', optional: true },
patch: { type: 'bool', optional: true },
ring: { type: 'bool', optional: true },
implant: { type: 'bool', optional: true },
diaphragm: { type: 'bool', optional: true },
none: { type: 'bool', optional: true },
other: { type: 'bool', optional: true },
note: { type: 'string', optional: true }
}
}
const PainSchema = {
name: 'Pain',
properties: {
cramps: { type: 'bool', optional: true },
ovulationPain: { type: 'bool', optional: true },
headache: { type: 'bool', optional: true },
backache: { type: 'bool', optional: true },
nausea: { type: 'bool', optional: true },
tenderBreasts: { type: 'bool', optional: true },
migraine: { type: 'bool', optional: true },
other: { type: 'bool', optional: true },
note: { type: 'string', optional: true }
}
}
const CycleDaySchema = {
name: 'CycleDay',
primaryKey: 'date',
properties: {
date: 'string',
temperature: {
type: 'Temperature',
optional: true
},
isCycleStart: 'bool',
bleeding: {
type: 'Bleeding',
optional: true
},
mucus: {
type: 'Mucus',
optional: true
},
cervix: {
type: 'Cervix',
optional: true
},
note: {
type: 'Note',
optional: true
},
desire: {
type: 'Desire',
optional: true
},
sex: {
type: 'Sex',
optional: true
},
pain: {
type: 'Pain',
optional: true
}
}
}
export default {
schema: [
CycleDaySchema,
TemperatureSchema,
BleedingSchema,
MucusSchema,
CervixSchema,
NoteSchema,
DesireSchema,
SexSchema,
PainSchema
],
schemaVersion: 2,
migration: (oldRealm, newRealm) => {
if (oldRealm.schemaVersion >= 2) return
const oldBleedingDays = oldRealm.objects('CycleDay')
.filtered('bleeding != null')
.sorted('date', true)
const { isMensesStart } = cycleModule({
bleedingDaysSortedByDate: oldBleedingDays
})
const newBleedingDays = newRealm.objects('CycleDay')
.filtered('bleeding != null')
.sorted('date', true)
oldBleedingDays.forEach((day, i) => {
newBleedingDays[i].isCycleStart = isMensesStart(day)
})
}
}
+2 -1
View File
@@ -1,4 +1,5 @@
import schema0 from './0.js' import schema0 from './0.js'
import schema1 from './1.js' import schema1 from './1.js'
import schema2 from './2.js'
export default [schema0, schema1] export default [schema0, schema1, schema2]
+96 -92
View File
@@ -5,6 +5,7 @@ const DAYS = joda.ChronoUnit.DAYS
export default function config(opts) { export default function config(opts) {
let bleedingDaysSortedByDate let bleedingDaysSortedByDate
let cycleStartsSortedByDate
let cycleDaysSortedByDate let cycleDaysSortedByDate
let maxBreakInBleeding let maxBreakInBleeding
let maxCycleLength let maxCycleLength
@@ -14,57 +15,22 @@ export default function config(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
bleedingDaysSortedByDate = require('../db').getBleedingDaysSortedByDate() bleedingDaysSortedByDate = require('../db').getBleedingDaysSortedByDate()
cycleStartsSortedByDate = require('../db').getCycleStartsSortedByDate()
cycleDaysSortedByDate = require('../db').getCycleDaysSortedByDate() cycleDaysSortedByDate = require('../db').getCycleDaysSortedByDate()
maxBreakInBleeding = 1 maxBreakInBleeding = 1
maxCycleLength = 99 maxCycleLength = 99
minCyclesForPrediction = 3 minCyclesForPrediction = 3
} else { } else {
bleedingDaysSortedByDate = opts.bleedingDaysSortedByDate || [] bleedingDaysSortedByDate = opts.bleedingDaysSortedByDate || []
cycleStartsSortedByDate = opts.cycleStartsSortedByDate || []
cycleDaysSortedByDate = opts.cycleDaysSortedByDate || [] cycleDaysSortedByDate = opts.cycleDaysSortedByDate || []
maxBreakInBleeding = opts.maxBreakInBleeding || 1 maxBreakInBleeding = opts.maxBreakInBleeding || 1
maxCycleLength = opts.maxCycleLength || 99 maxCycleLength = opts.maxCycleLength || 99
minCyclesForPrediction = opts.minCyclesForPrediction || 3 minCyclesForPrediction = opts.minCyclesForPrediction || 3
} }
function findLatestMensesStart(bleedingDays) {
if (!bleedingDays.length) return null
// assumes bleeding days are ordered latest first, and
// excluded values already removed
const lastMensesStart = bleedingDays.find((day, i) => {
return noBleedingDayWithinThreshold(day, bleedingDays.slice(i + 1))
})
function noBleedingDayWithinThreshold(day, previousBleedingDays) {
const localDate = LocalDate.parse(day.date)
const threshold = localDate.minusDays(maxBreakInBleeding + 1).toString()
return !previousBleedingDays.some(({ date }) => date >= threshold)
}
return lastMensesStart
}
function getLastMensesStartForDay(targetDateString) { function getLastMensesStartForDay(targetDateString) {
// the index of the first bleeding day before the target day return cycleStartsSortedByDate.find(start => start.date <= targetDateString)
const index = bleedingDaysSortedByDate.findIndex(day => {
return day.date <= targetDateString && !day.bleeding.exclude
})
if (index < 0) return null
const prevBleedingDays = bleedingDaysSortedByDate.slice(index)
return findLatestMensesStart(prevBleedingDays)
}
function getFollowingMensesStartForDay(targetDateString) {
const followingBleedingDays = bleedingDaysSortedByDate
.filter(day => !day.bleeding.exclude)
.reverse()
const firstBleedingDayAfterTargetDay = followingBleedingDays
.find(day => day.date > targetDateString)
return firstBleedingDayAfterTargetDay
} }
function getCycleDayNumber(targetDateString) { function getCycleDayNumber(targetDateString) {
@@ -78,87 +44,123 @@ export default function config(opts) {
return diffInDays + 1 return diffInDays + 1
} }
function getCyclesBefore(targetCycleStartDay) {
return collectPreviousCycles([], targetCycleStartDay.date)
}
function collectPreviousCycles(acc, startOfFollowingCycle) {
const cycle = getPreviousCycle(startOfFollowingCycle)
if (!cycle || !cycle.length) return acc
acc.push(cycle)
return collectPreviousCycles(acc, cycle[cycle.length - 1].date)
}
function getPreviousCycle(dateString) { function getPreviousCycle(dateString) {
const startOfCycle = getLastMensesStartForDay(dateString)
if (!startOfCycle) return null
const dateBeforeStartOfCycle = LocalDate
.parse(startOfCycle.date)
.minusDays(1)
.toString()
return getCycleForDay(dateBeforeStartOfCycle)
}
function getCycleForDay(dayOrDate) {
const dateString = typeof dayOrDate === 'string' ? dayOrDate : dayOrDate.date
const cycleStart = getLastMensesStartForDay(dateString) const cycleStart = getLastMensesStartForDay(dateString)
if (!cycleStart) return null if (!cycleStart) return null
const cycleStartIndex = cycleDaysSortedByDate.indexOf(cycleStart) const i = cycleStartsSortedByDate.indexOf(cycleStart)
const nextMensesStart = getFollowingMensesStartForDay(dateString) const earlierCycleStart = cycleStartsSortedByDate[i + 1]
if (!earlierCycleStart) return null
return getCycleForCycleStartDay(earlierCycleStart)
}
function getCyclesBefore(targetCycleStartDay) {
const startFromHere = cycleStartsSortedByDate.findIndex(start => {
return start.date < targetCycleStartDay.date
})
if (startFromHere < 0) return null
return cycleStartsSortedByDate
.slice(startFromHere)
.map(getCycleForCycleStartDay)
}
function getCycleForCycleStartDay(startDay) {
const cycleStartIndex = cycleDaysSortedByDate.indexOf(startDay)
const i = cycleStartsSortedByDate.indexOf(startDay)
const nextMensesStart = cycleStartsSortedByDate[i - 1]
if (nextMensesStart) { if (nextMensesStart) {
return cycleDaysSortedByDate.slice( return cycleDaysSortedByDate.slice(
cycleDaysSortedByDate.indexOf(nextMensesStart) + 1, cycleDaysSortedByDate.indexOf(nextMensesStart) + 1,
cycleStartIndex + 1 cycleStartIndex + 1,
) )
} else { } else {
return cycleDaysSortedByDate.slice(0, cycleStartIndex + 1) return cycleDaysSortedByDate.slice(0, cycleStartIndex + 1)
} }
} }
function getAllMensesStarts(initialBleedingDays = bleedingDaysSortedByDate) { function getCycleForDay(dayOrDate) {
return recurse(initialBleedingDays.filter(d => !d.bleeding.exclude)) const dateString = typeof dayOrDate === 'string' ? dayOrDate : dayOrDate.date
const cycleStart = getLastMensesStartForDay(dateString)
function recurse(bleedingDays, collectedDates) { if (!cycleStart) return null
collectedDates = collectedDates || [] return getCycleForCycleStartDay(cycleStart)
const lastStart = findLatestMensesStart(bleedingDays)
if (!lastStart) {
return collectedDates
} else {
collectedDates.push(lastStart.date)
const index = bleedingDays.indexOf(lastStart)
const remainingDays = bleedingDays.slice(index + 1)
return recurse(remainingDays, collectedDates)
} }
function isMensesStart(cycleDay) {
if (!cycleDay.bleeding || cycleDay.bleeding.exclude) return false
if (noBleedingDayWithinThresholdBefore(cycleDay)) return true
return false
// checks that there are no relevant bleeding days before
// the input cycleDay (returns boolean)
function noBleedingDayWithinThresholdBefore(cycleDay) {
const localDate = LocalDate.parse(cycleDay.date)
const threshold = localDate.minusDays(maxBreakInBleeding + 1).toString()
const bleedingDays = bleedingDaysSortedByDate
const index = bleedingDays.findIndex(day => day.date === cycleDay.date)
const candidates = bleedingDays.slice(index + 1)
return !candidates.some(day => {
return day.date >= threshold && !day.bleeding.exclude
})
} }
} }
function getCycleLength(cycleStartDates) { // returns all bleeding days that belong to one menses directly following
const cycleLengths = [] // the cycle day. used to set or clear new cycle starts when the target day
for (let i = 0; i < cycleStartDates.length - 1; i++) { // changes
const nextCycleStart = LocalDate.parse(cycleStartDates[i]) function getMensesDaysRightAfter(cycleDay) {
const cycleStart = LocalDate.parse(cycleStartDates[i + 1]) const bleedingDays = bleedingDaysSortedByDate
const cycleLength = cycleStart.until(nextCycleStart, DAYS) .filter(d => !d.bleeding.exclude)
if (cycleLength <= maxCycleLength) { cycleLengths.push(cycleLength) } .reverse()
const firstFollowingBleedingDayIndex = bleedingDays.findIndex(day => {
return day.date > cycleDay.date
})
return recurse(cycleDay, firstFollowingBleedingDayIndex, [])
// we look at the current bleeding day as well as the next, and decide
// whether they belong to one menses. if they do, we collect them, once
// they don't, we're done
function recurse(day, nextIndex, mensesDays) {
const next = bleedingDays[nextIndex]
if (!next) return mensesDays
if (!isWithinThreshold(day, next)) return mensesDays
mensesDays.unshift(next)
return recurse(next, nextIndex + 1, mensesDays)
} }
return cycleLengths
// checks whether the two days belong to one menses episode
function isWithinThreshold(bleedingDay, nextBleedingDay) {
const localDate = LocalDate.parse(bleedingDay.date)
const threshold = localDate.plusDays(maxBreakInBleeding + 1).toString()
return nextBleedingDay.date <= threshold
}
}
function getAllCycleLengths() {
return cycleStartsSortedByDate
.map(day => LocalDate.parse(day.date))
.reduce((lengths, cycleStart, i, startsAsLocalDates) => {
if (i === startsAsLocalDates.length - 1) return lengths
const prevCycleStart = startsAsLocalDates[i + 1]
const cycleLength = prevCycleStart.until(cycleStart, DAYS)
if (cycleLength <= maxCycleLength) { lengths.push(cycleLength) }
return lengths
}, [])
} }
function getPredictedMenses() { function getPredictedMenses() {
const allMensesStarts = getAllMensesStarts() const allMensesStarts = cycleStartsSortedByDate
const atLeastOneCycle = allMensesStarts.length > 1 const atLeastOneCycle = allMensesStarts.length > 1
if (!atLeastOneCycle || if (!atLeastOneCycle ||
allMensesStarts.length < minCyclesForPrediction allMensesStarts.length < minCyclesForPrediction
) { ) {
return [] return []
} }
const cycleLengths = getCycleLength(allMensesStarts) const cycleLengths = getAllCycleLengths()
const cycleInfo = getCycleLengthStats(cycleLengths) const cycleInfo = getCycleLengthStats(cycleLengths)
const periodDistance = Math.round(cycleInfo.mean) const periodDistance = Math.round(cycleInfo.mean)
let periodStartVariation let periodStartVariation
if (cycleInfo.stdDeviation === null) { if (cycleInfo.stdDeviation === null) {
periodStartVariation = 2 periodStartVariation = 2
} else if (cycleInfo.stdDeviation < 1.5) { // threshold is choosen a little arbitrarily } else if (cycleInfo.stdDeviation < 1.5) { // threshold is chosen a little arbitrarily
periodStartVariation = 1 periodStartVariation = 1
} else { } else {
periodStartVariation = 2 periodStartVariation = 2
@@ -166,7 +168,7 @@ export default function config(opts) {
if (periodDistance - 5 < periodStartVariation) { // otherwise predictions overlap if (periodDistance - 5 < periodStartVariation) { // otherwise predictions overlap
return [] return []
} }
let lastStart = LocalDate.parse(allMensesStarts[0]) let lastStart = LocalDate.parse(allMensesStarts[0].date)
const predictedMenses = [] const predictedMenses = []
for (let i = 0; i < 3; i++) { for (let i = 0; i < 3; i++) {
lastStart = lastStart.plusDays(periodDistance) lastStart = lastStart.plusDays(periodDistance)
@@ -181,13 +183,15 @@ export default function config(opts) {
return predictedMenses return predictedMenses
} }
return { return {
getCycleDayNumber, getCycleDayNumber,
getCycleForDay, getCycleForDay,
getPreviousCycle, getPreviousCycle,
getCyclesBefore, getCyclesBefore,
getAllMensesStarts, getAllCycleLengths,
getCycleLength, getPredictedMenses,
getPredictedMenses isMensesStart,
getMensesDaysRightAfter
} }
} }
@@ -7,6 +7,9 @@ export default function getColumnNamesForCsv() {
const schema = getSchema() const schema = getSchema()
const model = schema[schemaName] const model = schema[schemaName]
return Object.keys(model).reduce((acc, key) => { return Object.keys(model).reduce((acc, key) => {
// we don't want to include isCycleStart, because that is
// a derived value
if (key === 'isCycleStart') return acc
const prefixedKey = prefix ? [prefix, key].join('.') : key const prefixedKey = prefix ? [prefix, key].join('.') : key
const childSchemaName = model[key].objectType const childSchemaName = model[key].objectType
if (!childSchemaName) { if (!childSchemaName) {
+7 -1
View File
@@ -1,6 +1,11 @@
import csvParser from 'csvtojson' import csvParser from 'csvtojson'
import isObject from 'isobject' import isObject from 'isobject'
import { getSchema, tryToImportWithDelete, tryToImportWithoutDelete } from '../../db' import {
getSchema,
tryToImportWithDelete,
tryToImportWithoutDelete,
updateCycleStartsForAllCycleDays
} from '../../db'
import getColumnNamesForCsv from './get-csv-column-names' import getColumnNamesForCsv from './get-csv-column-names'
export default async function importCsv(csv, deleteFirst) { export default async function importCsv(csv, deleteFirst) {
@@ -49,6 +54,7 @@ export default async function importCsv(csv, deleteFirst) {
} else { } else {
tryToImportWithoutDelete(cycleDays) tryToImportWithoutDelete(cycleDays)
} }
updateCycleStartsForAllCycleDays()
} }
function validateHeaders(headers) { function validateHeaders(headers) {
+644 -166
View File
File diff suppressed because it is too large Load Diff