Skip to content

Commit

Permalink
added manager field to user event reporting for line managers to see …
Browse files Browse the repository at this point in the history
…reports
  • Loading branch information
matallen committed Apr 29, 2022
1 parent 2dcc789 commit e9fb1e6
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/main/java/com/redhat/sso/ninja/Heartbeat2.java
Original file line number Diff line number Diff line change
Expand Up @@ -600,8 +600,12 @@ public void publishGraphsData(Database2 db, Config config){

// add the Events to the graphs so they're available externally
try{
Map<String,String> filters=new MapBuilder<String,String>().put("daysOld", "180").put("events","User Promotion,Points Increment").put("asCSV", "true").build();
String events=(String)ec.getEventsV2(filters);
// Map<String,String> filters=;
String events=(String)ec.getEventsV2(new MapBuilder<String,String>()
.put("events","User Promotion,Points Increment")
.put("daysOld", "180")
.put("asCSV", "true")
.build());
if (200!=Http.post(url+"/events180", events).responseCode)
log.error("Error pushing 'events180' info to graphsProxy");
}catch (IOException e){ e.printStackTrace(); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,25 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;

import com.google.common.base.Joiner;
import com.redhat.sso.ninja.Config;
import com.redhat.sso.ninja.Database2;
import com.redhat.sso.ninja.Database2.EVENT_FIELDS;
import com.redhat.sso.ninja.user.CachedUserService;
import com.redhat.sso.ninja.user.UserService;
import com.redhat.sso.ninja.user.UserService.User;
import com.redhat.sso.ninja.utils.FluentCalendar;
import com.redhat.sso.ninja.utils.Json;
import com.redhat.sso.ninja.utils.MapBuilder;
Expand Down Expand Up @@ -224,6 +230,8 @@ private List<Map<String, String>> getFilteredEvents2(Map<String,String> filters)
}

// Enrich the event results without affecting the stored data
UserService userService=new CachedUserService();
boolean ldapDown=false;
for(Map<String, String> v:result){
// 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)){
Expand All @@ -237,7 +245,27 @@ private List<Map<String, String>> getFilteredEvents2(Map<String,String> filters)
v.put(EVENT_FIELDS.TEXT.v, v.get(EVENT_FIELDS.USER.v)+" registered");
}

try{
if (!ldapDown){
String kerberos=v.get("user");
if (StringUtils.isNotBlank(kerberos)){
List<User> ldapResult=userService.search("uid", kerberos);
if (ldapResult.size()>0){
User u=ldapResult.get(0);
v.put("user.manager", u.asMap().get("user.manager"));
}else{
v.put("user.manager", "");
}
}
}
}catch (NamingException e){
e.printStackTrace();
ldapDown=true;
}

}
// userService.printCacheStats();
System.out.println("results.size="+result.size());
return result;
}
}
31 changes: 31 additions & 0 deletions src/main/java/com/redhat/sso/ninja/user/CachedUserService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.redhat.sso.ninja.user;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.naming.NamingException;

public class CachedUserService extends UserService{
private Map<String,List<User>> cache=new HashMap<>();
int cachedCount=0;
int notCachedCount=0;

@Override
public List<User> search(String field, String value) throws NamingException{
boolean cached=true;
if (!cache.containsKey(field+value)){
notCachedCount+=1;
cache.put(field+value, super.search(field, value));
}
if (cached)
cachedCount+=1;//System.out.println("returned cached user: "+cache.get(field+value).get(0).getUid());
return cache.get(field+value);
}

public void printCacheStats(){
System.out.println("cached ["+cachedCount+"], not cached ["+notCachedCount+"]");
cachedCount=0;
notCachedCount=0;
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/redhat/sso/ninja/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import javax.naming.ldap.LdapContext;

import com.redhat.sso.ninja.Config;
import com.redhat.sso.ninja.utils.RegExHelper;

public class UserService {

Expand Down Expand Up @@ -71,6 +72,11 @@ public List<User> search(String field, String value) throws NamingException {
u.userInfo.put("geo", safeToString(attrs.get("rhatGeo")));
u.userInfo.put("hireDate", safeToString(attrs.get("rhatHireDate")));
u.userInfo.put("jobCode", safeToString(attrs.get("rhatJobCode")));
u.userInfo.put("manager", safeToString(attrs.get("manager")));
u.userInfo.put("user.manager", RegExHelper.extract(attrs.get("manager").toString(), "uid=(.+),ou", 1));



result2.add(u);
}
return result2;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.redhat.sso.ninja.controllers;

import org.junit.Test;

import com.redhat.sso.ninja.utils.MapBuilder;

public class EventsControllerTest{

@Test
public void testGetEventsV2_30DaysOld() throws Exception{

long s=System.currentTimeMillis();
new EventsController().getEventsV2(new MapBuilder<String,String>().put("daysOld","30").build());
// System.out.println(
//new ExportController().exportEvents(null, "csv")
// );
System.out.println("took "+(System.currentTimeMillis()-s+"ms"));
}
}

0 comments on commit e9fb1e6

Please sign in to comment.