From d105e88992a43ce1de0aae8d2842e9fc17969bce Mon Sep 17 00:00:00 2001 From: RobViren Date: Thu, 28 Oct 2021 12:59:01 -0500 Subject: [PATCH] More func --- get_top.go | 36 ++++++++++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 5 +++++ main.go | 9 +++++++++ process_excel.go | 17 ++++------------- runner.go | 25 +++++++++++++++++++++++++ CFY.sql => sql/CFY.sql | 0 top.sql => sql/get_top.sql | 23 +++++++++++------------ sql/schema.sql | 11 +++++++++++ 9 files changed, 102 insertions(+), 25 deletions(-) create mode 100644 get_top.go create mode 100644 runner.go rename CFY.sql => sql/CFY.sql (100%) rename top.sql => sql/get_top.sql (80%) create mode 100644 sql/schema.sql diff --git a/get_top.go b/get_top.go new file mode 100644 index 0000000..2be5fe4 --- /dev/null +++ b/get_top.go @@ -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 +} diff --git a/go.mod b/go.mod index 9f4d83e..f570a4f 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 6f3637c..c2a3ed5 100644 --- a/go.sum +++ b/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/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= diff --git a/main.go b/main.go index 0c357e9..d0b9cd1 100644 --- a/main.go +++ b/main.go @@ -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() } } diff --git a/process_excel.go b/process_excel.go index 04d1371..30b2658 100644 --- a/process_excel.go +++ b/process_excel.go @@ -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 diff --git a/runner.go b/runner.go new file mode 100644 index 0000000..1cd5b7e --- /dev/null +++ b/runner.go @@ -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 +} diff --git a/CFY.sql b/sql/CFY.sql similarity index 100% rename from CFY.sql rename to sql/CFY.sql diff --git a/top.sql b/sql/get_top.sql similarity index 80% rename from top.sql rename to sql/get_top.sql index cba36ad..d05f79b 100644 --- a/top.sql +++ b/sql/get_top.sql @@ -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; \ No newline at end of file + (sum(rev) over ( + partition by level2 + )) desc; \ No newline at end of file diff --git a/sql/schema.sql b/sql/schema.sql new file mode 100644 index 0000000..bbe6052 --- /dev/null +++ b/sql/schema.sql @@ -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 +); \ No newline at end of file