Skip to content

Commit

Permalink
Cleanup (#66)
Browse files Browse the repository at this point in the history
* minor correction to event log text (it was logging the card long ID twice in some cases)

* cleaning up unused code & commenting methods and where they're used ready for refactoring
  • Loading branch information
matallen authored Apr 30, 2020
1 parent b3cdd5c commit 2609dd0
Show file tree
Hide file tree
Showing 42 changed files with 298 additions and 1,908 deletions.
70 changes: 0 additions & 70 deletions src/main/java/com/redhat/sso/ninja/Backup.java

This file was deleted.

22 changes: 13 additions & 9 deletions src/main/java/com/redhat/sso/ninja/ChartsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.gdata.util.common.base.Pair;
import com.redhat.sso.ninja.chart.Chart2Json;
import com.redhat.sso.ninja.chart.DataSet2;
import com.redhat.sso.ninja.chart.ChartJson;
import com.redhat.sso.ninja.chart.DataSet;
import com.redhat.sso.ninja.utils.Json;
import com.redhat.sso.ninja.utils.LevelsUtil;
import com.redhat.sso.ninja.utils.MapBuilder;

@Path("/")
public class ChartsController{


// Mojo UI: https://mojo.redhat.com/community/communities-at-red-hat/communities-of-practice-operations/giveback-ninja-program/ninja-wall/overview
@GET
@Path("/ninjas")
public Response getNinjas() throws JsonGenerationException, JsonMappingException, IOException{
Expand All @@ -45,6 +47,7 @@ public Response getNinjas() throws JsonGenerationException, JsonMappingException
.build();
}

// Mojo UI: "race to black belt" here: https://mojo.redhat.com/community/communities-at-red-hat/communities-of-practice-operations/giveback-ninja-program
@GET
@Path("/leaderboard/{max}")
public Response getLeaderboard2(@PathParam("max") Integer max) throws JsonGenerationException, JsonMappingException, IOException{
Expand All @@ -64,7 +67,7 @@ static Integer total(Map<String,Integer> points){
}
return t;
}
public Chart2Json getParticipants(Integer max) throws JsonGenerationException, JsonMappingException, IOException{
public ChartJson getParticipants(Integer max) throws JsonGenerationException, JsonMappingException, IOException{
Database2 db=Database2.get();
Map<String, Map<String, Integer>> leaderboard=db.getLeaderboard();
Map<String, Integer> totals=new HashMap<String, Integer>();
Expand All @@ -89,8 +92,8 @@ public Chart2Json getParticipants(Integer max) throws JsonGenerationException, J
sortedTotals.put(e.getKey(), e.getValue());

// Build Chart data structure
Chart2Json c=new Chart2Json();
c.setDatasets(new ArrayList<DataSet2>());
ChartJson c=new ChartJson();
c.setDatasets(new ArrayList<DataSet>());
int count=0;
for(Entry<String, Integer> e:sortedTotals.entrySet()){
Map<String, String> userInfo=db.getUsers().get(e.getKey());
Expand All @@ -115,7 +118,7 @@ public Chart2Json getParticipants(Integer max) throws JsonGenerationException, J
}
c.getCustom2().add(Joiner.on(",").join(pastYearBadges));

if (c.getDatasets().size()<=0) c.getDatasets().add(new DataSet2());
if (c.getDatasets().size()<=0) c.getDatasets().add(new DataSet());
c.getDatasets().get(0).getData().add(e.getValue());
c.getDatasets().get(0).setBorderWidth(1);

Expand All @@ -132,24 +135,25 @@ public Chart2Json getParticipants(Integer max) throws JsonGenerationException, J
c.getDatasets().get(0).getBorderColor().add(colors.get(userInfo.get("level").toUpperCase()).getSecond());

count=count+1;
if (null!=max && count>=max) break; // hard maximum suppled as param
if (null!=max && count>=max) break; // hard maximum supplied as param
}

return c;
}

// UI call (user dashboard) - returns the payload to render a chart displaying the current points and points to the next level
// here: https://mojo.redhat.com/community/communities-at-red-hat/communities-of-practice-operations/giveback-ninja-program/dashboard/overview
@GET
@Path("/scorecard/nextlevel/{user}")
public Response getUserNextLevel(@PathParam("user") String user) throws JsonGenerationException, JsonMappingException, IOException{

Database2 db=Database2.get();
boolean userExists=db.getScoreCards().containsKey(user);

Chart2Json chart=new Chart2Json();
ChartJson chart=new ChartJson();
chart.getLabels().add("Earned");
chart.getLabels().add("To Next Level");
chart.getDatasets().add(new DataSet2());
chart.getDatasets().add(new DataSet());
chart.getDatasets().get(0).setBorderWidth(1);

if (userExists){
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/com/redhat/sso/ninja/ChatNotification.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
import com.redhat.sso.ninja.utils.Http.Response;
import com.redhat.sso.ninja.utils.MapBuilder;

/**
* Integration with google chat boards, to push notifications of events such as user promotions, script failures etc..
* @author mallen
*/
public class ChatNotification{
public enum ChatEvent{onRegistration,onBeltPromotion,onScriptError}
public static void main(String[] asd){
Config c=Config.get();
System.out.println("googlehangoutschat.webhook.template="+c.getOptions().get("googlehangoutschat.webhook.template"));

new ChatNotification().send(ChatEvent.onRegistration, "<https://your.site.com/people/fbloggs|Fred Bloggs> promoted to BLUE belt");
}
// public static void main(String[] asd){
// Config c=Config.get();
// System.out.println("googlehangoutschat.webhook.template="+c.getOptions().get("googlehangoutschat.webhook.template"));
// new ChatNotification().send(ChatEvent.onRegistration, "<https://your.site.com/people/fbloggs|Fred Bloggs> promoted to BLUE belt");
// }

public void send(ChatEvent type, String notificationText){
Config c=Config.get();
Expand Down
44 changes: 3 additions & 41 deletions src/main/java/com/redhat/sso/ninja/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,6 @@ public Config(String json){
}
}

// Config options to be able to configure
// - cycle/zero points every X weeks (or would you want a rolling total?)
// - multiple pools per user
// - each pool much have a configurable way of pulling the info (groovy?)
// - each pool points must have a configurable way of calculating the multiple pool values into a consolidated perception of score
// - Heartbeat to pull data from last time it was run - must be persistent and survive server restarts




public Map<String,String> getOptions() {if (options==null) options=new HashMap<String, String>(); return options;}
public List<Map<String,Object>> getScripts() {if (scripts==null) scripts=new ArrayList<Map<String, Object>>(); return scripts;}
Expand Down Expand Up @@ -106,37 +97,10 @@ public static Config get(){
if (!Config.STORAGE.getParentFile().exists()) Config.STORAGE.getParentFile().mkdirs();
// copy the default config over
IOUtils.copy(Config.class.getClassLoader().getResourceAsStream(STORAGE.getName()), new FileOutputStream(STORAGE));

}
// instance=new Config();
// }else{
log.info("Config loading (location="+Config.STORAGE.getAbsolutePath()+", size="+Config.STORAGE.length()+")");
String toLoad=IOUtils2.toStringAndClose(new FileInputStream(Config.STORAGE));
instance=Json.newObjectMapper(true).readValue(new ByteArrayInputStream(toLoad.getBytes()), Config.class);
// }
// UserController uc=new UserController();
// GoogleAddressResolution gar=new CachedGoogleAddressResolution(false);
// boolean changed=false;
// for(Architect a:instance.getArchitects().values()){
// if (a.getHome()==null || a.getHome().length()<=0){
// changed=true;
// List<User> userList=uc.search("uid", a.getUid());
// if (userList.size()!=1) continue; // uncertain? dont do anything
// User user=userList.get(0);
// a.setName(user.getName());
// String country=instance.countryCodeToName.get(user.getCountry());
// if (country==null){
// System.err.println("Unknown country code ["+user.getCountry()+"]");
// continue;
// }
// Map<String, String> formattedAddress=gar.getFormattedAddress(country);
// a.setHome(formattedAddress.get("longitude")+","+formattedAddress.get("latitude"));
// }
// }
// if (changed){
// String str=Json.newObjectMapper(false).writeValueAsString(instance);
// IOUtils2.writeAndClose(str.getBytes(), new FileOutputStream(new File("config2.json")));
// }
log.info("Config loading (location="+Config.STORAGE.getAbsolutePath()+", size="+Config.STORAGE.length()+")");
String toLoad=IOUtils2.toStringAndClose(new FileInputStream(Config.STORAGE));
instance=Json.newObjectMapper(true).readValue(new ByteArrayInputStream(toLoad.getBytes()), Config.class);

}catch(Exception e){
e.printStackTrace();
Expand All @@ -152,8 +116,6 @@ public void setOptions(Map<String,String> value) {

@JsonIgnore
public String getNextTaskNum(){
// Config cfg=Config.get();

if (!getValues().containsKey("lastTaskNum")){
getValues().put("lastTaskNum", 0);
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/redhat/sso/ninja/Database2.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public static String buildLink(Map<String,String> params){
return "([Trello card: "+params.get("linkId")+"|https://trello.com/c/"+params.get("linkId")+"] / "+params.get("id")+")";
}else if (params.get("id").startsWith("GH")){
if (params.get("pool").toLowerCase().contains("pull")){
return "([Github card: "+params.get("linkId")+"|https://github.com/"+params.get("org")+"/"+params.get("board")+"/pull/"+params.get("linkId")+"])";
return "([Github card: "+params.get("linkId")+"|https://github.com/"+params.get("org")+"/"+params.get("board")+"/pull/"+params.get("linkId")+"] / "+params.get("id")+")";
}else{ // assume "issues"
return "([Github card: "+params.get("linkId")+"|https://github.com/"+params.get("org")+"/"+params.get("board")+"/issues/"+params.get("linkId")+"])";
return "([Github card: "+params.get("linkId")+"|https://github.com/"+params.get("org")+"/"+params.get("board")+"/issues/"+params.get("linkId")+"] / "+params.get("id")+")";
}
//}else if (params.get("id").startsWith("GL")){
// return "([Gitlab card: "+params.get("linkId")+"|"+params.get("linkId")+"])";
Expand Down Expand Up @@ -91,10 +91,10 @@ public Database2 increment(String poolId, String userId, Integer increment, Map<
scorecards.get(userId).put(poolId, scorecards.get(userId).get(poolId)+increment);

if (params!=null && params.size()>1){ //because "id" is always added
addEvent("Points Increment", userId, increment+" point"+(increment<=1?"":"s")+" added to "+poolId+"("+params.get("id")+") "+buildLink(params));
addEvent("Points Increment", userId, increment+" point"+(increment<=1?"":"s")+" added to "+poolId+" "+buildLink(params));
}else{
// no params & therefore no link
addEvent("Points Increment", userId, increment+" point"+(increment<=1?"":"s")+" added to "+poolId+"("+params.get("id")+")");
addEvent("Points Increment", userId, increment+" point"+(increment<=1?"":"s")+" added to "+poolId+"");
}

}else{
Expand Down
21 changes: 13 additions & 8 deletions src/main/java/com/redhat/sso/ninja/ExportController.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,23 @@
import com.redhat.sso.ninja.utils.Json;
import com.redhat.sso.ninja.utils.MapBuilder;

/**
* Rest controller that exposes methods to export data from the system (events, scorecards etc..) in various formats (csv, json etc...)
* @author mallen
*/

@Path("/")
public class ExportController{

enum Format{
csv,json,xls
};

public static void main(String[] asd) throws IOException{
System.out.println(
new ExportController().exportScorecards(null, "xls").getEntity()
// new ExportController().exportEvents(null, "csv").getEntity()
);
}
// public static void main(String[] asd) throws IOException{
// System.out.println(
// new ExportController().exportScorecards(null, "xls").getEntity()
//// new ExportController().exportEvents(null, "csv").getEntity()
// );
// }

/* Comparator to order the header and fields of the exported data */
class HeaderComparator implements Comparator<String>{
Expand All @@ -69,10 +73,10 @@ public int getOrder(String hdr){
}


// Admin UI: Used to export the list of events for support purposes
@GET
@Path("/events/export/{format}")
public Response exportEvents(@Context HttpServletRequest request, @PathParam("format") String format) throws IOException{

List<Map<String, String>> data=new ManagementController().getAllEvents();

Set<String> headerset=new HashSet<String>();
Expand All @@ -91,6 +95,7 @@ public Response exportEvents(@Context HttpServletRequest request, @PathParam("fo
}


// Admin UI: Used to export the list of user/scorecards support or reporting purposes
@GET
@Path("/scorecards/export/{format}")
public Response exportScorecards(@Context HttpServletRequest request, @PathParam("format") String format) throws IOException{
Expand Down
Loading

0 comments on commit 2609dd0

Please sign in to comment.