<commit_before>package main

import (
	"database/sql"
	"fmt"
	"log"
	"os/user"
	"path/filepath"
	"time"

	"github.com/ecc1/dexcom"
	_ "github.com/mattn/go-sqlite3"
)

const (
	dbName = "glucose.db"
)

func main() {
	err := dexcom.Open()
	if err != nil {
		log.Fatal(err)
	}
	db := OpenDB()
	xact, err := db.Begin()
	if err != nil {
		db.Close()
		log.Fatal(err)
	}
	stmt, err := xact.Prepare("insert into glucose values (?, ?)")
	if err != nil {
		db.Close()
		log.Fatal(err)
	}
	n := 0
	proc := func(v []byte, _ dexcom.RecordContext) error {
		r := dexcom.EGVRecord{}
		err := r.Unmarshal(v)
		if err != nil {
			return err
		}
		_, err = stmt.Exec(glucoseRow(r))
		if err != nil {
			return err
		}
		fmt.Print(".")
		n++
		if n%80 == 0 {
			fmt.Println()
		}
		return nil
	}
	err = dexcom.ReadRecords(dexcom.EGV_DATA, proc)
	if n%80 != 0 {
		fmt.Println()
	}
	if err != nil {
		db.Close()
		log.Fatal(err)
	}
	xact.Commit()
	db.Close()
}

func OpenDB() *sql.DB {
	u, err := user.Current()
	if err != nil {
		log.Fatal(err)
	}
	home := u.HomeDir
	db, err := sql.Open("sqlite3", filepath.Join(home, dbName))
	if err != nil {
		log.Fatal(err)
	}
	// use integer (Unix time); datetime type stores value as a string
	stmt := `
	  create table if not exists glucose
	    (time integer primary key on conflict ignore,
	     value integer)
	`
	_, err = db.Exec(stmt)
	if err != nil {
		db.Close()
		log.Fatal(err)
	}
	return db
}

// glucoseRow returns the time and glucose value from an EGVRecord
// suitable for inserting into the database.
func glucoseRow(egv dexcom.EGVRecord) (int64, uint16) {
	return egv.Timestamp.DisplayTime.Round(time.Second).Unix(), egv.Glucose
}<commit_msg>Use ReadEgvRecords function<commit_after>package main

import (
	"database/sql"
	"log"
	"os/user"
	"path/filepath"
	"time"

	"github.com/ecc1/dexcom"
	_ "github.com/mattn/go-sqlite3"
)

const (
	dbName = "glucose.db"
)

func main() {
	db := OpenDB()
	xact, err := db.Begin()
	if err != nil {
		db.Close()
		log.Fatal(err)
	}
	err = dexcom.Open()
	if err != nil {
		log.Fatal(err)
	}
	egv, err := dexcom.ReadEgvRecords(time.Time{})
	if err != nil {
		log.Fatal(err)
	}
	stmt, err := xact.Prepare("insert into glucose values (?, ?)")
	if err != nil {
		db.Close()
		log.Fatal(err)
	}
	for _, r := range egv {
		_, err = stmt.Exec(glucoseRow(r))
		if err != nil {
			db.Close()
			log.Fatal(err)
		}
	}
	xact.Commit()
	db.Close()
}

func OpenDB() *sql.DB {
	u, err := user.Current()
	if err != nil {
		log.Fatal(err)
	}
	home := u.HomeDir
	db, err := sql.Open("sqlite3", filepath.Join(home, dbName))
	if err != nil {
		log.Fatal(err)
	}
	// use integer (Unix time); datetime type stores value as a string
	stmt := `
	  create table if not exists glucose
	    (time integer primary key on conflict ignore,
	     value integer)
	`
	_, err = db.Exec(stmt)
	if err != nil {
		db.Close()
		log.Fatal(err)
	}
	return db
}

// glucoseRow returns the time and glucose value from an EGVRecord
// suitable for inserting into the database.
func glucoseRow(egv dexcom.EgvRecord) (int64, uint16) {
	t := egv.Timestamp.DisplayTime
	return t.Unix(), egv.Glucose
}
{'fraction_non_alphanumeric': 0.16, 'fraction_numerical': 0.00672, 'mean_word_length': 3.0703125, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7265839', 'n_tokens_mistral': 1383, 'n_tokens_neox': 1258, 'n_words': 316}