angular-meteor icon indicating copy to clipboard operation
angular-meteor copied to clipboard

angular-meteor search performance

Open AnjirHossain opened this issue 11 years ago • 2 comments

I have the following setup in this repository.

Server: Meteor.publish('listings', function( searchProps ) {

check(searchProps, Object);
Listings._ensureIndex({"loc":"2d","make.name":1,"price":1,"year":1,"milage":1});

var selectors = {};

// find a better way to store all specificity than just if elses
if ( searchProps.address ) {
    selectors["loc"] = { 
        $near: [ searchProps.address.geometry.location.L,    searchProps.address.geometry.location.H],
        $maxDistance: 1.5/3963.2 // radius
    };      
} 

if ( searchProps.make.name ) {
    selectors["make.name"] = searchProps.make.name;
}

if ( searchProps.model.name ) {
    selectors["model.name"] = searchProps.model.name;
}

if ( searchProps.mileageMax && searchProps.mileageMin ) {
    selectors["milage"] = { 
        $gte: parseInt(searchProps.mileageMin), 
        $lte: parseInt(searchProps.mileageMax)
    };
} else {

    if ( searchProps.mileageMax ) {
        selectors["milage"] = {
            $lte: parseInt(searchProps.mileageMax)  
        };
    } 

    if ( searchProps.mileageMin ) {
        selectors["milage"] = {
            $gte: parseInt(searchProps.mileageMin)  
        };
    } 
}

if ( searchProps.priceMax && searchProps.priceMin ) {
    selectors["price"] = { 
        $gte: parseInt(searchProps.priceMin), 
        $lte: parseInt(searchProps.priceMax)
    };
} else {

    if ( searchProps.priceMax ) {
        selectors["price"] = {
            $lte: parseInt(searchProps.priceMax)    
        };
    } 

    if ( searchProps.priceMin ) {
        selectors["price"] = {
            $gte: parseInt(searchProps.priceMin)    
        };
    } 
}

if ( searchProps.priceMax && searchProps.priceMin ) {
    selectors["price"] = { 
        $gte: parseInt(searchProps.priceMin), 
        $lte: parseInt(searchProps.priceMax)
    };
} else {

    if ( searchProps.priceMax ) {
        selectors["price"] = {
            $lte: parseInt(searchProps.priceMax)    
        };
    } 

    if ( searchProps.priceMin ) {
        selectors["price"] = {
            $gte: parseInt(searchProps.priceMin)    
        };
    } 
}

if ( searchProps.yearMax && searchProps.yearMin ) {
    selectors["year"] = { 
        $gte: parseInt(searchProps.yearMin), 
        $lte: parseInt(searchProps.yearMax)
    };
} else {

    if ( searchProps.yearMax ) {
        selectors["year"] = {
            $lte: parseInt(searchProps.yearMax) 
        };
    } 

    if ( searchProps.yearMin ) {
        selectors["year"] = {
            $gte: parseInt(searchProps.yearMin) 
        };
    } 
}

// temp
return Listings.find({
    $or: [ selectors ]
});

});

Client: $scope.loading = false;

    $scope.beginSearch = function(a) {
        // console.log( a );

        if (!$scope.showFilterPopOver) {
            $scope.filterPopOverStateMutator();
        }

        // return false;
    };

    $scope.applyFilters = function(b) {

        if ( !b.address ) {

            if ( $scope.searchErrorNoAddress ){
                $scope.searchErrorNoAddress = false;
            } else {
                $scope.searchErrorNoAddress = true;
            }

            return;
        }

        console.log(b);
        $scope.searchErrorNoAddress = false;

        $scope.loading = true;

        /*$meteor.subscribe('listings', b).then(function (handler){ 
            $scope.filteredListings = $meteor.collection(Listings); 
            $scope.$apply();
            handler.stop();
        });*/

        $scope._searchFodderInHouse = b; // hax but fuck it

        $meteor.autorun($scope, function(){
            $scope.$meteorSubscribe('listings', $scope.getReactively('_searchFodderInHouse', true)).then(function( handler ) {

            // $scope.filteredListings = $meteor.collection(function(){
            //     return Listings.find({});
            // });

                // console.log($meteor.collection(Listings));

                $scope.filteredListings = $meteor.collection(Listings);
                $scope.loading = false;
            });            
        });

        $scope.filterPopOverStateMutator();
    };

    $scope.searchErrorNoAddress = false;

    $scope.$watch('searchFodder.address', function(context){
        // figure out way to watch entire object fcol
        if (!context) {
            console.log("this is what context is: " + context);
            $scope.filteredListings = [];
            $scope.filterLax = 6;
        }
    });

When the user completes a form to query the db for cars the latency is jarring. I've tried logging the results that come back from the server and it logs instantly, however when I attempt to bind it to the DOM like so:

<div class="listings-items-wrap"
    ng-repeat="listing in filteredListings | limitTo: filterLax track by listing._id">

        <div class="listings-item-scaffold col-md-6">

            <div class="listings-item" 
            style="background-image:url('/cfs/files/images/{{listing.listImgId}}/{{listing.imageName}}');">

                <article class="listings-item-preview"
                ng-click="listingWindowOpen(listing._id)">
                    <span class="col-md-12 lip-text-wrap">
                        <p style="text-align:center;" class="col-md-4 info-pipe">{{ ::listing.price | currency:"$":0}}</p>

                        <p style="text-align:center;" class="col-md-4 info-pipe">{{ ::listing.milage }} mi</p>

                        <p style="text-align:center;" class="col-md-4 info-pipe">{{ ::listing.year }}</p>

                        <p style="text-align:center;" class="col-md-12 peak">{{ ::listing.make.name }}, {{ ::listing.model.name }}<br>
                        {{::listing.address.formatted_address}}</p>
                    </span>
                </article>  
            </div>

        </div>
    </div>

the data takes at least 10 seconds to render via Angular ng-repeat, when I console.log after the data being returned through the subscription is fetched it shows up instantly. All MongoDB collections are indexed. Can someone point me towards the right direction in terms of performance?

AnjirHossain avatar Oct 03 '15 01:10 AnjirHossain

I tried going through the code in the repo and seeing where the problem could be, but I couldn't get it running in a scenario which will return listing so I could experience the problem. If you could tell me how exactly to reproduce the scenario, I could help. A wild guess from just seeing the code would be to move this line: https://github.com/AnjirHossain/Traverso/blob/master/client/find/find.js#L106 outside of the autorun, as it should not be inside the subscribe promise.

netanelgilad avatar Oct 24 '15 07:10 netanelgilad

Thank you for the response, the repo is actually deprecated and I thought I had taken it down. I fixed the problem by replacing ng-repeat with blaze's #each.

AnjirHossain avatar Oct 24 '15 19:10 AnjirHossain