-
Notifications
You must be signed in to change notification settings - Fork 239
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
25 changed files
with
786 additions
and
201 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import ProjectRepository from "packages/shared-models/src/lib/project.repository"; | ||
import { StatsQueue, getStatsQueueInstance } from "../queues/Stats"; | ||
|
||
export default class StatsByDayEvent { | ||
projectRepo: ProjectRepository | ||
statsQueue: StatsQueue | ||
constructor() { | ||
this.statsQueue = getStatsQueueInstance() | ||
this.projectRepo = new ProjectRepository() | ||
} | ||
async run() { | ||
const { projectsWMemberEnabled, projectsWCounterEnabled } = await this.projectRepo.getProjectsWithCountSettingEnabled() | ||
|
||
console.log('stats.day.event called', new Date()) | ||
if (!projectsWCounterEnabled || !projectsWCounterEnabled.length) { | ||
console.log('No project with project counter enabled') | ||
} | ||
|
||
projectsWCounterEnabled.map(pid => { | ||
this.statsQueue.addJob('unDoneTasksByProject', pid) | ||
}) | ||
|
||
if (!projectsWMemberEnabled || !projectsWMemberEnabled.length) { | ||
console.log('No project with member counter enabled') | ||
} | ||
projectsWMemberEnabled.map(pid => { | ||
this.statsQueue.addJob('doneTasksByMember', pid) | ||
}) | ||
|
||
} | ||
} |
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
16 changes: 16 additions & 0 deletions
16
packages/be-gateway/src/queues/Stats/DoneTasksByMemberJob.ts
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,16 @@ | ||
|
||
import StatsDoneTaskService from '../../services/stats/done.tasks.service' | ||
import { BaseJob } from '../BaseJob' | ||
|
||
|
||
export class DoneTasksByMemberJob extends BaseJob { | ||
name = 'doneTasksByMember' | ||
service: StatsDoneTaskService | ||
constructor() { | ||
super() | ||
this.service = new StatsDoneTaskService() | ||
} | ||
async implement(projectId: string) { | ||
await this.service.implement(projectId) | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/be-gateway/src/queues/Stats/UnDoneTasksByProjectJob.ts
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,16 @@ | ||
|
||
import StatsUnDoneTaskService from '../../services/stats/undone.tasks.service' | ||
import { BaseJob } from '../BaseJob' | ||
|
||
|
||
export class UnDoneTasksByProjectJob extends BaseJob { | ||
name = 'unDoneTasksByProject' | ||
service: StatsUnDoneTaskService | ||
constructor() { | ||
super() | ||
this.service = new StatsUnDoneTaskService() | ||
} | ||
async implement(projectId: string) { | ||
await this.service.implement(projectId) | ||
} | ||
} |
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,24 @@ | ||
import { BaseQueue } from '../BaseQueue' | ||
import { DoneTasksByMemberJob } from './DoneTasksByMemberJob' | ||
import { UnDoneTasksByProjectJob } from './UnDoneTasksByProjectJob' | ||
|
||
|
||
export class StatsQueue extends BaseQueue { | ||
constructor() { | ||
super() | ||
this.queueName = 'Stats' | ||
this.jobs = [new DoneTasksByMemberJob(), new UnDoneTasksByProjectJob()] | ||
|
||
this.run() | ||
} | ||
} | ||
|
||
let instance: StatsQueue = null | ||
|
||
export const getStatsQueueInstance = () => { | ||
if (!instance) { | ||
instance = new StatsQueue() | ||
} | ||
|
||
return instance | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import ProjectRepository from "packages/shared-models/src/lib/project.repository"; | ||
|
||
export default class ProjectService { | ||
projectRepo: ProjectRepository | ||
constructor() { | ||
this.projectRepo = new ProjectRepository() | ||
} | ||
async getAllProjectIds() { | ||
const projectIds = await this.projectRepo.getAvailableProjectIds() | ||
|
||
return projectIds | ||
} | ||
|
||
} |
134 changes: 134 additions & 0 deletions
134
packages/be-gateway/src/services/stats/done.tasks.service.ts
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,134 @@ | ||
import { StatsType, StatusType } from "@prisma/client"; | ||
import { lastDayOfMonth } from "date-fns"; | ||
import { pmClient } from "packages/shared-models/src/lib/_prisma"; | ||
import { sendDiscordLog } from "../../lib/log"; | ||
|
||
export default class StatsDoneTaskService { | ||
async implement(projectId: string) { | ||
|
||
try { | ||
const doneStatus = await pmClient.taskStatus.findMany({ | ||
where: { | ||
type: StatusType.DONE, | ||
}, | ||
select: { | ||
id: true, | ||
} | ||
}) | ||
|
||
const ids = doneStatus.map(d => d.id) | ||
|
||
const now = new Date() | ||
const y = now.getFullYear() | ||
const m = now.getMonth() | ||
const d = now.getDate() | ||
const month = m + 1 | ||
|
||
const firstDay = new Date(y, m, 1, 0, 0) | ||
const lastDay = lastDayOfMonth(now) | ||
lastDay.setHours(23) | ||
lastDay.setMinutes(59) | ||
|
||
const result = await pmClient.task.findMany({ | ||
where: { | ||
projectId, | ||
assigneeIds: { | ||
isEmpty: false | ||
}, | ||
taskStatusId: { | ||
in: ids | ||
}, | ||
OR: [ | ||
{ | ||
AND: [ | ||
{ | ||
dueDate: { | ||
gte: firstDay | ||
} | ||
}, | ||
{ | ||
dueDate: { | ||
lte: lastDay | ||
} | ||
}, | ||
|
||
] | ||
|
||
} | ||
] | ||
}, | ||
select: { | ||
id: true, | ||
assigneeIds: true, | ||
dueDate: true, | ||
} | ||
}) | ||
|
||
|
||
const totalByMembers = new Map<string, number>() | ||
|
||
result.forEach(r => { | ||
r.assigneeIds.forEach(a => { | ||
if (totalByMembers.has(a)) { | ||
totalByMembers.set(a, totalByMembers.get(a) + 1) | ||
} else { | ||
totalByMembers.set(a, 1) | ||
} | ||
}) | ||
|
||
}) | ||
|
||
totalByMembers.forEach(async (total, uid) => { | ||
|
||
const existing = await pmClient.stats.findFirst({ | ||
where: { | ||
projectId, | ||
type: StatsType.MEMBER_TASK_BY_DAY, | ||
uid, | ||
year: y, | ||
month, | ||
date: d | ||
} | ||
}) | ||
|
||
// create new if doesn't exist | ||
if (!existing) { | ||
await pmClient.stats.create({ | ||
data: { | ||
type: StatsType.MEMBER_TASK_BY_DAY, | ||
projectId, | ||
uid, | ||
year: y, | ||
month, | ||
date: d, | ||
data: { | ||
doneTotal: total | ||
}, | ||
updatedAt: new Date() | ||
} | ||
}) | ||
|
||
// update if existing | ||
} else { | ||
await pmClient.stats.update({ | ||
where: { | ||
id: existing.id | ||
}, | ||
data: { | ||
data: { | ||
doneTotal: total | ||
}, | ||
updatedAt: new Date() | ||
} | ||
|
||
}) | ||
} | ||
}) | ||
|
||
sendDiscordLog("Count done tasks per member finished") | ||
} catch (error) { | ||
sendDiscordLog("done.task.service.error: " + JSON.stringify(error)) | ||
console.log('done.task.service error', error) | ||
} | ||
} | ||
} |
Oops, something went wrong.