Esempio n. 1
0
func writePromptInText(pId string, pc PromptConfig, writer *csv.Writer, level int, evalue ExpectedResponseValue) {
	indent := ""
	pPromptType := ""
	for i := 0; i < level; i++ {
		indent = indent + "    "
	}
	if pId == "" {
		pId = pc.Id
		pPromptType = pc.PromptType
	}
	pId = indent + pId
	pText := pc.Text
	supportPromptId := pc.SupportPromptRef.Id
	var value = []string{strconv.Itoa(level), pId, pPromptType, evalue.Id, supportPromptId, "", "", ""}
	for i, v := range pText {
		value[5+i] = v
	}
	err := writer.Write(value)
	if err != nil {
		log.Fatal("Cannot write file", err)
	}
	level++
	ecs := pc.ExpectedResponses.Values
	for _, v := range ecs {
		writePromptInText(v.NextPromptRef.Id, v.NextPrompt, writer, level, v)
	}
}
func writeOneRecordAtaTime(writer *csv.Writer, data [][]string) {

	for _, row := range data {
		writer.Write(row)
	}
	writer.Flush()
}
Esempio n. 3
0
func (p *csvProcessor) flush(writer *csv.Writer, err error) error {
	if err != nil {
		return err
	}
	writer.Flush()
	return writer.Error()
}
func writeSalesData(conn *sql.DB, writer *csv.Writer, first int, last int, wg *sync.WaitGroup) {
	defer wg.Done()

	rows, err := conn.Query("select * from sales where id between ? and ?", first, last)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	var orderId int
	var userId int
	var orderAmount float64

	for rows.Next() {
		rows.Scan(&orderId, &userId, &orderAmount)

		writer.Write([]string{
			strconv.Itoa(orderId),
			strconv.Itoa(userId),
			strconv.FormatFloat(orderAmount, 'f', 6, 64),
		},
		)
	}

	writer.Flush()
}
Esempio n. 5
0
func csvExporter(c *csv.Writer, ms []*Metric) {
	lc := make(chan *LabelSet)
	quit := make(chan bool)
	for _, m := range ms {
		go m.EmitLabelSets(lc, quit)
		for {
			select {
			case l := <-lc:
				record := []string{m.Program,
					m.Name,
					m.Kind.String()}
				for k, v := range l.labels {
					record = append(record, k, v)
				}
				record = append(record, fmt.Sprintf("%s", l.datum.Time))
				record = append(record, fmt.Sprintf("%d", l.datum.Get()))
				err := c.Write(record)
				if err != nil {
					log.Printf("Failed to write csv record %q: %s\n", record, err)
				}
			case <-quit:
				goto next
			}
		}
	next:
	}
}
Esempio n. 6
0
func writeArrayOfScalars(writer *csv.Writer, vj reflect.Value) error {
	sa := make([]string, vj.Len())
	for i := 0; i < vj.Len(); i++ {
		sa[i] = fmt.Sprintf("%v", reflect.Indirect(vj.Index(i)))
	}
	return writer.Write(sa)
}
Esempio n. 7
0
func writeStructFields(writer *csv.Writer, str reflect.Value, dataModel interface{}) error {
	sa := make([]string, str.NumField())
	for i := 0; i < str.NumField(); i++ {
		sa[i] = fmt.Sprintf("%v", reflect.Indirect(str.Field(i)))
	}
	return writer.Write(sa)
}
Esempio n. 8
0
// NewWithCsvWriter writes all of the Rows in a Table to a CSV file using the options in the CSV writer.
// It assumes that all Rows have the same headers. Columns are written in alphabetical order.
func NewWithCsvWriter(writer *csv.Writer) optimus.Sink {
	return func(source optimus.Table) error {
		defer source.Stop()
		headers := []string{}
		wroteHeader := false
		for row := range source.Rows() {
			if !wroteHeader {
				headers = convertRowToHeader(row)
				sort.Strings(headers)
				if err := writer.Write(headers); err != nil {
					return err
				}
				wroteHeader = true
			}
			if err := writer.Write(convertRowToRecord(row, headers)); err != nil {
				return err
			}
		}
		if source.Err() != nil {
			return source.Err()
		}
		writer.Flush()
		if writer.Error() != nil {
			return writer.Error()
		}
		return nil
	}
}
Esempio n. 9
0
func NewAppend(path string) *kvAppend {
	kv := &kvAppend{
		kvMem: NewMemory(),
		queue: make(chan *record),
	}

	var lastErr error

	f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0644)
	if err != nil {
		lastErr = err
	} else {
		r := csv.NewReader(f)
		for {
			rec, err := r.Read()
			if err != nil {
				if err != io.EOF {
					lastErr = err
				}
				break
			}
			kv.kvMem.m[rec[0]] = []byte(rec[1])
		}
	}

	var w *csv.Writer
	if lastErr == nil {
		w = csv.NewWriter(f)
	}

	go func() {
		for r := range kv.queue {
			if lastErr == nil {
				var rec []string
				if r.Value != nil {
					rec = []string{r.Key, string(r.Value)}
				} else {
					rec = []string{r.Key}
				}
				if err = w.Write(rec); err != nil {
					lastErr = err
				}
				if r != nil && r.Err != nil {
					w.Flush()
					f.Sync()
					r.Err <- nil
					close(r.Err)
				}
			} else if r.Err != nil {
				r.Err <- lastErr
			}
		}
		if f != nil {
			f.Close()
		}
	}()

	return kv
}
Esempio n. 10
0
func writeArrayOfStringers(writer *csv.Writer, value reflect.Value) error {
	debug("        -- writeArrayOfStringers %d\n", value.Len())
	sa := make([]string, value.Len())
	for i := 0; i < value.Len(); i++ {
		sa[i] = fmt.Sprintf("%v", reflect.Indirect(value.Index(i)).Interface().(fmt.Stringer))
	}
	return writer.Write(sa)
}
Esempio n. 11
0
func processResults(config *Config, resultChan <-chan *JobResult) map[string]*JobStats {
	var resultFile *csv.Writer
	var allTestStats = make(map[string]*JobStats)
	var recentTestStats = make(map[string]*JobStats)

	if len(*queryStatsFile) > 0 {
		if file, err := os.Create(*queryStatsFile); err != nil {
			log.Fatalf("Could not open result file %s: %v",
				*queryStatsFile, err)
		} else {
			defer file.Close()

			resultFile = csv.NewWriter(file)
			defer resultFile.Flush()
		}
	}

	ticker := time.NewTicker(*updateInterval)
	if !*intermediateUpdates {
		ticker.Stop()
	}
	defer ticker.Stop()

	for {
		select {
		case jr, ok := <-resultChan:
			if !ok {
				return allTestStats
			}
			if resultFile != nil {
				resultFile.Write([]string{
					jr.Name,
					strconv.FormatInt(jr.Start.Nanoseconds()/1000, 10),
					strconv.FormatInt(jr.Elapsed.Nanoseconds()/1000, 10),
					strconv.FormatInt(jr.RowsAffected, 10),
				})
			}
			if _, ok := allTestStats[jr.Name]; !ok {
				allTestStats[jr.Name] = new(JobStats)
			}
			if _, ok := recentTestStats[jr.Name]; !ok {
				recentTestStats[jr.Name] = new(JobStats)
			}

			allTestStats[jr.Name].Update(jr)
			recentTestStats[jr.Name].Update(jr)

		case <-ticker.C:
			for name, stats := range recentTestStats {
				log.Printf("%s: %v", name, stats)
			}
			recentTestStats = make(map[string]*JobStats)
		}
	}
}
Esempio n. 12
0
func writeHeader(w *csv.Writer, d interface{}) error {
	v := reflect.ValueOf(d)
	header := make([]string, v.NumField())
	for i := 0; i < v.NumField(); i++ {
		header[i] = v.Type().Field(i).Name
	}
	if err := w.Write(header); err != nil {
		return err
	}
	return nil
}
Esempio n. 13
0
func writeBlobRec(csvWriter *csv.Writer, blob *blob) error {
	sha1Str := hex.EncodeToString(blob.sha1[:])
	rec := [][]string{
		{
			sha1Str,
			strconv.Itoa(blob.nSegment),
			strconv.Itoa(blob.offset),
			strconv.Itoa(blob.size),
		}}
	return csvWriter.WriteAll(rec)
}
Esempio n. 14
0
// Csv calls each day and then writes the month statistics in csv format
func (fsm *FlightStatMonth) Csv(w *csv.Writer) {
	days := []int{}
	for d, _ := range fsm.Day {
		days = append(days, d)
	}
	sort.Ints(days)
	for _, d := range days {
		fsd := fsm.Day[d]
		fsd.Csv(w)
	}
	w.Write([]string{fmt.Sprintf("Total %s", fsm.Date.Format("January 2006")), fmt.Sprintf("%d", fsm.Flights), fmt.Sprintf("%.2f", fsm.Airtime.Minutes())})
}
Esempio n. 15
0
File: qhttp.go Progetto: stone/qhttp
// Takes a *result struct and writes out lines to *csv.Writer
func writeCsvLine(w *csv.Writer, res *result) {
	headers_joined := strings.Join(res.headers, ",")
	// When we save to CSV duration is always in seconds
	duration_seconds := fmt.Sprintf("%v", res.time.Seconds())
	// We need a array of strings for the csv package.
	record := []string{res.url, res.httpStatus, headers_joined, duration_seconds}
	err := w.Write(record)
	if err != nil {
		fmt.Println("Problems writing to csv file")
	}
	w.Flush()
}
Esempio n. 16
0
// Csv calls each month and then writes the year statistics in csv format
func (fsy *FlightStatYear) Csv(w *csv.Writer) {
	month := []int{}
	for m, _ := range fsy.Month {
		month = append(month, int(m))
	}
	sort.Ints(month)
	for _, m := range month {
		fsm := fsy.Month[time.Month(m)]
		fsm.Csv(w)
	}
	w.Write([]string{fmt.Sprintf("Total %d", fsy.Year.Year()), fmt.Sprintf("%d", fsy.Flights), fmt.Sprintf("%.2f", fsy.Airtime.Minutes())})
}
Esempio n. 17
0
func webQuitHandler(diskwriter *csv.Writer, gzipwriter *gzip.Writer, csvfile *os.File) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "flushing to disk and shutting down")
		diskwriter.Flush()
		if gzipwriter != nil {
			gzipwriter.Flush()
			gzipwriter.Close()
		}
		csvfile.Close()
		os.Exit(0)
	}
}
Esempio n. 18
0
func (c Tweet) csvWriter(writer *csv.Writer, m chan Tweet) {
	var mutex = &sync.Mutex{}
	for i := range m {
		c = i
		//fmt.Println(c)
		data := []string{c.User, c.Post_date, c.Message}
		mutex.Lock()
		writer.Write(data)
		writer.Flush()
		mutex.Unlock()
	}
}
Esempio n. 19
0
func writeCsvRow(out *csv.Writer, name, matched, path string, rating float64, ratings int) {
	url := ""
	if path != "" {
		url = "https://play.google.com" + path
	}
	out.Write([]string{
		name,
		matched,
		url,
		fmt.Sprintf("%f", rating),
		strconv.Itoa(ratings),
	})
}
Esempio n. 20
0
func recurseprint(elem *ChronologyTreeStruct, csvwriter *csv.Writer, row *[]string, isocode string, level int, index int) {
	if index > 0 {
		csvwriter.Write(*row)
		*row = make([]string, 12)
	}
	if level > 0 {
		(*row)[(level-1)*3+0] = elem.Name[isocode]
		(*row)[(level-1)*3+1] = strconv.Itoa(dateToHuman(elem.Start_date))
		(*row)[(level-1)*3+2] = strconv.Itoa(dateToHuman(elem.End_date))
	}
	for i, e := range elem.Content {
		recurseprint(&e, csvwriter, row, isocode, level+1, i)
	}
}
Esempio n. 21
0
func writeTo(writer *csv.Writer, in interface{}) error {
	inValue, inType := getConcreteReflectValueAndType(in) // Get the concrete type (not pointer) (Slice<?> or Array<?>)
	if err := ensureInType(inType); err != nil {
		return err
	}
	inInnerWasPointer, inInnerType := getConcreteContainerInnerType(inType) // Get the concrete inner type (not pointer) (Container<"?">)
	if err := ensureInInnerType(inInnerType); err != nil {
		return err
	}
	inInnerStructInfo := getStructInfo(inInnerType) // Get the inner struct info to get CSV annotations
	csvHeadersLabels := make([]string, len(inInnerStructInfo.Fields))
	for i, fieldInfo := range inInnerStructInfo.Fields { // Used to write the header (first line) in CSV
		csvHeadersLabels[i] = fieldInfo.getFirstKey()
	}
	if err := writer.Write(csvHeadersLabels); err != nil {
		return err
	}
	inLen := inValue.Len()
	for i := 0; i < inLen; i++ { // Iterate over container rows
		for j, fieldInfo := range inInnerStructInfo.Fields {
			csvHeadersLabels[j] = ""
			inInnerFieldValue, err := getInnerField(inValue.Index(i), inInnerWasPointer, fieldInfo.IndexChain) // Get the correct field header <-> position
			if err != nil {
				return err
			}
			csvHeadersLabels[j] = inInnerFieldValue
		}
		if err := writer.Write(csvHeadersLabels); err != nil {
			return err
		}
	}
	writer.Flush()
	return writer.Error()
}
func (c Tweet) CsvWriter(writer *csv.Writer, m chan Tweet) {
	var mutex = &sync.Mutex{}
	for i := range m {
		c = i
		//fmt.Println(c)
		data := []string{c.User, c.Post_date, c.Message}
		//Introduced locks for write to csv file
		mutex.Lock()
		writer.Write(data)
		writer.Flush()
		mutex.Unlock()
		//lock closed
	}
}
Esempio n. 23
0
func (user *User) WriteCSV(writer *csv.Writer) {
	var fields []string = make([]string, 7)
	fields[0] = user.Name
	fields[1] = user.ContactInfo
	fields[2] = string(user.UserLevel)
	fields[3] = strings.Join(user.Sponsors, ";")
	if !user.ValidFrom.IsZero() {
		fields[4] = user.ValidFrom.Format("2006-01-02 15:04")
	}
	if !user.ValidTo.IsZero() {
		fields[5] = user.ValidTo.Format("2006-01-02 15:04")
	}
	fields[6] = strings.Join(user.Codes, ";")
	writer.Write(fields)
}
Esempio n. 24
0
func TeamHeader(w *csv.Writer) {
	err := w.Write([]string{
		"Scored",
		"Opponent Scored",
		"Possesions",
		"Efficiency",
		"D's",
		"Opponent Turns",
		"Percent D's",
	})
	if err != nil {
		log.Fatal(err)
	}
	w.Flush()
}
Esempio n. 25
0
func (t TeamStats) Summary(w *csv.Writer) {
	err := w.Write([]string{
		fmt.Sprintf("%v", t.Scored),
		fmt.Sprintf("%v", t.OppScore),
		fmt.Sprintf("%v", t.Possesions),
		fmt.Sprintf("%.2f", 100*float64(t.Scored)/float64(t.Possesions)),
		fmt.Sprintf("%v", t.Ds),
		fmt.Sprintf("%v", t.OpponentTurns),
		fmt.Sprintf("%.2f", 100*float64(t.Ds)/float64(t.OpponentTurns)),
	})
	if err != nil {
		log.Fatal(err)
	}
	w.Flush()
}
Esempio n. 26
0
func json2csv(r LineReader, w *csv.Writer, keys []string) {
	var line []byte
	var err error
	for {
		if err == io.EOF {
			return
		}
		line, err = r.ReadBytes('\n')
		if err != nil {
			if err != io.EOF {
				log.Printf("Input ERROR: %s", err)
				break
			}
		}
		if len(line) == 0 {
			continue
		}

		var data map[string]interface{}
		err = json.Unmarshal(line, &data)
		if err != nil {
			log.Printf("ERROR Json Decoding: %s - %v", err, line)
			continue
		}
		var record []string
		for _, key := range keys {
			if v, ok := data[key]; ok {
				switch v.(type) {
				case nil:
					record = append(record, "")
				case float64:
					f, _ := v.(float64)
					if math.Mod(f, 1.0) == 0.0 {
						record = append(record, fmt.Sprintf("%d", int(f)))
					} else {
						record = append(record, fmt.Sprintf("%f", f))
					}
				default:
					record = append(record, fmt.Sprintf("%+v", v))
				}
			} else {
				record = append(record, "")
			}
		}
		w.Write(record)
		w.Flush()
	}
}
Esempio n. 27
0
func outputCSV(w *csv.Writer, p types.ComplainerProfile, c types.Complaint) {
	zip := regexp.MustCompile("^.*(\\d{5}(-\\d{4})?).*$").ReplaceAllString(p.Address, "$1")

	r := []string{
		p.CallerCode, p.FullName, p.Address, zip, p.EmailAddress,
		fmt.Sprintf("%.4f", p.Lat), fmt.Sprintf("%.4f", p.Long),
		fmt.Sprintf("%d", c.Timestamp.UTC().Unix()),
		c.Timestamp.Format("2006/01/02"),
		c.Timestamp.Format("15:04:05"),
		c.Description, c.AircraftOverhead.FlightNumber, c.Activity,
	}

	if err := w.Write(r); err != nil {
		// ?
	}
}
Esempio n. 28
0
func process(node *html.Node, w *csv.Writer) error {
	for child := node.FirstChild; child != nil; child = child.NextSibling {
		if child.Type == html.ElementNode && child.Data == "a" {
			for _, a := range child.Attr {
				if a.Key == "href" {
					data := repoRegexp.FindStringSubmatch(a.Val)
					if len(data) == 4 {
						err := w.Write(data)
						return err
					}
					break
				}
			}
		}
		process(child, w)
	}
	return nil
}
func (item *AnimeConventionItem) writeToCSV(writer *csv.Writer) {
	mutex.Lock()
	records := [][]string{
		{item.name, item.location, item.city,
			item.state, item.country, item.startDate,
			item.endDate, item.latitude, item.longitude,
			item.advanceRate, item.atDoorRate, item.atDoorRateALL, item.atDoorRateThu,
			item.atDoorRateFri, item.atDoorRateSat, item.atDoorRateSun,
			item.siteURL, item.registerNowURL, item.description},
	}
	for _, record := range records {
		err := writer.Write(record)
		if err != nil {
			fmt.Errorf("Error:\n", err)
			return
		}
	}
	mutex.Unlock()
}
Esempio n. 30
0
// Print accumulator to the writer
func (a *Accumulator) Print(w *csv.Writer, gt GroupingType) error {
	var ss sort.StringSlice
	for key := range *a {
		ss = append(ss, key)
	}
	ss.Sort()
	for _, key := range ss {
		records, _ := (*a)[key]
		s := []string{
			key,
			records[0].Measurement,
			fmt.Sprintf(formatFloat, a.calc(records, gt)),
		}
		if err := w.Write(s); err != nil {
			return err
		}
	}
	return nil
}