Skip to content

Write several json items to a single file #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ target/
logs/
.idea/
*.iml
*.iws
*.ipr
/nbproject/
bin/
.settings/
.classpath
.project
.project
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ Now that you know how Steps are executed, let's take a look at how they are defi
| --------------- |----------------| --------------|
| config | array of objects | The json objects to be generated during this step |
| duration | integer | If 0, this step will run once. If -1, this step will run forever. Any of number is the time in milliseconds to run this step for. |
| quantity | integer | Only for file logger and duration set to 0. If > 1 this step will occurs quantity time for each file.|
| producerConfig | map of objects | Optional: producer configuration for this step - optional and specific for each producer. (See producer documentation) |

**Step Config**
Expand Down
14 changes: 14 additions & 0 deletions src/dist/conf/jsonFilesConfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"workflows": [{
"workflowName": "jsonfiles",
"workflowFilename": "jsonFilesWorkflow.json"
}],
"producers": [
{
"type": "file",
"output.directory": "/tmp",
"file.prefix": "data_",
"file.extension": ".json"
}
]
}
18 changes: 18 additions & 0 deletions src/dist/conf/jsonFilesWorkflow.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"eventFrequency": 0,
"varyEventFrequency": false,
"repeatWorkflow": true,
"timeBetweenRepeat": 1,
"varyRepeatFrequency": false,
"steps": [
{
"config": [
{
"id": "uuid()"
}
],
"duration": 0,
"quantity": 10
}
]
}
56 changes: 48 additions & 8 deletions src/main/java/net/acesinc/data/json/generator/EventGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Map;
import java.util.Random;

import net.acesinc.data.json.generator.log.FileLogger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand Down Expand Up @@ -70,10 +71,44 @@ public void runWorkflow() {
}

protected void runSequential() {

Iterator<WorkflowStep> it = workflow.getSteps().iterator();
while (running && it.hasNext()) {
WorkflowStep step = it.next();
executeStep(step);

// Store all events in this ArrayList for future processing by each logger type
ArrayList<String> events = new ArrayList<String>();

// custom "quantity" parameter added to each configuration step to allow for looping
long stepQuantity = step.getQuantity();
long stepQuantityRun = 0;

// All loggers are FileLoggers ?
boolean onlyFileLoggers = true;
for (EventLogger l : eventLoggers)
if (!(l instanceof FileLogger))
onlyFileLoggers = false;

if (!onlyFileLoggers || step.getDuration() != 0)
stepQuantity = 1;

while (stepQuantityRun < stepQuantity) {
executeStep(step, events);
stepQuantityRun++;
}
// Log all events here, instead, by concatenating the Events ArrayList from above
if (step.getDuration() == 0) {
for (EventLogger l : eventLoggers) {
if (l instanceof FileLogger) {
l.logEvent("[" + String.join(",\n", events) + "]"
, step.getProducerConfig());
} else {
l.logEvent(String.join("\n", events)
, step.getProducerConfig());
}
}
}
//executeStep(step);

if (!it.hasNext() && workflow.isRepeatWorkflow()) {
it = workflow.getSteps().iterator();
Expand All @@ -85,8 +120,8 @@ protected void runSequential() {
break;
}
}

}

}

protected void runRandom() {
Expand All @@ -96,7 +131,7 @@ protected void runRandom() {
Iterator<WorkflowStep> it = stepsCopy.iterator();
while (running && it.hasNext()) {
WorkflowStep step = it.next();
executeStep(step);
executeStep(step, null);

if (!it.hasNext() && workflow.isRepeatWorkflow()) {
Collections.shuffle(stepsCopy, new Random(System.currentTimeMillis()));
Expand All @@ -109,14 +144,13 @@ protected void runRandom() {
break;
}
}

}
}

protected void runRandomPickOne() {
while (running) {
WorkflowStep step = workflow.getSteps().get(generateRandomNumber(0, workflow.getSteps().size() - 1));;
executeStep(step);
executeStep(step, null);

if (workflow.isRepeatWorkflow()) {
try {
Expand All @@ -130,16 +164,22 @@ protected void runRandomPickOne() {
}
}

protected void executeStep(WorkflowStep step) {
protected void executeStep(WorkflowStep step, ArrayList<String> events) {
if (step.getDuration() == 0) {
//Just generate this event and move on to the next one
for (Map<String, Object> config : step.getConfig()) {
Map<String, Object> wrapper = new LinkedHashMap<>();
wrapper.put(null, config);
try {
String event = generateEvent(wrapper);
for (EventLogger l : eventLoggers) {
l.logEvent(event, step.getProducerConfig());

// Add the events to the Events Array
if (events!=null) {
events.add(event);
} else {
for (EventLogger l : eventLoggers) {
l.logEvent(event, step.getProducerConfig());
}
}
try {
performEventSleep(workflow);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,25 @@ public class WorkflowStep {
private List<Map<String, Object>> config;
private Map<String, Object> producerConfig;
private long duration;
private long quantity;

public WorkflowStep() {
config = new ArrayList<Map<String, Object>>();
producerConfig = new HashMap<>() ;
}


/**
* @return the quantity
*/
public long getQuantity() { return quantity <= 1 ? 1 : quantity; }

/**
* @param quantity the quantity to set
*/
public void setQuantity(long quantity) {
this.quantity = quantity;
}

/**
* @return the duration
*/
Expand Down Expand Up @@ -52,8 +65,8 @@ public List<Map<String, Object>> getConfig() {
public void setConfig(List<Map<String, Object>> config) {
this.config = config;
}
/**

/**
* @return the producerConfig
*/
public Map<String, Object> getProducerConfig() {
Expand Down
14 changes: 14 additions & 0 deletions src/main/resources/jsonFilesConfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"workflows": [{
"workflowName": "jsonfiles",
"workflowFilename": "jsonFilesWorkflow.json"
}],
"producers": [
{
"type": "file",
"output.directory": "/tmp",
"file.prefix": "data_",
"file.extension": ".json"
}
]
}
18 changes: 18 additions & 0 deletions src/main/resources/jsonFilesWorkflow.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"eventFrequency": 0,
"varyEventFrequency": false,
"repeatWorkflow": true,
"timeBetweenRepeat": 1,
"varyRepeatFrequency": false,
"steps": [
{
"config": [
{
"id": "uuid()"
}
],
"duration": 0,
"quantity": 10
}
]
}