func CreateAnalyticsRecord() analytics.AnalyticsRecord {
	a := analytics.AnalyticsRecord{}
	a.Method = "POST"
	a.Path = "/v1/resource"
	a.ContentLength = 123
	a.UserAgent = "Test User Agent"
	a.Day = 1
	a.Month = time.January
	a.Year = 2016
	a.Hour = 14
	a.ResponseCode = 202
	a.APIKey = "APIKEY123"
	a.TimeStamp = time.Now()
	a.APIVersion = "1"
	a.APIName = "Test API"
	a.APIID = "API123"
	a.OrgID = "ORG123"
	a.OauthID = "Oauth123"
	a.RequestTime = time.Now().Unix()
	a.RawRequest = "{\"field\": \"value\"}"
	a.RawResponse = "{\"id\": \"123\"}"
	//a.IPAddress = "192.168.99.100"
	a.Tags = []string{"tag-1", "tag-2"}
	a.ExpireAt = time.Date(2020, time.November, 10, 23, 0, 0, 0, time.UTC)

	return a
}
Beispiel #2
0
func (c *CSVPump) WriteData(data []interface{}) error {
	curtime := time.Now()
	fname := fmt.Sprintf("%d-%s-%d-%d.csv", curtime.Year(), curtime.Month().String(), curtime.Day(), curtime.Hour())
	fname = path.Join(c.csvConf.CSVDir, fname)

	var outfile *os.File
	var appendHeader bool

	if _, err := os.Stat(fname); os.IsNotExist(err) {
		var createErr error
		outfile, createErr = os.Create(fname)
		if createErr != nil {
			log.WithFields(logrus.Fields{
				"prefix": csvPrefix,
			}).Error("Failed to create new CSV file: ", createErr)
		}
		appendHeader = true
	} else {
		var appendErr error
		outfile, appendErr = os.OpenFile(fname, os.O_APPEND|os.O_WRONLY, 0600)
		if appendErr != nil {
			log.WithFields(logrus.Fields{
				"prefix": csvPrefix,
			}).Error("Failed to open CSV file: ", appendErr)
		}
	}

	defer outfile.Close()
	writer := csv.NewWriter(outfile)

	if appendHeader {
		startRecord := analytics.AnalyticsRecord{}
		var headers = startRecord.GetFieldNames()

		err := writer.Write(headers)
		if err != nil {
			log.WithFields(logrus.Fields{
				"prefix": csvPrefix,
			}).Error("Failed to write file headers: ", err)
			return err

		}
	}

	for _, v := range data {
		decoded := v.(analytics.AnalyticsRecord)

		toWrite := decoded.GetLineValues()
		// toWrite := []string{
		// 	decoded.Method,
		// 	decoded.Path,
		// 	strconv.FormatInt(decoded.ContentLength, 10),
		// 	decoded.UserAgent,
		// 	strconv.Itoa(decoded.Day),
		// 	decoded.Month.String(),
		// 	strconv.Itoa(decoded.Year),
		// 	strconv.Itoa(decoded.Hour),
		// 	strconv.Itoa(decoded.ResponseCode),
		// 	decoded.APIName,
		// 	decoded.APIVersion}
		err := writer.Write(toWrite)
		if err != nil {
			log.Error("File write failed!")
			log.Error(err)
		}

	}
	writer.Flush()
	return nil
}