From 4466d7328e8acf37340ee0722bf4d418a3ca3c50 Mon Sep 17 00:00:00 2001 From: Adriano Date: Tue, 11 May 2021 13:57:34 -0300 Subject: [PATCH] Show company name Fix js toggle --- fetch/fetch_fii.go | 37 ++++++++++++++++++++--------------- interfaces.go | 2 +- parsers/fiidb.go | 34 ++++++++++++++++++++------------ parsers/tables.go | 3 ++- reports/reports_html.go | 10 ++++++++++ reports/templates/fii.html | 20 +++++++++++++++++-- reports/templates/layout.html | 4 ++-- 7 files changed, 76 insertions(+), 34 deletions(-) diff --git a/fetch/fetch_fii.go b/fetch/fetch_fii.go index 0c8293d..123984f 100644 --- a/fetch/fetch_fii.go +++ b/fetch/fetch_fii.go @@ -187,10 +187,12 @@ func (fii *FII) reportIDs(code string, n int) ([]id, error) { // Parameters to list the report IDs for the last 'n' dividend reports timestamp := strconv.FormatInt(int64(time.Now().UnixNano()/1e6), 10) - cnpj, err := fii.CNPJ(code) + d, err := fii.Details(code) if err != nil { + fii.log.Debug("[reportID] error on fii.Details(%s): %v", code, err) return nil, err } + cnpj := d.DetailFund.CNPJ v := url.Values{ "d": []string{"2"}, "s": []string{"0"}, @@ -273,22 +275,22 @@ func (fii *FII) dividendReport(code string, ids []id) (*[]rapina.Dividend, error } // -// CNPJ returns the FII CNPJ from DB. If not found: -// fetches from server, stores it in the DB and returns the CNPJ. +// Details returns the FII Details from DB. If not found: +// fetches from server, stores it in the DB and returns the Details. // -func (fii *FII) CNPJ(fiiCode string) (string, error) { +func (fii *FII) Details(fiiCode string) (*rapina.FIIDetails, error) { if len(fiiCode) != 4 && len(fiiCode) != 6 { - return "", fmt.Errorf("wrong code '%s'", fiiCode) + return nil, fmt.Errorf("wrong code '%s'", fiiCode) } - cnpj, err := fii.parser.CNPJ(fiiCode) - if err != nil { - return "", err - } - if cnpj != "" { - return cnpj, nil + details, err := fii.parser.Details(fiiCode) + if err == nil && details.DetailFund.CNPJ != "" { + fii.log.Debug("CPNJ encontrado: %s", details.DetailFund.CNPJ) + return details, nil } + fii.log.Debug("FII details não encontrado no bd. Consultando web...") + // Fetch from server if not found in the database data := fmt.Sprintf(`{"typeFund":7,"cnpj":"0","identifierFund":"%s"}`, fiiCode[0:4]) enc := base64.URLEncoding.EncodeToString([]byte(data)) @@ -306,20 +308,23 @@ func (fii *FII) CNPJ(fiiCode string) (string, error) { resp, err := client.Get(fundDetailURL) if err != nil { - return "", err + return details, err } defer resp.Body.Close() if resp.StatusCode != 200 { - return "", fmt.Errorf("%s: %s", resp.Status, fundDetailURL) + return details, fmt.Errorf("%s: %s", resp.Status, fundDetailURL) } body, err := ioutil.ReadAll(resp.Body) if err != nil { - return "", errors.Wrap(err, "read body") + return details, errors.Wrap(err, "read body") } - _ = fii.parser.StoreFIIDetails(body) + err = fii.parser.StoreFIIDetails(body) + if err != nil { + return details, errors.Wrap(err, "store details") + } - return fii.parser.CNPJ(fiiCode) + return fii.parser.Details(fiiCode) } diff --git a/interfaces.go b/interfaces.go index 4131d47..5dd5fca 100644 --- a/interfaces.go +++ b/interfaces.go @@ -15,7 +15,7 @@ import "io" // SaveDividend parses and stores a fetched stream with dividends data and // returns a structured dividend object. type FIIParser interface { - CNPJ(code string) (string, error) + Details(code string) (*FIIDetails, error) StoreFIIDetails(stream []byte) error Dividends(code, monthYear string) (*[]Dividend, error) SaveDividend(stream map[string]string) (*Dividend, error) diff --git a/parsers/fiidb.go b/parsers/fiidb.go index 0b970ab..6afe286 100644 --- a/parsers/fiidb.go +++ b/parsers/fiidb.go @@ -64,38 +64,48 @@ func (fii *FIIParser) StoreFIIDetails(stream []byte) error { fii.mu.Lock() defer fii.mu.Unlock() - insert := "INSERT OR IGNORE INTO fii_details (cnpj, acronym, trading_code) VALUES (?,?,?)" - _, err := fii.db.Exec(insert, x.CNPJ, x.Acronym, x.TradingCode) + insert := `INSERT OR IGNORE INTO fii_details + (cnpj, acronym, trading_code, json) + VALUES (?,?,?,?);` + _, err := fii.db.Exec(insert, + x.CNPJ, x.Acronym, x.TradingCode, stream) return err } // -// CNPJ returns the FII CNPJ for the 'code' or +// Details returns the FII Details for the 'code' or // an empty string if not found in the db. // -func (fii *FIIParser) CNPJ(code string) (string, error) { +func (fii *FIIParser) Details(code string) (*rapina.FIIDetails, error) { + details := rapina.FIIDetails{} + if fii.db == nil { - return "", ErrDBUnset + return nil, ErrDBUnset } var query string if len(code) == 4 { - query = `SELECT cnpj FROM fii_details WHERE acronym=?` + query = `SELECT json FROM fii_details WHERE acronym=?` } else if len(code) == 6 { - query = `SELECT cnpj FROM fii_details WHERE trading_code=?` + query = `SELECT json FROM fii_details WHERE trading_code=?` } else { - return "", fmt.Errorf("invalid code '%s'", code) + return nil, fmt.Errorf("invalid code '%s'", code) } - var cnpj string + var jsonStr []byte row := fii.db.QueryRow(query, code) - err := row.Scan(&cnpj) + err := row.Scan(&jsonStr) if err != nil && err != sql.ErrNoRows { - return "", err + return nil, err + } + + if err := json.Unmarshal(jsonStr, &details); err != nil { + fii.log.Error("FII details [%v]: %s\n", err, string(jsonStr)) + return nil, errors.Wrap(err, "json unmarshal") } - return cnpj, nil + return &details, nil } // diff --git a/parsers/tables.go b/parsers/tables.go index e3e2606..d66a8d1 100644 --- a/parsers/tables.go +++ b/parsers/tables.go @@ -79,7 +79,8 @@ var createTableMap = map[string]string{ ( cnpj TEXT NOT NULL PRIMARY KEY, acronym varchar(4), - trading_code varchar(6) + trading_code varchar(6), + json varchar );`, "stock_quotes": `CREATE TABLE IF NOT EXISTS stock_quotes diff --git a/reports/reports_html.go b/reports/reports_html.go index e145d1c..8ade973 100644 --- a/reports/reports_html.go +++ b/reports/reports_html.go @@ -131,6 +131,7 @@ func serveTemplate(w http.ResponseWriter, r *http.Request, srv *Server) { type data struct { Code string + Name string Values []value } type value struct { @@ -174,8 +175,17 @@ func fiiDividends(srv *Server, codes []string, n int) *[]data { values = append(values, v) } + details, err := srv.fetchFII.Details(code) + var name string + if err == nil { + name = details.DetailFund.CompanyName + } else { + srv.log.Debug("Nome não encontrado: %v", err) + } + d := data{ Code: code, + Name: name, Values: values, } diff --git a/reports/templates/fii.html b/reports/templates/fii.html index 13eb3dc..b51a2da 100644 --- a/reports/templates/fii.html +++ b/reports/templates/fii.html @@ -2,14 +2,15 @@