Skip to content

Commit 73d141a

Browse files
committed
Reworked targeting, fixed som little issues with survey via url parameters, and updated the way target lines are rendered, so they are more likely to be visible.
1 parent bb6a991 commit 73d141a

12 files changed

Lines changed: 1069 additions & 75 deletions

File tree

backend/src/controllers/survey.controller.ts

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -64,37 +64,19 @@ class SurveyController {
6464
}
6565

6666
async getAllSurveys(req: Request, res: Response): Promise<void> {
67-
const { org, team, reasonLength, since, until, status } = req.query as { [key: string]: string | undefined };;
6867
try {
69-
const dateFilter: mongoose.FilterQuery<{
70-
$gte: Date;
71-
$lte: Date;
72-
}> = {};
73-
if (since) {
74-
dateFilter.$gte = new Date(since);
75-
}
76-
if (until) {
77-
dateFilter.$lte = new Date(until);
78-
}
79-
80-
const query = {
81-
filter: {
82-
...(org ? { org: String(org) } : {}),
83-
...(team ? { team: String(team) } : {}),
84-
...(reasonLength ? { $expr: { $and: [{ $gt: [{ $strLenCP: { $ifNull: ['$reason', ''] } }, 40] }, { $ne: ['$reason', null] }] } } : {}),
85-
...(Object.keys(dateFilter).length > 0 ? { createdAt: dateFilter } : {}),
86-
...(status ? { status } : {}),
87-
},
88-
projection: {
89-
_id: 0,
90-
__v: 0,
91-
}
92-
};
93-
94-
const Survey = mongoose.model('Survey');
95-
const surveys = await Survey.find(query.filter, query.projection);
68+
const { org, team, reasonLength, since, until, status } = req.query as { [key: string]: string | undefined };
69+
70+
const surveys = await surveyService.getAllSurveys({
71+
org,
72+
team,
73+
reasonLength,
74+
since,
75+
until,
76+
status
77+
});
78+
9679
res.status(200).json(surveys);
97-
9880
} catch (error) {
9981
res.status(500).json(error);
10082
}

backend/src/controllers/target.controller.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Request, Response } from 'express';
22
import TargetValuesService from '../services/target.service.js';
3+
import { TargetCalculationService } from '../services/target-calculation-service.js';
34

45
class TargetValuesController {
56
async getTargetValues(req: Request, res: Response): Promise<void> {
@@ -19,6 +20,28 @@ class TargetValuesController {
1920
res.status(500).json(error);
2021
}
2122
}
23+
24+
/**
25+
* Calculate targets based on current metrics, adoption, and survey data
26+
* @route GET /targets/calculate
27+
*/
28+
async calculateTargetValues(req: Request, res: Response): Promise<void> {
29+
try {
30+
const org = req.query.org || 'enterprise';
31+
const enableLogging = req.query.enableLogging === 'true';
32+
33+
// Create an instance of the calculation service
34+
const calculationService = new TargetCalculationService();
35+
36+
// Fetch data and calculate targets with optional logging
37+
const targets = await calculationService.fetchAndCalculateTargets(org, enableLogging);
38+
39+
return res.status(200).json(targets);
40+
} catch (error) {
41+
console.error('Error calculating target values:', error);
42+
return res.status(500).json({ error: 'Failed to calculate target values' });
43+
}
44+
}
2245
}
2346

2447
export default new TargetValuesController();

backend/src/routes/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ router.get('/status', setupController.getStatus);
5656

5757
router.get('/targets', targetValuesController.getTargetValues);
5858
router.post('/targets', targetValuesController.updateTargetValues);
59+
// Add the new route for target calculation
60+
router.get('/targets/calculate', targetValuesController.calculateTargetValues);
5961

6062
router.get('*', (req: Request, res: Response) => {
6163
res.status(404).send('Route not found');

backend/src/services/survey.service.ts

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1-
import { SurveyType } from "../models/survey.model.js";
21
import mongoose from 'mongoose';
32
import SequenceService from './sequence.service.js';
43
import logger from "./logger.js";
54

5+
// Define the SurveyType interface here instead of importing it
6+
export interface SurveyType {
7+
id: number;
8+
userId: string;
9+
org?: string;
10+
repo?: string;
11+
prNumber?: number;
12+
usedCopilot?: boolean;
13+
percentTimeSaved?: number;
14+
reason?: string;
15+
timeUsedFor?: string;
16+
kudos?: number;
17+
status?: string;
18+
hits?: number;
19+
createdAt?: Date;
20+
updatedAt?: Date;
21+
}
22+
623
class SurveyService {
724

825
async createSurvey(survey: SurveyType) {
@@ -48,6 +65,49 @@ class SurveyService {
4865
}
4966
}).sort({ updatedAt: -1 }).limit(20).exec();
5067
}
68+
69+
/**
70+
* Get all surveys based on filtering criteria
71+
*/
72+
async getAllSurveys(params: {
73+
org?: string;
74+
team?: string;
75+
reasonLength?: string;
76+
since?: string;
77+
until?: string;
78+
status?: string;
79+
}) {
80+
const { org, team, reasonLength, since, until, status } = params;
81+
82+
const dateFilter: mongoose.FilterQuery<{
83+
$gte: Date;
84+
$lte: Date;
85+
}> = {};
86+
87+
if (since) {
88+
dateFilter.$gte = new Date(since);
89+
}
90+
if (until) {
91+
dateFilter.$lte = new Date(until);
92+
}
93+
94+
const query = {
95+
filter: {
96+
...(org ? { org: String(org) } : {}),
97+
...(team ? { team: String(team) } : {}),
98+
...(reasonLength ? { $expr: { $and: [{ $gt: [{ $strLenCP: { $ifNull: ['$reason', ''] } }, 40] }, { $ne: ['$reason', null] }] } } : {}),
99+
...(Object.keys(dateFilter).length > 0 ? { createdAt: dateFilter } : {}),
100+
...(status ? { status } : {}),
101+
},
102+
projection: {
103+
_id: 0,
104+
__v: 0,
105+
}
106+
};
107+
108+
const Survey = mongoose.model('Survey');
109+
return Survey.find(query.filter, query.projection);
110+
}
51111
}
52112

53113
export default new SurveyService();

0 commit comments

Comments
 (0)