Skip to content

Commit

Permalink
Minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
im-denisenko committed Nov 29, 2021
1 parent e7203b9 commit 2be5184
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 20 deletions.
17 changes: 2 additions & 15 deletions generators.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,7 @@ func (self ReportGeneratorNaive) Generate(fp *os.File) (Report, error) {

report := make(Report)
for i := 0; i < len(transactions); i++ {
transaction := transactions[i]
if report[transaction.UserId] == nil {
report[transaction.UserId] = make(ReportRow)
}
report[transaction.UserId]["sum"] += transaction.Amount
report[transaction.UserId]["user_id"] = transaction.UserId
report[transaction.UserId]["category_"+transaction.Category] += transaction.Amount
report.Update(transactions[i])
}

return report, nil
Expand Down Expand Up @@ -76,14 +70,7 @@ func (self ReportGeneratorStream) Generate(fp *os.File) (Report, error) {
if err != nil {
return nil, err
}

if report[transaction.UserId] == nil {
report[transaction.UserId] = make(ReportRow)
}

report[transaction.UserId]["sum"] += transaction.Amount
report[transaction.UserId]["user_id"] = transaction.UserId
report[transaction.UserId]["category_"+transaction.Category] += transaction.Amount
report.Update(transaction)
}

token, err = decoder.Token()
Expand Down
1 change: 1 addition & 0 deletions input/10K_transactions.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions input/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Обычно файлы с данными не коммитят в репозиторий, но во имя самодостаточности
и для сохранения истории, я всё же оставлю один наглядный пример JSON'а, парсинг
которого реализован в данной задаче.
11 changes: 10 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,22 @@ type Transaction struct {
Category string `json:"category"`
}

func (self Report) Update(transaction Transaction) {
if self[transaction.UserId] == nil {
self[transaction.UserId] = make(ReportRow)
}
self[transaction.UserId]["sum"] += transaction.Amount
self[transaction.UserId]["user_id"] = transaction.UserId
self[transaction.UserId]["category_"+transaction.Category] += transaction.Amount
}

// -----------------------------------------------------------------------------
// Utils
// -----------------------------------------------------------------------------
var start = time.Now()
var helpArg = flag.Bool("h", false, "Print help and exit")
var algoArg = flag.String("a", "naive", "Algorithm to use: naive, stream")
var inputArg = flag.String("i", "input/10M.json", "Path to the input file")
var inputArg = flag.String("i", "input/10K_transactions.json", "Path to the input file")
var outputArg = flag.String("o", "output/report.json", "Path to the output file")
var formatArg = flag.String("f", "json", "Report format: json, csv, sqlite")
var quietArg = flag.Bool("q", false, "Disable logs output")
Expand Down
87 changes: 87 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,90 @@ func TestResolvePath(t *testing.T) {
t.Fatalf("absolute path resolve was incorrect")
}
}

func TestReportUpdateGroupByCategory(t *testing.T) {
report := make(Report)

report.Update(Transaction{UserId: 10, Amount: 10, Category: "aa"})
report.Update(Transaction{UserId: 10, Amount: 20, Category: "aa"})
report.Update(Transaction{UserId: 10, Amount: 40, Category: "bb"})

if len(report) != 1 {
t.Fatalf("unexpected report length")
}

if report[10]["user_id"] != 10 {
t.Fatalf("user_id was set incorrect")
}

if report[10]["sum"] != 70 {
t.Fatalf("sum was set incorrect")
}

if report[10]["category_aa"] != 30 {
t.Fatalf("category_aa was set incorrect")
}

if report[10]["category_bb"] != 40 {
t.Fatalf("category_bb was set incorrect")
}
}

func TestReportUpdateGroupByUser(t *testing.T) {
report := make(Report)

report.Update(Transaction{UserId: 10, Amount: 10, Category: "aa"})
report.Update(Transaction{UserId: 10, Amount: 20, Category: "aa"})
report.Update(Transaction{UserId: 11, Amount: 30, Category: "bb"})
report.Update(Transaction{UserId: 11, Amount: 40, Category: "bb"})

if len(report) != 2 {
t.Fatalf("unexpected report length")
}

if report[10]["user_id"] != 10 {
t.Fatalf("user_id was set incorrect")
}

if report[10]["sum"] != 30 {
t.Fatalf("sum was set incorrect")
}

if report[10]["category_aa"] != 30 {
t.Fatalf("category_aa was set incorrect")
}

if report[11]["user_id"] != 11 {
t.Fatalf("user_id was set incorrect")
}

if report[11]["sum"] != 70 {
t.Fatalf("sum was set incorrect")
}

if report[11]["category_bb"] != 70 {
t.Fatalf("category_aa was set incorrect")
}
}

func TestReportUpdateGroupEmptyCategory(t *testing.T) {
report := make(Report)

report.Update(Transaction{UserId: 10, Amount: 10})

if len(report) != 1 {
t.Fatalf("unexpected report length")
}

if report[10]["user_id"] != 10 {
t.Fatalf("user_id was set incorrect")
}

if report[10]["sum"] != 10 {
t.Fatalf("sum was set incorrect")
}

if report[10]["category_"] != 10 {
t.Fatalf("category_ was set incorrect")
}
}
6 changes: 2 additions & 4 deletions writers.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ type ReportWriterSQLite struct {
}

func (self ReportWriterSQLite) Write(report Report, outputPath string) error {
os.Remove(outputPath)

db, err := sql.Open("sqlite3", outputPath)

if err != nil {
Expand All @@ -101,10 +103,6 @@ func (self ReportWriterSQLite) Write(report Report, outputPath string) error {
defer db.Close()

_, err = db.Exec(`
DROP TABLE IF EXISTS users_report;
DROP TABLE IF EXISTS users_categories_report;
CREATE TABLE users_report (
id INT NOT NULL,
sum INT NOT NULL,
Expand Down

0 comments on commit 2be5184

Please sign in to comment.