<commit_before>package driver

import (
	"encoding/json"
	"log"
	"strings"
	"time"

	"github.com/Gujarats/API-Golang/util/logger"
	mgo "gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
	redis "gopkg.in/redis.v5"
)

type DriverData struct {
	Id       bson.ObjectId `json:"id" bson:"_id,omitempty"`
	Name     string        `json:"name"`
	Status   bool          `json:"status"`
	Location GeoJson       `json:"location"`
}

// struct for storing geo location in mongodb
type GeoJson struct {
	Type        string    `json:"type"`
	Coordinates []float64 `json:"coordinates"`
}

type City struct {
	Name string  `json:"name"`
	Lat  float64 `json:"lat"`
	Lon  float64 `json:"lon"`
}

// Global variable for storing database connection.
var mongo *mgo.Session
var redisConn *redis.Client

func (d *DriverData) GetConn(mongoSession *mgo.Session, redisConnection *redis.Client) {
	mongo = mongoSession
	redisConn = redisConnection
}

func init() {
	logger.InitLogger("Model Driver", "../../logs/", "Model.txt")
}

//===================REDIS====================//
func (d *DriverData) SaveDriversRedis(drivers []DriverData, city City) {
	savedTime := time.Now().Local().Format("01-02-2016")
	byteDrivers, _ := json.Marshal(drivers)
	byteCity, _ := json.Marshal(city)
	data := string(byteCity) + "++" + string(byteDrivers)

	// the key here is city;date;number
	redisConn.Set(city.Name+";"+savedTime, data, 0)
}

// the format of the key is : city;date
func (d *DriverData) DriversRedis(key string) (City, []DriverData) {
	var city City
	var drivers []DriverData

	dataString, _ := redisConn.Get(key).Result()
	if dataString == "" {
		log.Println("Drivers in redis nil")
		return city, drivers
	}

	// split data with ++
	dataSplit := strings.Split(dataString, "++")
	byteCity := []byte(dataSplit[0])
	byteDrivers := []byte(dataSplit[1])

	err := json.Unmarshal(byteCity, &city)
	if err != nil {
		log.Println(err)
	}
	err = json.Unmarshal(byteDrivers, &drivers)
	if err != nil {
		log.Println(err)
	}

	return city, drivers
}

//===================MongoDB====================//

func (d *DriverData) GetNearLocation(distance int64, lat, lon float64) []DriverData {
	collection := mongo.DB("Driver").C("driver")

	var driverLocation []DriverData
	err := collection.Find(bson.M{
		"location": bson.M{
			"$nearSphere": bson.M{
				"$geometry": bson.M{
					"type":        "Point",
					"coordinates": []float64{lon, lat},
				},
				"$maxDistance": distance,
			},
		},
		"status": true,
	}).Limit(21).All(&driverLocation)

	if err != nil {
		logger.CheckError("Mongo", err)
	}<commit_msg>increase limitation to get neareast driver<commit_after>package driver

import (
	"encoding/json"
	"log"
	"strings"
	"time"

	"github.com/Gujarats/API-Golang/util/logger"
	mgo "gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
	redis "gopkg.in/redis.v5"
)

type DriverData struct {
	Id       bson.ObjectId `json:"id" bson:"_id,omitempty"`
	Name     string        `json:"name"`
	Status   bool          `json:"status"`
	Location GeoJson       `json:"location"`
}

// struct for storing geo location in mongodb
type GeoJson struct {
	Type        string    `json:"type"`
	Coordinates []float64 `json:"coordinates"`
}

type City struct {
	Name string  `json:"name"`
	Lat  float64 `json:"lat"`
	Lon  float64 `json:"lon"`
}

// Global variable for storing database connection.
var mongo *mgo.Session
var redisConn *redis.Client

func (d *DriverData) GetConn(mongoSession *mgo.Session, redisConnection *redis.Client) {
	mongo = mongoSession
	redisConn = redisConnection
}

func init() {
	logger.InitLogger("Model Driver", "../../logs/", "Model.txt")
}

//===================REDIS====================//
func (d *DriverData) SaveDriversRedis(drivers []DriverData, city City) {
	savedTime := time.Now().Local().Format("01-02-2016")
	byteDrivers, _ := json.Marshal(drivers)
	byteCity, _ := json.Marshal(city)
	data := string(byteCity) + "++" + string(byteDrivers)

	// the key here is city;date;number
	redisConn.Set(city.Name+";"+savedTime, data, 0)
}

// the format of the key is : city;date
func (d *DriverData) DriversRedis(key string) (City, []DriverData) {
	var city City
	var drivers []DriverData

	dataString, _ := redisConn.Get(key).Result()
	if dataString == "" {
		log.Println("Drivers in redis nil")
		return city, drivers
	}

	// split data with ++
	dataSplit := strings.Split(dataString, "++")
	byteCity := []byte(dataSplit[0])
	byteDrivers := []byte(dataSplit[1])

	err := json.Unmarshal(byteCity, &city)
	if err != nil {
		log.Println(err)
	}
	err = json.Unmarshal(byteDrivers, &drivers)
	if err != nil {
		log.Println(err)
	}

	return city, drivers
}

//===================MongoDB====================//

func (d *DriverData) GetNearLocation(distance int64, lat, lon float64) []DriverData {
	collection := mongo.DB("Driver").C("driver")

	var driverLocation []DriverData
	err := collection.Find(bson.M{
		"location": bson.M{
			"$nearSphere": bson.M{
				"$geometry": bson.M{
					"type":        "Point",
					"coordinates": []float64{lon, lat},
				},
				"$maxDistance": distance,
			},
		},
		"status": true,
	}).Limit(100).All(&driverLocation)

	if err != nil {
		logger.CheckError("Mongo", err)
	}
{'fraction_non_alphanumeric': 0.18783888458559256, 'fraction_numerical': 0.011037955073586367, 'mean_word_length': 4.053816046966732, 'pattern_counts': {'":': 14, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2188884', 'n_tokens_mistral': 1979, 'n_tokens_neox': 1827, 'n_words': 417}