Merge branch '109-display-stats-about-period-length' into 'master'
Resolve "add stats button on home screen and show stats about period length" Closes #109 See merge request bloodyhealth/drip!41
This commit is contained in:
@@ -5,6 +5,7 @@ import Calendar from './components/calendar'
|
|||||||
import CycleDay from './components/cycle-day'
|
import CycleDay from './components/cycle-day'
|
||||||
import Chart from './components/chart/chart'
|
import Chart from './components/chart/chart'
|
||||||
import Settings from './components/settings'
|
import Settings from './components/settings'
|
||||||
|
import Stats from './components/stats'
|
||||||
|
|
||||||
// this is until react native fixes this bugg, see
|
// this is until react native fixes this bugg, see
|
||||||
// https://github.com/facebook/react-native/issues/18868#issuecomment-382671739
|
// https://github.com/facebook/react-native/issues/18868#issuecomment-382671739
|
||||||
@@ -16,5 +17,6 @@ export default createStackNavigator({
|
|||||||
calendar: { screen: Calendar },
|
calendar: { screen: Calendar },
|
||||||
cycleDay: { screen: CycleDay },
|
cycleDay: { screen: CycleDay },
|
||||||
chart: { screen: Chart },
|
chart: { screen: Chart },
|
||||||
settings: { screen: Settings }
|
settings: { screen: Settings },
|
||||||
|
stats: { screen: Stats}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -87,6 +87,12 @@ export default class Home extends Component {
|
|||||||
title="delete everything">
|
title="delete everything">
|
||||||
</Button>
|
</Button>
|
||||||
</View>
|
</View>
|
||||||
|
<View style={styles.homeButton}>
|
||||||
|
<Button
|
||||||
|
onPress={() => navigate('stats')}
|
||||||
|
title="Go to stats">
|
||||||
|
</Button>
|
||||||
|
</View>
|
||||||
</View>
|
</View>
|
||||||
</ScrollView>
|
</ScrollView>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
import React, { Component } from 'react'
|
||||||
|
import {
|
||||||
|
Text,
|
||||||
|
View,
|
||||||
|
ScrollView
|
||||||
|
} from 'react-native'
|
||||||
|
import { LocalDate, ChronoUnit } from 'js-joda'
|
||||||
|
import styles from '../styles/index'
|
||||||
|
import cycleModule from '../lib/cycle'
|
||||||
|
import getCycleInfo from '../lib/cycle-length'
|
||||||
|
|
||||||
|
export default class Stats extends Component {
|
||||||
|
render() {
|
||||||
|
const allMensesStarts = cycleModule().getAllMensesStarts()
|
||||||
|
const statsText = determineStatsText(allMensesStarts)
|
||||||
|
return (
|
||||||
|
<ScrollView>
|
||||||
|
<View>
|
||||||
|
<Text style={styles.stats}>{statsText}</Text>
|
||||||
|
</View>
|
||||||
|
</ScrollView>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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])
|
||||||
|
cycleLengths.push(cycleStart.until(nextCycleStart, ChronoUnit.DAYS))
|
||||||
|
}
|
||||||
|
return cycleLengths
|
||||||
|
}
|
||||||
|
|
||||||
|
function determineStatsText(allMensesStarts) {
|
||||||
|
const emptyStats = 'At least one completed cycle is needed to present you with stats here.'
|
||||||
|
if (allMensesStarts.length < 2) {
|
||||||
|
return emptyStats
|
||||||
|
} else {
|
||||||
|
const cycleLengths = getCycleLength(allMensesStarts)
|
||||||
|
const numberOfCycles = cycleLengths.length
|
||||||
|
if (numberOfCycles === 1) {
|
||||||
|
return `You have documented one cycle of ${cycleLengths[0]} days.`
|
||||||
|
}
|
||||||
|
const cycleInfo = getCycleInfo(cycleLengths)
|
||||||
|
const statsText = `Stats are based on ${numberOfCycles} completed cycles.\n\n\
|
||||||
|
Average cycle length: ${cycleInfo.mean} days\n\nShortest cycle: ${cycleInfo.minimum} days\nLongest cycle: ${cycleInfo.maximum} days\nMedian length (meaning 50% of cycles are of this length or shorter): ${cycleInfo.median} days\nStandard deviation: ${cycleInfo.stdDeviation}`
|
||||||
|
return statsText
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,38 +1,38 @@
|
|||||||
import assert from 'assert'
|
import assert from 'assert'
|
||||||
|
|
||||||
export default function getPeriodLengthStats(cycleLengths) {
|
export default function getCycleLengthStats(cycleLengths) {
|
||||||
throwIfArgsAreNotInRequiredFormat(cycleLengths)
|
throwIfArgsAreNotInRequiredFormat(cycleLengths)
|
||||||
const periodLengthStats = {}
|
const cycleLengthStats = {}
|
||||||
const sortedCycleLengths = cycleLengths.sort((a, b) => {
|
const sortedCycleLengths = cycleLengths.sort((a, b) => {
|
||||||
return a - b
|
return a - b
|
||||||
})
|
})
|
||||||
periodLengthStats.minimum = sortedCycleLengths[0]
|
cycleLengthStats.minimum = sortedCycleLengths[0]
|
||||||
periodLengthStats.maximum = sortedCycleLengths[cycleLengths.length - 1]
|
cycleLengthStats.maximum = sortedCycleLengths[cycleLengths.length - 1]
|
||||||
periodLengthStats.mean = Math.round(
|
cycleLengthStats.mean = Math.round(
|
||||||
cycleLengths.reduce(getSum) / cycleLengths.length * 100
|
cycleLengths.reduce(getSum) / cycleLengths.length * 100
|
||||||
) / 100
|
) / 100
|
||||||
// median
|
// median
|
||||||
if (cycleLengths.length % 2 == 1) {
|
if (cycleLengths.length % 2 == 1) {
|
||||||
periodLengthStats.median = sortedCycleLengths[
|
cycleLengthStats.median = sortedCycleLengths[
|
||||||
(cycleLengths.length + 1) / 2 - 1
|
(cycleLengths.length + 1) / 2 - 1
|
||||||
]
|
]
|
||||||
} else {
|
} else {
|
||||||
const middle = cycleLengths.length / 2
|
const middle = cycleLengths.length / 2
|
||||||
periodLengthStats.median = (sortedCycleLengths[middle - 1] +
|
cycleLengthStats.median = (sortedCycleLengths[middle - 1] +
|
||||||
sortedCycleLengths[middle]) / 2
|
sortedCycleLengths[middle]) / 2
|
||||||
}
|
}
|
||||||
// corrected standard deviation (based on unbiased sample variance)
|
// corrected standard deviation (based on unbiased sample variance)
|
||||||
if (cycleLengths.length > 1) {
|
if (cycleLengths.length > 1) {
|
||||||
const sumOfSquares = cycleLengths.map(cycleLength => {
|
const sumOfSquares = cycleLengths.map(cycleLength => {
|
||||||
return Math.pow(cycleLength - periodLengthStats.mean, 2)
|
return Math.pow(cycleLength - cycleLengthStats.mean, 2)
|
||||||
}).reduce(getSum)
|
}).reduce(getSum)
|
||||||
periodLengthStats.stdDeviation = Math.round(
|
cycleLengthStats.stdDeviation = Math.round(
|
||||||
Math.sqrt(sumOfSquares / (cycleLengths.length - 1 )) * 100
|
Math.sqrt(sumOfSquares / (cycleLengths.length - 1 )) * 100
|
||||||
) / 100
|
) / 100
|
||||||
} else {
|
} else {
|
||||||
periodLengthStats.stdDeviation = null
|
cycleLengthStats.stdDeviation = null
|
||||||
}
|
}
|
||||||
return periodLengthStats
|
return cycleLengthStats
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSum(total, num) {
|
function getSum(total, num) {
|
||||||
+15
-1
@@ -130,10 +130,24 @@ export default function config(opts) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getAllMensesStarts(day, collectedDates) {
|
||||||
|
day = day || LocalDate.now().toString()
|
||||||
|
collectedDates = collectedDates || []
|
||||||
|
const lastStart = getLastMensesStart(day)
|
||||||
|
if (!lastStart) {
|
||||||
|
return collectedDates
|
||||||
|
} else {
|
||||||
|
const newDay = LocalDate.parse(lastStart.date).minusDays(1).toString()
|
||||||
|
collectedDates.push(lastStart.date)
|
||||||
|
return getAllMensesStarts(newDay, collectedDates)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
getCycleDayNumber,
|
getCycleDayNumber,
|
||||||
getCycleForDay,
|
getCycleForDay,
|
||||||
getPreviousCycle,
|
getPreviousCycle,
|
||||||
getCyclesBefore
|
getCyclesBefore,
|
||||||
|
getAllMensesStarts
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Generated
+1494
-1494
File diff suppressed because it is too large
Load Diff
@@ -82,5 +82,11 @@ export default StyleSheet.create({
|
|||||||
marginTop: 15,
|
marginTop: 15,
|
||||||
marginLeft: 'auto',
|
marginLeft: 'auto',
|
||||||
marginRight: 'auto'
|
marginRight: 'auto'
|
||||||
|
},
|
||||||
|
stats: {
|
||||||
|
fontSize: 18,
|
||||||
|
margin: 30,
|
||||||
|
textAlign: 'left',
|
||||||
|
textAlignVertical: 'center'
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -1,14 +1,14 @@
|
|||||||
import chai from 'chai'
|
import chai from 'chai'
|
||||||
import { AssertionError } from 'assert'
|
import { AssertionError } from 'assert'
|
||||||
|
|
||||||
import periodInfo from '../lib/period-length'
|
import cycleInfo from '../lib/cycle-length'
|
||||||
|
|
||||||
const expect = chai.expect
|
const expect = chai.expect
|
||||||
|
|
||||||
describe('getPeriodLengthStats', () => {
|
describe('getCycleLengthStats', () => {
|
||||||
it('works for a simple odd-numbered array', () => {
|
it('works for a simple odd-numbered array', () => {
|
||||||
const periodLengths = [99, 5, 1, 2, 100]
|
const cycleLengths = [99, 5, 1, 2, 100]
|
||||||
const result = periodInfo(periodLengths)
|
const result = cycleInfo(cycleLengths)
|
||||||
const expectedResult = {
|
const expectedResult = {
|
||||||
minimum: 1,
|
minimum: 1,
|
||||||
maximum: 100,
|
maximum: 100,
|
||||||
@@ -20,8 +20,8 @@ describe('getPeriodLengthStats', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('works for a simple even-numbered array', () => {
|
it('works for a simple even-numbered array', () => {
|
||||||
const periodLengths = [4, 1, 15, 2, 20, 5]
|
const cycleLengths = [4, 1, 15, 2, 20, 5]
|
||||||
const result = periodInfo(periodLengths)
|
const result = cycleInfo(cycleLengths)
|
||||||
const expectedResult = {
|
const expectedResult = {
|
||||||
minimum: 1,
|
minimum: 1,
|
||||||
maximum: 20,
|
maximum: 20,
|
||||||
@@ -32,8 +32,8 @@ describe('getPeriodLengthStats', () => {
|
|||||||
expect(result).to.eql(expectedResult)
|
expect(result).to.eql(expectedResult)
|
||||||
})
|
})
|
||||||
it('works for an one-element array', () => {
|
it('works for an one-element array', () => {
|
||||||
const periodLengths = [42]
|
const cycleLengths = [42]
|
||||||
const result = periodInfo(periodLengths)
|
const result = cycleInfo(cycleLengths)
|
||||||
const expectedResult = {
|
const expectedResult = {
|
||||||
minimum: 42,
|
minimum: 42,
|
||||||
maximum: 42,
|
maximum: 42,
|
||||||
@@ -45,20 +45,20 @@ describe('getPeriodLengthStats', () => {
|
|||||||
})
|
})
|
||||||
describe('when args are wrong', () => {
|
describe('when args are wrong', () => {
|
||||||
it('throws when arg object is an empty array', () => {
|
it('throws when arg object is an empty array', () => {
|
||||||
const periodLengths = []
|
const cycleLengths = []
|
||||||
expect(() => periodInfo(periodLengths).to.throw(AssertionError))
|
expect(() => cycleInfo(cycleLengths).to.throw(AssertionError))
|
||||||
})
|
})
|
||||||
it('throws when arg object is not in right format', () => {
|
it('throws when arg object is not in right format', () => {
|
||||||
const wrongObject = { hello: 'world' }
|
const wrongObject = { hello: 'world' }
|
||||||
expect(() => periodInfo(wrongObject).to.throw(AssertionError))
|
expect(() => cycleInfo(wrongObject).to.throw(AssertionError))
|
||||||
})
|
})
|
||||||
it('throws when arg array contains a string', () => {
|
it('throws when arg array contains a string', () => {
|
||||||
const wrongElement = [4, 1, 15, '2', 20, 5]
|
const wrongElement = [4, 1, 15, '2', 20, 5]
|
||||||
expect(() => periodInfo(wrongElement).to.throw(AssertionError))
|
expect(() => cycleInfo(wrongElement).to.throw(AssertionError))
|
||||||
})
|
})
|
||||||
it('throws when arg array contains a NaN', () => {
|
it('throws when arg array contains a NaN', () => {
|
||||||
const wrongElement = [4, 1, 15, NaN, 20, 5]
|
const wrongElement = [4, 1, 15, NaN, 20, 5]
|
||||||
expect(() => periodInfo(wrongElement).to.throw(AssertionError))
|
expect(() => cycleInfo(wrongElement).to.throw(AssertionError))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
Reference in New Issue
Block a user