11package com .upc .gessi .qrapids .app .domain .controllers ;
22
3- import com .upc .gessi .qrapids .app .domain .models .Alert ;
4- import com .upc .gessi .qrapids .app .domain .models .AlertStatus ;
5- import com .upc .gessi .qrapids .app .domain .models .AlertType ;
6- import com .upc .gessi .qrapids .app .domain .models .Project ;
3+ import com .upc .gessi .qrapids .app .domain .exceptions .ProjectNotFoundException ;
4+ import com .upc .gessi .qrapids .app .domain .models .*;
75import com .upc .gessi .qrapids .app .domain .repositories .Alert .AlertRepository ;
86import com .upc .gessi .qrapids .app .domain .exceptions .AlertNotFoundException ;
7+ import com .upc .gessi .qrapids .app .domain .repositories .Metric .MetricRepository ;
8+ import com .upc .gessi .qrapids .app .domain .repositories .Profile .ProfileProjectStrategicIndicatorsRepository ;
9+ import com .upc .gessi .qrapids .app .domain .repositories .Project .ProjectRepository ;
10+ import com .upc .gessi .qrapids .app .domain .repositories .QualityFactor .QualityFactorRepository ;
11+ import com .upc .gessi .qrapids .app .domain .repositories .StrategicIndicator .StrategicIndicatorRepository ;
912import org .springframework .beans .factory .annotation .Autowired ;
1013import org .springframework .data .util .Pair ;
1114import org .springframework .stereotype .Service ;
@@ -21,9 +24,30 @@ public class AlertsController {
2124 @ Autowired
2225 private AlertRepository alertRepository ;
2326
27+ @ Autowired
28+ private ProjectRepository projectRepository ;
29+
30+ @ Autowired
31+ private MetricRepository metricRepository ;
32+
33+ @ Autowired
34+ private QualityFactorRepository factorRepository ;
35+
36+ @ Autowired
37+ private StrategicIndicatorRepository strategicIndicatorRepository ;
38+
39+ @ Autowired
40+ private ProfileProjectStrategicIndicatorsRepository profileProjectStrategicIndicatorsRepository ;
41+
2442 @ Autowired
2543 private QRPatternsController qrPatternsController ;
2644
45+ @ Autowired
46+ private ProjectsController projectsController ;
47+
48+ @ Autowired
49+ private ProfilesController profilesController ;
50+
2751 public Alert getAlertById (long alertId ) throws AlertNotFoundException {
2852 Optional <Alert > alertOptional = alertRepository .findById (alertId );
2953 if (alertOptional .isPresent ()) {
@@ -52,10 +76,109 @@ public Pair<Long, Long> countNewAlerts(Project project) {
5276 return Pair .of (newAlerts , newAlertsWithQR );
5377 }
5478
79+ public Pair <Long , Long > countNewAlertsByProfile (Project project , String profileId ) throws ProjectNotFoundException {
80+ long newAlertsCount = alertRepository .countByProject_IdAndStatus (project .getId (), AlertStatus .NEW );
81+ long newAlertsWithQRCount = alertRepository .countByProject_IdAndReqAssociatIsTrueAndStatusEquals (project .getId (), AlertStatus .NEW );
82+ if ((profileId != null ) && (!profileId .equals ("null" ))) { // if profile not null
83+ Profile profile = profilesController .findProfileById (profileId );
84+ if (profile .getAllSIByProject (project )) { // if allSI true --> return all new alerts count
85+ return Pair .of (newAlertsCount , newAlertsWithQRCount );
86+ } else { // if allSI false --> return filtered new alerts
87+ List <Alert > allNewAlerts = alertRepository .getByProject_IdAndStatus (project .getId (), AlertStatus .NEW );
88+ List <Alert > allNewAlertsWithQR = alertRepository .getByProject_IdAndReqAssociatIsTrueAndStatusEquals (project .getId (), AlertStatus .NEW );
89+ List <Alert > filteredNewAlerts = filterByProfile (project , profile , allNewAlerts );
90+ List <Alert > filteredNewAlertsWithQR = filterByProfile (project , profile , allNewAlertsWithQR );
91+ return Pair .of (Long .valueOf (filteredNewAlerts .size ()), Long .valueOf (filteredNewAlertsWithQR .size ()));
92+ }
93+ } else { // if profile is null --> return all alerts
94+ return Pair .of (newAlertsCount , newAlertsWithQRCount );
95+ }
96+ }
97+
5598 public void createAlert (String id , String name , AlertType type , float value , float threshold , String category , Project project ) {
5699 Alert alert = new Alert (id , name , type , value , threshold , category , new Date (), AlertStatus .NEW , false , project );
57100 boolean hasReq = qrPatternsController .existsPatternForAlert (alert );
58101 alert .setReqAssociat (hasReq );
59102 alertRepository .save (alert );
60103 }
104+
105+ public void checkMetricAlert (String externalId , float value , String prj ){
106+ // get project from data base
107+ Project p = projectRepository .findByExternalId (prj );
108+ // get metric threshold from data base
109+ Metric m = metricRepository .findByExternalIdAndProjectId (externalId , p .getId ());
110+ // check if the value is below the threshold then create new alert for this metric
111+ if (m .getThreshold () != null && value < m .getThreshold ()) {
112+ // createAlert( id, name, type, value, threshold, category, project)
113+ createAlert (externalId , m .getName (), AlertType .METRIC , value , m .getThreshold (), externalId , p );
114+ }
115+ }
116+
117+ public void checkFactorAlert (String externalId , float value , String prj ){
118+ // get project from data base
119+ Project p = projectRepository .findByExternalId (prj );
120+ // get factor threshold from data base
121+ Factor f = factorRepository .findByExternalIdAndProjectId (externalId , p .getId ());
122+ // check if the value is below the threshold then create new alert for this factor
123+ if (f .getThreshold () != null && value < f .getThreshold ()) {
124+ // createAlert( id, name, type, value, threshold, category, project)
125+ createAlert (externalId , f .getName (), AlertType .FACTOR , value , f .getThreshold (), externalId , p );
126+ }
127+ }
128+
129+ public void checkStrategicIndicatorAlert (String externalId , float value , String prj ){
130+ // get project from data base
131+ Project p = projectRepository .findByExternalId (prj );
132+ // get factor threshold from data base
133+ Strategic_Indicator si = strategicIndicatorRepository .findByExternalIdAndProjectId (externalId , p .getId ());
134+ // check if the value is below the threshold then create new alert for this factor
135+ if (si .getThreshold () != null && value < si .getThreshold ()) {
136+ // createAlert( id, name, type, value, threshold, category, project)
137+ createAlert (externalId , si .getName (), AlertType .STRATEGIC_INDICATOR , value , si .getThreshold (), externalId , p );
138+ }
139+ }
140+
141+ public List <Alert > getAlertsByProjectAndProfile (Project project , String profileId ) throws ProjectNotFoundException {
142+ List <Alert > alerts = alertRepository .findByProject_IdOrderByDateDesc (project .getId ());
143+ if ((profileId != null ) && (!profileId .equals ("null" ))) { // if profile not null
144+ Profile profile = profilesController .findProfileById (profileId );
145+ if (profile .getAllSIByProject (project )) { // allSI true --> filter only by profile quality level
146+ if (profile .getQualityLevel ().equals (Profile .QualityLevel .METRICS_FACTORS ))
147+ alerts .removeIf (a -> a .getType ().equals (AlertType .STRATEGIC_INDICATOR ));
148+ else if (profile .getQualityLevel ().equals (Profile .QualityLevel .METRICS )) {
149+ alerts .removeIf (a -> a .getType ().equals (AlertType .STRATEGIC_INDICATOR ));
150+ alerts .removeIf (a -> a .getType ().equals (AlertType .FACTOR ));
151+ }
152+ return alerts ;
153+ } else { // if allSI false --> return alerts filtered by profile quality level and SIs
154+ return filterByProfile (project ,profile ,alerts );
155+ }
156+ } else { // if profile is null --> return all alerts
157+ return alerts ;
158+ }
159+ }
160+
161+ public List <Alert > filterByProfile (Project project , Profile profile , List <Alert > alerts ) {
162+ List <ProfileProjectStrategicIndicators > ppsiList =
163+ profileProjectStrategicIndicatorsRepository .findByProfileAndProject (profile ,project );
164+ List <Alert > result = new ArrayList <>();
165+ Profile .QualityLevel ql = profile .getQualityLevel ();
166+ for (Alert a : alerts ) {
167+ for (ProfileProjectStrategicIndicators ppsi : ppsiList ) {
168+ if (a .getType ().equals (AlertType .STRATEGIC_INDICATOR ) && a .getId_element ().equals (ppsi .getStrategicIndicator ().getExternalId ()) && !result .contains (a )
169+ && ql .equals (Profile .QualityLevel .ALL )) result .add (a );
170+ List <StrategicIndicatorQualityFactors > siqfList = ppsi .getStrategicIndicator ().getStrategicIndicatorQualityFactorsList ();
171+ for (StrategicIndicatorQualityFactors siqf : siqfList ) {
172+ if (a .getType ().equals (AlertType .FACTOR ) && a .getId_element ().equals (siqf .getFactor ().getExternalId ()) && !result .contains (a )
173+ && (ql .equals (Profile .QualityLevel .ALL ) || ql .equals (Profile .QualityLevel .METRICS_FACTORS ))) result .add (a );
174+ List <String > metrics = siqf .getFactor ().getMetrics ();
175+ for (String m : metrics ) {
176+ if (a .getType ().equals (AlertType .METRIC ) && a .getId_element ().equals (m ) && !result .contains (a ))
177+ result .add (a );
178+ }
179+ }
180+ }
181+ }
182+ return result ;
183+ }
61184}
0 commit comments