示例#1
0
文件: endpoints.go 项目: catanm/gms
func GetVideos(ctx context.Context) error {
	var result []m.Video

	// Get the info from the context
	fromDate, fromDateExists := utils.ParseDateFromContext("from_", ctx)
	toDate, toDateExists := utils.ParseDateFromContext("to_", ctx)

	limit, err := strconv.Atoi(ctx.FormValue("limit"))
	if err != nil {
		limit = 5
	}

	skip, err := strconv.Atoi(ctx.FormValue("skip"))
	if err != nil {
		skip = 0
	}

	var currentUser, ok = ctx.Data()["user"].(m.User)
	isPersonal, err := strconv.ParseBool(ctx.FormValue("personal"))
	if err != nil {
		isPersonal = false
	}

	query := bson.M{}
	if ok && isPersonal {
		if fromDateExists || toDateExists {
			query["date"] = bson.M{"$gte": fromDate, "$lte": toDate}
			query["user"] = currentUser.Id
		} else {
			query["user"] = currentUser.Id
		}
	} else {
		isPersonal = false
		if fromDateExists || toDateExists {
			query["date"] = bson.M{"$gte": fromDate, "$lte": toDate}
		}
	}

	if !fromDateExists && !toDateExists {
		var lastVideo m.Image
		var oneQuery = bson.M{}
		if isPersonal {
			oneQuery["user"] = currentUser.Id
		}
		if err = m.GetDB("Video").Find(oneQuery).Sort("-date").One(&lastVideo); err == nil {
			query["date"] = bson.M{"$gte": lastVideo.Date.AddDate(0, 0, -1), "$lte": lastVideo.Date.AddDate(0, 0, 1)}
		}
	}

	if err := m.GetDB("Video").Find(query).Skip(skip).Limit(limit).All(&result); err != nil {
		log.Error("Failed to search for tracks: " + err.Error())
		return goweb.API.Respond(ctx, 200, nil, []string{"Failed to find the tracks."})
	}

	return goweb.API.RespondWithData(ctx, result)
}
示例#2
0
文件: endpoints.go 项目: catanm/gms
func GetStats(ctx context.Context) error {
	var err error
	var pageType = ctx.FormValue("page")
	query := bson.M{}
	//barchartQuery := bson.M{}

	var totalFileSize int64 = 0

	var imageCount int = 0

	if pageType == "pstats" {

		var currentUser, ok = ctx.Data()["user"].(m.User)
		if ok {
			fmt.Println("USER OK")
			query["user"] = currentUser.Id
			//barchartQuery["user"] = currentUser.Id

			var identifier = currentUser.Username
			if identifier == "" {
				identifier = currentUser.Email
			}

			var dirs []os.FileInfo
			dirs, err = ioutil.ReadDir("../uploads/" + identifier)
			if err != nil {
				log.Error("Failed to read dir:" + err.Error())
			}
			fmt.Println("ID:" + identifier)
			for i := 0; i < len(dirs); i++ {
				totalFileSize += dirs[i].Size()
			}
			imageCount = len(dirs)
		} else {
			return goweb.API.Respond(ctx, 200, nil, []string{"Please log in to view your stats."})
		}
	} else {
		var subdirInfo []os.FileInfo
		var dirInfo []os.FileInfo

		dirInfo, err = ioutil.ReadDir("../uploads/")
		if err != nil {
			log.Error("Failed to read dir:" + err.Error())
		}

		for i := 0; i < len(dirInfo); i++ {
			if dirInfo[i].IsDir() {
				subdirInfo, err = ioutil.ReadDir("../uploads/" + dirInfo[i].Name())
				if err != nil {
					log.Error("Failed to read dir:" + err.Error())
				}
				for j := 0; j < len(subdirInfo); j++ {
					totalFileSize += subdirInfo[j].Size()
				}
				imageCount += len(subdirInfo)
			}
		}
	}

	// convert to MB
	var fileSizeInMB = strconv.FormatFloat((float64(totalFileSize) / 1000000), 'f', 2, 32)
	fmt.Println(fileSizeInMB)

	// Get the info from the context
	fromDate, fromDateExists := utils.ParseDateFromContext("from_", ctx)
	toDate, toDateExists := utils.ParseDateFromContext("to_", ctx)

	//Total number of users
	userCount, err := m.GetDB("User").Count()
	if err != nil {
		log.Error("Failed to find user count: " + err.Error())
	}

	//Total number of tracks
	trailCount, err := m.GetDB("Track").Find(query).Count()
	if err != nil {
		log.Error("Failed to find track count: " + err.Error())
	}

	// Query add date/time
	if fromDateExists || toDateExists {
		query["date"] = bson.M{"$gte": fromDate, "$lte": toDate}
	}

	//Filtered images and tracks by date/time and user
	filteredImageCount, err := m.GetDB("Image").Find(query).Count()
	if err != nil {
		log.Error("Failed to find image count: " + err.Error())
	}
	filteredTrackCount, err := m.GetDB("Track").Find(query).Count()
	if err != nil {
		log.Error("Failed to find track count: " + err.Error())
	}

	fmt.Print("FILTERED IMAGE AND TRACK COUNT: ")
	fmt.Print(filteredImageCount)
	fmt.Print(" ")
	fmt.Print(filteredTrackCount)

	fmt.Println("")
	fmt.Println("")

	// Barchart number of images
	var dDay, dMonth, dYear = getDifference(fromDate, toDate)

	var imageCurrentCount int
	var imageCountBarchartValues []int
	var gpsCurrentCount int
	var gpsCountBarchartValues []int
	var currentLabel string
	var barchartLabels []string

	var nextDate, endNextDate time.Time

	var February = leapYearDays(fromDate.Year())
	var months = [13]int{0, 31, February, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}

	if dYear < 1 {
		if dMonth < 1 { // days view

			for i := 0; i <= dDay; i++ {

				if i == 0 {
					nextDate, endNextDate = getDates(fromDate, 0, 0)
				}
				query["date"] = bson.M{"$gte": nextDate, "$lte": endNextDate}

				imageCurrentCount, err = m.GetDB("Image").Find(query).Count()
				if err != nil {
					log.Error("Failed to find image count: " + err.Error())
				}
				imageCountBarchartValues = append(imageCountBarchartValues, imageCurrentCount)

				gpsCurrentCount, err = m.GetDB("Track").Find(query).Count()
				if err != nil {
					log.Error("Failed to find gps count: " + err.Error())
				}
				gpsCountBarchartValues = append(gpsCountBarchartValues, gpsCurrentCount)

				currentLabel = strconv.Itoa(nextDate.Day()) + " " + nextDate.Month().String() + " " + strconv.Itoa(nextDate.Year())
				barchartLabels = append(barchartLabels, currentLabel)

				nextDate, endNextDate = getDates(nextDate.AddDate(0, 0, 1), 0, 0)
			}

		} else { // months view

			for i := 0; i <= dMonth; i++ {

				if i == 0 { //first third
					nextDate, endNextDate = getDates(fromDate, 0, months[int(fromDate.Month())])

				} else if i == dMonth { //last third
					nextDate = nextDate.AddDate(0, 1, 1-nextDate.Day())
					nextDate, endNextDate = getDates(nextDate, 0, toDate.Day())

				} else { // everything else from the beginning to the end of month
					nextDate = nextDate.AddDate(0, 1, 1-nextDate.Day())
					mEnd := months[int(nextDate.Month())]
					nextDate, endNextDate = getDates(nextDate, 0, mEnd)
				}

				query["date"] = bson.M{"$gte": nextDate, "$lte": endNextDate}

				imageCurrentCount, err = m.GetDB("Image").Find(query).Count()
				if err != nil {
					log.Error("Failed to find image count: " + err.Error())
				}
				imageCountBarchartValues = append(imageCountBarchartValues, imageCurrentCount)

				gpsCurrentCount, err = m.GetDB("Track").Find(query).Count()
				if err != nil {
					log.Error("Failed to find gps count: " + err.Error())
				}
				gpsCountBarchartValues = append(gpsCountBarchartValues, gpsCurrentCount)

				currentLabel = nextDate.Month().String() + " " + strconv.Itoa(nextDate.Year())
				barchartLabels = append(barchartLabels, currentLabel)

			}
		}

	} else { // years view

		for i := 0; i <= dYear; i++ {

			if i == 0 { //first third
				nextDate, endNextDate = getDates(fromDate, 12, months[int(fromDate.Month())]) //31?

			} else if i == dMonth { //last third
				nextDate = nextDate.AddDate(1, 1-int(nextDate.Month()), 1-nextDate.Day())
				nextDate, endNextDate = getDates(nextDate, int(toDate.Month()), toDate.Day())

			} else { // everything else from the beginning to the end of year
				nextDate = nextDate.AddDate(1, 1-int(nextDate.Month()), 1-nextDate.Day())
				nextDate, endNextDate = getDates(nextDate, 12, months[int(nextDate.Month())]) //31?
			}

			query["date"] = bson.M{"$gte": nextDate, "$lte": endNextDate}

			imageCurrentCount, err = m.GetDB("Image").Find(query).Count()
			if err != nil {
				log.Error("Failed to find image count: " + err.Error())
			}
			imageCountBarchartValues = append(imageCountBarchartValues, imageCurrentCount)

			gpsCurrentCount, err = m.GetDB("Track").Find(query).Count()
			if err != nil {
				log.Error("Failed to find gps count: " + err.Error())
			}
			gpsCountBarchartValues = append(gpsCountBarchartValues, gpsCurrentCount)

			currentLabel = nextDate.Month().String() + " " + strconv.Itoa(nextDate.Year())
			barchartLabels = append(barchartLabels, currentLabel)
		}

	}

	var stats = []int{userCount, imageCount, trailCount, filteredImageCount, filteredTrackCount}
	var result []interface{} = []interface{}{imageCountBarchartValues, barchartLabels, gpsCountBarchartValues, stats, fileSizeInMB}

	return goweb.API.RespondWithData(ctx, result)
}
示例#3
0
文件: endpoints.go 项目: catanm/gms
func GetTrails(ctx context.Context) error {
	var result []m.Track

	// Get the info from the context
	isHeatmap, err := strconv.ParseBool(ctx.FormValue("heatmap"))
	if err != nil {
		isHeatmap = false
	}

	isPersonal, err := strconv.ParseBool(ctx.FormValue("personal"))
	if err != nil {
		isPersonal = false
	}

	fromDate, fromDateExists := utils.ParseDateFromContext("from_", ctx)
	toDate, toDateExists := utils.ParseDateFromContext("to_", ctx)

	if !isPersonal {
		err = m.GetDB("Track").Find(bson.M{}).All(&result)

	} else if isPersonal {
		var query bson.M
		var currentUser, ok = ctx.Data()["user"].(m.User)
		if ok && !fromDateExists && !toDateExists {
			query = bson.M{}
			query["user"] = currentUser.Id
			err = m.GetDB("Track").Find(query).All(&result)

		} else if ok && fromDateExists && toDateExists {
			query = bson.M{"user": currentUser.Id, "date": bson.M{"$gte": fromDate, "$lte": toDate}}
			err = m.GetDB("Track").Find(query).All(&result)

		}
	}
	if !isHeatmap {
		mapName := "GpsTrails"

		var buffer bytes.Buffer

		buffer.WriteString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<kml xmlns=\"http://earth.google.com/kml/2.0\">\n<Document>\n<name>")
		buffer.WriteString(mapName)
		buffer.WriteString("</name>\n<description>Glasgow Memories Server</description>\n")
		for i := 0; i < len(color); i++ {

			buffer.WriteString("<Style id=\"line")
			buffer.WriteString(strconv.Itoa(i + 1))
			buffer.WriteString("\">\n<LineStyle>\n<color>")
			buffer.WriteString(color[i%len(color)])
			buffer.WriteString("</color>\n<width>4</width>\n</LineStyle>\n</Style>\n\n")
		}

		for i := 0; i < len(result); i++ {
			buffer.WriteString("<Placemark>\n<name>Track ")
			buffer.WriteString(result[i].Date.String())
			buffer.WriteString("</name>\n<styleUrl>#line")
			buffer.WriteString(strconv.Itoa(utils.GetStyle(strings.Split(result[i].Date.String()[0:10], "-"), len(color)) + 1))
			buffer.WriteString("</styleUrl>\n<LineString>\n<altitudeMode>relative</altitudeMode>\n<coordinates>\n")
			for j := 0; j < len(result[i].Coordinates); j++ {
				buffer.WriteString(result[i].Coordinates[j].Lon + "," + result[i].Coordinates[j].Lat + ",0\n")
			}
			buffer.WriteString("</coordinates>\n</LineString>\n</Placemark>\n")
		}
		buffer.WriteString("</Document>\n")
		buffer.WriteString("</kml>\n")
		ctx.HttpResponseWriter().Header().Set("Content-Type", "application/vnd.google-earth.kml+xml")
		ctx.HttpResponseWriter().Write([]byte(buffer.String()))
		return nil
		// return goweb.Respond.With(ctx, http.StatusOK, []byte(buffer.String()))
	} else {
		var formattedData [][]string
		for i := 0; i < len(result); i++ {
			for j := 0; j < len(result[i].Coordinates); j++ {
				formattedData = utils.Append(formattedData, []string{result[i].Coordinates[j].Lat, result[i].Coordinates[j].Lon})
			}
		}
		return goweb.API.RespondWithData(ctx, formattedData)
	}
}
示例#4
0
文件: endpoints.go 项目: catanm/gms
func GetImages(ctx context.Context) error {
	var result []m.Image

	// Get the info from the context
	fromDate, fromDateExists := utils.ParseDateFromContext("from_", ctx)
	toDate, toDateExists := utils.ParseDateFromContext("to_", ctx)

	searchType := ctx.FormValue("upload")
	if searchType != "" {
		searchType = "uploaded"
	} else {
		searchType = "date"
	}

	limit, err := strconv.Atoi(ctx.FormValue("limit"))
	if err != nil {
		limit = 20
	}

	skip, err := strconv.Atoi(ctx.FormValue("skip"))
	if err != nil {
		skip = 0
	}

	var currentUser, ok = ctx.Data()["user"].(m.User)
	isPersonal, err := strconv.ParseBool(ctx.FormValue("personal"))
	if err != nil {
		isPersonal = false
	}
	query := bson.M{}
	if ok && isPersonal {
		if fromDateExists || toDateExists {
			query[searchType] = bson.M{"$gte": fromDate, "$lte": toDate}
			query["user"] = currentUser.Id
		} else {
			query["user"] = currentUser.Id
		}
	} else {
		isPersonal = false
		if fromDateExists || toDateExists {
			query[searchType] = bson.M{"$gte": fromDate, "$lte": toDate}
		}
	}

	addLocation := true
	var lat, long, radius float64
	rad, err := strconv.Atoi(ctx.FormValue("radius"))
	if err != nil {
		addLocation = false
	}
	if addLocation {
		radius = float64(rad)
		lat, err = strconv.ParseFloat(ctx.FormValue("lat"), 64)
		if err != nil {
			addLocation = false
		}
		long, err = strconv.ParseFloat(ctx.FormValue("log"), 64)
		if err != nil {
			addLocation = false
		}
	}

	if addLocation {
		query["lat"] = bson.M{"$gt": lat - radius, "$lt": lat + radius}
		query["log"] = bson.M{"$gt": long - radius, "$lt": long + radius}
	}

	showAll := ctx.FormValue("allImages")
	if showAll == "" {
		query["show"] = true
	}
	keyMoments := ctx.FormValue("keyMoments")
	if keyMoments != "" {
		query["processed"] = true
	}

	if err = m.GetDB("Image").Find(query).Sort("-" + searchType).Skip(skip).Limit(limit).All(&result); err != nil {
		log.Error("Failed to search for images: " + err.Error())
		return goweb.API.Respond(ctx, 200, nil, []string{"Failed to find the images."})
	}
	return goweb.API.RespondWithData(ctx, result)
}