@@ -9,118 +9,131 @@ import { calcTeamAlignment } from "./services/team-alignment";
99import { aggregateHotspots , findHotspotFiles } from "./services/hotspot" ;
1010import { calcChangeCoupling } from "./services/change-coupling" ;
1111import { Options } from "./options/options" ;
12+ import { Limits } from "./model/limit" ;
1213
1314export function setupExpress ( options : Options ) {
14- const app = express ( ) ;
15-
16- app . use ( express . json ( ) ) ;
17-
18- app . get ( "/api/config" , ( req , res ) => {
19- res . sendFile ( path . join ( cwd ( ) , options . config ) ) ;
20- } ) ;
21-
22- app . post ( "/api/config" , ( req , res ) => {
23- const newConfig = req . body ;
24- const configPath = path . join ( cwd ( ) , options . config ) ;
25-
26- fs . writeFile (
27- configPath ,
28- JSON . stringify ( newConfig , null , 2 ) ,
29- "utf8" ,
30- ( error ) => {
31- if ( error ) {
32- return res . status ( 500 ) . json ( { error } ) ;
33- }
34- res . json ( { } ) ;
15+ const app = express ( ) ;
16+
17+ app . use ( express . json ( ) ) ;
18+
19+ app . get ( "/api/config" , ( req , res ) => {
20+ res . sendFile ( path . join ( cwd ( ) , options . config ) ) ;
21+ } ) ;
22+
23+ app . post ( "/api/config" , ( req , res ) => {
24+ const newConfig = req . body ;
25+ const configPath = path . join ( cwd ( ) , options . config ) ;
26+
27+ fs . writeFile (
28+ configPath ,
29+ JSON . stringify ( newConfig , null , 2 ) ,
30+ "utf8" ,
31+ ( error ) => {
32+ if ( error ) {
33+ return res . status ( 500 ) . json ( { error } ) ;
3534 }
36- ) ;
37- } ) ;
38-
39- app . get ( "/api/modules" , ( req , res ) => {
40- try {
41- const result = calcModuleInfo ( options ) ;
42- res . json ( result ) ;
43- } catch ( e : any ) {
44- console . log ( "error" , e ) ;
45- res . status ( 500 ) . json ( { message : e . message } ) ;
46- }
47- } ) ;
48-
49- app . get ( "/api/folders" , ( req , res ) => {
50- try {
51- const result = inferFolders ( options ) ;
52- res . json ( result ) ;
53- } catch ( e : any ) {
54- console . log ( "error" , e ) ;
55- res . status ( 500 ) . json ( { message : e . message } ) ;
56- }
57- } ) ;
58-
59- app . get ( "/api/coupling" , ( req , res ) => {
60- try {
61- const result = calcCoupling ( options ) ;
62- res . json ( result ) ;
63- } catch ( e : any ) {
64- console . log ( "error" , e ) ;
65- res . status ( 500 ) . json ( { message : e . message } ) ;
66- }
67- } ) ;
68-
69- app . get ( "/api/change-coupling" , async ( req , res ) => {
70- try {
71- const result = await calcChangeCoupling ( options ) ;
72- res . json ( result ) ;
73- } catch ( e : any ) {
74- console . log ( "error" , e ) ;
75- res . status ( 500 ) . json ( { message : e . message } ) ;
35+ res . json ( { } ) ;
7636 }
77- } ) ;
78-
79- app . get ( "/api/team-alignment" , async ( req , res ) => {
80- const byUser = Boolean ( req . query . byUser ) ;
81-
82- try {
83- const result = await calcTeamAlignment ( byUser , options ) ;
84- res . json ( result ) ;
85- } catch ( e : any ) {
86- console . log ( "error" , e ) ;
87- res . status ( 500 ) . json ( { message : e . message } ) ;
88- }
89- } ) ;
90-
91- app . get ( "/api/hotspots/aggregated" , async ( req , res ) => {
92- const minScore = Number ( req . query . minScore ) || - 1 ;
93- const criteria = { minScore, module : "" } ;
94-
95- try {
96- const result = await aggregateHotspots ( criteria , options ) ;
97- res . json ( result ) ;
98- } catch ( e : any ) {
99- console . log ( "error" , e ) ;
100- res . status ( 500 ) . json ( { message : e . message } ) ;
101- }
102- } ) ;
103-
104- app . get ( "/api/hotspots" , async ( req , res ) => {
105- const minScore = Number ( req . query . minScore ) || - 1 ;
106- const module = req . query . module ? String ( req . query . module ) : "" ;
107-
108- const criteria = { minScore, module } ;
109-
110- try {
111- const result = await findHotspotFiles ( criteria , options ) ;
112- res . json ( result ) ;
113- } catch ( e : any ) {
114- console . log ( "error" , e ) ;
115- res . status ( 500 ) . json ( { message : e . message } ) ;
116- }
117- } ) ;
118-
119- app . use ( express . static ( path . join ( __dirname , ".." , "public" ) ) ) ;
120-
121- app . get ( "*" , ( req , res ) => {
122- res . sendFile ( path . join ( __dirname , ".." , "public" , "index.html" ) ) ;
123- } ) ;
124- return app ;
125- }
126-
37+ ) ;
38+ } ) ;
39+
40+ app . get ( "/api/modules" , ( req , res ) => {
41+ try {
42+ const result = calcModuleInfo ( options ) ;
43+ res . json ( result ) ;
44+ } catch ( e : any ) {
45+ console . log ( "error" , e ) ;
46+ res . status ( 500 ) . json ( { message : e . message } ) ;
47+ }
48+ } ) ;
49+
50+ app . get ( "/api/folders" , ( req , res ) => {
51+ try {
52+ const result = inferFolders ( options ) ;
53+ res . json ( result ) ;
54+ } catch ( e : any ) {
55+ console . log ( "error" , e ) ;
56+ res . status ( 500 ) . json ( { message : e . message } ) ;
57+ }
58+ } ) ;
59+
60+ app . get ( "/api/coupling" , ( req , res ) => {
61+ try {
62+ const result = calcCoupling ( options ) ;
63+ res . json ( result ) ;
64+ } catch ( e : any ) {
65+ console . log ( "error" , e ) ;
66+ res . status ( 500 ) . json ( { message : e . message } ) ;
67+ }
68+ } ) ;
69+
70+ app . get ( "/api/change-coupling" , async ( req , res ) => {
71+ const limits = getLimits ( req ) ;
72+
73+ try {
74+ const result = await calcChangeCoupling ( limits , options ) ;
75+ res . json ( result ) ;
76+ } catch ( e : any ) {
77+ console . log ( "error" , e ) ;
78+ res . status ( 500 ) . json ( { message : e . message } ) ;
79+ }
80+ } ) ;
81+
82+ app . get ( "/api/team-alignment" , async ( req , res ) => {
83+ const byUser = Boolean ( req . query . byUser ) ;
84+ const limits = getLimits ( req ) ;
85+
86+ try {
87+ const result = await calcTeamAlignment ( byUser , limits , options ) ;
88+ res . json ( result ) ;
89+ } catch ( e : any ) {
90+ console . log ( "error" , e ) ;
91+ res . status ( 500 ) . json ( { message : e . message } ) ;
92+ }
93+ } ) ;
94+
95+ app . get ( "/api/hotspots/aggregated" , async ( req , res ) => {
96+ const minScore = Number ( req . query . minScore ) || - 1 ;
97+ const criteria = { minScore, module : "" } ;
98+
99+ const limits = getLimits ( req ) ;
100+
101+ try {
102+ const result = await aggregateHotspots ( criteria , limits , options ) ;
103+ res . json ( result ) ;
104+ } catch ( e : any ) {
105+ console . log ( "error" , e ) ;
106+ res . status ( 500 ) . json ( { message : e . message } ) ;
107+ }
108+ } ) ;
109+
110+ app . get ( "/api/hotspots" , async ( req , res ) => {
111+ const minScore = Number ( req . query . minScore ) || - 1 ;
112+ const module = req . query . module ? String ( req . query . module ) : "" ;
113+ const criteria = { minScore, module } ;
114+
115+ const limits = getLimits ( req ) ;
116+
117+ try {
118+ const result = await findHotspotFiles ( criteria , limits , options ) ;
119+ res . json ( result ) ;
120+ } catch ( e : any ) {
121+ console . log ( "error" , e ) ;
122+ res . status ( 500 ) . json ( { message : e . message } ) ;
123+ }
124+ } ) ;
125+
126+ app . use ( express . static ( path . join ( __dirname , ".." , "public" ) ) ) ;
127+
128+ app . get ( "*" , ( req , res ) => {
129+ res . sendFile ( path . join ( __dirname , ".." , "public" , "index.html" ) ) ;
130+ } ) ;
131+ return app ;
132+ }
133+
134+ function getLimits ( req : express . Request ) : Limits {
135+ return {
136+ limitCommits : parseInt ( "" + req . query . limitCommits ) || null ,
137+ limitMonths : parseInt ( "" + req . query . limitMonths ) || null ,
138+ } ;
139+ }
0 commit comments