fix up movies index route [wip]

This commit is contained in:
ducklet 2021-08-04 01:04:13 +02:00
parent 7cc540b6fd
commit bae0415a24
4 changed files with 210 additions and 8 deletions

View file

@ -29,8 +29,8 @@
<span class="mediatype">{{ item.media_type }}</span>
</td>
<td>
<span class="score imdb-score tag is-large" title="IMDb rating (1-10)">{{ imdb_rating(item.imdb_score) }}</span>
<span class="score tag is-info is-large" title="User rating (1-10)">{{ imdb_rating(item.user_score) }}</span>
<span class="score imdb-score tag is-large" :title="`IMDb rating (1-10) / ${item.imdb_votes} votes`">{{ imdb_rating(item.imdb_score) }}</span>
<span class="score tag is-info is-large" :title="`User rating (1-10) / ${item.user_scores.length} votes / σ = ${imdb_stdev(item.user_scores)}`">{{ avg_imdb_rating(item.user_scores) }}</span>
</td>
<td>
<span>{{ duration(item.runtime) }}</span>
@ -44,6 +44,20 @@
<script lang="ts">
import { defineComponent } from "vue"
import { mean, pstdev } from "../utils.ts"
function avg_imdb_rating(scores) {
return imdb_rating(scores.length === 0 ? null : mean(scores))
}
function imdb_stdev(scores) {
return pstdev(scores.map(imdb_rating_from_score))
}
function imdb_rating_from_score(score) {
return Math.round((score * 9) / 100 + 1)
}
function imdb_rating(score) {
if (score == null) {
@ -85,7 +99,9 @@ export default defineComponent({
observer.observe(this.$refs.sentinel)
},
methods: {
avg_imdb_rating,
imdb_rating,
imdb_stdev,
duration,
},
})

View file

@ -1,3 +1,18 @@
export function is_object(x) {
return x !== null && typeof x === "object" && !Array.isArray(x)
}
export function sum(nums) {
return nums.reduce((s, n) => s + n, 0)
}
export function mean(nums) {
return sum(nums) / nums.length
}
export function pstdev(nums, mu=null) {
if (mu === null) {
mu = mean(nums)
}
return Math.sqrt(mean(nums.map((n) => (n - mu) ** 2)))
}