Skip to content

Commit 8287035

Browse files
authored
Merge pull request #10 from larrydiamond/QueryingApplications
Querying applications
2 parents 250a2f6 + a4f6b39 commit 8287035

File tree

6 files changed

+82
-4
lines changed

6 files changed

+82
-4
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Copyright (C) 2023-present Larry Diamond, All Rights Reserved.
3+
*
4+
* This program is distributed in the hope that it will be useful,
5+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
6+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7+
* license linked below for more details.
8+
*
9+
* For license terms, see <https://github.com/larrydiamond/sqgraph/blob/main/LICENSE.md>.
10+
**/
11+
package com.ldiamond.sqgraph;
12+
13+
import lombok.Data;
14+
15+
@Data
16+
public class ApiProjectsSearchResults {
17+
ApiProjectsSearchResultsComponents [] components;
18+
}
19+
20+
@Data
21+
class ApiProjectsSearchResultsComponents {
22+
String key;
23+
String name;
24+
25+
public Application getApplication () {
26+
Application application = new Application();
27+
application.setKey (key);
28+
application.setTitle (name);
29+
return application;
30+
}
31+
}

src/main/java/com/ldiamond/sqgraph/Config.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
package com.ldiamond.sqgraph;
1212

1313
import java.io.Serializable;
14+
import java.util.List;
1415

1516
import lombok.Data;
1617

@@ -23,6 +24,7 @@ public class Config implements Serializable {
2324
int maxReportHistory;
2425
String pdf;
2526
String dashboard;
27+
List<Application> expandedApplications;
2628
}
2729

2830
@Data
@@ -41,4 +43,5 @@ class Application implements Serializable {
4143
private static final long serialVersionUID = 2422222041950251807L;
4244
String key;
4345
String title;
46+
String query;
4447
}

src/main/java/com/ldiamond/sqgraph/DashboardOutput.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ public static BufferedImage outputDashboard (HashBasedTable<String, String, Doub
4646
dashboardColumns [dcOffset++] = dcCol + " ";
4747
}
4848

49-
String [] [] dashboardFormattedData = new String [config.getApplications().length] [];
49+
String [] [] dashboardFormattedData = new String [config.getExpandedApplications().size()] [];
5050

5151
int rowLoop = 0;
52-
for (Application app : config.getApplications()) {
52+
for (Application app : config.getExpandedApplications()) {
5353
Map<String,Double> rowMap = dashboardData.column(app.getTitle());
5454
String [] dRow = new String [1 + dashboardData.rowKeySet().size()];
5555
dRow [0] = " " + app.getTitle();

src/main/java/com/ldiamond/sqgraph/SqgraphApplication.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,30 @@ public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
105105
return null;
106106
}
107107

108+
// Copy the config applications to the expanded applications and perform any searches
109+
config.setExpandedApplications(new ArrayList<>());
110+
for (Application app : config.getApplications()) {
111+
if (app.getKey() != null) {
112+
config.getExpandedApplications().add(app);
113+
} else {
114+
if (app.getQuery() != null) {
115+
final String uri = config.getUrl() + "/api/projects/search?qualifiers=TRK&q=" + app.getQuery();
116+
ResponseEntity<ApiProjectsSearchResults> response = restTemplate.exchange(uri, HttpMethod.GET, new HttpEntity<String>(headers), ApiProjectsSearchResults.class);
117+
ApiProjectsSearchResults result = response.getBody();
118+
if ((result != null) && (result.getComponents() != null)) {
119+
for (ApiProjectsSearchResultsComponents c : result.getComponents()) {
120+
config.getExpandedApplications().add(c.getApplication());
121+
}
122+
}
123+
}
124+
}
125+
}
126+
108127
Map<String, String> titleLookup = new HashMap<>();
109128
final SimpleDateFormat sdfsq = new SimpleDateFormat("yyyy-MM-dd");
110129

111130
Map<String, AssembledSearchHistory> rawMetrics = new HashMap<>();
112-
for (Application app : config.getApplications()) {
131+
for (Application app : config.getExpandedApplications()) {
113132
String key = app.getKey();
114133
titleLookup.put(key, app.getTitle());
115134
try {
@@ -179,7 +198,7 @@ public static AssembledSearchHistory getHistory (final Config config, final Stri
179198
ResponseEntity<SearchHistory> response = restTemplate.exchange(uri, HttpMethod.GET, new HttpEntity<String>(headers), SearchHistory.class);
180199
SearchHistory result = response.getBody();
181200
if (result != null) {
182-
if ((result.getPaging() != null) && (result.getPaging().total == page)) {
201+
if ((result.getPaging() != null) && (result.getPaging().total <= page)) {
183202
notYetLastPage = false;
184203
try {
185204
Thread.sleep(1); // SonarCloud implemented rate limiting, https://docs.github.com/en/rest/rate-limit?apiVersion=2022-11-28, sorry for contributing to the problem. I guess we all got popular :)

src/test/java/com/ldiamond/sqgraph/ArchitectureUnitTests.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ public class ArchitectureUnitTests {
3131
@ArchTest
3232
static final ArchRule no_treeset = noClasses().should().callConstructor(java.util.TreeSet.class).because("Use ConcurrentSkipListSet");
3333

34+
@ArchTest
35+
static final ArchRule no_threadgroup = noClasses().should().callConstructor(java.lang.ThreadGroup.class).because("Use ExecutorService");
36+
3437
@ArchTest
3538
static final ArchRule no_treemap = noClasses().should().callConstructor(java.util.TreeMap.class).because("Use ConcurrentSkipListMap");
3639

src/test/java/com/ldiamond/sqgraph/SqgraphApplicationTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,4 +301,26 @@ void getHistory() {
301301
assertEquals(1, ash.getMeasures().get(2).history.length);
302302
assertEquals(1, ash.getMeasures().get(3).history.length);
303303
}
304+
305+
@Test
306+
void getHistoryEmpty() {
307+
308+
final Config config = new Config();
309+
config.setUrl("prefix");
310+
311+
SearchHistory sh = new SearchHistory();
312+
Paging paging = new Paging();
313+
paging.setTotal(0);
314+
sh.setPaging(paging);
315+
Measures[] measuresArray = new Measures[0];
316+
sh.setMeasures(measuresArray);
317+
318+
ResponseEntity<SearchHistory> rsh = new ResponseEntity<>(sh, null, HttpStatus.OK);
319+
HttpHeaders httpHeaders = new HttpHeaders();
320+
HttpEntity<String> hes = new HttpEntity<>(httpHeaders);
321+
when (restTemplate.exchange("prefix/api/measures/search_history?from=blah&p=1&ps=999&component=blah&metrics=blah",HttpMethod.GET,hes,SearchHistory.class)).thenReturn(rsh);
322+
AssembledSearchHistory ash = SqgraphApplication.getHistory(config, "blah", "blah", "blah", httpHeaders, restTemplate);
323+
324+
assertEquals(0, ash.getMeasures().size());
325+
}
304326
}

0 commit comments

Comments
 (0)