Skip to content
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

Task/diary food stat #201

Open
wants to merge 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,19 @@

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.bosik.diacomp.core.entities.business.FoodMassed;
import org.bosik.diacomp.core.entities.business.diary.DiaryRecord;
import org.bosik.diacomp.core.entities.business.diary.records.MealRecord;
import org.bosik.diacomp.core.entities.business.dishbase.DishItem;
import org.bosik.diacomp.core.persistence.parsers.Parser;
import org.bosik.diacomp.core.persistence.parsers.ParserDiaryRecord;
import org.bosik.diacomp.core.persistence.serializers.Serializer;
import org.bosik.diacomp.core.persistence.utils.SerializerAdapter;
import org.bosik.diacomp.core.services.ObjectService;
import org.bosik.diacomp.core.utils.Utils;
import org.bosik.diacomp.web.backend.common.UserDataService;
import org.bosik.diacomp.web.backend.features.base.dish.DishBaseLocalService;
import org.bosik.diacomp.web.backend.features.base.dish.DishEntityRepository;
import org.bosik.merklesync.HashUtils;
import org.bosik.merklesync.MerkleTree;
import org.bosik.merklesync.Versioned;
Expand All @@ -36,8 +41,10 @@

import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.SortedMap;
import java.util.TimeZone;
import java.util.TreeMap;
Expand All @@ -57,7 +64,10 @@ public class DiaryLocalService implements UserDataService<DiaryRecord>
private DiaryEntityRepository repository;

@Autowired
private CachedDiaryHashTree cachedHashTree;
private DishEntityRepository dishEntityRepository;

@Autowired
private CachedDiaryHashTree cachedHashTree;

public static Versioned<DiaryRecord> convert(DiaryEntity e)
{
Expand Down Expand Up @@ -144,6 +154,50 @@ public List<Versioned<DiaryRecord>> findAll(int userId, boolean includeRemoved)
}
}

public Map<String, Double> getFoodStatistics(int userId, Date from, Date to)
{
final List<DiaryEntity> entities = repository.findByUserIdAndTimeCacheBetweenAndDeletedIsFalseOrderByTimeCache(
userId, from, to);
final List<FoodMassed> diaryFood = convert(entities).stream()
.filter(v -> v.getData() instanceof MealRecord)
.flatMap(v -> ((MealRecord) v.getData()).getItems().stream())
.collect(toList());

final List<Versioned<DishItem>> dishes = DishBaseLocalService.convert(
dishEntityRepository.findByUserIdAndDeletedIsFalse(userId));

final Map<String, Double> stat = new HashMap<>();

for (FoodMassed food : diaryFood)
{
final Optional<Versioned<DishItem>> dish = dishes.stream()
.filter(d -> d.getData().getName().equals(food.getName()))
.findFirst();

if (dish.isPresent())
{
for (FoodMassed dishItem : dish.get().getData().getContent())
{
final String name = dishItem.getName();
double mass = food.getMass() / dish.get().getData().getRealMass() * dishItem.getMass();

stat.putIfAbsent(name, 0.0);
stat.put(name, stat.getOrDefault(name, 0.0) + mass);
}
}
else
{
final String name = food.getName();
double mass = food.getMass();

stat.putIfAbsent(name, 0.0);
stat.put(name, stat.getOrDefault(name, 0.0) + mass);
}
}

return stat;
}

@Override
public Versioned<DiaryRecord> findById(int userId, String id)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ public Integer count()
return diaryService.count(userId);
}

@GetMapping("/foodstat")
public Map<String, Double> getFoodStatistics(
@RequestParam("from") String parTimeFrom,
@RequestParam("to") String parTimeTo)
{
final int userId = getUserId();
final Date timeFrom = Utils.parseTimeUTC(parTimeFrom);
final Date timeTo = Utils.parseTimeUTC(parTimeTo);

return diaryService.getFoodStatistics(userId, timeFrom, timeTo);
}

@GetMapping("/count/{prefix}")
public Integer count(@PathVariable(name = "prefix") String prefix)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ public void export()
static class Config
{
@Bean
public DiaryLocalService employeeService(DiaryEntityRepository repository, CachedDiaryHashTree cachedHashTree)
public DiaryLocalService diaryLocalService(DiaryEntityRepository repository, CachedDiaryHashTree cachedHashTree)
{
return new DiaryLocalService(repository, cachedHashTree);
return new DiaryLocalService(repository, null, cachedHashTree);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ public FoodMassed get(int index)
return items.get(index);
}

public List<FoodMassed> getItems()
{
return items;
}

public int count()
{
return items.size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class DishItem implements NamedRelative, Serializable

// ================================ GET / SET ================================

private double getRealMass()
public double getRealMass()
{
if (mass != null)
{
Expand Down