Skip to content

Commit

Permalink
#115 Schedular: Schedular report sending same reports multiple time
Browse files Browse the repository at this point in the history
#115 Schedular: Schedular report sending same reports multiple time
  • Loading branch information
anildahalsussol committed Aug 18, 2023
1 parent 995ef81 commit 4ea0f14
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 20 deletions.
53 changes: 41 additions & 12 deletions pkg/datasource/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/pkg/errors"
)

func NewSchedule(ID string, interval int, nextReportTime int, name string, description string, lookback string, reportGroupID string, time string, day int, dateFormat string, datePosition string) Schedule {
func NewSchedule(ID string, interval int, nextReportTime int, name string, description string, lookback string, reportGroupID string, time string, day int, dateFormat string, datePosition string, status string) Schedule {
schedule := Schedule{
ID: ID,
Interval: interval,
Expand All @@ -23,6 +23,7 @@ func NewSchedule(ID string, interval int, nextReportTime int, name string, descr
PanelDetails: []ReportContent{},
DateFormat: dateFormat,
DatePosition: datePosition,
Status: status,
}
return schedule
}
Expand All @@ -46,10 +47,10 @@ func (datasource *MsupplyEresDatasource) GetSchedules() ([]Schedule, error) {
defer rows.Close()

for rows.Next() {
var ID, Name, Description, ReportGroupID, Time, Lookback, DateFormat, DatePosition string
var ID, Name, Description, ReportGroupID, Time, Lookback, DateFormat, DatePosition, Status string
var Day, Interval, NextReportTime int

err = rows.Scan(&ID, &Interval, &NextReportTime, &Name, &Description, &Lookback, &ReportGroupID, &Time, &Day, &DateFormat, &DatePosition)
err = rows.Scan(&ID, &Interval, &NextReportTime, &Name, &Description, &Lookback, &ReportGroupID, &Time, &Day, &DateFormat, &DatePosition, &Status)
if err != nil {
err = ereserror.New(500, errors.Wrap(err, frame.Function), "Could not scan schedule rows")
return nil, err
Expand All @@ -74,6 +75,7 @@ func (datasource *MsupplyEresDatasource) GetSchedules() ([]Schedule, error) {
PanelDetails: reportContent,
DateFormat: DateFormat,
DatePosition: DatePosition,
Status: Status,
}
schedules = append(schedules, schedule)
}
Expand All @@ -100,10 +102,10 @@ func (datasource *MsupplyEresDatasource) GetSchedule(id string) (*Schedule, erro
}

for rows.Next() {
var ID, Name, Description, ReportGroupID, Time, Lookback, DateFormat, DatePosition string
var ID, Name, Description, ReportGroupID, Time, Lookback, DateFormat, DatePosition, Status string
var Day, Interval, NextReportTime int

err = rows.Scan(&ID, &Interval, &NextReportTime, &Name, &Description, &Lookback, &ReportGroupID, &Time, &Day, &DateFormat, &DatePosition)
err = rows.Scan(&ID, &Interval, &NextReportTime, &Name, &Description, &Lookback, &ReportGroupID, &Time, &Day, &DateFormat, &DatePosition, &Status)
if err != nil {
log.DefaultLogger.Error("GetSchedules: rows.Scan(): ", err.Error())
return nil, err
Expand All @@ -128,6 +130,7 @@ func (datasource *MsupplyEresDatasource) GetSchedule(id string) (*Schedule, erro
PanelDetails: reportContent,
DateFormat: DateFormat,
DatePosition: DatePosition,
Status: Status,
}
schedules = append(schedules, schedule)
}
Expand All @@ -140,22 +143,46 @@ func (datasource *MsupplyEresDatasource) GetSchedule(id string) (*Schedule, erro

}

func (datasource *MsupplyEresDatasource) UpdateSchedule(id string, schedule Schedule) (*Schedule, error) {
func (datasource *MsupplyEresDatasource) UpdateSchedule(id string, status string, schedule Schedule) (*Schedule, error) {
db, err := sql.Open("sqlite", datasource.DataPath)
defer db.Close()
if err != nil {
log.DefaultLogger.Error("UpdateSchedule: sql.Open()", err.Error())
return nil, err
}

stmt, err := db.Prepare("UPDATE Schedule SET nextReportTime = ?, interval = ?, name = ?, description = ?, lookback = ?, reportGroupID = ?, time = ?, day = ?, dateFormat = ?, datePosition = ? where id = ?")
stmt, err := db.Prepare("UPDATE Schedule SET nextReportTime = ?, interval = ?, name = ?, description = ?, lookback = ?, reportGroupID = ?, time = ?, day = ?, dateFormat = ?, datePosition = ?, status = ? where id = ?")
if err != nil {
log.DefaultLogger.Error("UpdateSchedule: db.Prepare()", err.Error())
return nil, err
}

schedule.UpdateNextReportTime()
_, err = stmt.Exec(schedule.NextReportTime, schedule.Interval, schedule.Name, schedule.Description, schedule.Lookback, schedule.ReportGroupID, schedule.Time, schedule.Day, schedule.DateFormat, schedule.DatePosition, id)
_, err = stmt.Exec(schedule.NextReportTime, schedule.Interval, schedule.Name, schedule.Description, schedule.Lookback, schedule.ReportGroupID, schedule.Time, schedule.Day, schedule.DateFormat, schedule.DatePosition, schedule.Status, id)
defer stmt.Close()
if err != nil {
log.DefaultLogger.Error("UpdateSchedule: stmt.Exec()", err.Error())
return nil, err
}

return &schedule, nil
}

func (datasource *MsupplyEresDatasource) UpdateScheduleProgess(id string, status string, schedule Schedule) (*Schedule, error) {
db, err := sql.Open("sqlite", datasource.DataPath)
defer db.Close()
if err != nil {
log.DefaultLogger.Error("UpdateSchedule: sql.Open()", err.Error())
return nil, err
}

stmt, err := db.Prepare("UPDATE Schedule SET nextReportTime = ?, interval = ?, name = ?, description = ?, lookback = ?, reportGroupID = ?, time = ?, day = ?, dateFormat = ?, datePosition = ?, status = ? where id = ?")
if err != nil {
log.DefaultLogger.Error("UpdateSchedule: db.Prepare()", err.Error())
return nil, err
}

_, err = stmt.Exec(schedule.NextReportTime, schedule.Interval, schedule.Name, schedule.Description, schedule.Lookback, schedule.ReportGroupID, schedule.Time, schedule.Day, schedule.DateFormat, schedule.DatePosition, schedule.Status, id)
defer stmt.Close()
if err != nil {
log.DefaultLogger.Error("UpdateSchedule: stmt.Exec()", err.Error())
Expand Down Expand Up @@ -209,22 +236,24 @@ func (datasource *MsupplyEresDatasource) OverdueSchedules() ([]Schedule, error)
return nil, err
}

rows, err := db.Query("SELECT * FROM Schedule WHERE strftime(\"%s\", \"now\") > nextReportTime")
rows, err := db.Query("SELECT * FROM Schedule WHERE strftime(\"%s\", \"now\") > nextReportTime and status = \"success\"")
log.DefaultLogger.Info("executing SELECT * FROM Schedule WHERE strftime(\"%s\", \"now\") > nextReportTime and status = success")

if err != nil {
log.DefaultLogger.Error("OverdueSchedules: db.Query", err.Error())
return nil, err
}

var schedules []Schedule
for rows.Next() {
var ID, Name, Description, ReportGroupID, Time, Lookback, DateFormat, DatePosition string
var ID, Name, Description, ReportGroupID, Time, Lookback, DateFormat, DatePosition, Status string
var Day, Interval, NextReportTime int
err = rows.Scan(&ID, &Interval, &NextReportTime, &Name, &Description, &Lookback, &ReportGroupID, &Time, &Day, &DateFormat, &DatePosition)
err = rows.Scan(&ID, &Interval, &NextReportTime, &Name, &Description, &Lookback, &ReportGroupID, &Time, &Day, &DateFormat, &DatePosition, &Status)
if err != nil {
log.DefaultLogger.Error("OverdueSchedules: sql.Open", err.Error())
return nil, err
}
schedules = append(schedules, NewSchedule(ID, Interval, NextReportTime, Name, Description, Lookback, ReportGroupID, Time, Day, DateFormat, DatePosition))
schedules = append(schedules, NewSchedule(ID, Interval, NextReportTime, Name, Description, Lookback, ReportGroupID, Time, Day, DateFormat, DatePosition, Status))
}

return schedules, nil
Expand Down
9 changes: 5 additions & 4 deletions pkg/datasource/schedule_with_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Schedule struct {
PanelDetails []ReportContent `json:"panelDetails"`
DateFormat string `json:"dateFormat"`
DatePosition string `json:"datePosition"`
Status string `json:"status"`
}

type ReportContent struct {
Expand All @@ -44,7 +45,7 @@ func (datasource *MsupplyEresDatasource) CreateScheduleWithDetails(scheduleWithD
defer sqlClient.Db.Close()

if scheduleWithDetails.ID == "" {
stmt, err := sqlClient.Db.Prepare("INSERT INTO Schedule (id, nextReportTime, interval, name, description, lookback,reportGroupID,time,day,dateFormat, datePosition) VALUES (?,?,?,?,?,?,?,?,?,?,?) RETURNING *")
stmt, err := sqlClient.Db.Prepare("INSERT INTO Schedule (id, nextReportTime, interval, name, description, lookback,reportGroupID,time,day,dateFormat, datePosition, status) VALUES (?,?,?,?,?,?,?,?,?,?,?,?) RETURNING *")
if err != nil {
err = ereserror.New(500, errors.Wrap(err, frame.Function), "Could not create schedule record")
return nil, err
Expand All @@ -55,13 +56,13 @@ func (datasource *MsupplyEresDatasource) CreateScheduleWithDetails(scheduleWithD

scheduleWithDetails.UpdateNextReportTime()

_, err = stmt.Exec(scheduleWithDetails.ID, scheduleWithDetails.NextReportTime, scheduleWithDetails.Interval, scheduleWithDetails.Name, scheduleWithDetails.Description, scheduleWithDetails.Lookback, scheduleWithDetails.ReportGroupID, scheduleWithDetails.Time, scheduleWithDetails.Day, scheduleWithDetails.DateFormat, scheduleWithDetails.DatePosition)
_, err = stmt.Exec(scheduleWithDetails.ID, scheduleWithDetails.NextReportTime, scheduleWithDetails.Interval, scheduleWithDetails.Name, scheduleWithDetails.Description, scheduleWithDetails.Lookback, scheduleWithDetails.ReportGroupID, scheduleWithDetails.Time, scheduleWithDetails.Day, scheduleWithDetails.DateFormat, scheduleWithDetails.DatePosition, scheduleWithDetails.Status)
if err != nil {
err = ereserror.New(500, errors.Wrap(err, frame.Function), "Could not create schedule record")
return nil, err
}
} else {
stmt, err := sqlClient.Db.Prepare("UPDATE Schedule SET nextReportTime = ?, interval = ?, name = ?, description = ?, lookback = ?, reportGroupID = ?, time = ?, day = ?, dateFormat = ?, datePosition = ? where id = ? RETURNING *")
stmt, err := sqlClient.Db.Prepare("UPDATE Schedule SET nextReportTime = ?, interval = ?, name = ?, description = ?, lookback = ?, reportGroupID = ?, time = ?, day = ?, dateFormat = ?, datePosition = ?, status = ? where id = ? RETURNING *")
if err != nil {
err = ereserror.New(500, errors.Wrap(err, frame.Function), "Could not update schedule record")
return nil, err
Expand All @@ -70,7 +71,7 @@ func (datasource *MsupplyEresDatasource) CreateScheduleWithDetails(scheduleWithD

scheduleWithDetails.UpdateNextReportTime()

_, err = stmt.Exec(scheduleWithDetails.NextReportTime, scheduleWithDetails.Interval, scheduleWithDetails.Name, scheduleWithDetails.Description, scheduleWithDetails.Lookback, scheduleWithDetails.ReportGroupID, scheduleWithDetails.Time, scheduleWithDetails.Day, scheduleWithDetails.DateFormat, scheduleWithDetails.DatePosition, scheduleWithDetails.ID)
_, err = stmt.Exec(scheduleWithDetails.NextReportTime, scheduleWithDetails.Interval, scheduleWithDetails.Name, scheduleWithDetails.Description, scheduleWithDetails.Lookback, scheduleWithDetails.ReportGroupID, scheduleWithDetails.Time, scheduleWithDetails.Day, scheduleWithDetails.DateFormat, scheduleWithDetails.DatePosition, scheduleWithDetails.Status, scheduleWithDetails.ID)
if err != nil {
err = ereserror.New(500, errors.Wrap(err, frame.Function), "Could not update schedule record")
return nil, err
Expand Down
10 changes: 6 additions & 4 deletions pkg/report-emailer/report_emailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (re *ReportEmailer) Init() {

// Set up scheduler which will try to send reports every 10 minutes
c := cron.New()
c.AddFunc("@every 2m", func() {
c.AddFunc("@every 5m", func() {
re.CreateReports()
})

Expand Down Expand Up @@ -58,7 +58,8 @@ func (re *ReportEmailer) cleanup(schedules []datasource.Schedule) {
for _, schedule := range schedules {

log.DefaultLogger.Info(fmt.Sprintf("Deleting %s.xlsx...", schedule.Name))
path := GetFilePath(schedule.Name)
schedularFileName := GetFormattedFileName(schedule.Name, schedule.DateFormat, schedule.DatePosition)
path := GetFilePath(schedularFileName)
err := os.Remove(path)
if err != nil {
// Failure case shouldn't be much of a problem since we're using the schedule ID for the report name, at the moment
Expand All @@ -67,7 +68,7 @@ func (re *ReportEmailer) cleanup(schedules []datasource.Schedule) {
}

schedule.UpdateNextReportTime()
re.datasource.UpdateSchedule(schedule.ID, schedule)
re.datasource.UpdateSchedule(schedule.ID, "success", schedule)
}

re.inProgress = false
Expand All @@ -76,7 +77,6 @@ func (re *ReportEmailer) cleanup(schedules []datasource.Schedule) {
func (re *ReportEmailer) CreateReport(schedule datasource.Schedule, authConfig *auth.AuthConfig, datasourceID int, em Emailer) error {

log.DefaultLogger.Debug("ReportEmailer.createReport: start")

emails := make(map[string][]string)
panels := make(map[string][]api.TablePanel)

Expand Down Expand Up @@ -215,6 +215,8 @@ func (re *ReportEmailer) CreateReports() {
panels := make(map[string][]api.TablePanel)
for _, schedule := range schedules {
reportGroup, err := re.datasource.ReportGroupFromSchedule(schedule)
re.datasource.UpdateScheduleProgess(schedule.ID, "progress", schedule)

if err != nil {
log.DefaultLogger.Error("ReportEmailer.createReports: ReportGroupFromSchedule: " + err.Error())
return
Expand Down

0 comments on commit 4ea0f14

Please sign in to comment.