Exemple #1
0
func displayCountries(syncName string, conn redis.Conn, rewardIDs []string) {
	values, err := redis.Values(conn.Do("SMEMBERS", syncName))
	if err != nil {
		logrus.Fatal(err)
	}

	conn.Send("MULTI")

	for _, value := range values {
		b, ok := value.([]byte)
		if ok {
			orderID := string(b)
			conn.Send("HGETALL", syncName+"_order_"+orderID)
		}
	}

	values, err = redis.Values(conn.Do("EXEC"))
	if err != nil {
		logrus.Fatal(err)
	}

	countries := make(map[string]int)
	nbContributions := 0

	for _, value := range values {
		stringMap, _ := redis.StringMap(value, nil)

		nbItems, _ := strconv.Atoi(stringMap["nbItems"])
		if nbItems == 0 {
			continue
		}

		accept := true
		// filter reward ids
		if rewardIDs != nil && len(rewardIDs) > 0 {
			accept = false
			// HACK: considering there's only one
			// single item per order (id: 0)
			rewardID := stringMap["item0_product"]
			//logrus.Println(rewardID)

			// if rewardID == "177453" {
			// 	logrus.Printf("%#v", stringMap)
			// }

			for _, id := range rewardIDs {
				if id == rewardID {
					accept = true
					break
				}
			}
		}

		if accept {
			paymentStatus, _ := strconv.Atoi(stringMap["status"])
			accept = clientapi.OrderStatus(paymentStatus) == clientapi.OrderStatusPaymentDone ||
				clientapi.OrderStatus(paymentStatus) == clientapi.OrderStatusInvalid
		}

		if accept {
			//logrus.Println(stringMap["statusDisplay"])

			if _, exist := countries[stringMap["shippingCountry"]]; !exist {
				countries[stringMap["shippingCountry"]] = 1
			} else {
				countries[stringMap["shippingCountry"]]++
			}

			nbContributions++
		}
	}

	pl := make(PairList, len(countries))
	i := 0

	for country, count := range countries {
		if country == "" {
			country = "none"
		}
		// fmt.Println(country, ":", count)
		pl[i] = Pair{country, count}
		i++
	}

	sort.Sort(sort.Reverse(pl))

	for _, pair := range pl {
		fmt.Println(pair.Key, ":", pair.Value)
	}
	fmt.Println("contributions:", nbContributions)
}
Exemple #2
0
func exportPaidOrders(syncName string, conn redis.Conn, rewardIDs []string) {

	resp, err := http.Get("http://country.io/names.json")
	if err != nil {
		logrus.Fatal(err)
	}

	defer resp.Body.Close()

	jsonCountriesData, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		logrus.Fatal(err)
	}

	//countriesMap := make(map[string]interface{})
	countriesMap := make(map[string]string)

	err = json.Unmarshal([]byte(jsonCountriesData), &countriesMap)

	if err != nil {
		logrus.Fatal(err)
	}

	values, err := redis.Values(conn.Do("SMEMBERS", syncName))
	if err != nil {
		logrus.Fatal(err)
	}

	var file *xlsx.File
	var sheet *xlsx.Sheet

	var row *xlsx.Row
	var cell *xlsx.Cell

	file = xlsx.NewFile()
	sheet, err = file.AddSheet("Sheet1")
	if err != nil {
		logrus.Fatal(err)
	}

	// get invalid orders, to exclude them form export
	invalidOrders, errInvalidOrders := redis.Strings(redis.Values(conn.Do("SMEMBERS", syncName+"_invalidOrders")))
	if errInvalidOrders != nil {
		logrus.Fatal(errInvalidOrders)
	}
	// logrus.Println("invalidOrders:", invalidOrders)

	conn.Send("MULTI")

	// tmp
	nbEntries := 0
	nbPaymentInvalid := 0
	nbInvalid := 0

	for _, value := range values {
		b, ok := value.([]byte)
		if ok {
			orderID := string(b)
			conn.Send("HGETALL", syncName+"_order_"+orderID)
		}
	}

	values, err = redis.Values(conn.Do("EXEC"))
	if err != nil {
		logrus.Fatal(err)
	}

	for _, value := range values {
		stringMap, _ := redis.StringMap(value, nil)

		nbItems, _ := strconv.Atoi(stringMap["nbItems"])
		if nbItems == 0 {
			continue
		}

		accept := true
		// filter reward ids
		if rewardIDs != nil && len(rewardIDs) > 0 {
			accept = false
			// HACK: considering there's only one
			// single item per order (id: 0)
			rewardID := stringMap["item0_product"]
			//logrus.Println(rewardID)

			for _, id := range rewardIDs {
				if id == rewardID {
					accept = true
					break
				}
			}
		}

		if accept {
			paymentStatus, _ := strconv.Atoi(stringMap["status"])
			accept = clientapi.OrderStatus(paymentStatus) == clientapi.OrderStatusPaymentDone
			if !accept && clientapi.OrderStatus(paymentStatus) == clientapi.OrderStatusInvalid {
				nbPaymentInvalid++
			}
		}

		if accept {
			// invalid orders are the one that can't be sent because users made a mistake
			// in the shipping address (lefting one or more fields blank)
			accept = isValid(invalidOrders, stringMap["orderId"])
			if !accept {
				nbInvalid++
			}
		}

		if accept {

			//logrus.Println(stringMap["statusDisplay"])
			// logrus.Println(stringMap)

			// logrus.Println(stringMap["firstName"]+" "+stringMap["lastName"], "|",
			// 	stringMap["shippingAddr1"], "|",
			// 	stringMap["shippingAddr2"], "|",
			// 	stringMap["shippingCity"], "|",
			// 	stringMap["shippingCode"], "|",
			// 	stringMap["shippingCountry"], "|",
			// 	stringMap["email"], "|")

			row = sheet.AddRow()
			cell = row.AddCell()
			cell.Value = stringMap["firstName"] + " " + stringMap["lastName"]

			cell = row.AddCell()
			cell.Value = stringMap["shippingAddr1"]

			cell = row.AddCell()
			cell.Value = stringMap["shippingAddr2"]

			cell = row.AddCell()
			cell.Value = stringMap["shippingCity"]

			// state
			cell = row.AddCell()
			if stringMap["shippingCountry"] == "US" {
				cell.Value = stringMap["shippingState"]
			}

			cell = row.AddCell()
			cell.Value = stringMap["shippingCode"]

			// country name
			cell = row.AddCell()
			cell.Value = countriesMap[stringMap["shippingCountry"]]

			cell = row.AddCell()
			cell.Value = stringMap["shippingCountry"]

			// phone number
			cell = row.AddCell()
			cell.Value = stringMap["shippingPhoneNumber"]

			cell = row.AddCell()
			cell.Value = stringMap["email"]

			nbEntries++
			// if nbDisplayed >= 10 {
			// 	break
			// }
		}
	}

	fileName := "/data/" + syncName + ".xlsx"
	err = file.Save(fileName)
	if err != nil {
		logrus.Fatal(err)
	}

	logrus.Println(nbEntries, "entries written in", fileName)
	logrus.Println("(" + strconv.Itoa(nbPaymentInvalid) + " payment invalid)")
	logrus.Println("(" + strconv.Itoa(nbInvalid) + " invalid shipping addresses)")
}