英文:
How to create .csv every day
问题
在我的应用程序中,我监听 nats 并获得一个包含数据的映射。我需要使用映射中的数据生成每日的 CSV 文件。目前,数据会连续地写入文件。我应该如何在每天结束时才将数据写入文件?
type Hits struct {
HitMap map[string]map[SearchRequestKey]uint32
Mu sync.RWMutex
Log zerolog.Logger
TimeStart time.Time
MinHits int
WasDelete map[string]int64
Cnt1 float32
Version int
QQMap map[string]struct{}
GitlabToken string
}
type SearchRequestKey struct {
Query string
Date string
}
func FileAdd(hits *models.Hits) {
file, err := os.OpenFile("query-hits.csv", os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
fmt.Println(err)
}
writer := csv.NewWriter(file)
writer.Comma = '|'
for key, value := range hits.HitMap {
for query, shard := range value {
err := writer.Write([]string{query.Query, key, strconv.Itoa(int(shard))})
if err != nil {
fmt.Println(err)
}
}
}
}
以上是你提供的代码。
英文:
In my application, I listen to nats and the output I get is a map with data. I need to generate a daily csv file using the data from the map.
Now the data from the map is written to the file continuously. How can I write data to the file only at the end of each day?
type Hits struct {
HitMap map[string]map[SearchRequestKey]uint32
Mu sync.RWMutex
Log zerolog.Logger
TimeStart time.Time
MinHits int
WasDelete map[string]int64
Cnt1 float32
Version int
QQMap map[string]struct{}
GitlabToken string
}
type SearchRequestKey struct {
Query string
Date string
}
func FileAdd(hits *models.Hits) {
file, err := os.OpenFile("query-hits.csv", os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
fmt.Println(err)
}
writer := csv.NewWriter(file)
writer.Comma = '|'
for key, value := range hits.HitMap {
for query, shard := range value {
err := writer.Write([]string{query.Query, key, strconv.Itoa(int(shard))})
if err != nil {
fmt.Println(err)
}
}
}
}
答案1
得分: 1
为了每天创建一个 .csv 文件,您需要在循环中运行代码,并将当前时间作为起始点。经过 24 小时,该函数将再次生成文件。最后,使用 time.AfterFunc() 的方式有所帮助。不要忘记使用互斥锁。
type Hits struct {
HitMap map[string]map[SearchRequestKey]uint32
Mu sync.RWMutex
Log zerolog.Logger
TimeStart time.Time
MinHits int
WasDelete map[string]int64
Cnt1 float32
Version int
QQMap map[string]struct{}
GitlabToken string
}
type SearchRequestKey struct {
Query string
Date string
}
func FileAdd(hits *models.Hits) {
hits.Mu.Lock()
defer hits.Mu.Unlock()
for {
year, month, day := time.Now().Date()
file := strconv.Itoa(year) + month.String() + strconv.Itoa(day)
fileName, err := os.Create(file)
if err != nil {
fmt.Println(err)
}
writer := csv.NewWriter(fileName)
writer.Comma = '|'
for key, value := range hits.HitMap {
for query, shard := range value {
err := writer.Write([]string{query.Query, key, strconv.Itoa(int(shard))})
if err != nil {
fmt.Println(err)
}
}
}
time.Sleep(24 * time.Hour) //only after 24 hours
}
}
func FuncThatStart (hits *models.Hits) {
hits.Mu.Lock()
defer hits.Mu.Unlock()
// generate for one day
today := time.Now()
tomorrow := today.Add(24 * time.Hour) // same time as in time.Sleep()
time.AfterFunc(time.Until(tomorrow), func() {
go storage.FileAdd(hits) //goroutine handle one time per day
})
}
感谢您的评论!
英文:
In order to create a .csv file daily, you need to run the code in a loop and make the current time as the starting point. After 24 hours the function will generate the file again. Finally the way with time.AfterFunc() helped. Don't forget to use mutex.
type Hits struct {
HitMap map[string]map[SearchRequestKey]uint32
Mu sync.RWMutex
Log zerolog.Logger
TimeStart time.Time
MinHits int
WasDelete map[string]int64
Cnt1 float32
Version int
QQMap map[string]struct{}
GitlabToken string
}
type SearchRequestKey struct {
Query string
Date string
}
func FileAdd(hits *models.Hits) {
hits.Mu.Lock()
defer hits.Mu.Unlock()
for {
year, month, day := time.Now().Date()
file := strconv.Itoa(year) + month.String() + strconv.Itoa(day)
fileName, err := os.Create(file)
if err != nil {
fmt.Println(err)
}
writer := csv.NewWriter(fileName)
writer.Comma = '|'
for key, value := range hits.HitMap {
for query, shard := range value {
err := writer.Write([]string{query.Query, key, strconv.Itoa(int(shard))})
if err != nil {
fmt.Println(err)
}
}
}
time.Sleep(24 * time.Hour) //only after 24 hours
}
}
func FuncThatStart (hits *models.Hits) {
hits.Mu.Lock()
defer hits.Mu.Unlock()
// generate for one day
today := time.Now()
tomorrow := today.Add(24 * time.Hour) // same time as in time.Sleep()
time.AfterFunc(time.Until(tomorrow), func() {
go storage.FileAdd(hits) //goroutine handle one time per day
})
}
Thanks for comments
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论