Skip to content

Commit

Permalink
support for smartsheets points integration, cleanup of old point allo…
Browse files Browse the repository at this point in the history
…cation methods hanging around, and logging the script execution again (#138)
  • Loading branch information
matallen authored Apr 14, 2021
1 parent 3a5c952 commit 47f9758
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 131 deletions.
19 changes: 2 additions & 17 deletions src/main/java/com/redhat/sso/ninja/Database2.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,8 @@ public static String buildLinkMarkdown(Map<String,String> params){
}else{ // assume "issues"
return "<https://github.com/"+params.get("org")+"/"+params.get("board")+"/issues/"+params.get("linkId")+">";
}
}
return "";
}

// my custom UI format
public static String buildLink(Map<String,String> params){
if (!params.containsKey("linkId")) return "";
if (params.get("id").startsWith("TR")){
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")+"] / "+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")+"] / "+params.get("id")+")";
}
//}else if (params.get("id").startsWith("GL")){
// return "([Gitlab card: "+params.get("linkId")+"|"+params.get("linkId")+"])";
}else if (params.get("id").startsWith("SS")){ // smartsheets
return "[Smartsheets: "+params.get("linkId")+"](https://app.smartsheet.com/sheets/"+params.get("board")+")";
}
return "";
}
Expand Down
80 changes: 1 addition & 79 deletions src/main/java/com/redhat/sso/ninja/Heartbeat2.java
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ public void run() {
if (0==process.exitValue()){
for(String line:process.lines())
allocate.allocatePoints2(line);
db.addEvent("Script Execution Succeeded", "", command +" (took "+(System.currentTimeMillis()-start)+"ms)");
}else{ // error
db.addEvent("Script Execution FAILED", "", command+"\nERROR (stderr):\n"+ Joiner.on("\n").join(process.lines()));
new ChatNotification().send(ChatEvent.onScriptError, name+" script failure occurred. Please investigate");
Expand Down Expand Up @@ -651,85 +652,6 @@ public void levelUpChecks(Database2 db){
}


public void allocatePoints(Database2 db, InputStream is, Map<String,Object> script, File scriptFolder, Map<String, String> poolToUserIdMapper) throws NumberFormatException, UnsupportedEncodingException, IOException{
BufferedReader stdInput=new BufferedReader(new InputStreamReader(is));
Pattern paramsPattern=Pattern.compile(".*(\\[.*\\]).*");

StringBuffer scriptLog=new StringBuffer();
String s;
while ((s=stdInput.readLine()) != null){
s=s.trim();
scriptLog.append(s).append("\n");
log.debug(s);

Map<String, String> params=new HashMap<String, String>();
// check for params here, extract them if present for use later on
if (s.matches(".* \\[.*\\]")){
Matcher m=paramsPattern.matcher(s);
if (m.find()){
String paramsExtract=m.group(1);
params.putAll(new ParamParser().splitParams(paramsExtract.replaceAll("\\[", "").replaceAll("\\]", "").trim()));
s=s.replaceAll("\\[.*\\]", "").trim();
}
}

if (s.startsWith("#")){ // Informational lines only, some may need to be added to event logging as reasons points were not awarded



}else if (s.contains("/")){ // ignore the line if it doesn't contain a slash
String[] split=s.split("/");

// take the last section of the script name as the pool id. so "trello" stays as "trello", but "trello.thoughtleadership" becomes "thoughtleadership" where the "trello" part is the source type/context
String pool=(String)script.get("name");
String[] splitPool=pool.split("\\.");
pool=splitPool[splitPool.length-1];

String actionId;
String poolUserId;
Integer inc;

if (split.length==4){ //pool.sub
pool=pool+"."+split[0];
actionId=split[1];
poolUserId=split[2];
inc=Integer.valueOf(split[3]);

params.put("id", actionId);
params.put("pool", pool);

if (!db.getPointsDuplicateChecker().contains(actionId+"."+poolUserId)){
db.getPointsDuplicateChecker().add(actionId+"."+poolUserId);

String userId=poolToUserIdMapper.get(poolUserId);

if (null!=userId){
// System.out.println(poolUserId+" mapped to "+userId);
// log.info("Incrementing registered user "+poolUserId+" by "+inc);
db.increment(pool, userId, inc, params);//.save();
}else{
log.info("Unable to find '"+poolUserId+"' "+script.get("name")+" user - not registered? "+Database2.buildLink(params));
db.addEvent("Lost Points", poolUserId +"("+script.get("name")+")", script.get("name")+" user '"+poolUserId+"' was not found - not registered? "+Database2.buildLink(params));
}
}else{
// it's a duplicate increment for that actionId & user, so ignore it
log.warn(actionId+"."+poolUserId+" is a duplicate");
}

}else{
// dont increment because we dont know the structure of the script data
}


}
}

scriptFolder.mkdirs();
IOUtils.write(scriptLog.toString(), new FileOutputStream(new File(scriptFolder, "last.log")));


}

}


Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/redhat/sso/ninja/PointsAllocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public void allocatePoints2(String line) throws UnsupportedEncodingException{

// take the last section of the script name as the pool id. so "trello" stays as "trello", but "trello.thoughtleadership" becomes "thoughtleadership" where the "trello" part is the source type/context
String pool=(String)scriptName;
if (params.containsKey("pool")){
log.debug("updating pool name from params from ["+pool+"] to ["+params.get("pool")+"] ");
pool=params.get("pool");
}
String[] splitPool=pool.split("\\.");
pool=splitPool[splitPool.length-1];

Expand All @@ -78,8 +82,8 @@ public void allocatePoints2(String line) throws UnsupportedEncodingException{
if (null!=userId){
db.increment(pool, userId, inc, params);//.save();
}else{
log.info("Unable to find '"+poolUserId+"' "+scriptName+" user - not registered? "+Database2.buildLink(params));
db.addEvent("Lost Points", poolUserId +"("+scriptName+")", scriptName+" user '"+poolUserId+"' was not found - not registered? "+Database2.buildLink(params));
log.info("Unable to find '"+poolUserId+"' "+scriptName+" user - not registered? "+Database2.buildLinkMarkdown(params));
db.addEvent("Lost Points", poolUserId +"("+scriptName+")", scriptName+" user '"+poolUserId+"' was not found - not registered? "+Database2.buildLinkMarkdown(params));
}
}else{
// it's a duplicate increment for that actionId & user, so ignore it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,23 @@ public Response getEventsV1(@Context HttpServletRequest request) throws JsonGene
return NewResponse.status(200).entity(Json.newObjectMapper(true).writeValueAsString(getFilteredEvents1(filters))).build();
}
private List<Map<String, String>> getFilteredEvents1(Map<String,String> filters) throws JsonGenerationException, JsonMappingException, IOException{
// System.out.println("getFilteredEvents1");
Database2 db=Database2.get();
List<Map<String, String>> result=new ArrayList<Map<String,String>>();

// bug fixing, the "text" field sometimes got written back to the database events, so here we're going to strip it back out
int changed=0;
for(Map<String, String> e:db.getEvents()){
if ((e.get("type").equals("Points Increment") || e.get("type").equals("New User")) && e.containsKey("text")) e.remove("text");
changed+=1;
}
if (changed>0){
log.info("updated "+changed+" events to strip out unnecessary 'text' fields");
db.save();
}

if (filters.size()<=0){
result.addAll(db.getEvents());
for (Map<String, String> e:db.getEvents())
result.add(new HashMap<>(e));
}else{
for(Map<String, String> e:db.getEvents()){
HashMap<String,String> v=new HashMap<>(e);
Expand All @@ -138,7 +150,7 @@ private List<Map<String, String>> getFilteredEvents1(Map<String,String> filters)
// add a generated "text" field if none exists - this is because on the events UI there is not enough space for all the separated fields
if ("Points Increment".equals(v.get(EVENT_FIELDS.TYPE.v)) && !v.containsKey(EVENT_FIELDS.TEXT.v)){
Integer points=Integer.parseInt(v.get(EVENT_FIELDS.POINTS.v));
v.put(EVENT_FIELDS.TEXT.v, points+" point"+(points<=1?"":"s")+" added to "+v.get(EVENT_FIELDS.POOL.v)+" "+convertSourceToShowdown(v.get(EVENT_FIELDS.SOURCE.v)));
v.put(EVENT_FIELDS.TEXT.v, points+" point"+(points<=1?"":"s")+" added to "+v.get(EVENT_FIELDS.POOL.v)+" "+v.get(EVENT_FIELDS.SOURCE.v));
}

if ("New User".equals(v.get(EVENT_FIELDS.TYPE.v))){
Expand All @@ -150,16 +162,6 @@ private List<Map<String, String>> getFilteredEvents1(Map<String,String> filters)
return result;
}

// convert link format to showdown markdown?
private String convertSourceToShowdown(String source){
Pattern p=Pattern.compile(".+\\((.+)\\)");
Matcher m=p.matcher(source);
if (m.find()){
String link=m.group(1);
return "<"+link+">";
}
return source;
}

// // Admin/Support UI call to display all events, user events or specific types of events
// @GET
Expand Down Expand Up @@ -228,7 +230,7 @@ private List<Map<String, String>> getFilteredEvents2(Map<String,String> filters)
// System.out.println("adding a text field");
// if (!v.containsKey(EVENT_FIELDS.TEXT.v)){// && v.containsKey(EVENT_FIELDS.POINTS.v)){
Integer points=Integer.parseInt(v.get(EVENT_FIELDS.POINTS.v));
v.put(EVENT_FIELDS.TEXT.v, points+" point"+(points<=1?"":"s")+" added to "+v.get(EVENT_FIELDS.POOL.v)+" "+convertSourceToShowdown(v.get(EVENT_FIELDS.SOURCE.v)));
v.put(EVENT_FIELDS.TEXT.v, points+" point"+(points<=1?"":"s")+" added to "+v.get(EVENT_FIELDS.POOL.v)+" "+v.get(EVENT_FIELDS.SOURCE.v));
}

if ("New User".equals(v.get(EVENT_FIELDS.TYPE.v))){
Expand Down
25 changes: 6 additions & 19 deletions src/main/webapp/events.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,14 @@ function loadDataTable(){
var markDownConverter = new showdown.Converter();
var str = markDownConverter.makeHtml(result);
//remove root paragraphs <p></p>
str = str.substring(3);
str = str.substring(0, str.length - 4);
result = str;
if (str!=undefined){
if (str==undefined || str.length<=3) console.log("error: str="+result)
str = str.substring(3);
str = str.substring(0, str.length - 4);
result = str;
}
}
if (/.*\[.+\].*/.test(row['text'])){ // look for a set of square brackets
var before=/.*\[(.+)\].*/.exec(row['text']);
var split=before[1].split("|");
var title=split[0];
var link=split[1];
link="<a href='"+link+"'>"+title+"</a>";
var find1=escapeRegExp(("["+before[1]+"]").replace(/\[/g,'\\[').replace(/\]/g,'\\]').replace(/\|/g,'\\|'));
var find2=("["+before[1]+"]").replace(/\[/g,'\\[').replace(/\]/g,'\\]').replace(/\|/g,'\\|');
var after=row['text'].replace(new RegExp(find2, 'g'), link);
result=after;
}
return result;
}},
{ "targets": 3, "orderable": true, "render": function (data,type,row){
Expand Down

0 comments on commit 47f9758

Please sign in to comment.