Refactor
This commit is contained in:
17
main.go
17
main.go
@@ -2,14 +2,10 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"log"
|
"fmt"
|
||||||
)
|
|
||||||
|
|
||||||
func checkErr(err error) {
|
"github.com/robviren/xl/runner"
|
||||||
if err != nil {
|
)
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var (
|
var (
|
||||||
@@ -23,11 +19,12 @@ func main() {
|
|||||||
flag.IntVar(&month, "month", 0, "Sets the month for the data to process")
|
flag.IntVar(&month, "month", 0, "Sets the month for the data to process")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
run := NewRunner()
|
run := runner.NewRunner()
|
||||||
if excel_file != "" {
|
if excel_file != "" {
|
||||||
ProcessExcel(excel_file)
|
run.ProcessExcel(excel_file)
|
||||||
} else if get_top {
|
} else if get_top {
|
||||||
run.GetTop()
|
data := run.GetTop()
|
||||||
|
fmt.Println(data)
|
||||||
} else {
|
} else {
|
||||||
flag.PrintDefaults()
|
flag.PrintDefaults()
|
||||||
}
|
}
|
||||||
|
|||||||
5
runner/description.go
Normal file
5
runner/description.go
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package runner
|
||||||
|
|
||||||
|
func (r *Runner) DescriptionTYD(description string) []Data {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
package main
|
package runner
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
@@ -10,17 +9,7 @@ import (
|
|||||||
//go:embed sql/get_top.sql
|
//go:embed sql/get_top.sql
|
||||||
var get_top string
|
var get_top string
|
||||||
|
|
||||||
type GetTopResult struct {
|
func (r *Runner) GetTop() []Data {
|
||||||
Description string `db:"description,omitempty"`
|
|
||||||
Sku string `db:"sku,omitempty"`
|
|
||||||
Level2 string `db:"level2,omitempty"`
|
|
||||||
Level3 string `db:"level3,omitempty"`
|
|
||||||
Rev float64 `db:"rev,omitempty"`
|
|
||||||
Qty int64 `db:"qty,omitempty"`
|
|
||||||
Trend sql.NullFloat64 `db:"trend,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Runner) GetTop() []GetTopResult {
|
|
||||||
//Get Current Days
|
//Get Current Days
|
||||||
current_day := time.Now()
|
current_day := time.Now()
|
||||||
year := current_day.Format("2006")
|
year := current_day.Format("2006")
|
||||||
@@ -29,8 +18,9 @@ func (r *Runner) GetTop() []GetTopResult {
|
|||||||
four_previous_month := current_day.AddDate(0, -4, 0).Format("01")
|
four_previous_month := current_day.AddDate(0, -4, 0).Format("01")
|
||||||
|
|
||||||
query_string := fmt.Sprintf(get_top, year, previous_month, four_previous_month, two_previous_month)
|
query_string := fmt.Sprintf(get_top, year, previous_month, four_previous_month, two_previous_month)
|
||||||
var top []GetTopResult
|
var top []Data
|
||||||
err := r.db.Select(&top, query_string)
|
err := r.db.Select(&top, query_string)
|
||||||
checkErr(err)
|
checkErr(err)
|
||||||
|
|
||||||
return top
|
return top
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package runner
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -18,18 +18,19 @@ import (
|
|||||||
var schema string
|
var schema string
|
||||||
|
|
||||||
type Data struct {
|
type Data struct {
|
||||||
Description string
|
Description string `db:"description,omitempty"`
|
||||||
SKU string
|
SKU string `db:"sku,omitempty"`
|
||||||
Level2 string
|
Level2 string `db:"level2,omitempty"`
|
||||||
Level3 string
|
Level3 string `db:"level3,omitempty"`
|
||||||
Revenue float64
|
Revenue float64 `db:"revenue,omitempty"`
|
||||||
Cost float64
|
Cost float64 `db:"cost,omitempty"`
|
||||||
Quantity int64
|
Quantity int64 `db:"quantity,omitempty"`
|
||||||
Year int64
|
Year int64 `db:"year,omitempty"`
|
||||||
Month int64
|
Month int64 `db:"month,omitempty"`
|
||||||
|
Trend sql.NullFloat64 `db:"trend,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func ProcessExcel(path string) {
|
func (r *Runner) ProcessExcel(path string) {
|
||||||
fmt.Println("Processing excel file...")
|
fmt.Println("Processing excel file...")
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
|
||||||
@@ -79,10 +80,10 @@ func ProcessExcel(path string) {
|
|||||||
|
|
||||||
values := []string{}
|
values := []string{}
|
||||||
for _, elm := range data {
|
for _, elm := range data {
|
||||||
values = append(values, fmt.Sprintf(`("%s","%s","%s","%s",%f,%f,%d,"%d","%d")`, strings.ReplaceAll(elm.Description, "\"", ""), elm.SKU, elm.Level2, elm.Level3, elm.Revenue, elm.Cost, elm.Quantity, elm.Year, elm.Month))
|
values = append(values, fmt.Sprintf(`("%s","%s","%s","%s",%f,%f,%d,"%d","%d",%f)`, strings.ReplaceAll(elm.Description, "\"", ""), elm.SKU, elm.Level2, elm.Level3, elm.Revenue, elm.Cost, elm.Quantity, elm.Year, elm.Month, 0.0))
|
||||||
}
|
}
|
||||||
|
|
||||||
stmt := "INSERT INTO data (description,sku,level2,level3,revenue,cost,quantity,year,month) VALUES " + strings.Join(values, ",")
|
stmt := "INSERT INTO data (description,sku,level2,level3,revenue,cost,quantity,year,month,trend) VALUES " + strings.Join(values, ",")
|
||||||
_, err = db.Exec(stmt)
|
_, err = db.Exec(stmt)
|
||||||
checkErr(err)
|
checkErr(err)
|
||||||
|
|
||||||
1
runner/results.go
Normal file
1
runner/results.go
Normal file
@@ -0,0 +1 @@
|
|||||||
|
package runner
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package runner
|
||||||
|
|
||||||
import "github.com/jmoiron/sqlx"
|
import "github.com/jmoiron/sqlx"
|
||||||
|
|
||||||
@@ -3,8 +3,8 @@ SELECT
|
|||||||
sku,
|
sku,
|
||||||
level2,
|
level2,
|
||||||
level3,
|
level3,
|
||||||
rev,
|
revenue,
|
||||||
qty,
|
quantity,
|
||||||
trend
|
trend
|
||||||
from
|
from
|
||||||
(
|
(
|
||||||
@@ -16,8 +16,8 @@ from
|
|||||||
SUM(revenue) DESC
|
SUM(revenue) DESC
|
||||||
) rownum,
|
) rownum,
|
||||||
SUM(revenue) OVER (PARTITION BY level2) total,
|
SUM(revenue) OVER (PARTITION BY level2) total,
|
||||||
ROUND(SUM(revenue), 2) as rev,
|
ROUND(SUM(revenue), 2) as revenue,
|
||||||
SUM(quantity) as qty,
|
SUM(quantity) as quantity,
|
||||||
trend
|
trend
|
||||||
FROM
|
FROM
|
||||||
(
|
(
|
||||||
@@ -57,8 +57,8 @@ from
|
|||||||
)
|
)
|
||||||
WHERE
|
WHERE
|
||||||
rownum <= 10
|
rownum <= 10
|
||||||
and rev > 0
|
and revenue > 0
|
||||||
order by
|
order by
|
||||||
(sum(rev) over (
|
(sum(revenue) over (
|
||||||
partition by level2
|
partition by level2
|
||||||
)) desc;
|
)) desc;
|
||||||
9
runner/util.go
Normal file
9
runner/util.go
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
package runner
|
||||||
|
|
||||||
|
import "log"
|
||||||
|
|
||||||
|
func checkErr(err error) {
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user