Skip to content

Commit 8a21a2a

Browse files
committed
Refactor adoption and target services: export AdoptionType, enhance getAllSettings method with type, and implement calculateTargets for improved target initialization
1 parent ee3b05e commit 8a21a2a

File tree

4 files changed

+56
-64
lines changed

4 files changed

+56
-64
lines changed

backend/src/services/adoption.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import logger from './logger.js';
44
import { SeatEntry } from './seats.service.js';
55
import { SeatType } from 'models/seats.model.js';
66

7-
type AdoptionType = {
7+
export type AdoptionType = {
88
_id?: string;
99
enterprise: string | null;
1010
org: string | null;

backend/src/services/settings.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class SettingsService {
3939
return this.settings;
4040
}
4141

42-
async getAllSettings() {
42+
async getAllSettings(): Promise<SettingsType> {
4343
try {
4444
const Setting = mongoose.model('Settings');
4545
const settingsArray = await Setting.find<{

backend/src/services/target.service.ts

Lines changed: 53 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import mongoose from 'mongoose';
2-
import adoptionService from './adoption.service.js';
2+
import adoptionService, { AdoptionType } from './adoption.service.js';
33
import app from 'index.js';
4+
import { SettingsType } from './settings.service.js';
45

56
interface Target {
67
current: number;
@@ -53,75 +54,66 @@ class TargetValuesService {
5354
}
5455
}
5556

57+
calculateTargets(settings: SettingsType, adoptions: AdoptionType[]): Targets {
58+
59+
console.log('tmp', settings);
60+
61+
const topAdoptions = adoptions
62+
.sort((a, b) => b.totalActive - a.totalActive)
63+
.slice(0, 10);
64+
65+
const averages = topAdoptions.reduce((acc, curr) => {
66+
return {
67+
totalSeats: acc.totalSeats + curr.totalSeats,
68+
totalActive: acc.totalActive + curr.totalActive,
69+
totalInactive: acc.totalInactive + curr.totalInactive
70+
};
71+
}, { totalSeats: 0, totalActive: 0, totalInactive: 0 });
72+
73+
const avgTotalSeats = Math.round(averages.totalSeats / topAdoptions.length);
74+
const avgTotalActive = Math.round(averages.totalActive / topAdoptions.length);
75+
76+
return {
77+
org: {
78+
seats: { current: avgTotalSeats, target: avgTotalSeats, max: avgTotalSeats },
79+
adoptedDevs: { current: avgTotalActive, target: avgTotalActive, max: avgTotalSeats },
80+
monthlyDevsReportingTimeSavings: { current: 0, target: 0, max: avgTotalSeats },
81+
percentOfSeatsReportingTimeSavings: { current: 0, target: 0, max: 100 },
82+
percentOfSeatsAdopted: {
83+
current: Math.round((avgTotalActive / avgTotalSeats) * 100),
84+
target: Math.round((avgTotalActive / avgTotalSeats) * 100),
85+
max: 100
86+
},
87+
percentOfMaxAdopted: { current: 0, target: 0, max: 100 },
88+
},
89+
user: {
90+
dailySuggestions: { current: 0, target: 0, max: 0 },
91+
dailyAcceptances: { current: 0, target: 0, max: 0 },
92+
dailyChatTurns: { current: 0, target: 0, max: 0 },
93+
dailyDotComChats: { current: 0, target: 0, max: 0 },
94+
weeklyPRSummaries: { current: 0, target: 0, max: 0 },
95+
weeklyTimeSavedHrs: { current: 0, target: 0, max: 0 },
96+
},
97+
impact: {
98+
monthlyTimeSavingsHrs: { current: 0, target: 0, max: 0 },
99+
annualTimeSavingsAsDollars: { current: 0, target: 0, max: 0 },
100+
productivityOrThroughputBoostPercent: { current: 0, target: 0, max: 100 },
101+
},
102+
};
103+
}
104+
56105
async initialize() {
57106
try {
58107
const Targets = mongoose.model('Targets');
59108
const existingTargets = await Targets.findOne();
60109

61-
if (!existingTargets) {
62-
const {
63-
devCostPerYear = 100000,
64-
percentCoding,
65-
percentTimeSaved,
66-
developerCount,
67-
hoursPerYear
68-
} = await app.settingsService.getAllSettings();
69-
70-
console.log('tmp', {
71-
devCostPerYear,
72-
percentCoding,
73-
percentTimeSaved,
74-
developerCount,
75-
hoursPerYear
76-
});
77-
110+
if (1 || !existingTargets) {
111+
const settings = await app.settingsService.getAllSettings();
78112
const adoptions = await adoptionService.getAllAdoptions2({
79113
filter: { enterprise: 'enterprise' },
80114
projection: {}
81115
});
82-
83-
const topAdoptions = adoptions
84-
.sort((a, b) => b.totalActive - a.totalActive)
85-
.slice(0, 10);
86-
87-
const averages = topAdoptions.reduce((acc, curr) => {
88-
return {
89-
totalSeats: acc.totalSeats + curr.totalSeats,
90-
totalActive: acc.totalActive + curr.totalActive,
91-
totalInactive: acc.totalInactive + curr.totalInactive
92-
};
93-
}, { totalSeats: 0, totalActive: 0, totalInactive: 0 });
94-
95-
const avgTotalSeats = Math.round(averages.totalSeats / topAdoptions.length);
96-
const avgTotalActive = Math.round(averages.totalActive / topAdoptions.length);
97-
98-
const initialData: Targets = {
99-
org: {
100-
seats: { current: avgTotalSeats, target: avgTotalSeats, max: avgTotalSeats },
101-
adoptedDevs: { current: avgTotalActive, target: avgTotalActive, max: avgTotalSeats },
102-
monthlyDevsReportingTimeSavings: { current: 0, target: 0, max: avgTotalSeats },
103-
percentOfSeatsReportingTimeSavings: { current: 0, target: 0, max: 100 },
104-
percentOfSeatsAdopted: {
105-
current: Math.round((avgTotalActive / avgTotalSeats) * 100),
106-
target: Math.round((avgTotalActive / avgTotalSeats) * 100),
107-
max: 100
108-
},
109-
percentOfMaxAdopted: { current: 0, target: 0, max: 100 },
110-
},
111-
user: {
112-
dailySuggestions: { current: 0, target: 0, max: 0 },
113-
dailyAcceptances: { current: 0, target: 0, max: 0 },
114-
dailyChatTurns: { current: 0, target: 0, max: 0 },
115-
dailyDotComChats: { current: 0, target: 0, max: 0 },
116-
weeklyPRSummaries: { current: 0, target: 0, max: 0 },
117-
weeklyTimeSavedHrs: { current: 0, target: 0, max: 0 },
118-
},
119-
impact: {
120-
monthlyTimeSavingsHrs: { current: 0, target: 0, max: 0 },
121-
annualTimeSavingsAsDollars: { current: 0, target: 0, max: 0 },
122-
productivityOrThroughputBoostPercent: { current: 0, target: 0, max: 100 },
123-
},
124-
};
116+
const initialData = this.calculateTargets(settings, adoptions);
125117
await Targets.create(initialData);
126118
}
127119
} catch (error) {

frontend/src/app/main/main.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ <h1>
6767
}
6868
</mat-select>
6969
<ng-template #singleInstall>
70-
<span>{{ installations[0].account?.login }}</span>
70+
<span>{{ installations[0]?.account?.login }}</span>
7171
</ng-template>
7272
</h1>
7373
</ng-container>

0 commit comments

Comments
 (0)