Aggregate Sum Returns 0

9 posts, 0 answers
  1. Jayme
    Jayme avatar
    17 posts
    Member since:
    Jun 2014

    Posted 21 Aug 2015 Link to this post

    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

  2. Rosen
    Admin
    Rosen avatar
    3234 posts

    Posted 25 Aug 2015 Link to this post

    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!
     
  3. Kendo UI is VS 2017 Ready
  4. Jayme
    Jayme avatar
    17 posts
    Member since:
    Jun 2014

    Posted 25 Aug 2015 in reply to Rosen Link to this post

    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

  5. Rosen
    Admin
    Rosen avatar
    3234 posts

    Posted 26 Aug 2015 Link to this post

    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!
     
  6. Jayme
    Jayme avatar
    17 posts
    Member since:
    Jun 2014

    Posted 26 Aug 2015 in reply to Rosen Link to this post

    Yes, but then what do we modify in the client code? 
  7. Rosen
    Admin
    Rosen avatar
    3234 posts

    Posted 27 Aug 2015 Link to this post

    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!
     
  8. Jayme
    Jayme avatar
    17 posts
    Member since:
    Jun 2014

    Posted 27 Aug 2015 in reply to Rosen Link to this post

    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

     

     

  9. Rosen
    Admin
    Rosen avatar
    3234 posts

    Posted 28 Aug 2015 Link to this post

    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!
     
  10. Jayme
    Jayme avatar
    17 posts
    Member since:
    Jun 2014

    Posted 28 Aug 2015 in reply to Rosen Link to this post

    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

Back to Top
Kendo UI is VS 2017 Ready