//For every whisper file, maps whisper data points to TSM data points for
//given time range, this is just mapping points from one Data structure to other
// not writing to files
func (migrationData *MigrationData) MapWSPToTSMByWhisperFile(from time.Time, until time.Time) []TsmPoint {
	var tsmPoints []TsmPoint
	var tsmPoint TsmPoint

	for _, wspFile := range migrationData.wspFiles {
		w, err := whisper.Open(wspFile)
		fmt.Println("Migrating Data From ", wspFile, "For TimeRange ", from, until, "Size", w.Header.Archives[0].Size())
		if err != nil {
			log.Fatal(err)
		}

		//the first argument is interval, since it's not required for migration
		//using _
		_, wspPoints, err := w.FetchUntilTime(from, until)
		w.Close()
		if err != nil {
			log.Fatal(err)
		}

		if len(wspPoints) == 0 {
			continue
		}
		var tagConfig *TagConfig
		var mtf *MTF
		if mtf = migrationData.GetMTF(wspFile); mtf != nil {
		} else { //Create and add the pattern
			for {
				tagConfig = NewConfig(wspFile)
				if tagConfig != nil {
					break
				}
			}
			migrationData.tagConfigs = append(migrationData.tagConfigs, *tagConfig)

			mtf = &MTF{Measurement: tagConfig.Measurement, Tags: tagConfig.Tags,
				Field: tagConfig.Field}
		}

		tsmPoint.key = CreateTSMKey(mtf)
		tsmPoint.values = make([]tsm1.Value, len(wspPoints))
		for j, wspPoint := range wspPoints {
			tsmPoint.values[j] = tsm1.NewValue(int64(wspPoint.Timestamp), wspPoint.Value)
		}
		tsmPoints = append(tsmPoints, tsmPoint)
	}
	return tsmPoints
}
func (migrationData *MigrationData) GetWhisperInfo() error {
	for _, wspFile := range migrationData.wspFiles {
		w, err := whisper.Open(wspFile)
		if err != nil {
			return err
		}
		t, err := w.GetOldest()
		if err != nil {
			return err
		}
		_, pts, err := w.Fetch(t)
		if err != nil {
			return err
		}
		fmt.Printf("Whisper File : %s\n", wspFile)
		fmt.Println("Oldest Data in File : ", time.Unix(int64(t), 0))
		fmt.Println("Number of whisper points : ", len(pts))
		fmt.Println("-----------------------------------------------------------------------")
		w.Close()
	}
	return nil
}
func (migrationData *MigrationData) WriteUsingV2() {
	from := migrationData.from
	until := migrationData.until
	c, _ := client.NewHTTPClient(client.HTTPConfig{
		Addr:     migrationData.host + ":" + migrationData.port,
		Username: migrationData.username,
		Password: migrationData.password,
	})
	defer c.Close()

	createDBString := fmt.Sprintf("Create Database %v", migrationData.dbName)
	createDBQuery := client.NewQuery(createDBString, "", "")
	_, err := c.Query(createDBQuery)
	if err != nil {
		fmt.Println(err)
		return
	}

	var bp client.BatchPoints
	bp, _ = client.NewBatchPoints(client.BatchPointsConfig{
		Database:  migrationData.dbName,
		Precision: "s",
	})

	for _, wspFile := range migrationData.wspFiles {
		w, err := whisper.Open(wspFile)
		if err != nil {
			log.Fatal(err)
		}
		fmt.Println("Migrating Data From ", wspFile, "For TimeRange ", from, until, "Size", w.Header.Archives[0].Size())
		//the first return argument is interval, since it's not required for migration
		//using _
		_, wspPoints, err := w.FetchUntilTime(from, until)
		w.Close()
		if err != nil {
			log.Fatal(err)
		}

		if len(wspPoints) == 0 {
			continue
		}
		var tagConfig *TagConfig
		var mtf *MTF
		if mtf = migrationData.GetMTF(wspFile); mtf != nil {
		} else { //Create and add the pattern
			for {
				tagConfig = NewConfig(wspFile)
				if tagConfig != nil {
					break
				}
			}
			migrationData.tagConfigs = append(migrationData.tagConfigs, *tagConfig)

			mtf = &MTF{Measurement: tagConfig.Measurement, Tags: tagConfig.Tags,
				Field: tagConfig.Field}
		}

		var tags map[string]string
		tags = make(map[string]string)
		for _, tagConfigTag := range mtf.Tags {
			tags[tagConfigTag.Tagkey] = tagConfigTag.Tagvalue
		}
		var fields map[string]interface{}
		fields = make(map[string]interface{})

		for _, wspPoint := range wspPoints {
			fields[mtf.Field] = wspPoint.Value
			pt, _ := client.NewPoint(mtf.Measurement, tags, fields,
				time.Unix(int64(wspPoint.Timestamp), 0))
			bp.AddPoint(pt)
		}
		c.Write(bp)
	}
	return
}