This is a migrated thread and some comments may be shown as answers.

Aggregate Sum Returns 0

8 Answers 178 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Jayme
Top achievements
Rank 1
Jayme asked on 21 Aug 2015, 05:00 PM

I am attempting to sum an Amount field on a kendo datasource, but the sum is always 0.

Datasource:
...
 return new kendo.data.DataSource({
        type: "odata",
        transport: {
            read: {
                url: serviceBaseUrl,
                dataType: "json"
            }
        },
        batch: false,
        serverPaging: true,
        serverSorting: true,
        serverFiltering: true,
        serverAggregates: true,
        aggregate: { field: "Amount", aggregate: "sum", type: "number" },
        schema: {
            data: function (data) { return data.value; },
            total: function (data) { return data["odata.count"]; },
            model: {
                id: "OrderID",
                fields: {
                    OrderID: { type: "int" },
                    MerchantID: { type: "int", required: true },
                    Amount: { type: "number", required: true },
                    PurchaseDate: { type: "date", defaultValue: new Date() }
                }
            }
        },
        error: function (e) {
            return (e.xhr.responseText);
            this.cancelChanges();
        },
        success: function (e) {
            alert('Success!');
        }
    });

Angular JS Controller:

 var dsYearOrders = new orderService(ngAuthSettings);
    $scope.thisyear = kendo.parseDate(moment().startOf('year').format('MM/DD/YYYY'));
    var yearfilters = SetFilter($scope.thisyear)
    dsYearOrders.filter(yearfilters);
    $scope.yearoforders = dsYearOrders; // -- this works, we see our chart data as intended
    $scope.sumYearSales = dsYearOrders.aggregates().Amount.sum;​​ // --Returns 0

 

I feel like I am missing something in the schema to return the proper aggregate amount, no?  Thanks in advance,

 Jayme

8 Answers, 1 is accepted

Sort by
0
Rosen
Telerik team
answered on 25 Aug 2015, 07:10 AM

Hello Jayme,

Looking at the code snippet you have pasted it seems that you are using OData service to do the server processing. However, as far as I'm aware there is no OData implementation which supports aggregation. This, I suspect it the cause for the empty aggregates. Therefore, you should consider letting the DataSource to process the data and set the server operations to false.

Regards,
Rosen
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Jayme
Top achievements
Rank 1
answered on 25 Aug 2015, 10:53 AM

Yes, we are using OData. I am still new to Kendo Datasources. With the example I have above, what would I change to allow the processing on the client? (In the datasource and kendo chart?)

 Thank you,

Jayme

0
Rosen
Telerik team
answered on 26 Aug 2015, 06:43 AM

Hello Jayme,

In order to use client-side processing you will need to set the serverPaging, serverFiltering, serverSorting, serverAggregates etc. options to false.

Regards,
Rosen
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Jayme
Top achievements
Rank 1
answered on 26 Aug 2015, 11:21 AM
Yes, but then what do we modify in the client code? 
0
Rosen
Telerik team
answered on 27 Aug 2015, 06:08 AM

Hello Jayme,

I'm not sure I understood your question. Please clarify? If you meant how to change the configuration in order to enable client-side processing, as I said you should set  serverPaging, serverFiltering, serverSorting, serverAggregates etc. DataSource options to false. 

return new kendo.data.DataSource({
    type: "odata",
    transport: {
         /*..*/
    },
    batch: false,
    serverPaging: false,
    serverSorting: false,
    serverFiltering: false,
    serverAggregates: false,
    aggregate: { field: "Amount", aggregate: "sum", type: "number" },
    schema: {
     /*..*/
    },
    /*...*/
});

Regards,
Rosen
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Jayme
Top achievements
Rank 1
answered on 27 Aug 2015, 11:57 AM

I don't know that in reality we would ever turn off server filtering as that is not optimal for performance.  Can't we just set the serverAggregates to false to allow for the aggregate to be handled on the client, but continue filtering on the client?    

 I am clear on how to modify the Kendo Datasource.  What is not clear is how to get the aggregate on the client.  The following is our client code - this does not return the sum: 

   var dsYearOrders = new orderService(ngAuthSettings);
   $scope.yearoforders = dsYearOrders; // -- this works, we see our chart data as intended
   $scope.sumYearSales = dsYearOrders.aggregates().Amount.sum;​​ // --aggregate is undefined

 
So, my question is: How do I get the sum of the Amounts returned by the Odata using the aggregate function of the Kendo Datasource? Is this possible where I am using Odata or do I need to write a function to loop through and calculate the sum?

I use this same datasource (with server processing set to true) for charts and it calculates the sum perfectly.  I would like to figure out how to get the sum without using a chart.


Thank you for your time to reply and helping me understand how everything works.

 

Jayme

 

 

0
Rosen
Telerik team
answered on 28 Aug 2015, 07:56 AM

Hello Jayme,

You could set only the serverAggregates option to false. However you should be aware that this may cause incorrect results based on the other options. As the aggregates will be calculated only over the available on the client data, having server paging enabled will cause just part of whole the data to be processed on the client. 

Due to its specifics, the Chart widget do not use the DataSource to calculate the aggregates but its own processing engine. This is way there are aggregates shown in the Chart when using serverAggregates option to true. 

The setup you are using to configure the aggregates seems correct. Only the server vs. client processing should be set correctly in order the values to be calculated.

Regards,
Rosen
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Jayme
Top achievements
Rank 1
answered on 28 Aug 2015, 01:35 PM

I set all of the options to false, but still get 0 for the sum of Amount when making the call:

$scope.sumYearSales = dsYearOrders.aggregates().Amount.sum;​​ // --Returns 0 

I needed to move on, so I handle this in my backend which is probably better for performance anyway.  Thanks for your time,

Jayme

Tags
Data Source
Asked by
Jayme
Top achievements
Rank 1
Answers by
Rosen
Telerik team
Jayme
Top achievements
Rank 1
Share this question
or