This commit is contained in:
2021-10-29 10:09:55 -05:00
parent d105e88992
commit 2116778276
10 changed files with 47 additions and 44 deletions

17
main.go
View File

@@ -2,14 +2,10 @@ package main
import (
"flag"
"log"
)
"fmt"
func checkErr(err error) {
if err != nil {
log.Fatal(err)
}
}
"github.com/robviren/xl/runner"
)
func main() {
var (
@@ -23,11 +19,12 @@ func main() {
flag.IntVar(&month, "month", 0, "Sets the month for the data to process")
flag.Parse()
run := NewRunner()
run := runner.NewRunner()
if excel_file != "" {
ProcessExcel(excel_file)
run.ProcessExcel(excel_file)
} else if get_top {
run.GetTop()
data := run.GetTop()
fmt.Println(data)
} else {
flag.PrintDefaults()
}

5
runner/description.go Normal file
View File

@@ -0,0 +1,5 @@
package runner
func (r *Runner) DescriptionTYD(description string) []Data {
}

View File

@@ -1,7 +1,6 @@
package main
package runner
import (
"database/sql"
_ "embed"
"fmt"
"time"
@@ -10,17 +9,7 @@ import (
//go:embed sql/get_top.sql
var get_top string
type GetTopResult struct {
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 {
func (r *Runner) GetTop() []Data {
//Get Current Days
current_day := time.Now()
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")
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)
checkErr(err)
return top
}

View File

@@ -1,4 +1,4 @@
package main
package runner
import (
"fmt"
@@ -18,18 +18,19 @@ import (
var schema string
type Data struct {
Description string
SKU string
Level2 string
Level3 string
Revenue float64
Cost float64
Quantity int64
Year int64
Month int64
Description string `db:"description,omitempty"`
SKU string `db:"sku,omitempty"`
Level2 string `db:"level2,omitempty"`
Level3 string `db:"level3,omitempty"`
Revenue float64 `db:"revenue,omitempty"`
Cost float64 `db:"cost,omitempty"`
Quantity int64 `db:"quantity,omitempty"`
Year int64 `db:"year,omitempty"`
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...")
start := time.Now()
@@ -79,10 +80,10 @@ func ProcessExcel(path string) {
values := []string{}
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)
checkErr(err)

1
runner/results.go Normal file
View File

@@ -0,0 +1 @@
package runner

View File

@@ -1,4 +1,4 @@
package main
package runner
import "github.com/jmoiron/sqlx"

View File

@@ -3,8 +3,8 @@ SELECT
sku,
level2,
level3,
rev,
qty,
revenue,
quantity,
trend
from
(
@@ -16,8 +16,8 @@ from
SUM(revenue) DESC
) rownum,
SUM(revenue) OVER (PARTITION BY level2) total,
ROUND(SUM(revenue), 2) as rev,
SUM(quantity) as qty,
ROUND(SUM(revenue), 2) as revenue,
SUM(quantity) as quantity,
trend
FROM
(
@@ -57,8 +57,8 @@ from
)
WHERE
rownum <= 10
and rev > 0
and revenue > 0
order by
(sum(rev) over (
(sum(revenue) over (
partition by level2
)) desc;

9
runner/util.go Normal file
View File

@@ -0,0 +1,9 @@
package runner
import "log"
func checkErr(err error) {
if err != nil {
log.Fatal(err)
}
}