Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .ignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/public/jquery.js
/public/*.min.*
/pack
/msg
/pkg/geo/GeoLite2-Country.mmdb.gz
/db/migrate
60 changes: 60 additions & 0 deletions e.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package goatcounter

import (
"time"
)

type (
ExportInfo struct {
CreatedAt time.Time `json:"created_at"`
Version int `json:"version"`
Site string `json:"site"`
}
ExportLanguage struct {
ISO6393 string `db:"iso_639_3" json:"iso_639_3"`
Name string `db:"name" json:"name"`
}
ExportBrowserStat struct {
Day string `db:"day" json:"day"`
PathID PathID `db:"path_id" json:"path_id"`
BrowserID BrowserID `db:"browser_id" json:"browser_id"`
Count int `db:"count" json:"count"`
}
ExportSystemStat struct {
Day string `db:"day" json:"day"`
PathID PathID `db:"path_id" json:"path_id"`
SystemID SystemID `db:"system_id" json:"system_id"`
Count int `db:"count" json:"count"`
}
ExportLocationStat struct {
Day string `db:"day" json:"day"`
PathID PathID `db:"path_id" json:"path_id"`
Location string `db:"location" json:"location"`
Count int `db:"count" json:"count"`
}
ExportSizeStat struct {
Day string `db:"day" json:"day"`
PathID PathID `db:"path_id" json:"path_id"`
Width int `db:"width" json:"width"`
Count int `db:"count" json:"count"`
}
ExportLanguageStat struct {
Day string `db:"day" json:"day"`
PathID PathID `db:"path_id" json:"path_id"`
Language string `db:"language" json:"language"`
Count int `db:"count" json:"count"`
}
ExportCampaignStat struct {
Day string `db:"day" json:"day"`
PathID PathID `db:"path_id" json:"path_id"`
CampaignID CampaignID `db:"campaign_id" json:"campaign_id"`
Ref string `db:"ref" json:"ref"`
Count int `db:"count" json:"count"`
}
ExportRefStat struct {
Hour string `db:"hour" json:"hour"`
PathID PathID `db:"path_id" json:"path_id"`
RefID RefID `db:"ref_id" json:"ref_id"`
Count int `db:"total" json:"count"`
}
)
1 change: 0 additions & 1 deletion export.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ func (e Export) Exists() bool {
if e.Path == "" {
return false
}

_, err := os.Stat(e.Path)
return err == nil
}
Expand Down
18 changes: 10 additions & 8 deletions exportcsv.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,23 @@ func (e *Export) CreateCSV(ctx context.Context, startFrom HitID) (*os.File, erro

err := zdb.Insert(ctx, e)
if err != nil {
return nil, errors.Wrap(err, "Export.Create")
return nil, errors.Wrap(err, "Export.CreateCSV")
}

fp, err := os.Create(e.Path)
return fp, errors.Wrap(err, "Export.Create")
return fp, errors.Wrap(err, "Export.CreateCSV")
}

// Export all data to a CSV file.
func (e *Export) RunCSV(ctx context.Context, fp *os.File, mailUser bool) {
l := log.Module("export").With("id", e.ID)
l.Info(ctx, "export started")
l.Info(ctx, "CSV export started")

gzfp := gzip.NewWriter(fp)
defer fp.Close() // No need to error-check; just for safety.
defer gzfp.Close()
defer func() {
gzfp.Close()
fp.Close()
}()

c := csv.NewWriter(gzfp)
c.Write([]string{ExportCSVVersion + "Path", "Title", "Event", "UserAgent",
Expand Down Expand Up @@ -104,7 +106,7 @@ func (e *Export) RunCSV(ctx context.Context, fp *os.File, mailUser bool) {
e.Error = new(exportErr.Error())
err := zdb.Update(ctx, e, "error")
if err != nil {
log.Error(ctx, err)
l.Error(ctx, err)
}

_ = gzfp.Close()
Expand All @@ -118,12 +120,12 @@ func (e *Export) RunCSV(ctx context.Context, fp *os.File, mailUser bool) {
l.Error(ctx, err)
return
}

err = fp.Sync() // Ensure stat is correct.
if err != nil {
l.Error(ctx, err)
return
}

stat, err := fp.Stat()
size := "0"
if err == nil {
Expand All @@ -150,7 +152,7 @@ func (e *Export) RunCSV(ctx context.Context, fp *os.File, mailUser bool) {
e.FinishedAt = new(ztime.Now(ctx))
zdb.Update(ctx, e, "finished_at", "num_rows", "size", "hash", "last_hit_id")
if err != nil {
log.Error(ctx, err)
l.Error(ctx, err)
}

if mailUser {
Expand Down
Loading
Loading