Beispiel #1
0
func (rc *ResourceController) GetResources(ctx *gin.Context) {
	req := ctx.Request
	resourceType := getResourceType(req.URL)

	logger.Log.WithFields(
		logrus.Fields{"resource type": resourceType}).Info("GetResources")

	resources := ptm_models.NewSliceForResourceName(resourceType, 0, 0)
	c := rc.Database().C(ptm_models.GetCollectionName(resourceType))
	// retrieve all documents in the collection
	// TODO Restrict this to resource type, just to be extra safe
	query := buildSearchQuery(resourceType, ctx)
	logger.Log.WithFields(
		logrus.Fields{"query": query}).Info("GetResources")
	err := c.Find(query).All(resources)
	if err != nil {
		if err == mgo.ErrNotFound {
			ctx.String(http.StatusNotFound, "Not Found")
			ctx.Abort()
			return
		} else {
			ctx.AbortWithError(http.StatusBadRequest, err)
			return
		}
	}

	ctx.JSON(http.StatusOK, resources)
}
Beispiel #2
0
func GetRecordMatchRunMetricsHandler(provider func() *mgo.Database) gin.HandlerFunc {
	return func(ctx *gin.Context) {
		resourceType := "RecordMatchRun"

		recordMatchSystemInterfaceId := ctx.Query("recordMatchSystemInterfaceId")
		validRecordMatchSystemInterfaceId := len(recordMatchSystemInterfaceId) > 1 && len(recordMatchSystemInterfaceId) <= 24 && bson.IsObjectIdHex(recordMatchSystemInterfaceId)
		recordSetId := ctx.Query("recordSetId")
		validRecordSetId := len(recordSetId) > 1 && len(recordSetId) <= 24 && bson.IsObjectIdHex(recordSetId)

		logger.Log.WithFields(
			logrus.Fields{"resource type": resourceType,
				"rec match sys": recordMatchSystemInterfaceId,
				"record set":    recordSetId}).Info("GetRecordMatchRunMetrics")

		resources := ptm_models.NewSliceForResourceName(resourceType, 0, 0)
		c := provider().C(ptm_models.GetCollectionName(resourceType))

		var query *mgo.Query

		if validRecordSetId {
			logger.Log.WithFields(
				// find the record match runs with masterRecordSetId or queryRecordSetId == record set id
				logrus.Fields{"validRecord Set Id": validRecordSetId, "record set": recordSetId}).Info("GetRecordMatchRunMetrics")

			recordSetBsonID, _ := ptm_models.ToBsonObjectID(recordSetId)
			query = c.Find(bson.M{"$or": []bson.M{bson.M{"masterRecordSetId": recordSetBsonID}, bson.M{"queryRecordSetId": recordSetBsonID}}})

		} else if validRecordMatchSystemInterfaceId {
			recordMatchSystemInterfaceBsonId, _ := ptm_models.ToBsonObjectID(recordMatchSystemInterfaceId)
			query = c.Find(bson.M{"recordMatchSystemInterfaceId": recordMatchSystemInterfaceBsonId})

		} else { // no query parameters were provided
			// get all record runs with, primarily, metrics only
			// retrieve all documents in the collection
			// TODO Restrict this to resourc type, just to be extra safe
			query = c.Find(bson.M{})
		}

		// constrain which fields are returned
		err := query.Select(bson.M{"meta": 1, "metrics": 1,
			"recordMatchSystemInterfaceId": 1, "matchingMode": 1,
			"recordResourceType": 1, "masterRecordSetId": 1, "queryRecordSetId": 1,
			"recordMatchContextId": 1}).All(resources)

		if err != nil {
			if err == mgo.ErrNotFound {
				ctx.String(http.StatusNotFound, "Not Found")
				ctx.Abort()
				return
			}
			ctx.AbortWithError(http.StatusBadRequest, err)
		}

		ctx.JSON(http.StatusOK, resources)
	}
}