2023-04-11 22:22:51 +00:00
|
|
|
import {GetServerSidePropsContext} from 'next'
|
2023-04-12 15:15:17 +00:00
|
|
|
import {createServerSupabaseClient} from '@supabase/auth-helpers-nextjs'
|
2023-02-21 21:21:11 +00:00
|
|
|
import {
|
|
|
|
getTopRepo,
|
|
|
|
getTotalFollowers,
|
|
|
|
getTotalForks,
|
|
|
|
getTotalRepos,
|
|
|
|
getTotalStars
|
|
|
|
} from '@/lib/github'
|
2023-01-28 21:59:01 +00:00
|
|
|
import {getAllArticles} from '@/lib/getAllArticles'
|
2023-02-01 22:33:03 +00:00
|
|
|
import {getTopArtist, getTopGenre} from '@/lib/spotify'
|
2023-02-02 21:47:35 +00:00
|
|
|
import {getStats} from '@/lib/statsfm'
|
2023-01-29 23:04:28 +00:00
|
|
|
import {Metric} from '@/types'
|
2023-01-28 21:24:05 +00:00
|
|
|
|
2023-04-11 22:22:51 +00:00
|
|
|
export async function getDashboardData(context: GetServerSidePropsContext) {
|
2023-04-12 15:15:17 +00:00
|
|
|
const supabaseClient = createServerSupabaseClient(context)
|
|
|
|
const {data: views} = await supabaseClient.rpc('total_views')
|
2023-01-28 21:24:05 +00:00
|
|
|
const [totalRepos, totalFollowers] = await Promise.all([
|
|
|
|
getTotalRepos(),
|
|
|
|
getTotalFollowers()
|
|
|
|
])
|
|
|
|
|
2023-02-21 21:21:11 +00:00
|
|
|
const topRepo = await getTopRepo()
|
2023-01-28 21:24:05 +00:00
|
|
|
const totalStars = await getTotalStars(totalRepos)
|
2023-02-21 17:03:38 +00:00
|
|
|
const totalForks = await getTotalForks(totalRepos)
|
2023-01-29 11:24:06 +00:00
|
|
|
const totalArticles = (await getAllArticles()).length
|
2023-02-01 22:33:03 +00:00
|
|
|
const topArtist = await getTopArtist()
|
2023-02-02 21:47:35 +00:00
|
|
|
const {genre} = await getTopGenre()
|
2023-05-28 20:15:39 +00:00
|
|
|
// const {hoursListened, minutesListened, streams} = await getStats()
|
2023-01-28 21:24:05 +00:00
|
|
|
|
2023-01-29 23:04:28 +00:00
|
|
|
const metrics: Metric[] = [
|
2023-05-28 20:15:39 +00:00
|
|
|
// {
|
|
|
|
// title: "Streams",
|
|
|
|
// value: +streams,
|
|
|
|
// group: "Spotify",
|
|
|
|
// href: "https://open.spotify.com/?"
|
|
|
|
// },
|
|
|
|
// {
|
|
|
|
// title: "Hours listened",
|
|
|
|
// value: +hoursListened,
|
|
|
|
// group: "Spotify",
|
|
|
|
// href: "https://open.spotify.com/?"
|
|
|
|
// },
|
|
|
|
// {
|
|
|
|
// title: "Minutes listened",
|
|
|
|
// value: +minutesListened,
|
|
|
|
// group: "Spotify",
|
|
|
|
// href: "https://open.spotify.com/?"
|
|
|
|
// },
|
2023-01-30 22:28:27 +00:00
|
|
|
{
|
|
|
|
title: "Top genre",
|
2023-02-02 21:47:35 +00:00
|
|
|
value: genre,
|
2023-01-30 22:28:27 +00:00
|
|
|
group: "Spotify",
|
2023-02-01 22:33:03 +00:00
|
|
|
href: "https://open.spotify.com/?"
|
2023-01-30 22:28:27 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "Top artist",
|
2023-02-01 22:33:03 +00:00
|
|
|
value: topArtist.artist,
|
2023-01-30 22:28:27 +00:00
|
|
|
group: "Spotify",
|
2023-02-01 22:33:03 +00:00
|
|
|
href: topArtist.uri
|
2023-01-30 22:28:27 +00:00
|
|
|
},
|
2023-01-28 21:24:05 +00:00
|
|
|
{
|
2023-01-29 22:16:58 +00:00
|
|
|
title: "Repos",
|
2023-02-10 17:01:27 +00:00
|
|
|
value: +totalRepos,
|
2023-01-29 22:16:58 +00:00
|
|
|
group: "GitHub",
|
2023-01-28 23:04:49 +00:00
|
|
|
href: "https://github.com/r-freeman?tab=repositories"
|
2023-01-28 21:24:05 +00:00
|
|
|
},
|
2023-02-21 21:21:11 +00:00
|
|
|
{
|
|
|
|
title: "Top repo",
|
|
|
|
value: topRepo.name,
|
|
|
|
group: "GitHub",
|
|
|
|
href: topRepo.url
|
|
|
|
},
|
2023-01-28 21:24:05 +00:00
|
|
|
{
|
2023-01-29 22:16:58 +00:00
|
|
|
title: "Followers",
|
2023-02-10 17:01:27 +00:00
|
|
|
value: +totalFollowers,
|
2023-01-29 22:16:58 +00:00
|
|
|
group: "GitHub",
|
2023-01-28 23:04:49 +00:00
|
|
|
href: "https://github.com/r-freeman?tab=followers"
|
2023-01-28 21:24:05 +00:00
|
|
|
},
|
|
|
|
{
|
2023-01-29 22:16:58 +00:00
|
|
|
title: "Stars",
|
2023-02-10 17:01:27 +00:00
|
|
|
value: +totalStars,
|
2023-01-29 22:16:58 +00:00
|
|
|
group: "GitHub",
|
2023-01-28 21:24:05 +00:00
|
|
|
href: "https://github.com/r-freeman/"
|
2023-01-28 21:59:01 +00:00
|
|
|
},
|
2023-02-21 17:03:38 +00:00
|
|
|
{
|
|
|
|
title: "Forks",
|
|
|
|
value: +totalForks,
|
|
|
|
group: "GitHub",
|
|
|
|
href: "https://github.com/r-freeman/"
|
|
|
|
},
|
2023-01-28 21:59:01 +00:00
|
|
|
{
|
2023-01-30 21:06:04 +00:00
|
|
|
title: "Total articles",
|
2023-02-10 17:01:27 +00:00
|
|
|
value: +totalArticles,
|
2023-04-12 20:25:53 +00:00
|
|
|
group: "Blog",
|
2023-01-28 21:59:01 +00:00
|
|
|
href: "/writing"
|
|
|
|
},
|
2023-04-12 15:15:17 +00:00
|
|
|
{
|
2023-04-12 20:25:53 +00:00
|
|
|
title: "Total article views",
|
|
|
|
value: +views,
|
|
|
|
group: "Blog",
|
2023-04-13 20:00:47 +00:00
|
|
|
href: "/writing"
|
2023-04-12 15:15:17 +00:00
|
|
|
}
|
2023-01-28 21:24:05 +00:00
|
|
|
]
|
2023-01-29 22:16:58 +00:00
|
|
|
|
2023-01-29 23:04:28 +00:00
|
|
|
// sort metrics into named groups
|
|
|
|
const groups = metrics.reduce((acc: { [key: string]: Metric[] }, item) => {
|
2023-01-29 22:16:58 +00:00
|
|
|
(acc[item.group] = acc[item.group] || []).push(item);
|
2023-01-29 22:32:21 +00:00
|
|
|
return acc
|
2023-01-29 23:04:28 +00:00
|
|
|
}, {} as { [key: string]: Metric[] })
|
2023-01-29 22:16:58 +00:00
|
|
|
|
|
|
|
return Object.entries(groups).map(([groupName, groupItems]) => {
|
|
|
|
return {groupName, groupItems}
|
|
|
|
})
|
2023-01-28 21:24:05 +00:00
|
|
|
}
|