-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
175 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,47 @@ | ||
import { MonthlyStat } from '#actions/stats/get_monthly' | ||
import PostStats from '#actions/stats/post_stats' | ||
import UserStats from '#actions/stats/user_stats' | ||
import Collection from '#models/collection' | ||
import Post from '#models/post' | ||
import Taxonomy from '#models/taxonomy' | ||
|
||
export interface GetDashboardCountsContract { | ||
posts: BigInt | ||
postSeconds: BigInt | ||
series: BigInt | ||
topics: BigInt | ||
posts: { | ||
total: BigInt | ||
monthly: MonthlyStat[] | ||
} | ||
users: { | ||
total: bigint | ||
total: BigInt | ||
monthly: MonthlyStat[] | ||
} | ||
completedLessons: { | ||
total: BigInt | ||
monthly: MonthlyStat[] | ||
} | ||
watchSeconds: { | ||
total: BigInt | ||
monthly: MonthlyStat[] | ||
} | ||
postSeconds: BigInt | ||
} | ||
|
||
export default class GetDashboardCounts { | ||
static async handle(): Promise<GetDashboardCountsContract> { | ||
return { | ||
posts: await this.#countPublishedPosts(), | ||
postSeconds: await this.#countPostSeconds(), | ||
series: await this.#countSeries(), | ||
topics: await this.#countTopics(), | ||
posts: { | ||
total: await PostStats.getTotalPublished(), | ||
monthly: await PostStats.getMonthlyPublished(), | ||
}, | ||
users: { | ||
total: await UserStats.getTotal(), | ||
monthly: await UserStats.getMonthlyRegistrations(), | ||
}, | ||
completedLessons: { | ||
total: await PostStats.getLessonsUsersHaveCompleted(), | ||
monthly: await PostStats.getMonthlyLessonsUsersHaveCompleted(), | ||
}, | ||
watchSeconds: { | ||
total: await PostStats.getLessonsWatchDuration(), | ||
monthly: await PostStats.getMonthlyLessonsWatchDuration(), | ||
}, | ||
postSeconds: await PostStats.getTotalPostSeconds(), | ||
} | ||
} | ||
|
||
static async #countPublishedPosts() { | ||
return Post.query() | ||
.apply((scope) => scope.published()) | ||
.getCount() | ||
} | ||
|
||
static async #countPostSeconds() { | ||
return Post.query() | ||
.apply((scope) => scope.published()) | ||
.getSum('video_seconds') | ||
} | ||
|
||
static async #countSeries() { | ||
return Collection.query().wherePublic().whereNull('parentId').getCount() | ||
} | ||
|
||
static async #countTopics() { | ||
return Taxonomy.query().getCount() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import db from '@adonisjs/lucid/services/db' | ||
import { DateTime } from 'luxon' | ||
import GetMonthly from './get_monthly.js' | ||
import Post from '#models/post' | ||
import States from '#enums/states' | ||
import Progress from '#models/progress' | ||
|
||
export default class PostStats { | ||
static async getTotalPublished() { | ||
return Post.query() | ||
.apply((scope) => scope.published()) | ||
.getCount() | ||
} | ||
|
||
static async getMonthlyPublished( | ||
startDate: DateTime<true> = DateTime.now().minus({ year: 1 }).startOf('month') | ||
) { | ||
const query = db | ||
.from('posts') | ||
.where('state_id', States.PUBLIC) | ||
.where('publish_at', '<=', DateTime.now().toSQL()) | ||
|
||
return GetMonthly.count(query, { startDate, monthlyColumn: 'publish_at' }) | ||
} | ||
|
||
static async getTotalPostSeconds() { | ||
return Post.query() | ||
.apply((scope) => scope.published()) | ||
.getSum('video_seconds') | ||
} | ||
|
||
static async getLessonsUsersHaveCompleted() { | ||
return Progress.query().where('isCompleted', true).getCount() | ||
} | ||
|
||
static async getMonthlyLessonsUsersHaveCompleted( | ||
startDate: DateTime<true> = DateTime.now().minus({ year: 1 }).startOf('month') | ||
) { | ||
const query = db.from('progresses').where('is_completed', true) | ||
|
||
return GetMonthly.count(query, { | ||
startDate, | ||
monthlyColumn: 'updated_at', | ||
}) | ||
} | ||
|
||
static async getLessonsWatchDuration() { | ||
return Progress.query().getSum('watch_seconds') | ||
} | ||
|
||
static async getMonthlyLessonsWatchDuration( | ||
startDate: DateTime<true> = DateTime.now().minus({ year: 1 }).startOf('month') | ||
) { | ||
return GetMonthly.sum(db.from('progresses'), { | ||
startDate, | ||
monthlyColumn: 'updated_at', | ||
aggregateColumn: 'watch_seconds', | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters