Skip to content

Commit

Permalink
feat: add page count to user stats #55
Browse files Browse the repository at this point in the history
  • Loading branch information
bayang committed Jan 3, 2023
1 parent 5f8f603 commit f51df40
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
40 changes: 35 additions & 5 deletions src/jelu-ui/src/components/UserStats.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="ts">
import { useTitle } from '@vueuse/core';
import { BarElement, CategoryScale, Chart as ChartJS, ChartData, Legend, LinearScale, Title, Tooltip } from 'chart.js';
import { BarElement, CategoryScale, Chart as ChartJS, ChartData, Legend, LinearScale, Title, Tooltip, LineController, PointElement, LineElement } from 'chart.js';
import dayjs from "dayjs";
import { Ref, ref, watch } from "vue";
import { Bar } from 'vue-chartjs';
Expand All @@ -14,7 +14,7 @@ const { t } = useI18n({
useTitle('Jelu | Stats')
ChartJS.register(Title, Tooltip, Legend, BarElement, CategoryScale, LinearScale)
ChartJS.register(Title, Tooltip, Legend, BarElement, CategoryScale, LinearScale, LineController, PointElement, LineElement)
const getYears = () => {
dataService.yearsWithStats()
Expand All @@ -36,18 +36,29 @@ const getAllStats = () => {
const updatedChartData = {
labels: labels,
datasets: [
{
type: 'line',
label: 'pages read',
backgroundColor: '#3abff8',
borderColor: '#3abff8',
borderWidth: 2,
yAxisID: 'y2',
data: res.map(r => r.pageCount)
},
{
label: 'finished',
yAxisID: 'y1',
backgroundColor: '#bbbbbb',
data: res.map(r => r.finished)
},
{
label: 'dropped',
yAxisID: 'y1',
backgroundColor: '#f87979',
data: res.map(r => r.dropped)
}
]
}
} as any // mixed charts typing is broken
chartData.value = { ...updatedChartData }
loading.value = false
})
Expand All @@ -67,18 +78,29 @@ const getYearStats = () => {
const updatedChartData = {
labels: labels,
datasets: [
{
type: 'line',
label: 'pages read',
backgroundColor: '#3abff8',
borderColor: '#3abff8',
borderWidth: 2,
yAxisID: 'y2',
data: res.map(r => r.pageCount)
},
{
label: 'finished',
backgroundColor: '#bbbbbb',
yAxisID: 'y1',
data: res.map(r => r.finished)
},
{
label: 'dropped',
backgroundColor: '#f87979',
yAxisID: 'y1',
data: res.map(r => r.dropped)
}
]
}
} as any // mixed charts typing is broken
yearChartData.value = { ...updatedChartData }
loading.value = false
})
Expand All @@ -97,7 +119,15 @@ const yearChartData = ref<ChartData<'bar'>>({
datasets: []
})
const chartOptions = ref({
responsive: true
responsive: true,
scales : {
y1: {
position: 'left'
},
y2: {
position: 'right'
}
}
})
const years: Ref<Array<number>> = ref([])
Expand Down
6 changes: 4 additions & 2 deletions src/jelu-ui/src/model/YearStats.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
export interface YearStats {
dropped: number,
finished: number,
year: number
year: number,
pageCount: number
}

export interface MonthStats {
dropped: number,
finished: number,
year: number,
month: number
month: number,
pageCount: number
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ class ReadingEventsController(
if (it.eventType == ReadingEventType.DROPPED) {
yearStats[year] = yearStats[year]!!.copy(dropped = yearStats[year]!!.dropped + 1)
} else if (it.eventType == ReadingEventType.FINISHED) {
yearStats[year] = yearStats[year]!!.copy(finished = yearStats[year]!!.finished + 1)
yearStats[year] = yearStats[year]!!.copy(finished = yearStats[year]!!.finished + 1, pageCount = yearStats[year]!!.pageCount + (it.userBook.book.pageCount ?: 0))
}
} else {
if (it.eventType == ReadingEventType.DROPPED) {
yearStats[year] = YearStatsDto(year = year, dropped = 1)
} else if (it.eventType == ReadingEventType.FINISHED) {
yearStats[year] = YearStatsDto(year = year, finished = 1)
yearStats[year] = YearStatsDto(year = year, finished = 1, pageCount = it.userBook.book.pageCount ?: 0)
}
}
}
Expand Down Expand Up @@ -142,13 +142,13 @@ class ReadingEventsController(
if (it.eventType == ReadingEventType.DROPPED) {
monthStats[month] = monthStats[month]!!.copy(dropped = monthStats[month]!!.dropped + 1)
} else if (it.eventType == ReadingEventType.FINISHED) {
monthStats[month] = monthStats[month]!!.copy(finished = monthStats[month]!!.finished + 1)
monthStats[month] = monthStats[month]!!.copy(finished = monthStats[month]!!.finished + 1, pageCount = monthStats[month]!!.pageCount + (it.userBook.book.pageCount ?: 0))
}
} else {
if (it.eventType == ReadingEventType.DROPPED) {
monthStats[month] = MonthStatsDto(year = year, dropped = 1, month = month)
} else if (it.eventType == ReadingEventType.FINISHED) {
monthStats[month] = MonthStatsDto(year = year, finished = 1, month = month)
monthStats[month] = MonthStatsDto(year = year, finished = 1, month = month, pageCount = it.userBook.book.pageCount ?: 0)
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/io/github/bayang/jelu/dto/ReadStatsDto.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package io.github.bayang.jelu.dto
data class YearStatsDto(
val dropped: Int = 0,
val finished: Int = 0,
val year: Int
val year: Int,
val pageCount: Int = 0
)

data class MonthStatsDto(
val dropped: Int = 0,
val finished: Int = 0,
val year: Int,
val month: Int,
val pageCount: Int = 0
)

0 comments on commit f51df40

Please sign in to comment.