Skip to content

Commit

Permalink
working on #229, working on #228
Browse files Browse the repository at this point in the history
  • Loading branch information
phasenraum2010 committed Aug 6, 2017
1 parent f601e61 commit e6f482a
Show file tree
Hide file tree
Showing 16 changed files with 240 additions and 4 deletions.
12 changes: 12 additions & 0 deletions src/main/java/org/woehlke/twitterwall/ScheduledTasks.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ public void fetchUsersFromDefinedUserList(){
}
}


@Scheduled(fixedRate = FIXED_RATE_FOR_SCHEDULAR_REMOVE_OLD_DATA_FROM_STORAGE)
public void removeOldDataFromStorage(){
String msg = "remove Old Data From Storage: ";
if(schedulerProperties.getRemoveOldDataFromStorageAllow() && !schedulerProperties.getSkipFortesting()) {
Task task = asyncStartTask.removeOldDataFromStorage();
log.info(msg+ "SCHEDULED: task "+task.getUniqueId());
}
}

@Autowired
public ScheduledTasks(SchedulerProperties schedulerProperties, AsyncStartTask mqAsyncStartTask) {
this.schedulerProperties = schedulerProperties;
Expand All @@ -82,6 +92,8 @@ public ScheduledTasks(SchedulerProperties schedulerProperties, AsyncStartTask mq

private final static long FIXED_RATE_FOR_SCHEDULAR_FETCH_USER_LIST = ZWOELF_STUNDEN;

private final static long FIXED_RATE_FOR_SCHEDULAR_REMOVE_OLD_DATA_FROM_STORAGE = EINE_STUNDE;

private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);

private final SchedulerProperties schedulerProperties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public class SchedulerProperties {
@NotNull
private String fetchUserListName;

@NotNull
private Boolean removeOldDataFromStorageAllow;

public Boolean getAllowFetchTweetsFromTwitterSearch() {
return allowFetchTweetsFromTwitterSearch;
}
Expand Down Expand Up @@ -99,4 +102,12 @@ public String getFetchUserListName() {
public void setFetchUserListName(String fetchUserListName) {
this.fetchUserListName = fetchUserListName;
}

public Boolean getRemoveOldDataFromStorageAllow() {
return removeOldDataFromStorageAllow;
}

public void setRemoveOldDataFromStorageAllow(Boolean removeOldDataFromStorageAllow) {
this.removeOldDataFromStorageAllow = removeOldDataFromStorageAllow;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ public enum TaskType {
CONTROLLER_CREATE_IMPRINT_USER,
CONTROLLER_CREATE_TESTDATA_TWEETS,
CONTROLLER_CREATE_TESTDATA_USERS,
CONTROLLER_ADD_USER_FOR_SCREEN_NAME
CONTROLLER_ADD_USER_FOR_SCREEN_NAME,
REMOVE_OLD_DATA_FROM_STORAGE
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ public interface AsyncStartTask {
Task createTestDataForTweets();

Task createTestDataForUser();

Task removeOldDataFromStorage();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.woehlke.twitterwall.scheduled.mq.endpoint;

import org.springframework.messaging.Message;
import org.woehlke.twitterwall.scheduled.mq.msg.TaskMessage;
import org.woehlke.twitterwall.scheduled.mq.msg.TweetMessage;

import java.util.List;

public interface FindTweetsToRemoveSplitter {

List<Message<TweetMessage>> splitMessage(Message<TaskMessage> message);
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ public Task createTestDataForUser() {
return send(taskType);
}

@Override
public Task removeOldDataFromStorage() {
TaskType taskType = TaskType.REMOVE_OLD_DATA_FROM_STORAGE;
return send(taskType);
}

private Task send(TaskType taskType){
SendType sendType = SendType.FIRE_AND_FORGET;
String msg = "START Task "+taskType+" via MQ by "+sendType;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.woehlke.twitterwall.scheduled.mq.endpoint.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;
import org.woehlke.twitterwall.oodm.entities.Task;
import org.woehlke.twitterwall.oodm.entities.Tweet;
import org.woehlke.twitterwall.oodm.entities.parts.CountedEntities;
import org.woehlke.twitterwall.oodm.service.CountedEntitiesService;
import org.woehlke.twitterwall.oodm.service.TaskService;
import org.woehlke.twitterwall.oodm.service.TweetService;
import org.woehlke.twitterwall.scheduled.mq.endpoint.FindTweetsToRemoveSplitter;
import org.woehlke.twitterwall.scheduled.mq.msg.TaskMessage;
import org.woehlke.twitterwall.scheduled.mq.msg.TweetMessage;

import java.util.ArrayList;
import java.util.List;

@Component("mqFindTweetsToRemoveSplitter")
public class FindTweetsToRemoveSplitterImpl implements FindTweetsToRemoveSplitter {

private final TweetService tweetService;

private final TaskService taskService;

private final CountedEntitiesService countedEntitiesService;

@Autowired
public FindTweetsToRemoveSplitterImpl(TweetService tweetService, TaskService taskService, CountedEntitiesService countedEntitiesService) {
this.tweetService = tweetService;
this.taskService = taskService;
this.countedEntitiesService = countedEntitiesService;
}

@Override
public List<Message<TweetMessage>> splitMessage(Message<TaskMessage> message) {
CountedEntities countedEntities = countedEntitiesService.countAll();
List<Message<TweetMessage>> tweets = new ArrayList<>();
TaskMessage msgIn = message.getPayload();
long id = msgIn.getTaskId();
Task task = taskService.findById(id);
task = taskService.start(task,countedEntities);
int pageTweet = 1;
int pageSize = 20;
Pageable pageRequestTweet = new PageRequest(pageTweet, pageSize);
//TODO: #229 https://github.com/phasenraum2010/twitterwall2/issues/229
Page<Tweet> tweetList = tweetService.getAll(pageRequestTweet);
int loopId = 0;
int loopAll = tweetList.getContent().size();
for (Tweet tweet: tweetList) {
loopId++;
TweetMessage tweetMsg = new TweetMessage(msgIn,tweet);
Message<TweetMessage> mqMessageOut =
MessageBuilder.withPayload(tweetMsg)
.copyHeaders(message.getHeaders())
.setHeader("tw_lfd_nr",loopId)
.setHeader("tw_all",loopAll)
.build();
tweets.add(mqMessageOut);
}
return tweets;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ public Task createTestDataForUser() {
return sendAndReceiveUser(taskType);
}

@Override
public Task removeOldDataFromStorage() {
TaskType taskType = TaskType.REMOVE_OLD_DATA_FROM_STORAGE;
return sendAndReceiveTweet(taskType);
}

@Override
public User createImprintUser() {
TaskType taskType = TaskType.CONTROLLER_CREATE_IMPRINT_USER;
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ twitterwall:
fetchUserListAllow: ${TWITTERWALL_SCHEDULER_USER_LIST_ALLOW}
fetchUserListName: ${TWITTERWALL_SCHEDULER_USER_LIST_NAME}
herokuDbRowsLimit: ${TWITTERWALL_SCHEDULER_HEROKU_DB_LIMIT}
removeOldDataFromStorageAllow: ${TWITTERWALL_SCHEDULER_ALLOW_REMOVE_OLD_DATA_FROM_STORAGE}
skipFortesting: false
testdata:
oodm:
Expand Down
45 changes: 45 additions & 0 deletions src/main/resources/integration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@
</int:interceptors>
</int:channel>

<int:channel id="removeOldDataFromStorageChannel" datatype="org.woehlke.twitterwall.scheduled.mq.msg.TaskMessage">
<int:interceptors>
<int:wire-tap channel="logger"/>
</int:interceptors>
</int:channel>

<int:router id="startTaskRouter" input-channel="startTaskChannel" expression="payload.taskType">
<int:mapping value="UPDATE_TWEETS" channel="updateTweetsChannel"/>
Expand All @@ -89,6 +94,7 @@
<int:mapping value="CONTROLLER_CREATE_TESTDATA_USERS" channel="createTestDataUsersChannel"/>
<int:mapping value="CONTROLLER_CREATE_TESTDATA_TWEETS" channel="createTestDataTweetsChannel"/>
<int:mapping value="CONTROLLER_CREATE_IMPRINT_USER" channel="createImprintUserChannel"/>
<int:mapping value="REMOVE_OLD_DATA_FROM_STORAGE" channel="removeOldDataFromStorageChannel"/>
</int:router>

<int:chain id="updateTweetsChain" input-channel="updateTweetsChannel">
Expand Down Expand Up @@ -272,6 +278,23 @@
method="persistUser" />
</int:chain>

<int:chain id="removeOldDataFromStorageChain" input-channel="removeOldDataFromStorageChannel">
<int:splitter
id="removeOldDataFromStorageSplitter"
ref="mqFindTweetsToRemoveSplitter"
method="splitMessage" />
<int:aggregator
id="removeOldDataFromStorageAggregator"
message-store="store"
release-strategy="releaserSimpleSequenceSizeReleaseStrategy" />
<int:service-activator
id="removeOldDataFromStorageFinisher"
ref="mqTweetFinisher"
method="finish" />
</int:chain>



<int:channel id="startTaskFireAndForgetChannel" datatype="org.woehlke.twitterwall.scheduled.mq.msg.TaskMessage">
<int:dispatcher task-executor="myThreadPoolTaskExecutor"/>
</int:channel>
Expand All @@ -288,6 +311,7 @@
<int:mapping value="FETCH_USERS_FROM_LIST" channel="fetchUsersFromListFireAndForgetChannel"/>
<int:mapping value="CONTROLLER_CREATE_TESTDATA_USERS" channel="createTestDataUsersFireAndForgetChannel"/>
<int:mapping value="CONTROLLER_CREATE_TESTDATA_TWEETS" channel="createTestDataTweetsFireAndForgetChannel"/>
<int:mapping value="REMOVE_OLD_DATA_FROM_STORAGE" channel="removeOldDataFromStorageFireAndForgetChannel"/>
</int:router>

<int:channel id="updateTweetsFireAndForgetChannel" datatype="org.woehlke.twitterwall.scheduled.mq.msg.TaskMessage">
Expand Down Expand Up @@ -332,6 +356,12 @@
</int:interceptors>
</int:channel>

<int:channel id="removeOldDataFromStorageFireAndForgetChannel" datatype="org.woehlke.twitterwall.scheduled.mq.msg.TaskMessage">
<int:interceptors>
<int:wire-tap channel="logger"/>
</int:interceptors>
</int:channel>


<int:chain id="updateTweetsFireAndForgetChain" input-channel="updateTweetsFireAndForgetChannel">
<int:splitter
Expand Down Expand Up @@ -500,4 +530,19 @@
method="finishAsnyc" />
</int:chain>

<int:chain id="removeOldDataFromStorageFireAndForgetChain" input-channel="removeOldDataFromStorageFireAndForgetChannel">
<int:splitter
id="removeOldDataFromStorageFireAndForgetSplitter"
ref="mqFindTweetsToRemoveSplitter"
method="splitMessage" />
<int:aggregator
id="removeOldDataFromStorageFireAndForgetAggregator"
message-store="store"
release-strategy="releaserSimpleSequenceSizeReleaseStrategy" />
<int:service-activator
id="removeOldDataFromStorageFireAndForgetFinisher"
ref="mqTweetFinisher"
method="finishAsnyc" />
</int:chain>

</beans>
2 changes: 1 addition & 1 deletion src/main/resources/templates/tickersymbol/all.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<tbody>
<tr th:each="myPageItem : ${myPageContent}">
<td>
<span th:include="dataview :: tw-data-view-id" th:with="myPageItem=${myPageItem}"></span>
<span th:include="layoutDataview :: tw-data-view-id" th:with="myPageItem=${myPageItem}"></span>
</td>
<td>
<div class="row">
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/org/woehlke/twitterwall/ScheduledTasksTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ public void updateUserProfilesFromMentions() throws Exception {
public void fetchUsersFromDefinedUserList() throws Exception {
scheduledTasks.fetchUsersFromDefinedUserList();
}

@Test
public void removeOldDataFromStorage() throws Exception {
scheduledTasks.removeOldDataFromStorage();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
public abstract class AbstractMqEndpointTest {

protected boolean assertCountedEntities(CountedEntities beforeTest, CountedEntities afterTest) {
boolean result = true;

boolean resultTask = afterTest.getCountTask() > beforeTest.getCountTask();
boolean resultTaskHistory = afterTest.getCountTaskHistory() > beforeTest.getCountTaskHistory();
Expand All @@ -30,7 +29,36 @@ protected boolean assertCountedEntities(CountedEntities beforeTest, CountedEntit
Assert.assertTrue(resultMention);
Assert.assertTrue(resultTickerSymbol);

result = resultTask && resultTaskHistory && resultTweets && resultUser && resultUrl && resultUrlCache && resultHashTags && resultHashTags && resultMedia && resultMention && resultTickerSymbol;
boolean result = resultTask && resultTaskHistory && resultTweets && resultUser && resultUrl && resultUrlCache && resultHashTags && resultHashTags && resultMedia && resultMention && resultTickerSymbol;

return result;
}

protected boolean assertCountedEntitiesReduced(CountedEntities beforeTest, CountedEntities afterTest) {

boolean resultTask = afterTest.getCountTask() < beforeTest.getCountTask();
boolean resultTaskHistory = afterTest.getCountTaskHistory() < beforeTest.getCountTaskHistory();
boolean resultTweets = afterTest.getCountTweets()<=beforeTest.getCountTweets();
boolean resultUser = afterTest.getCountUser()<=beforeTest.getCountUser();
boolean resultUrl = afterTest.getCountUrl() <= beforeTest.getCountUrl();
boolean resultUrlCache = afterTest.getCountUrlCache()<= beforeTest.getCountUrlCache();
boolean resultHashTags = afterTest.getCountHashTags()<=beforeTest.getCountHashTags();
boolean resultMedia = afterTest.getCountMedia()<=beforeTest.getCountMedia();
boolean resultMention = afterTest.getCountMention() <=beforeTest.getCountMention();
boolean resultTickerSymbol = afterTest.getCountTickerSymbol() <= beforeTest.getCountTickerSymbol();

Assert.assertTrue(resultTask);
Assert.assertTrue(resultTaskHistory);
Assert.assertTrue(resultTweets);
Assert.assertTrue(resultUser);
Assert.assertTrue(resultUrl);
Assert.assertTrue(resultUrlCache);
Assert.assertTrue(resultHashTags);
Assert.assertTrue(resultMedia);
Assert.assertTrue(resultMention);
Assert.assertTrue(resultTickerSymbol);

boolean result = resultTask && resultTaskHistory && resultTweets && resultUser && resultUrl && resultUrlCache && resultHashTags && resultHashTags && resultMedia && resultMention && resultTickerSymbol;

return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ public interface AsyncStartTaskTest {
void fetchTweetsFromSearchTest() throws Exception;

void fetchUsersFromListTest() throws Exception;

void removeOldDataFromStorage()throws Exception;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.woehlke.twitterwall.scheduled.mq.endpoint;

import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
Expand Down Expand Up @@ -105,4 +106,23 @@ public void fetchUsersFromListTest() throws Exception {
Assert.assertTrue(ok);
log.info(msg+"FINISHED TEST");
}

//TODO: #229 https://github.com/phasenraum2010/twitterwall2/issues/229
@Ignore
@Test
@Override
public void removeOldDataFromStorage() throws Exception {
String msg = "removeOldDataFromStorage: ";
log.info(msg+"START TEST");
CountedEntities beforeTest = countedEntitiesService.countAll();
Task task = this.mqAsyncStartTask.removeOldDataFromStorage();
log.info(msg+"created Task = "+task.getUniqueId());
Assert.assertNotNull(task);
Assert.assertNotNull(task.getUniqueId());
Assert.assertEquals(SendType.FIRE_AND_FORGET,task.getSendType());
CountedEntities afterTest = countedEntitiesService.countAll();
boolean ok = assertCountedEntitiesReduced(beforeTest,afterTest);
Assert.assertTrue(ok);
log.info(msg+"FINISHED TEST");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
Expand Down Expand Up @@ -102,6 +103,23 @@ public void fetchUsersFromListTest() throws Exception {
log.info(msg+"FINISHED TEST");
}

//TODO: #229 https://github.com/phasenraum2010/twitterwall2/issues/229
@Ignore
@Test
@Override
public void removeOldDataFromStorage() throws Exception {
String msg = "removeOldDataFromStorage: ";
log.info(msg+"START TEST");
CountedEntities beforeTest = countedEntitiesService.countAll();
Task task = this.mqStartTask.removeOldDataFromStorage();
log.info(msg+"created Task = "+task.getUniqueId());
Assert.assertEquals(SendType.SEND_AND_WAIT_FOR_RESULT,task.getSendType());
CountedEntities afterTest = countedEntitiesService.countAll();
boolean ok = assertCountedEntitiesReduced(beforeTest,afterTest);
Assert.assertTrue(ok);
log.info(msg+"FINISHED TEST");
}

@Test
public void createImprintUserTest() throws Exception {
String msg = "createImprintUserTest: ";
Expand Down

0 comments on commit e6f482a

Please sign in to comment.