Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update metrics for date ranges #172

Merged
merged 1 commit into from
Aug 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/database/migrations/1619179734907-users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ VALUES(uid(),now(),now(),'Admin','[email protected]','admin','Portal','${hash}
await queryRunner.query(
'ALTER TABLE APPLIEDJOB ADD COLUMN LOCATION CHARACTER VARYING(255)',
);

await queryRunner.query('ALTER TABLE APPLIEDJOB ADD COLUMN CREATED DATE');

await queryRunner.query(
"INSERT INTO USERROLE(uid,name) values(UID(), 'SUPER USER')",
);
Expand Down
2 changes: 1 addition & 1 deletion src/modules/job/services/job.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class JobService extends BaseService<Job> {
return sessionUser;
}
async apply({ job, user }): Promise<{ message: string }> {
const query = `INSERT INTO APPLIEDJOB(USERId, JOBID) VALUES(${user.id}, ${job.id})`;
const query = `INSERT INTO APPLIEDJOB(CREATED, USERId, JOBID) VALUES(NOW(),${user.id}, ${job.id})`;
await this.userrepository.manager.query(query);
return {
message: `You have successfully applied to with name <${job.name}>`,
Expand Down
2 changes: 1 addition & 1 deletion src/modules/user/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class AuthController {
(role) => role.name === 'SUPER USER' || role.name === 'ADMIN',
);
if (admin.length > 0) {
const metrics = await this.authService.getMertics();
const metrics = await this.authService.getMertics(query);
return res.status(HttpStatus.OK).send(metrics);
} else {
return res
Expand Down
59 changes: 43 additions & 16 deletions src/modules/user/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import { JwtService } from '@nestjs/jwt';
import { InjectRepository } from '@nestjs/typeorm';
import { Job } from '../../job/entities/job.entity';
import { In, Repository } from 'typeorm';
import { Between, In, Repository } from 'typeorm';
import { User } from '../entities/user.entity';
import jwt_decode from 'jwt-decode';
import {
Expand Down Expand Up @@ -80,20 +80,47 @@ export class AuthService {
relations: getRelations(fields, metaData),
});
}
async getMertics(): Promise<any> {
const users = await this.userrepository.count();
const companies = await this.companyrepository.count();
const applications = Number(
(
await this.jobrepository.manager.query(
'SELECT COUNT(*) FROM APPLIEDJOB',
)
)[0]['count'],
);
const jobs = await this.jobrepository.count();
return {
message: 'Job Portal Admin Metrics',
metrics: { users, companies, applications, jobs },
};
async getMertics(query: { startDate: Date; endDate: Date }): Promise<any> {
if (Object.keys(query).length === 0) {
const users = await this.userrepository.count();
const companies = await this.companyrepository.count();
const applications = Number(
(
await this.jobrepository.manager.query(
'SELECT COUNT(*) FROM APPLIEDJOB',
)
)[0]['count'],
);
const jobs = await this.jobrepository.count();
return {
message: 'Job Portal Admin Metrics',
metrics: { users, companies, applications, jobs },
};
} else {
const date = new Date(query.startDate);
date.setDate(date.getDate() - 1);
const startdate = date.toISOString().split('T')[0];
const dates = new Date(query.endDate);
dates.setDate(dates.getDate() + 1);
const enddate = dates.toISOString().split('T')[0];
const users = await this.userrepository.count({
where: { created: Between(startdate, enddate) },
});
const companies = await this.companyrepository.count({
where: { created: Between(startdate, enddate) },
});
const applications = Number(
(
await this.jobrepository.manager.query(
`SELECT COUNT(*) FROM APPLIEDJOB WHERE CREATED >= '${startdate}' AND CREATED <='${enddate}'`,
)
)[0]['count'],
);
const jobs = await this.jobrepository.count();
return {
message: `Job Portal Admin Metrics from ${query.startDate} to ${query.endDate}`,
metrics: { users, companies, applications, jobs },
};
}
}
}