You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Keena Grepo <ke...@gmail.com> on 2019/08/15 03:16:06 UTC

Apache Cayenne - Merge Data

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.

Re: Apache Cayenne - Merge Data

Posted by Andrus Adamchik <an...@objectstyle.org>.

> On Aug 15, 2019, at 12:49 PM, Keena Grepo <ke...@gmail.com> wrote:
> 
> 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);

Ah great. So you are using ObjectSelect.query, not  ObjectSelect.columnQuery. Then everything makes sense.

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

Good to know that it worked. Happy to help.

Andrus



Re: Apache Cayenne - Merge Data

Posted by Keena Grepo <ke...@gmail.com>.
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 <an...@objectstyle.org> 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 <an...@objectstyle.org> 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 <ke...@gmail.com> 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.
> > 
> 
> 

Re: Apache Cayenne - Merge Data

Posted by Andrus Adamchik <an...@objectstyle.org>.
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 <an...@objectstyle.org> 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 <ke...@gmail.com> 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.
> 


Re: Apache Cayenne - Merge Data

Posted by Andrus Adamchik <an...@objectstyle.org>.
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 <ke...@gmail.com> 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.