diff --git a/api.go b/api.go index d42c03c..9683e09 100644 --- a/api.go +++ b/api.go @@ -74,6 +74,9 @@ func (this *VultureBackend) getIndexes() (interface{}, error) { func (this *VultureBackend) getMetaData(query *mgo.Query) (map[string]interface{}, error) { meta := make(map[string]interface{}) + if query == nil { + return meta, nil + } var err error if meta["count"], err = query.Count(); err != nil { return meta, err @@ -95,7 +98,7 @@ func (this *VultureBackend) GetDocumentByIndex(index int, query interface{}) (ma return this.wrapDocumentWithMetadata(doc, queryResult) } -func (this *VultureBackend) wrapDocumentWithMetadata(doc map[string]interface{}, query *mgo.Query) (map[string]interface{}, error) { +func (this *VultureBackend) wrapDocumentWithMetadata(doc interface{}, query *mgo.Query) (map[string]interface{}, error) { ret := make(map[string]interface{}) var err error ret["document"] = doc @@ -105,6 +108,18 @@ func (this *VultureBackend) wrapDocumentWithMetadata(doc map[string]interface{}, return ret, nil } +func (this *VultureBackend) GetAllDocuments(query interface{}) (interface{}, error) { + var result []map[string]interface{} + + iter := this.Collection.Find(query).Iter() + iter.All(&result) + if err := iter.Close(); err != nil { + return nil, err + } + + return this.wrapDocumentWithMetadata(result, nil) +} + func getAvailableServers(w http.ResponseWriter, request *http.Request) (interface{}, error) { return params.MongoServers, nil } @@ -142,6 +157,23 @@ func getDocumentByIndex(w http.ResponseWriter, request *http.Request) (interface return vb.GetDocumentByIndex(index, nil) } +func getAllDocuments(w http.ResponseWriter, request *http.Request) (interface{}, error) { + vb, err := GetVultureBackend(request) + if err != nil { + return nil, err + } + vars := mux.Vars(request) + queryString, ok := vars["query"] + if !ok || queryString == "" { + return vb.GetAllDocuments(nil) + } + query := make(map[string]interface{}) + if err := json.Unmarshal([]byte(queryString), &query); err != nil { + return nil, errors.New("invalid json") + } + return vb.GetAllDocuments(query) +} + func getDocumentById(w http.ResponseWriter, request *http.Request) (interface{}, error) { vb, err := GetVultureBackend(request) if err != nil { diff --git a/client/coffee/controllers/geojsonmapCtrl.coffee b/client/coffee/controllers/geojsonmapCtrl.coffee index 36de54e..3c5c6c3 100644 --- a/client/coffee/controllers/geojsonmapCtrl.coffee +++ b/client/coffee/controllers/geojsonmapCtrl.coffee @@ -1,32 +1,50 @@ root = this; +mapCenter = + lat: 46.1227 + lng: -72.6169 + zoom: 7 +getSingleDataUrl = (routeParams) -> + url = "/api/#{ routeParams.server }/#{ routeParams.database }/#{ routeParams.collection }" + if routeParams.query + url = "#{url}/query/#{ routeParams.query }" + return "#{ url }/idx/#{ Number(routeParams.idx) - 1 }" + +getAllDataUrl = (routeParams) -> + url = "/api/#{ routeParams.server }/#{ routeParams.database }/#{ routeParams.collection }" + if routeParams.query + url = "#{url}/query/#{ routeParams.query }" + return "#{ url }/all" + +getDocumentUrl = (routeParams, id) -> + return "/#{ routeParams.server }/#{ routeParams.database }/#{ routeParams.collection }/_id/#{ id }" + +transformListOfGeoJsonToGeometryCollection = (resData, routeParams) -> + ret = + type: "GeometryCollection" + geometries: [] + for doc in resData.document + geojson = doc[routeParams.key] + if not geojson.properties + geojson.properties = {} + geojson.properties["_vulture_url_link"] = getDocumentUrl(routeParams, doc._id) + ret.geometries.push(geojson) + return ret + root.controllers.controller('geojsonmapCtrl', ['$scope', '$routeParams', '$location', '$http', ($scope, $routeParams, $location, $http) -> $scope.geojson = {} $scope.geojsonData = {} $scope.idx = Number($routeParams.idx) - $scope.center = # experimental point around Québec - lat: 46.1227 - lng: -72.6169 - zoom: 7 - - url = "/api/#{ $routeParams.server }/#{ $routeParams.database }/#{ $routeParams.collection }" - if $routeParams.query - url = "#{url}/query/#{$routeParams.query}" - url = "#{ url }/idx/#{$routeParams.idx - 1}" - $http.get(url).then((res) -> - $scope.geojson = res.data.document[$routeParams.key] - if not $scope.geojson.properties - $scope.geojson.properties = {} - url = "/#{ $routeParams.server }/#{ $routeParams.database }/#{ $routeParams.collection }/_id/#{ res.data.document._id }" - $scope.geojson.properties["_vulture_url_link"] = url - $scope.meta = res.data.meta - $scope.geojsonData = - data: $scope.geojson - style: undefined - resetStyleOnMouseout: true - ) + $scope.center = mapCenter + $scope.getAggredatedUrl = () -> + url = "#/#{ $routeParams.server }/#{ $routeParams.database }/#{ $routeParams.collection }/idx/all" + if $routeParams.query + url = "#{url}/query/#{$routeParams.query}" + "#{url}/geojson/#{$routeParams.key}" + + $scope.previousDocumentUrl = () -> url = "#/#{ $routeParams.server }/#{ $routeParams.database }/#{ $routeParams.collection }/idx/#{$scope.idx - 1}" if $routeParams.query @@ -44,6 +62,34 @@ root.controllers.controller('geojsonmapCtrl', ['$scope', '$routeParams', '$locat $scope.$on "leafletDirectiveMap.geojsonClick", (ev, featureSelected, leafletEvent) -> $location.url(featureSelected.properties._vulture_url_link); - + + $scope.initSingleDocument = ()-> + url = getSingleDataUrl($routeParams) + $http.get(url).then (res) -> + geojson = res.data.document[$routeParams.key] + if not geojson.properties + geojson.properties = {} + geojson.properties["_vulture_url_link"] = getDocumentUrl($routeParams, res.data.document._id) + $scope.meta = res.data.meta + $scope.geojsonData = + data: geojson + style: undefined + resetStyleOnMouseout: true + + $scope.initAllDocuments = () -> + url = getAllDataUrl($routeParams) + $http.get(url).then (res) -> + $scope.meta = res.data.meta + $scope.geojsonData = + data: transformListOfGeoJsonToGeometryCollection(res.data, $routeParams) + style: undefined + resetStyleOnMouseout: true + + if $routeParams.idx + $scope.mode = 'single_document' + $scope.initSingleDocument() + else + $scope.mode = 'all' + $scope.initAllDocuments() ]) diff --git a/client/coffee/controllers/topBarCtrl.coffee b/client/coffee/controllers/topBarCtrl.coffee index fd9499f..65ba991 100644 --- a/client/coffee/controllers/topBarCtrl.coffee +++ b/client/coffee/controllers/topBarCtrl.coffee @@ -11,7 +11,7 @@ root.controllers.controller('topBarCtrl', ['$scope', '$location', ($scope, $loca $scope.path = joinArray($scope.path[0...6], $scope.path[7...-1]) $scope.getHistoryUrl = (idx) -> ret = "#/" + ($scope.path[i] for i in [0...idx]).join("/") - if _.indexOf(["idx", "geojson", "query", "_id", "field"], $scope.path[idx]) != -1 + if _.indexOf(["idx", "geojson", "query", "_id", "field", "all"], $scope.path[idx]) != -1 return "" if idx == 0 ret += "servers" diff --git a/client/coffee/vultureApp.coffee b/client/coffee/vultureApp.coffee index ebeca96..feb39c6 100644 --- a/client/coffee/vultureApp.coffee +++ b/client/coffee/vultureApp.coffee @@ -19,7 +19,9 @@ config(['$routeProvider', ($routeProvider) -> $routeProvider.when('/:server/:database/:collection/_id/:id/field/:field', {templateUrl: 'partials/field.html', controller: 'fieldCtrl'}) $routeProvider.when('/:server/:database/:collection/idx/:idx', {templateUrl: 'partials/document.html', controller: 'documentCtrl'}) $routeProvider.when('/:server/:database/:collection/idx/:idx/query/:query', {templateUrl: 'partials/document.html', controller: 'documentCtrl'}) + $routeProvider.when('/:server/:database/:collection/idx/all/geojson/:key', {templateUrl: 'partials/geojsonmap.html', controller: 'geojsonmapCtrl'}) $routeProvider.when('/:server/:database/:collection/idx/:idx/geojson/:key', {templateUrl: 'partials/geojsonmap.html', controller: 'geojsonmapCtrl'}) + $routeProvider.when('/:server/:database/:collection/idx/all/query/:query/geojson/:key', {templateUrl: 'partials/geojsonmap.html', controller: 'geojsonmapCtrl'}) $routeProvider.when('/:server/:database/:collection/idx/:idx/query/:query/geojson/:key', {templateUrl: 'partials/geojsonmap.html', controller: 'geojsonmapCtrl'}) $routeProvider.otherwise({redirectTo: '/servers'}) diff --git a/client/partials/geojsonmap.html b/client/partials/geojsonmap.html index d43b45a..d2c5779 100644 --- a/client/partials/geojsonmap.html +++ b/client/partials/geojsonmap.html @@ -1,13 +1,25 @@ -
+
- {{idx}} / {{ meta.count}} + {{idx}} / {{ meta.count }}
+
+
+ Showing a data set of {{geojsonData.data.geometries.length}} datum. +
+
+
+
+ +
+
\ No newline at end of file diff --git a/main.go b/main.go index 1a9140f..9e44c8a 100644 --- a/main.go +++ b/main.go @@ -71,7 +71,11 @@ func serveApi(router *mux.Router) { router.Handle("/{server}/{database}/collections", ApiHandler(getAvailableCollections)) router.Handle("/{server}/{database}/{collection}/idx/{index:[0-9]+}", ApiHandler(getDocumentByIndex)) router.Handle("/{server}/{database}/{collection}/_id/{id}", ApiHandler(getDocumentById)) + router.Handle("/{server}/{database}/{collection}/all", ApiHandler(getAllDocuments)) router.Handle("/{server}/{database}/{collection}/query/{query}/idx/{index:[0-9]+}", ApiHandler(getDocumentByQueryAndIndex)) + router.Handle("/{server}/{database}/{collection}/query/{query}/all", ApiHandler(getAllDocuments)) + + // getAllDocuments } func createMuxRouter() http.Handler {