More func
This commit is contained in:
36
get_top.go
Normal file
36
get_top.go
Normal 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
1
go.mod
@@ -8,6 +8,7 @@ require (
|
|||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/jmoiron/sqlx v1.3.4 // indirect
|
||||||
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
|
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
|
||||||
github.com/richardlehane/mscfb v1.0.3 // indirect
|
github.com/richardlehane/mscfb v1.0.3 // indirect
|
||||||
github.com/richardlehane/msoleps v1.0.1 // indirect
|
github.com/richardlehane/msoleps v1.0.1 // indirect
|
||||||
|
|||||||
5
go.sum
5
go.sum
@@ -1,5 +1,10 @@
|
|||||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
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/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 h1:gDp86IdQsN/xWjIEmr9MF6o9mpksUgh0fu+9ByFxzIU=
|
||||||
github.com/mattn/go-sqlite3 v1.14.8/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
|
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=
|
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
|
||||||
|
|||||||
9
main.go
9
main.go
@@ -14,12 +14,21 @@ func checkErr(err error) {
|
|||||||
func main() {
|
func main() {
|
||||||
var (
|
var (
|
||||||
excel_file string
|
excel_file string
|
||||||
|
get_top bool
|
||||||
|
month int
|
||||||
)
|
)
|
||||||
|
|
||||||
flag.StringVar(&excel_file, "file", "", "The path to the excel file for processing")
|
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()
|
flag.Parse()
|
||||||
|
|
||||||
|
run := NewRunner()
|
||||||
if excel_file != "" {
|
if excel_file != "" {
|
||||||
ProcessExcel(excel_file)
|
ProcessExcel(excel_file)
|
||||||
|
} else if get_top {
|
||||||
|
run.GetTop()
|
||||||
|
} else {
|
||||||
|
flag.PrintDefaults()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,21 +10,12 @@ import (
|
|||||||
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
"github.com/xuri/excelize/v2"
|
"github.com/xuri/excelize/v2"
|
||||||
|
|
||||||
|
_ "embed"
|
||||||
)
|
)
|
||||||
|
|
||||||
var schema = `
|
//go:embed sql/schema.sql
|
||||||
CREATE TABLE IF NOT EXISTS data (
|
var schema string
|
||||||
description text,
|
|
||||||
sku text,
|
|
||||||
level2 text,
|
|
||||||
level3 text,
|
|
||||||
revenue float,
|
|
||||||
cost float,
|
|
||||||
quantity integer,
|
|
||||||
year integer,
|
|
||||||
month integer
|
|
||||||
);
|
|
||||||
`
|
|
||||||
|
|
||||||
type Data struct {
|
type Data struct {
|
||||||
Description string
|
Description string
|
||||||
|
|||||||
25
runner.go
Normal file
25
runner.go
Normal 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
|
||||||
|
}
|
||||||
@@ -5,10 +5,7 @@ SELECT
|
|||||||
level3,
|
level3,
|
||||||
rev,
|
rev,
|
||||||
qty,
|
qty,
|
||||||
trend,
|
trend
|
||||||
sum(rev) over (
|
|
||||||
partition by level2
|
|
||||||
) theorder
|
|
||||||
from
|
from
|
||||||
(
|
(
|
||||||
SELECT
|
SELECT
|
||||||
@@ -37,22 +34,22 @@ from
|
|||||||
*,
|
*,
|
||||||
sum(quantity) FILTER (
|
sum(quantity) FILTER (
|
||||||
WHERE
|
WHERE
|
||||||
year = 2021
|
year = %[1]s
|
||||||
and month between 6
|
and month between %[3]s
|
||||||
and 8
|
and %[4]s
|
||||||
) over (PARTITION BY description) as past,
|
) over (PARTITION BY description) as past,
|
||||||
sum(quantity) FILTER (
|
sum(quantity) FILTER (
|
||||||
WHERE
|
WHERE
|
||||||
year = 2021
|
year = %[1]s
|
||||||
and month = 9
|
and month = %[2]s
|
||||||
) over (PARTITION BY description) as present
|
) over (PARTITION BY description) as present
|
||||||
from
|
from
|
||||||
data
|
data
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
where
|
where
|
||||||
year = 2021
|
year = %[1]s
|
||||||
and month = 9
|
and month = %[2]s
|
||||||
GROUP BY
|
GROUP BY
|
||||||
description
|
description
|
||||||
ORDER BY
|
ORDER BY
|
||||||
@@ -62,4 +59,6 @@ WHERE
|
|||||||
rownum <= 10
|
rownum <= 10
|
||||||
and rev > 0
|
and rev > 0
|
||||||
order by
|
order by
|
||||||
theorder desc;
|
(sum(rev) over (
|
||||||
|
partition by level2
|
||||||
|
)) desc;
|
||||||
11
sql/schema.sql
Normal file
11
sql/schema.sql
Normal 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
|
||||||
|
);
|
||||||
Reference in New Issue
Block a user