Skip to content

Commit

Permalink
Added resolvers for all date ranged dashboard queries
Browse files Browse the repository at this point in the history
  • Loading branch information
rayceramsay committed Sep 3, 2024
1 parent 1cc7f42 commit edc3f6d
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 6 deletions.
142 changes: 142 additions & 0 deletions backend/src/graphql/resolvers/analytics.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,35 @@ const analyticsResolver = {
}
},

getScholarApplyClicksRankedWithDateRange: async (_: any, { startDate, endDate }: any, { dataSources }: any) => {
const { db } = dataSources
const client = await establishConnection(db)
try {
const query = `
SELECT scholar.scholar_id, scholar.name, scholar.email, COUNT(*) AS clicks
FROM apply_clicks
JOIN scholar ON apply_clicks.scholar_id = scholar.scholar_id
WHERE apply_clicks.click_time BETWEEN $1 AND $2
GROUP BY scholar.scholar_id, scholar.name, scholar.email
ORDER BY clicks DESC
`
const resp = await client.query(query, [startDate, endDate])
console.log(resp.rows)
const formattedRows = resp.rows.map((row: any) => ({
scholarId: row.scholar_id,
apply_count: parseInt(row.clicks),
scholarName: row.name,
scholarEmail: row.email,
}))
return formattedRows
} catch (err) {
console.error(err)
throw new Error('Failed to fetch scholar job clicks with date range')
} finally {
client.release()
}
},

getScholarJobClicksRanked: async (
_: any,
args: any,
Expand Down Expand Up @@ -195,6 +224,35 @@ const analyticsResolver = {
}
},

getScholarJobClicksRankedWithDateRange: async (_: any, { startDate, endDate }: any, { dataSources }: any) => {
const { db } = dataSources
const client = await establishConnection(db)
try {
const query = `
SELECT scholar.scholar_id, scholar.name, scholar.email, COUNT(*) AS clicks
FROM job_clicks
JOIN scholar ON job_clicks.scholar_id = scholar.scholar_id
WHERE job_clicks.click_time BETWEEN $1 AND $2
GROUP BY scholar.scholar_id, scholar.name, scholar.email
ORDER BY clicks DESC
`
const resp = await client.query(query, [startDate, endDate])
console.log(resp.rows)
const formattedRows = resp.rows.map((row: any) => ({
scholarId: row.scholar_id,
job_count: parseInt(row.clicks),
scholarName: row.name,
scholarEmail: row.email,
}))
return formattedRows
} catch (err) {
console.error(err)
throw new Error('Failed to fetch scholar job clicks with date range')
} finally {
client.release()
}
},

getScholarClicksBySchool: async (
_: any,
args: any,
Expand Down Expand Up @@ -224,6 +282,32 @@ const analyticsResolver = {
}
},

getScholarClicksBySchoolWithDateRange: async (_: any, { startDate, endDate }: any, { dataSources }: any) => {
const { db } = dataSources
const client = await establishConnection(db)
try {
const query = `
SELECT school, COUNT(*) AS scholar_click_count
FROM job_clicks JOIN scholar ON job_clicks.scholar_id = scholar.scholar_id
WHERE job_clicks.click_time BETWEEN $1 AND $2
GROUP BY school
ORDER BY scholar_click_count DESC
`
const resp = await client.query(query, [startDate, endDate])
console.log(resp.rows)
const formattedRows = resp.rows.map((row: any) => ({
school: row.school,
scholar_click_count: parseInt(row.scholar_click_count),
}))
return formattedRows
} catch (err) {
console.error('Error executing query:', err)
throw new Error('Failed to get scholar clicks by school with date range')
} finally {
client.release()
}
},

getScholarEmployerClicksRanked: async (
_: any,
args: any,
Expand Down Expand Up @@ -256,6 +340,35 @@ const analyticsResolver = {
}
},

getScholarEmployerClicksRankedWithDateRange: async (_: any, { startDate, endDate }: any, { dataSources }: any) => {
const { db } = dataSources
const client = await establishConnection(db)
try {
const query = `
SELECT scholar.scholar_id, scholar.name, scholar.email, COUNT(*) AS clicks
FROM employer_clicks
JOIN scholar ON employer_clicks.scholar_id = scholar.scholar_id
WHERE employer_clicks.click_time BETWEEN $1 AND $2
GROUP BY scholar.scholar_id, scholar.name, scholar.email
ORDER BY clicks DESC
`
const resp = await client.query(query, [startDate, endDate])
console.log(resp.rows)
const formattedRows = resp.rows.map((row: any) => ({
scholarId: row.scholar_id,
employer_count: parseInt(row.clicks),
scholarName: row.name,
scholarEmail: row.email,
}))
return formattedRows
} catch (err) {
console.error(err)
throw new Error('Failed to fetch scholar employer clicks with date range')
} finally {
client.release()
}
},

getJobClicks: async (_: any, args: any, { dataSources }: any) => {
const { db } = dataSources;
const client = await establishConnection(db);
Expand Down Expand Up @@ -435,6 +548,7 @@ const analyticsResolver = {
client.release();
}
},

