Ejemplo n.º 1
0
func main() {
	visitsFile := flag.String("visits_file", "data/domain_visits_cache", "string")
	spreadsFile := flag.String("spreads_file", "data/domain_spread_cache", "string")
	numberOfProcess := flag.Int("process_num", 2, "int")
	flag.Parse()

	runtime.GOMAXPROCS(*numberOfProcess)

	dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
	if err != nil {
		log.Fatal(err)
	}

	file, err := os.Open(dir + "/" + *visitsFile)

	if err != nil {
		log.Fatal("File not found ", dir+"/"+*visitsFile)
	}

	defer file.Close()

	/*

		SOLUTION 1

	*/

	dataQueue := make(chan []string)
	facebookUniqueIds := make(map[string]*synced.IntSetSynced)
	facebookPageViews := new(synced.MapViewsSynced)
	facebookPageViews.Map = make(map[string]int64)

	waitGroup.Add(*numberOfProcess + 1)

	go file_processing.ReadFileSync(file, dataQueue, &waitGroup)

	/*

		Creating map[DATE]uniqueIds for Facebook, created whole unique ids map instead of just counts
		so it can be reused in second solution

		Creating facebookPageViews map with number of pageViews per day so we can calculate standard deviaton
		of page views per day

	*/

	for processId := 0; processId < *numberOfProcess; processId++ {
		go wakoopa.ProcessVisitsLine(dataQueue, facebookUniqueIds, facebookPageViews, &waitGroup)
	}

	waitGroup.Wait()

	uniqueIdsPerDay := make(map[string]int64)
	for date, uniqueIds := range facebookUniqueIds {
		uniqueIdsPerDay[date] = uniqueIds.Len()
		fmt.Printf("Date: %s, UniqueIds: %d \n", date, uniqueIdsPerDay[date])
	}

	/*

		SOLUTION 2

	*/

	waitGroup.Add(*numberOfProcess + 1)

	file, err = os.Open(dir + "/" + *spreadsFile)

	if err != nil {
		log.Fatal("File not found ", dir+"/"+*spreadsFile)
	}

	defer file.Close()

	dataQueue = make(chan []string)
	averageSecondsPerDay := make(map[string]float64)

	go file_processing.ReadFileSync(file, dataQueue, &waitGroup)

	/*

		Create map of average seconds spend on google.* between 20:00 and 23:00 and also visited facebook
		(reused facebook visits from first soltion)

	*/

	for processId := 0; processId < *numberOfProcess; processId++ {
		go wakoopa.ProcessSpreadsLine(dataQueue, facebookUniqueIds, averageSecondsPerDay, &waitGroup)
	}

	waitGroup.Wait()

	for date, seconds := range averageSecondsPerDay {
		fmt.Printf("Date: %s, AverageSecondsSpendOnGoogle: %f \n", date, seconds)
	}

	meanFacebookPageViews := statistics.GetMean(facebookPageViews.Map)
	std := statistics.GetStandardDeviation(facebookPageViews.Map, meanFacebookPageViews)
	fmt.Printf("Standard deviaton of pageViews on Facebook per day: %f \n", std)
}