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 {stats as labels} from './labels'
import AppText from './app-text'
import { getCycleStartsSortedByDate } from '../db'
export default class Stats extends Component {
render() {
const allMensesStarts = cycleModule().getAllMensesStarts()
const allMensesStarts = getCycleStartsSortedByDate()
const atLeastOneCycle = allMensesStarts.length > 1
let cycleLengths
let numberOfCycles
let cycleInfo
if (atLeastOneCycle) {
cycleLengths = cycleModule().getCycleLength(allMensesStarts)
cycleLengths = cycleModule().getAllCycleLengths()
numberOfCycles = cycleLengths.length
if (numberOfCycles > 1) {
cycleInfo = getCycleInfo(cycleLengths)
+66 -6
View File
@@ -4,8 +4,11 @@ import nodejs from 'nodejs-mobile-react-native'
import fs from 'react-native-fs'
import restart from 'react-native-restart'
import schemas from './schemas'
import cycleModule from '../lib/cycle'
let db
let isMensesStart
let getMensesDaysRightAfter
export async function openDb ({ hash, persistConnection }) {
const realmConfig = {}
@@ -32,9 +35,11 @@ export async function openDb ({ hash, persistConnection }) {
))
if (persistConnection) db = connection
const cycle = cycleModule()
isMensesStart = cycle.isMensesStart
getMensesDaysRightAfter = cycle.getMensesDaysRightAfter
}
export function getBleedingDaysSortedByDate() {
return db.objects('CycleDay').filtered('bleeding != null').sorted('date', true)
}
@@ -45,9 +50,61 @@ export function getCycleDaysSortedByDate() {
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) {
db.write(() => {
cycleDay[symptom] = val
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
}
})
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) {
db.write(() => {
result = db.create('CycleDay', {
date: localDate
date: localDate,
isCycleStart: false
})
})
}
@@ -77,8 +135,10 @@ export function getPreviousTemperature(cycleDay) {
return winner.temperature.value
}
export function tryToCreateCycleDay(day, i) {
function tryToCreateCycleDayFromImport(day, i) {
try {
// we cannot know this yet, gets detected afterwards
day.isCycleStart = false
db.create('CycleDay', day)
} catch (err) {
const msg = `Line ${i + 1}(${day.date}): ${err.message}`
@@ -106,7 +166,7 @@ export function getSchema() {
export function tryToImportWithDelete(cycleDays) {
db.write(() => {
db.delete(db.objects('CycleDay'))
cycleDays.forEach(tryToCreateCycleDay)
cycleDays.forEach(tryToCreateCycleDayFromImport)
})
}
@@ -115,7 +175,7 @@ export function tryToImportWithoutDelete(cycleDays) {
cycleDays.forEach((day, i) => {
const existing = getCycleDay(day.date)
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 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) {
let bleedingDaysSortedByDate
let cycleStartsSortedByDate
let cycleDaysSortedByDate
let maxBreakInBleeding
let maxCycleLength
@@ -14,57 +15,22 @@ export default function config(opts) {
// we only want to require (and run) the db module
// when not running the tests
bleedingDaysSortedByDate = require('../db').getBleedingDaysSortedByDate()
cycleStartsSortedByDate = require('../db').getCycleStartsSortedByDate()
cycleDaysSortedByDate = require('../db').getCycleDaysSortedByDate()
maxBreakInBleeding = 1
maxCycleLength = 99
minCyclesForPrediction = 3
} else {
bleedingDaysSortedByDate = opts.bleedingDaysSortedByDate || []
cycleStartsSortedByDate = opts.cycleStartsSortedByDate || []
cycleDaysSortedByDate = opts.cycleDaysSortedByDate || []
maxBreakInBleeding = opts.maxBreakInBleeding || 1
maxCycleLength = opts.maxCycleLength || 99
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) {
// the index of the first bleeding day before the target day
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
return cycleStartsSortedByDate.find(start => start.date <= targetDateString)
}
function getCycleDayNumber(targetDateString) {
@@ -78,87 +44,123 @@ export default function config(opts) {
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) {
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)
if (!cycleStart) return null
const cycleStartIndex = cycleDaysSortedByDate.indexOf(cycleStart)
const nextMensesStart = getFollowingMensesStartForDay(dateString)
const i = cycleStartsSortedByDate.indexOf(cycleStart)
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) {
return cycleDaysSortedByDate.slice(
cycleDaysSortedByDate.indexOf(nextMensesStart) + 1,
cycleStartIndex + 1
cycleStartIndex + 1,
)
} else {
return cycleDaysSortedByDate.slice(0, cycleStartIndex + 1)
}
}
function getAllMensesStarts(initialBleedingDays = bleedingDaysSortedByDate) {
return recurse(initialBleedingDays.filter(d => !d.bleeding.exclude))
function getCycleForDay(dayOrDate) {
const dateString = typeof dayOrDate === 'string' ? dayOrDate : dayOrDate.date
const cycleStart = getLastMensesStartForDay(dateString)
if (!cycleStart) return null
return getCycleForCycleStartDay(cycleStart)
}
function recurse(bleedingDays, collectedDates) {
collectedDates = collectedDates || []
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) {
const cycleLengths = []
for (let i = 0; i < cycleStartDates.length - 1; i++) {
const nextCycleStart = LocalDate.parse(cycleStartDates[i])
const cycleStart = LocalDate.parse(cycleStartDates[i + 1])
const cycleLength = cycleStart.until(nextCycleStart, DAYS)
if (cycleLength <= maxCycleLength) { cycleLengths.push(cycleLength) }
// returns all bleeding days that belong to one menses directly following
// the cycle day. used to set or clear new cycle starts when the target day
// changes
function getMensesDaysRightAfter(cycleDay) {
const bleedingDays = bleedingDaysSortedByDate
.filter(d => !d.bleeding.exclude)
.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() {
const allMensesStarts = getAllMensesStarts()
const allMensesStarts = cycleStartsSortedByDate
const atLeastOneCycle = allMensesStarts.length > 1
if (!atLeastOneCycle ||
allMensesStarts.length < minCyclesForPrediction
) {
return []
}
const cycleLengths = getCycleLength(allMensesStarts)
const cycleLengths = getAllCycleLengths()
const cycleInfo = getCycleLengthStats(cycleLengths)
const periodDistance = Math.round(cycleInfo.mean)
let periodStartVariation
if (cycleInfo.stdDeviation === null) {
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
} else {
periodStartVariation = 2
@@ -166,7 +168,7 @@ export default function config(opts) {
if (periodDistance - 5 < periodStartVariation) { // otherwise predictions overlap
return []
}
let lastStart = LocalDate.parse(allMensesStarts[0])
let lastStart = LocalDate.parse(allMensesStarts[0].date)
const predictedMenses = []
for (let i = 0; i < 3; i++) {
lastStart = lastStart.plusDays(periodDistance)
@@ -181,13 +183,15 @@ export default function config(opts) {
return predictedMenses
}
return {
getCycleDayNumber,
getCycleForDay,
getPreviousCycle,
getCyclesBefore,
getAllMensesStarts,
getCycleLength,
getPredictedMenses
getAllCycleLengths,
getPredictedMenses,
isMensesStart,
getMensesDaysRightAfter
}
}
@@ -7,6 +7,9 @@ export default function getColumnNamesForCsv() {
const schema = getSchema()
const model = schema[schemaName]
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 childSchemaName = model[key].objectType
if (!childSchemaName) {
+7 -1
View File
@@ -1,6 +1,11 @@
import csvParser from 'csvtojson'
import isObject from 'isobject'
import { getSchema, tryToImportWithDelete, tryToImportWithoutDelete } from '../../db'
import {
getSchema,
tryToImportWithDelete,
tryToImportWithoutDelete,
updateCycleStartsForAllCycleDays
} from '../../db'
import getColumnNamesForCsv from './get-csv-column-names'
export default async function importCsv(csv, deleteFirst) {
@@ -49,6 +54,7 @@ export default async function importCsv(csv, deleteFirst) {
} else {
tryToImportWithoutDelete(cycleDays)
}
updateCycleStartsForAllCycleDays()
}
function validateHeaders(headers) {
+649 -171
View File
File diff suppressed because it is too large Load Diff