Skip to content

Commit

Permalink
Merge pull request #10 from larrydiamond/QueryingApplications
Browse files Browse the repository at this point in the history
Querying applications
  • Loading branch information
larrydiamond authored Oct 15, 2023
2 parents 250a2f6 + a4f6b39 commit 8287035
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 4 deletions.
31 changes: 31 additions & 0 deletions src/main/java/com/ldiamond/sqgraph/ApiProjectsSearchResults.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright (C) 2023-present Larry Diamond, All Rights Reserved.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* license linked below for more details.
*
* For license terms, see <https://github.com/larrydiamond/sqgraph/blob/main/LICENSE.md>.
**/
package com.ldiamond.sqgraph;

import lombok.Data;

@Data
public class ApiProjectsSearchResults {
ApiProjectsSearchResultsComponents [] components;
}

@Data
class ApiProjectsSearchResultsComponents {
String key;
String name;

public Application getApplication () {
Application application = new Application();
application.setKey (key);
application.setTitle (name);
return application;
}
}
3 changes: 3 additions & 0 deletions src/main/java/com/ldiamond/sqgraph/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package com.ldiamond.sqgraph;

import java.io.Serializable;
import java.util.List;

import lombok.Data;

Expand All @@ -23,6 +24,7 @@ public class Config implements Serializable {
int maxReportHistory;
String pdf;
String dashboard;
List<Application> expandedApplications;
}

@Data
Expand All @@ -41,4 +43,5 @@ class Application implements Serializable {
private static final long serialVersionUID = 2422222041950251807L;
String key;
String title;
String query;
}
4 changes: 2 additions & 2 deletions src/main/java/com/ldiamond/sqgraph/DashboardOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ public static BufferedImage outputDashboard (HashBasedTable<String, String, Doub
dashboardColumns [dcOffset++] = dcCol + " ";
}

String [] [] dashboardFormattedData = new String [config.getApplications().length] [];
String [] [] dashboardFormattedData = new String [config.getExpandedApplications().size()] [];

int rowLoop = 0;
for (Application app : config.getApplications()) {
for (Application app : config.getExpandedApplications()) {
Map<String,Double> rowMap = dashboardData.column(app.getTitle());
String [] dRow = new String [1 + dashboardData.rowKeySet().size()];
dRow [0] = " " + app.getTitle();
Expand Down
23 changes: 21 additions & 2 deletions src/main/java/com/ldiamond/sqgraph/SqgraphApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,30 @@ public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return null;
}

// Copy the config applications to the expanded applications and perform any searches
config.setExpandedApplications(new ArrayList<>());
for (Application app : config.getApplications()) {
if (app.getKey() != null) {
config.getExpandedApplications().add(app);
} else {
if (app.getQuery() != null) {
final String uri = config.getUrl() + "/api/projects/search?qualifiers=TRK&q=" + app.getQuery();
ResponseEntity<ApiProjectsSearchResults> response = restTemplate.exchange(uri, HttpMethod.GET, new HttpEntity<String>(headers), ApiProjectsSearchResults.class);
ApiProjectsSearchResults result = response.getBody();
if ((result != null) && (result.getComponents() != null)) {
for (ApiProjectsSearchResultsComponents c : result.getComponents()) {
config.getExpandedApplications().add(c.getApplication());
}
}
}
}
}

Map<String, String> titleLookup = new HashMap<>();
final SimpleDateFormat sdfsq = new SimpleDateFormat("yyyy-MM-dd");

Map<String, AssembledSearchHistory> rawMetrics = new HashMap<>();
for (Application app : config.getApplications()) {
for (Application app : config.getExpandedApplications()) {
String key = app.getKey();
titleLookup.put(key, app.getTitle());
try {
Expand Down Expand Up @@ -179,7 +198,7 @@ public static AssembledSearchHistory getHistory (final Config config, final Stri
ResponseEntity<SearchHistory> response = restTemplate.exchange(uri, HttpMethod.GET, new HttpEntity<String>(headers), SearchHistory.class);
SearchHistory result = response.getBody();
if (result != null) {
if ((result.getPaging() != null) && (result.getPaging().total == page)) {
if ((result.getPaging() != null) && (result.getPaging().total <= page)) {
notYetLastPage = false;
try {
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 :)
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/com/ldiamond/sqgraph/ArchitectureUnitTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public class ArchitectureUnitTests {
@ArchTest
static final ArchRule no_treeset = noClasses().should().callConstructor(java.util.TreeSet.class).because("Use ConcurrentSkipListSet");

@ArchTest
static final ArchRule no_threadgroup = noClasses().should().callConstructor(java.lang.ThreadGroup.class).because("Use ExecutorService");

@ArchTest
static final ArchRule no_treemap = noClasses().should().callConstructor(java.util.TreeMap.class).because("Use ConcurrentSkipListMap");

Expand Down
22 changes: 22 additions & 0 deletions src/test/java/com/ldiamond/sqgraph/SqgraphApplicationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -301,4 +301,26 @@ void getHistory() {
assertEquals(1, ash.getMeasures().get(2).history.length);
assertEquals(1, ash.getMeasures().get(3).history.length);
}

@Test
void getHistoryEmpty() {

final Config config = new Config();
config.setUrl("prefix");

SearchHistory sh = new SearchHistory();
Paging paging = new Paging();
paging.setTotal(0);
sh.setPaging(paging);
Measures[] measuresArray = new Measures[0];
sh.setMeasures(measuresArray);

ResponseEntity<SearchHistory> rsh = new ResponseEntity<>(sh, null, HttpStatus.OK);
HttpHeaders httpHeaders = new HttpHeaders();
HttpEntity<String> hes = new HttpEntity<>(httpHeaders);
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);
AssembledSearchHistory ash = SqgraphApplication.getHistory(config, "blah", "blah", "blah", httpHeaders, restTemplate);

assertEquals(0, ash.getMeasures().size());
}
}

0 comments on commit 8287035

Please sign in to comment.