getJobTagRankingByClicksWithDateRange: async (_: any, { startDate, endDate }: any, { dataSources }: any) => {
const { db } = dataSources
const client = await establishConnection(db)
Expand All @@ -460,6 +574,7 @@ const analyticsResolver = {
client.release()
}
},

getApplyClicksForScholar: async (
_: any,
{ scholarId }: any,
Expand Down Expand Up @@ -731,6 +846,33 @@ const analyticsResolver = {
}
},

getEmployerJobPostingsRankingWithDateRange: async (_: any, { startDate, endDate }: any, { dataSources }: any) => {
const { db } = dataSources
const client = await establishConnection(db)
try {
const query = `
SELECT employer.name, employer.employer_id, COUNT(*) AS job_click_count
FROM job JOIN job_clicks ON job.job_id = job_clicks.job_id JOIN employer ON job.employer_id = employer.employer_id
WHERE live = true AND job_clicks.click_time BETWEEN $1 AND $2
GROUP BY employer.employer_id
ORDER BY job_click_count DESC
`
const resp = await client.query(query, [startDate, endDate])
console.log(resp.rows)
const formattedRows = resp.rows.map((row: any) => ({
employerName: row.name,
employerId: row.employer_id,
job_posting_click_count: parseInt(row.job_click_count),
}))
return formattedRows
} catch (err) {
console.error('Error executing query:', err)
throw new Error('Failed to get employer job postings ranking with date range')
} finally {
client.release()
}
},

getNumDaysSinceLastJobPostByEmployer: async (
_: any,
args: any,
Expand Down
7 changes: 6 additions & 1 deletion backend/src/graphql/typeDefs/analytics.typedef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,23 @@ export const analyticsTypeDefs = gql`
getEmployerClicksRanked: [RankedEmployerClick]
getJobTagRanking: [JobTagRanking]
getJobTagRankingByClicks: [JobTagRankingByClick]
getJobTagRankingByClicksWithDateRange(startDate: Date, endDate: Date): [JobTagRankingByClick]
getJobLocationRanking: [JobLocationRanking]
getJobDeadlineRankingByMonth: [JobDeadlineRanking]
getScholarsRankedByMajor: [MajorRanking]
getScholarsRankedByYear: [YearRanking]
getPercentageOfScholarsWithAllowedNotifications: Int
getScholarApplyClicksRanked: [ApplyClickRank]
getScholarApplyClicksRankedWithDateRange(startDate: Date, endDate: Date): [ApplyClickRank]
getScholarJobClicksRanked: [JobClickRank]
getScholarJobClicksRankedWithDateRange(startDate: Date, endDate: Date): [JobClickRank]
getScholarEmployerClicksRanked: [EmployerClickRank]
getScholarEmployerClicksRankedWithDateRange(startDate: Date, endDate: Date): [EmployerClickRank]
getJobClicksRankedByApply: [RankedJobClick]
getScholarClicksBySchool: [ScholarClicksBySchool]
getScholarClicksBySchoolWithDateRange(startDate: Date, endDate: Date): [ScholarClicksBySchool]
getEmployerJobPostingsRanking: [EmployerJobPostingRank]
getEmployerJobPostingsRankingWithDateRange(startDate: Date, endDate: Date): [EmployerJobPostingRank]
getNumDaysSinceLastJobPostByEmployer: [EmployerLastJobPost]
getMostPopularJobTagsByEmployer: [EmployerJobTagRanking]
getJobClicksForScholar(scholarId: Int): [ScholarJobClicks]
Expand All @@ -37,7 +43,6 @@ export const analyticsTypeDefs = gql`
getNumberOfActiveScholars: Int
getNumberOfAllowedScholars: Int
getClicksCustomAnalytics(startDate: Date, endDate: Date, interval: String, clickType: String): [CustomAnalytics]
getJobTagRankingByClicksWithDateRange(startDate: Date, endDate: Date): [JobTagRankingByClick]
}
type Mutation {
Expand Down
10 changes: 5 additions & 5 deletions frontend/graphql/queries/analyticsQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,23 +324,23 @@ export const GET_ANALYTICS_DASHBOARD_DATA = gql`
tag
click_count
}
employerJobPostingClicks: getEmployerJobPostingsRanking {
employerJobPostingClicks: getEmployerJobPostingsRankingWithDateRange(startDate: $startDate, endDate: $endDate) {
employerName
job_posting_click_count
}
scholarClicksBySchool: getScholarClicksBySchool {
scholarClicksBySchool: getScholarClicksBySchoolWithDateRange(startDate: $startDate, endDate: $endDate) {
school
scholar_click_count
}
scholarJobClicks: getScholarJobClicksRanked {
scholarJobClicks: getScholarJobClicksRankedWithDateRange(startDate: $startDate, endDate: $endDate) {
scholarName
job_count
}
scholarApplyClicks: getScholarApplyClicksRanked {
scholarApplyClicks: getScholarApplyClicksRankedWithDateRange(startDate: $startDate, endDate: $endDate) {
scholarName
apply_count
}
scholarEmployerClicks: getScholarEmployerClicksRanked {
scholarEmployerClicks: getScholarEmployerClicksRankedWithDateRange(startDate: $startDate, endDate: $endDate) {
scholarName
employer_count
}
Expand Down

0 comments on commit edc3f6d

Please sign in to comment.