forked from altran/Valuereporter
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#3 Architecture for Observed Activity.
- Loading branch information
Showing
3 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
src/main/java/org/valuereporter/activity/ActivitiesResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package org.valuereporter.activity; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.slf4j.Logger; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.ws.rs.POST; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.PathParam; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
import java.io.IOException; | ||
import java.io.StringWriter; | ||
import java.io.Writer; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.slf4j.LoggerFactory.getLogger; | ||
|
||
/** | ||
* Created by baardl on 02.03.16. | ||
*/ | ||
@Component | ||
@Path("/activities") | ||
public class ActivitiesResource { | ||
private static final Logger log = getLogger(ActivitiesResource.class); | ||
|
||
private final ObjectMapper mapper; | ||
|
||
@Autowired | ||
public ActivitiesResource(ObjectMapper mapper) { | ||
this.mapper = mapper; | ||
} | ||
|
||
@POST | ||
@Path("/{prefix}") | ||
@Produces(MediaType.APPLICATION_JSON) | ||
public Response addObservationActivity(@PathParam("prefix") String prefix, String jsonBody){ | ||
log.trace("addObservationMethod prefix {} , jsonBody {}.", prefix, jsonBody); | ||
List<ObservedActivity> observedActivities = null; | ||
try { | ||
observedActivities = mapper.readValue(jsonBody, new TypeReference<ArrayList<ObservedActivityJson>>(){ }); | ||
if (observedActivities != null) { | ||
for (ObservedActivity observedActivity : observedActivities) { | ||
observedActivity.setPrefix(prefix); | ||
} | ||
} | ||
} catch (IOException e) { | ||
log.warn("Unexpected error trying to produce list of ObservedActivity from \n prefix {} \n json {}, \n Reason {}",prefix, jsonBody, e.getMessage()); | ||
return Response.status(Response.Status.NOT_ACCEPTABLE).entity("Error converting to requested format.").build(); | ||
} | ||
|
||
long updatedCount = observedActivities.size(); //writeOperations.addObservations(prefix,observedMethods); | ||
String message = "added " + updatedCount + " observedMethods."; | ||
Writer strWriter = new StringWriter(); | ||
try { | ||
mapper.writeValue(strWriter, message); | ||
} catch (IOException e) { | ||
log.error("Could not convert {} to JSON.", updatedCount, e); | ||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Error converting to requested format.").build(); | ||
} | ||
return Response.ok(strWriter.toString()).build(); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/org/valuereporter/activity/ObservedActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package org.valuereporter.activity; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* This class will represent an activity. This activity will have some main characteristics: | ||
* - name: Unique identifier for a type of activity | ||
* - startTime: When the activity occured | ||
* - data: map of parameters, aka payload | ||
* | ||
* These methods will be forwarded to the Valuereporter analyzer, as an activity that has occured. | ||
* | ||
* @author <a href="[email protected]">Bard Lind</a> | ||
*/ | ||
public class ObservedActivity { | ||
//The name is the identification of a method. Typically the name is the full name, including class, and package. | ||
private String prefix = ""; | ||
|
||
public String getPrefix() { | ||
return prefix; | ||
} | ||
|
||
public void setPrefix(String prefix) { | ||
this.prefix = prefix; | ||
} | ||
|
||
private final String name; | ||
private final long startTime; | ||
private final Map<String,Object> data; | ||
|
||
public ObservedActivity(String name, long startTime, Map<String,Object> data) { | ||
this.name = name; | ||
this.startTime = startTime; | ||
this.data = data; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public long getStartTime() { | ||
return startTime; | ||
} | ||
|
||
|
||
public Map<String, Object> getData() { | ||
return data; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ObservedActivity{" + | ||
"name='" + name + '\'' + | ||
", startTime=" + startTime + | ||
", data=" + data + | ||
'}'; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/org/valuereporter/activity/ObservedActivityJson.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.valuereporter.activity; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* @author <a href="[email protected]">Bard Lind</a> | ||
*/ | ||
public class ObservedActivityJson extends ObservedActivity { | ||
|
||
@JsonCreator | ||
public ObservedActivityJson(@JsonProperty("name") String name, @JsonProperty("startTime") long startTime, @JsonProperty("data") Map<String,Object> data) { | ||
super(name, startTime, data); | ||
} | ||
} |