Skip to content

Commit

Permalink
Refactoring of model to be mroe generic
Browse files Browse the repository at this point in the history
  • Loading branch information
michmzr committed Jul 14, 2024
1 parent 25ed727 commit 250d7b4
Show file tree
Hide file tree
Showing 17 changed files with 153 additions and 84 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,6 @@ src/.DS_Store
src/main/.DS_Store
src/main/raporting/.DS_Store
!/.env

.bin/
node_modules
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package eu.cybershu.pocketstats.db;

import eu.cybershu.pocketstats.pocket.api.DomainMetadata;
import eu.cybershu.pocketstats.pocket.api.ItemStatus;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
Expand All @@ -14,7 +13,7 @@
@Data
@NoArgsConstructor
@EqualsAndHashCode(of = {"id", "url", "title", "status", "timeAdded", "timeRead"})
public class PocketItem {
public class Item {
@Id
private String id;
private String url;
Expand All @@ -33,4 +32,10 @@ public class PocketItem {
private String lang;
private DomainMetadata domainMetadata;
private List<String> tags;

/**
* article, email, document...
**/
private String category; // todo as a enum
private Source source;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@
import eu.cybershu.pocketstats.pocket.api.ListItem;
import eu.cybershu.pocketstats.pocket.api.Tag;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@Mapper(componentModel = "spring")
public interface PocketItemMapper {
PocketItemMapper INSTANCE = Mappers.getMapper(PocketItemMapper.class);
public interface ItemMapper {
ItemMapper INSTANCE = Mappers.getMapper(ItemMapper.class);

static List<String> mapTags(Map<String, Tag> tags) {
if (tags == null || tags.isEmpty()) return Collections.emptyList();
else return tags.keySet().stream().toList();
}

PocketItem apiToEntity(ListItem item);
@Mapping(target = "category", constant = "article")
@Mapping(target = "source", constant = "POCKET")
Item apiToEntity(ListItem item);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
import java.util.Optional;

@Service
public interface PocketItemRepository extends MongoRepository<PocketItem, String> {
Optional<PocketItem> findById(String id);
public interface ItemRepository extends MongoRepository<Item, String> {
Optional<Item> findById(String id);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eu.cybershu.pocketstats.pocket.api;
package eu.cybershu.pocketstats.db;

import com.fasterxml.jackson.annotation.JsonValue;

Expand Down
13 changes: 13 additions & 0 deletions src/main/java/eu/cybershu/pocketstats/db/Source.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package eu.cybershu.pocketstats.db;

public enum Source {
POCKET("pocket"),
INSTAPAPER("instapaper"),
READER("reader");

private final String sourceType;

Source(String sourceType) {
this.sourceType = sourceType;
}
}
24 changes: 12 additions & 12 deletions src/main/java/eu/cybershu/pocketstats/pocket/PocketApiService.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
public class PocketApiService {
private final HttpClient client;
private final ObjectMapper mapper;
private final PocketItemRepository pocketItemRepository;
private final ItemRepository itemRepository;
private final MigrationStatusRepository migrationStatusRepository;
private final PocketAuthorizationService authorizationService;
private final PocketItemMapper pocketItemMapper;
private final ItemMapper itemMapper;

private final EventsPublisher eventsPublisher;

Expand All @@ -39,7 +39,7 @@ public class PocketApiService {
@Value("${auth.pocket.url.get}")
private String pocketGetUrl;

public PocketApiService(PocketItemRepository pocketItemRepository,
public PocketApiService(ItemRepository itemRepository,
MigrationStatusRepository migrationStatusRepository,
PocketAuthorizationService authorizationService, EventsPublisher eventsPublisher) {
this.migrationStatusRepository = migrationStatusRepository;
Expand All @@ -51,8 +51,8 @@ public PocketApiService(PocketItemRepository pocketItemRepository,
.connectTimeout(Duration.ofSeconds(20))
.build();
this.mapper = new ObjectMapper();
this.pocketItemMapper = PocketItemMapper.INSTANCE;
this.pocketItemRepository = pocketItemRepository;
this.itemMapper = ItemMapper.INSTANCE;
this.itemRepository = itemRepository;
}

public SyncStatus importFromSinceLastUpdate() throws IOException, InterruptedException {
Expand Down Expand Up @@ -88,19 +88,19 @@ public Integer importAllToDbSince(Instant sinceWhen) throws IOException, Interru
.items()
.values()
.stream()
.map(pocketItemMapper::apiToEntity)
.map(itemMapper::apiToEntity)
.toList();

List<PocketItem> pocketItems = pocketItemRepository.saveAll(models);
List<Item> items = itemRepository.saveAll(models);

MigrationStatus migrationStatus = new MigrationStatus();
migrationStatus.id(UUID.randomUUID()
.toString());
migrationStatus.date(Instant.now());
migrationStatus.migratedItems(pocketItems.size());
migrationStatus.migratedItems(items.size());
migrationStatusRepository.save(migrationStatus);

return pocketItems.size();
return items.size();
}

public int importAll() throws IOException, InterruptedException {
Expand All @@ -110,7 +110,7 @@ public int importAll() throws IOException, InterruptedException {
final var count = 300;
int gotItems = 0;

List<PocketItem> importedItems = new LinkedList<>();
List<Item> importedItems = new LinkedList<>();
while (true) {
log.debug("offset:{}, count:{}", offset, count);

Expand All @@ -131,13 +131,13 @@ public int importAll() throws IOException, InterruptedException {
var models = pocketResponse.items()
.values()
.stream()
.map(pocketItemMapper::apiToEntity)
.map(itemMapper::apiToEntity)
.toList();
importedItems.addAll(models);

gotItems += items.size();

pocketItemRepository.saveAll(importedItems);
itemRepository.saveAll(importedItems);

offset += count;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import eu.cybershu.pocketstats.pocket.api.ListItem;
import lombok.extern.slf4j.Slf4j;

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

@Slf4j
public class PocketResponseItemsDeserializer extends JsonDeserializer<Map<String, ListItem>> {
@Override
public Map<String, ListItem> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
Expand All @@ -27,7 +29,12 @@ public Map<String, ListItem> deserialize(JsonParser p, DeserializationContext ct
if (node.isObject()) {
TypeReference<HashMap<String, ListItem>> typeRef = new TypeReference<>() {
};
return codec.readValue(node.traverse(), typeRef);
try {
return codec.readValue(node.traverse(), typeRef);
} catch (NoSuchFieldError e) {
log.error("Error deserializing field", e);
throw e;
}
} else {
throw new IllegalArgumentException("Expected items as object. Got " + node);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public record ListItem(@JsonProperty("item_id") String id, @JsonProperty("resolved_id") String resolvedId,
@JsonProperty("given_url") String url, @JsonProperty("given_title") String title,
@JsonDeserialize(converter = StringBooleanToBoolean.class) @JsonProperty("favorite") Boolean favorite,
@JsonProperty("status") ItemStatus status,
@JsonProperty("status") PocketItemStatus status,
@JsonProperty("time_added") @JsonDeserialize(converter = LongToInstantConverter.class) Instant timeAdded,
@JsonProperty("time_updated") @JsonDeserialize(converter = LongToInstantConverter.class) Instant timeUpdated,
@JsonDeserialize(converter = LongToInstantConverter.class) @JsonProperty("time_read") Instant timeRead,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mongodb.client.AggregateIterable;
import com.mongodb.client.MongoCollection;
import eu.cybershu.pocketstats.db.ItemStatus;
import eu.cybershu.pocketstats.events.UserSynchronizedItemsEvent;
import eu.cybershu.pocketstats.stats.DayStat;
import eu.cybershu.pocketstats.stats.DayStatsRecords;
Expand Down Expand Up @@ -244,16 +245,16 @@ public PeriodItemsStats itemsStatsTotal() {
.append("count",
new Document("$sum", 1L)))));

Map<ItemStatus, Long> itemStats = new HashMap<>();
Map<PocketItemStatus, Long> itemStats = new HashMap<>();
for (Document docs : result) {
String name = docs.getString("_id");
long count = docs.getLong("count");
itemStats.put(ItemStatus.valueOf(name), count);
itemStats.put(PocketItemStatus.valueOf(name), count);
}

return new PeriodItemsStats(
itemStats.get(ItemStatus.TO_READ) + itemStats.get(ItemStatus.ARCHIVED),
itemStats.get(ItemStatus.ARCHIVED));
itemStats.get(PocketItemStatus.TO_READ) + itemStats.get(PocketItemStatus.ARCHIVED),
itemStats.get(PocketItemStatus.ARCHIVED));
}

private MongoCollection<Document> getPocketItemsCollection() {
Expand Down Expand Up @@ -313,7 +314,7 @@ public ActivityHeatmapStats heatmapOfStatus(StatsWithStatusType type) {
match = new Document("$match",
new Document("status",
new Document("$exists", true)
.append("$ne", ItemStatus.DELETED)));
.append("$ne", PocketItemStatus.DELETED)));
} else {
match = new Document("$match",
new Document("status", itemStatus.name()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package eu.cybershu.pocketstats.pocket.api;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

public enum PocketItemStatus {
TO_READ(0),
ARCHIVED(1),
DELETED(2);

private final int status;

PocketItemStatus(int status) {
this.status = status;
}

@JsonValue
public int getStatus() {
return status;
}

@JsonCreator
public static PocketItemStatus forValue(int status) {
for (PocketItemStatus itemStatus : PocketItemStatus.values()) {
if (itemStatus.getStatus() == status) {
return itemStatus;
}
}

throw new IllegalArgumentException("Unknown status: " + status);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package eu.cybershu.pocketstats.stats;

import eu.cybershu.pocketstats.pocket.api.ItemStatus;
import eu.cybershu.pocketstats.db.ItemStatus;

public enum StatsWithStatusType {
ARCHIVED,
Expand Down
Loading

0 comments on commit 250d7b4

Please sign in to comment.