Skip to content

Commit

Permalink
cron job (#3)
Browse files Browse the repository at this point in the history
* cron job

* cron job

* changes updated

* removed from go.sum
  • Loading branch information
rajkumar081997 committed Dec 28, 2020
1 parent 1ab7b93 commit 54eaf81
Showing 1 changed file with 40 additions and 24 deletions.
64 changes: 40 additions & 24 deletions postgres/postgre.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import (
"encoding/json"
"errors"
"fmt"
logger "github.com/ipfs/go-log/v2"
"github.com/lib/pq"
"io/ioutil"
"os"
"strconv"
"time"

logger "github.com/ipfs/go-log/v2"
_ "github.com/lib/pq"
)

var tableDoesNotExist string = "undefined_table"

type insertdata struct {
Project string
Key string
Expand All @@ -26,7 +28,6 @@ type Out struct {
Key string `json:"public-key"`
Ip string `json:"ip"`
Hash string `json:"hash"`
Timestamp string `json:"timestamp"`
}

type mClientInsertdata struct {
Expand Down Expand Up @@ -88,19 +89,13 @@ func GenerateIndex(
ip string,
hashvalue string,
) (*Out, error) {

insertdata := newConfig(projectid, key, ip, hashvalue)
timestamp := time.Now().Unix()
bcn, err := getBCN(timestamp, db)
if err != nil {
log.Error("Unable to get bcn %s", err.Error())
return nil, err
}
err = createTable(db, bcn)
if err != nil {
log.Errorf("Unable to create table %s", err.Error())
return nil, err
}
jsonData, err := insertion(db, bcn, insertdata)
if err != nil {
log.Errorf("Unable to insert data %s", err.Error())
Expand Down Expand Up @@ -201,29 +196,50 @@ func createTable(db *sql.DB, bcn int64) error {
return nil
}

func insertion(db *sql.DB, bcn int64, insertdata *insertdata) (*Out, error) {
func subInsertion(db *sql.DB, bcn int64, insertdata *insertdata) (string, error) {
tablename := fmt.Sprintf("downloads_requests_%#v", bcn)
query := fmt.Sprintf(`insert into %s (projectId,publicKey,ip,hash)VALUES($1,$2,$3,$4) returning downloadindex`, tablename)
var id string
err := db.QueryRow(query, insertdata.Project, insertdata.Key, insertdata.Ip, insertdata.Hash).Scan(&id)
if err != nil {
log.Errorf("Unable to excute insert query %s", err.Error())
return nil, err
return "", err
}
return id, nil
}

func insertion(db *sql.DB, bcn int64, insertdata *insertdata) (*Out, error) {
id, err := subInsertion(db, bcn, insertdata)
if err != nil {
if err, ok := err.(*pq.Error); ok {
if err.Code.Name() == tableDoesNotExist {
err := createTable(db, bcn)
if err != nil {
log.Errorf("Failed to create table", err.Error())
return nil, err
}
id, err = subInsertion(db, bcn, insertdata)
if err != nil {
log.Errorf("Unable to execute insert query after manually table creation %s", err.Error())
return nil, err
}
} else {
log.Errorf("Unable to excute insert query %s", err.Error())
return nil, err
}

}
}
dataretrive := fmt.Sprintf(`select * from %s where downloadindex = %s`, tablename, id)
rows, err := db.Query(dataretrive)
indx, err := strconv.ParseInt(id, 10, 64)
if err != nil {
log.Error("Data retrival from select is failed %s", err.Error())
log.Errorf("Failed to convert string to in64 %s", err.Error())
return nil, err
}
defer rows.Close()
result := &Out{}
for rows.Next() {
err := rows.Scan(&result.Downloadindex, &result.Project, &result.Key, &result.Ip, &result.Hash, &result.Timestamp)
if err != nil {
log.Errorf("Unable to get resultant tuple from database %s", err.Error())
return nil, err
}
result := &Out{
Downloadindex: indx,
Project: insertdata.Project,
Ip: insertdata.Ip,
Key: insertdata.Key,
Hash: insertdata.Hash,
}
return result, nil
}

0 comments on commit 54eaf81

Please sign in to comment.