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

5
runner/description.go Normal file
View File

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

26
runner/get_top.go Normal file
View File

@@ -0,0 +1,26 @@
package runner
import (
_ "embed"
"fmt"
"time"
)
//go:embed sql/get_top.sql
var get_top string
func (r *Runner) GetTop() []Data {
//Get Current Days
current_day := time.Now()
year := current_day.Format("2006")
previous_month := current_day.AddDate(0, -1, 0).Format("01")
two_previous_month := current_day.AddDate(0, -2, 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)
var top []Data
err := r.db.Select(&top, query_string)
checkErr(err)
return top
}

92
runner/process_excel.go Normal file
View File

@@ -0,0 +1,92 @@
package runner
import (
"fmt"
"strconv"
"strings"
"time"
"database/sql"
_ "github.com/mattn/go-sqlite3"
"github.com/xuri/excelize/v2"
_ "embed"
)
//go:embed sql/schema.sql
var schema string
type Data struct {
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 (r *Runner) ProcessExcel(path string) {
fmt.Println("Processing excel file...")
start := time.Now()
f, err := excelize.OpenFile(path)
checkErr(err)
rows, err := f.GetRows("Sheet1")
checkErr(err)
const offset = 3
data := []Data{}
for i, row := range rows {
if i >= offset {
new_data := Data{SKU: row[33], Level2: row[29], Level3: row[30], Description: row[44]}
//Parse Rev
rev, err := strconv.ParseFloat(row[72], 64)
checkErr(err)
new_data.Revenue = rev
//Parse Cost
cost, err := strconv.ParseFloat(row[16], 64)
checkErr(err)
new_data.Cost = cost
//Parse Quantity
qty, err := strconv.ParseInt(row[71], 10, 64)
checkErr(err)
new_data.Quantity = qty
//Parse Time
day, err := time.Parse("2006-01", row[26])
checkErr(err)
new_data.Year = int64(day.Year())
new_data.Month = int64(day.Month())
data = append(data, new_data)
}
}
db, err := sql.Open("sqlite3", "data.db")
checkErr(err)
defer db.Close()
db.Exec("DROP TABLE IF EXISTS data")
db.Exec(schema)
values := []string{}
for _, elm := range data {
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,trend) VALUES " + strings.Join(values, ",")
_, err = db.Exec(stmt)
checkErr(err)
elapsed := time.Since(start)
fmt.Printf("Operation complete in %s\n", elapsed)
}

1
runner/results.go Normal file
View File

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

25
runner/runner.go Normal file
View File

@@ -0,0 +1,25 @@
package runner
import "github.com/jmoiron/sqlx"
type Runner struct {
db *sqlx.DB
}
func NewRunner() *Runner {
db, err := sqlx.Connect("sqlite3", "data.db")
checkErr(err)
runner := &Runner{
db,
}
return runner
}
func NewRunnerPath(path string) *Runner {
db, err := sqlx.Connect("sqlite3", path)
checkErr(err)
runner := &Runner{
db,
}
return runner
}

27
runner/sql/CFY.sql Normal file
View File

@@ -0,0 +1,27 @@
WITH const as (select "%ROBOSHOT%" as target)
SELECT
description,
-- year,
-- month,
sum(revenue),
sum(quantity)
from
data
where
description like const.target
and year = 2021
and month BETWEEN 7
and 9
group by
description;
select
description,
sum(revenue),
sum(quantity)
from
data
where
description like const.target
and year = 2021
and month BETWEEN 7
and 9;

64
runner/sql/get_top.sql Normal file
View File

@@ -0,0 +1,64 @@
SELECT
description,
sku,
level2,
level3,
revenue,
quantity,
trend
from
(
SELECT
*,
ROW_NUMBER () OVER (
PARTITION BY level2
ORDER BY
SUM(revenue) DESC
) rownum,
SUM(revenue) OVER (PARTITION BY level2) total,
ROUND(SUM(revenue), 2) as revenue,
SUM(quantity) as quantity,
trend
FROM
(
select
*,
cast(past as float) / 3 as past,
round(
cast(present as float) /(cast(past as float) / 3.0),
2
) as trend
from
(
select
*,
sum(quantity) FILTER (
WHERE
year = %[1]s
and month between %[3]s
and %[4]s
) over (PARTITION BY description) as past,
sum(quantity) FILTER (
WHERE
year = %[1]s
and month = %[2]s
) over (PARTITION BY description) as present
from
data
)
)
where
year = %[1]s
and month = %[2]s
GROUP BY
description
ORDER BY
total DESC
)
WHERE
rownum <= 10
and revenue > 0
order by
(sum(revenue) over (
partition by level2
)) desc;

11
runner/sql/schema.sql Normal file
View File

@@ -0,0 +1,11 @@
CREATE TABLE IF NOT EXISTS data (
description text,
sku text,
level2 text,
level3 text,
revenue float,
cost float,
quantity integer,
year integer,
month integer
);

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)
}
}