More func

This commit is contained in:
2021-10-28 12:59:01 -05:00
parent 5092c10859
commit d105e88992
9 changed files with 102 additions and 25 deletions

36
get_top.go Normal file
View File

@@ -0,0 +1,36 @@
package main
import (
"database/sql"
_ "embed"
"fmt"
"time"
)
//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 {
//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 []GetTopResult
err := r.db.Select(&top, query_string)
checkErr(err)
return top
}

1
go.mod
View File

@@ -8,6 +8,7 @@ require (
)
require (
github.com/jmoiron/sqlx v1.3.4 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/richardlehane/mscfb v1.0.3 // indirect
github.com/richardlehane/msoleps v1.0.1 // indirect

5
go.sum
View File

@@ -1,5 +1,10 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/jmoiron/sqlx v1.3.4 h1:wv+0IJZfL5z0uZoUjlpKgHkgaFSYD+r9CfrXjEXsO7w=
github.com/jmoiron/sqlx v1.3.4/go.mod h1:2BljVx/86SuTyjE+aPYlHCTNvZrnJXghYGpNiXLBMCQ=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mattn/go-sqlite3 v1.14.8 h1:gDp86IdQsN/xWjIEmr9MF6o9mpksUgh0fu+9ByFxzIU=
github.com/mattn/go-sqlite3 v1.14.8/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=

View File

@@ -14,12 +14,21 @@ func checkErr(err error) {
func main() {
var (
excel_file string
get_top bool
month int
)
flag.StringVar(&excel_file, "file", "", "The path to the excel file for processing")
flag.BoolVar(&get_top, "get_top", false, "A command to get the top performers for the current month")
flag.IntVar(&month, "month", 0, "Sets the month for the data to process")
flag.Parse()
run := NewRunner()
if excel_file != "" {
ProcessExcel(excel_file)
} else if get_top {
run.GetTop()
} else {
flag.PrintDefaults()
}
}

View File

@@ -10,21 +10,12 @@ import (
_ "github.com/mattn/go-sqlite3"
"github.com/xuri/excelize/v2"
_ "embed"
)
var schema = `
CREATE TABLE IF NOT EXISTS data (
description text,
sku text,
level2 text,
level3 text,
revenue float,
cost float,
quantity integer,
year integer,
month integer
);
`
//go:embed sql/schema.sql
var schema string
type Data struct {
Description string

25
runner.go Normal file
View File

@@ -0,0 +1,25 @@
package main
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
}

View File

@@ -5,10 +5,7 @@ SELECT
level3,
rev,
qty,
trend,
sum(rev) over (
partition by level2
) theorder
trend
from
(
SELECT
@@ -37,22 +34,22 @@ from
*,
sum(quantity) FILTER (
WHERE
year = 2021
and month between 6
and 8
year = %[1]s
and month between %[3]s
and %[4]s
) over (PARTITION BY description) as past,
sum(quantity) FILTER (
WHERE
year = 2021
and month = 9
year = %[1]s
and month = %[2]s
) over (PARTITION BY description) as present
from
data
)
)
where
year = 2021
and month = 9
year = %[1]s
and month = %[2]s
GROUP BY
description
ORDER BY
@@ -62,4 +59,6 @@ WHERE
rownum <= 10
and rev > 0
order by
theorder desc;
(sum(rev) over (
partition by level2
)) desc;

11
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
);