Re: Apache Cayenne - Merge Data

Keena Grepo <[email protected]>
Newsgroups gmane.comp.java.cayenne.user
Message-ID <pony-493149467983a110086d60fa5cc78360b328d806-f10a52e2292343fd4c54789756bcca335ef388ca@user.cayenne.apache.org>
Hi Andrus,

Thanks for the help and for the quick reply. Following your idea and sample code, I was now able to display the data. 

> List<UtilizationData> result = ...
> Set<LocalDateTime> timestampsUnsorted = new HashSet<>();
> Map<String, Map<LocalDateTime , Integer>> valuesByHost = new HashMap<>();
--- here, I used StreamEx to get the distinct timestamp from the List<UtilizationData> result, and to group the result by hostname.


List<String> distTimestamp = StreamEx.of(result).distinct(UtilizationData::getTimestamp).map(UtilizationData::getTimestamp).toList();
Map<String, List<UtilizationData>> groupedUtil = StreamEx.of(result).groupingBy(UtilizationData::getHostname);

<table>
	<thead>
		<tr>
			<th>HOSTNAME</th>
			<th th:each="datetime : ${distTimestamp}" th:text="${datetime}"></th>
		 </tr>
	</thead>
	<tbody>
		<tr th:each="utilization : ${groupedUtil}">
		         <td th:text="${utilization.key}"></td>
		         <td th:each="hostname : ${utilization.value}" th:text="${hostname.getHourlyUsedPercentage()}"></td>
		</tr>
	</tbody>
</table>


BTW, here is my Cayenne query : 
List<UtilizationData> utilData = ObjectSelect.query(UtilizationData.class).where(UtilizationData.EPOCH_TIME.between(dateStart, dateEnd)).orderBy(UtilizationData.HOSTNAME.desc(), UtilizationData.EPOCH_TIME.asc()).select(context);

Regards,
Keena

On 2019/08/15 07:25:17, Andrus Adamchik <[email protected]> wrote: 
> A small correction for Thymeleaf table:
> 
> <table>
>  <tr>
>     <th>HOST</th>
>     <th th:each="ts: ${timestamps}" th:text="${ts}"/>
>  </tr>
>  <tr th:each="hostData: ${valuesByHost}">
>     <td th:text="${hostData.key}"/>
>     <td th:each="ts: ${timestamps}" th:text="${hostData.value.get(ts)}"/>
>  </tr>
> </table>
> 
> Of course I haven't tried running this for real, so there may be more issues with the example (esp. considering that I've never used Thymeleaf in my life :) ), but it should give an idea.
> 
> Andrus
> 
> 
> > On Aug 15, 2019, at 10:20 AM, Andrus Adamchik <[email protected]> wrote:
> > 
> > Hi Keena,
> > 
> > The short answer is - write some Java code to transform the Cayenne result into a data structure appropriate for Thymeleaf display. 
> > 
> > I don't know anything about Thymeleaf, but from quick googling, it might look like this:
> > 
> > <table>
> >  <tr>
> >     <th>HOST</th>
> >     <th th:each="ts: ${timestamps}" th:text="${ts}"/>
> >  </tr>
> >  <tr th:each="hostData: ${valuesByHost}">
> >     <td th:each="ts: ${timestamps}" th:text="${hostData.get(ts)}"/>
> >  </tr>
> > </table>
> > 
> > 
> > Now you need to build "timestamps" and "valuesByHost" collections:
> > 
> > List<UtilizationData> result = ...
> > Set<LocalDateTime> timestampsUnsorted = new HashSet<>();
> > Map<String, Map<LocalDateTime , Integer>> valuesByHost = new HashMap<>();
> > 
> > // iterate over "result", populating "timestampsUnsorted" and "valuesByHost" from the object data
> > ...
> > 
> > List<LocalDateTime> timestamps = new ArrayList<>(timestampsUnsorted);
> > Collections.sort(timestamps);
> > 
> > HTH,
> > Andrus
> > 
> > P.S. BTW, can you show your Cayenne query. You are saying that you have ObjectSelect.columnQuery (which normally returns a List<Object> or List<Object[]>), while your result seems to be a List<UtilizationData>, which would be the case if you ran a regular (not column) ObjectSelect query.
> > 
> > 
> >> On Aug 15, 2019, at 6:16 AM, Keena Grepo <[email protected]> wrote:
> >> 
> >> Hi, 
> >> 
> >> I have this data returned by ObjectSelect.columnQuery :
> >> 
> >> [{<ObjectId:UtilizationData, id=1>; committed; [hostname=>cc001; percentagePerHour=>100; timestamp=>2019-07-09 00:00:00]}, {<ObjectId:UtilizationData, id=2>; committed; [hostname=>cc001; percentagePerHour=>50; timestamp=>2019-07-09 01:00:00]}, {<ObjectId:UtilizationData, id=3>; committed; [hostname=>cc001; percentagePerHour=>50; ; timestamp=>2019-07-09 02:00:00]}, {<ObjectId:UtilizationData, id=4>; committed; [hostname=>cc001; percentagePerHour=>83; timestamp=>2019-07-09 03:00:00]}, ...]
> >> 
> >> And I want to display the data in a table with "HOST", and timestamps as table head. Then for the table data are the value of hostname and the percentagePerHour for each respective timestamp (like the sample table below). How can I achieve this? 
> >> -------------------------------------------------------------------------------------------------------------------------------
> >> | HOST  | 2019-07-09 00:00:00 | 2019-07-09 01:00:00 | 2019-07-09 02:00:00 | 2019-07-09 03:00:00 |
> >> -------------------------------------------------------------------------------------------------------------------------------
> >> | cc001  | 100                            | 50                               | 50                              | 83                               |
> >> -------------------------------------------------------------------------------------------------------------------------------
> >> 
> >> By the way, I am using Springboot + Thymeleaf.
> >> 
> >> Thanks.
> > 
> 
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.