Skip to content

Commit

Permalink
fix: Change all fields with INTERVAL to HH:MM:SS format
Browse files Browse the repository at this point in the history
Signed-off-by: Ashish Kumar <ashishkr23438@gmail.com>
  • Loading branch information
krashish8 committed Dec 29, 2021
1 parent 6e8c70c commit 13b4805
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 38 deletions.
4 changes: 2 additions & 2 deletions internal/database/break.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ import (

type CreateBreakParams struct {
VehicleID *int64 `json:"vehicle_id,string" example:"1234567812345678" validate:"required" swaggerignore:"true"`
Service *int64 `json:"service" validate:"omitempty,min=0" example:"120"`
Service *string `json:"service" validate:"omitempty" example:"00:02:00"`
Data *interface{} `json:"data" swaggertype:"object,string" example:"key1:value1,key2:value2"`
}

type UpdateBreakParams struct {
VehicleID *int64 `json:"vehicle_id,string" example:"1234567812345678" swaggerignore:"true"`
Service *int64 `json:"service" validate:"omitempty,min=0" example:"120"`
Service *string `json:"service" validate:"omitempty" example:"00:02:00"`
Data *interface{} `json:"data" swaggertype:"object,string" example:"key1:value1,key2:value2"`
}

Expand Down
4 changes: 2 additions & 2 deletions internal/database/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func createResource(resource string, resourceStruct interface{}) (sql string, ar

// Convert any interval field to its type
if _, intervalFieldFound := util.IntervalFields[field]; intervalFieldFound {
val = val + "* '1 sec'::INTERVAL"
val = val + "::INTERVAL"
}

if i == 0 {
Expand All @@ -74,7 +74,7 @@ func updateResource(resource string, resourceStruct interface{}, id int64) (sql

// Convert any interval field to its type
if _, intervalFieldFound := util.IntervalFields[field]; intervalFieldFound {
val = val + "* '1 sec'::INTERVAL"
val = val + "::INTERVAL"
}

if i == 0 {
Expand Down
8 changes: 4 additions & 4 deletions internal/database/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ import (

type CreateJobParams struct {
Location *util.LocationParams `json:"location" validate:"required"`
Setup *int64 `json:"setup" validate:"omitempty,min=0" example:"0"`
Service *int64 `json:"service" validate:"omitempty,min=0" example:"120"`
Setup *string `json:"setup" validate:"omitempty" example:"00:00:00"`
Service *string `json:"service" validate:"omitempty" example:"00:02:00"`
Delivery *[]int64 `json:"delivery" validate:"omitempty,dive,min=0" example:"10,20"`
Pickup *[]int64 `json:"pickup" validate:"omitempty,dive,min=0" example:"5,15"`
Skills *[]int32 `json:"skills" validate:"omitempty,dive,min=0" example:"1,5"`
Expand All @@ -49,8 +49,8 @@ type CreateJobParams struct {

type UpdateJobParams struct {
Location *util.LocationParams `json:"location"`
Setup *int64 `json:"setup" validate:"omitempty,min=0" example:"0"`
Service *int64 `json:"service" validate:"omitempty,min=0" example:"120"`
Setup *string `json:"setup" validate:"omitempty" example:"00:00:00"`
Service *string `json:"service" validate:"omitempty" example:"00:02:00"`
Delivery *[]int64 `json:"delivery" validate:"omitempty,dive,min=0" example:"10,20"`
Pickup *[]int64 `json:"pickup" validate:"omitempty,dive,min=0" example:"5,15"`
Skills *[]int32 `json:"skills" validate:"omitempty,dive,min=0" example:"1,5"`
Expand Down
14 changes: 7 additions & 7 deletions internal/database/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
type Break struct {
ID int64 `json:"id,string" example:"1234567812345678"`
VehicleID int64 `json:"vehicle_id,string" example:"1234567812345678"`
Service int64 `json:"service" example:"120"`
Service string `json:"service" example:"00:02:00"`
Data interface{} `json:"data" swaggertype:"object,string" example:"key1:value1,key2:value2"`
CreatedAt string `json:"created_at" example:"2021-12-01 13:00:00"`
UpdatedAt string `json:"updated_at" example:"2021-12-01 13:00:00"`
Expand All @@ -52,8 +52,8 @@ type BreakTimeWindow struct {
type Job struct {
ID int64 `json:"id,string" example:"1234567812345678"`
Location util.LocationParams `json:"location"`
Setup int64 `json:"setup" example:"0"`
Service int64 `json:"service" example:"120"`
Setup string `json:"setup" example:"00:00:00"`
Service string `json:"service" example:"00:02:00"`
Delivery []int64 `json:"delivery" example:"10,20"`
Pickup []int64 `json:"pickup" example:"5,15"`
Skills []int32 `json:"skills" example:"1,5"`
Expand Down Expand Up @@ -83,11 +83,11 @@ type Project struct {
type Shipment struct {
ID int64 `json:"id,string" example:"1234567812345678"`
PLocation util.LocationParams `json:"p_location" `
PSetup int64 `json:"p_setup" example:"0"`
PService int64 `json:"p_service" example:"120"`
PSetup string `json:"p_setup" example:"00:00:00"`
PService string `json:"p_service" example:"00:02:00"`
DLocation util.LocationParams `json:"d_location"`
DSetup int64 `json:"d_setup" example:"0"`
DService int64 `json:"d_service" example:"120"`
DSetup string `json:"d_setup" example:"00:00:00"`
DService string `json:"d_service" example:"00:02:00"`
Amount []int64 `json:"amount" example:"5,15"`
Skills []int32 `json:"skills" example:"1,5"`
Priority int32 `json:"priority" example:"10"`
Expand Down
16 changes: 8 additions & 8 deletions internal/database/shipment.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ import (

type CreateShipmentParams struct {
PLocation *util.LocationParams `json:"p_location" validate:"required"`
PSetup *int64 `json:"p_setup" validate:"omitempty,min=0" example:"0"`
PService *int64 `json:"p_service" validate:"omitempty,min=0" example:"120"`
PSetup *string `json:"p_setup" validate:"omitempty" example:"00:00:00"`
PService *string `json:"p_service" validate:"omitempty" example:"00:02:00"`
DLocation *util.LocationParams `json:"d_location" validate:"required"`
DSetup *int64 `json:"d_setup" validate:"omitempty,min=0" example:"0"`
DService *int64 `json:"d_service" validate:"omitempty,min=0" example:"120"`
DSetup *string `json:"d_setup" validate:"omitempty" example:"00:00:00"`
DService *string `json:"d_service" validate:"omitempty" example:"00:02:00"`
Amount *[]int64 `json:"amount" validate:"omitempty,dive,min=0" example:"5,15"`
Skills *[]int32 `json:"skills" validate:"omitempty,dive,min=0" example:"1,5"`
Priority *int32 `json:"priority" validate:"omitempty,min=0,max=100" example:"10"`
Expand All @@ -51,11 +51,11 @@ type CreateShipmentParams struct {

type UpdateShipmentParams struct {
PLocation *util.LocationParams `json:"p_location"`
PSetup *int64 `json:"p_setup" validate:"omitempty,min=0" example:"0"`
PService *int64 `json:"p_service" validate:"omitempty,min=0" example:"120"`
PSetup *string `json:"p_setup" validate:"omitempty" example:"00:00:00"`
PService *string `json:"p_service" validate:"omitempty" example:"00:02:00"`
DLocation *util.LocationParams `json:"d_location"`
DSetup *int64 `json:"d_setup" validate:"omitempty,min=0" example:"0"`
DService *int64 `json:"d_service" validate:"omitempty,min=0" example:"120"`
DSetup *string `json:"d_setup" validate:"omitempty" example:"00:00:00"`
DService *string `json:"d_service" validate:"omitempty" example:"00:02:00"`
Amount *[]int64 `json:"amount" validate:"omitempty,dive,min=0" example:"5,15"`
Skills *[]int32 `json:"skills" validate:"omitempty,dive,min=0" example:"1,5"`
Priority *int32 `json:"priority" validate:"omitempty,min=0,max=100" example:"10"`
Expand Down
18 changes: 18 additions & 0 deletions internal/util/db_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ func HandleDBError(err error) error {
err = fmt.Errorf("Shipment with the given 'shipment_id' does not exist")
case "breaks_vehicle_id_fkey":
err = fmt.Errorf("Vehicle with the given 'vehicle_id' does not exist")

case "jobs_check":
err = fmt.Errorf("Field 'pickup' and 'delivery' must have same length")

case "breaks_time_windows_check":
err = fmt.Errorf("Field 'tw_open' must be less than or equal to field 'tw_close'")
case "jobs_time_windows_check":
Expand All @@ -60,12 +62,28 @@ func HandleDBError(err error) error {
err = fmt.Errorf("Field 'tw_open' must be less than or equal to field 'tw_close'")
case "shipments_time_windows_check":
err = fmt.Errorf("Field 'tw_open' must be less than or equal to field 'tw_close'")

case "jobs_time_windows_pkey":
err = fmt.Errorf("Jobs time window with given values already exist")
case "shipments_time_windows_pkey":
err = fmt.Errorf("Shipments time window with given values already exist")
case "breaks_time_windows_pkey":
err = fmt.Errorf("Breaks time window with given values already exist")

case "jobs_service_check":
err = fmt.Errorf("Field 'service' must be non-negative with the format 'HH:MM:SS'")
case "jobs_setup_check":
err = fmt.Errorf("Field 'setup' must be non-negative with the format 'HH:MM:SS'")
case "shipments_p_service_check":
err = fmt.Errorf("Field 'p_service' must be non-negative with the format 'HH:MM:SS'")
case "shipments_d_service_check":
err = fmt.Errorf("Field 'd_service' must be non-negative with the format 'HH:MM:SS'")
case "shipments_p_setup_check":
err = fmt.Errorf("Field 'p_setup' must be non-negative with the format 'HH:MM:SS'")
case "shipments_d_setup_check":
err = fmt.Errorf("Field 'd_setup' must be non-negative with the format 'HH:MM:SS'")
case "breaks_service_check":
err = fmt.Errorf("Field 'service' must be non-negative with the format 'HH:MM:SS'")
}
}
}
Expand Down
21 changes: 7 additions & 14 deletions internal/util/format_ical.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ type Schedule struct {
Location LocationParams `json:"location"`
Arrival string `json:"arrival" example:"2021-12-01 13:00:00"`
Departure string `json:"departure" example:"2021-12-01 13:00:00"`
TravelTime int64 `json:"travel_time" example:"1000"`
SetupTime int64 `json:"setup_time" example:"0"`
ServiceTime int64 `json:"service_time" example:"120"`
WaitingTime int64 `json:"waiting_time" example:"0"`
TravelTime string `json:"travel_time" example:"00:16:40"`
SetupTime string `json:"setup_time" example:"00:00:00"`
ServiceTime string `json:"service_time" example:"00:02:00"`
WaitingTime string `json:"waiting_time" example:"00:00:00"`
Load []int64 `json:"load" example:"0,0"`
VehicleData interface{} `json:"vehicle_data" swaggertype:"object,string" example:"key1:value1,key2:value2"`
TaskData interface{} `json:"task_data" swaggertype:"object,string" example:"key1:value1,key2:value2"`
Expand Down Expand Up @@ -85,13 +85,6 @@ func parseTime(time_str string) time.Time {
return t
}

func secondsToTime(totalSecs int64) string {
hours := totalSecs / 3600
minutes := (totalSecs % 3600) / 60
seconds := totalSecs % 60
return fmt.Sprintf("%02d:%02d:%02d", hours, minutes, seconds)
}

func getSummary(schedule Schedule) string {
return fmt.Sprintf("%s - Vehicle %d", strings.Title(schedule.Type), schedule.VehicleID)
}
Expand All @@ -104,9 +97,9 @@ func getDescription(schedule Schedule) string {
desc := fmt.Sprintf("Project ID: %d\n", schedule.ProjectID)
desc += fmt.Sprintf("Vehicle ID: %d\n", schedule.VehicleID)
desc += fmt.Sprintf("Task ID: %d\n", schedule.TaskID)
desc += fmt.Sprintf("Travel Time: %s\n", secondsToTime(schedule.TravelTime))
desc += fmt.Sprintf("Service Time: %s\n", secondsToTime(schedule.ServiceTime))
desc += fmt.Sprintf("Waiting Time: %s\n", secondsToTime(schedule.WaitingTime))
desc += fmt.Sprintf("Travel Time: %s\n", schedule.TravelTime)
desc += fmt.Sprintf("Service Time: %s\n", schedule.ServiceTime)
desc += fmt.Sprintf("Waiting Time: %s\n", schedule.WaitingTime)
desc += fmt.Sprintf("Load: %d\n", schedule.Load)
return desc
}
Expand Down
2 changes: 1 addition & 1 deletion internal/util/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func GetOutputFields(resourceStruct interface{}) (sql string) {
}

if _, intervalFieldFound := IntervalFields[fieldName]; intervalFieldFound {
fieldName = fmt.Sprintf("EXTRACT(epoch FROM %s)", fieldName)
fieldName = fmt.Sprintf("to_char(%s, 'HH24:MI:SS')", fieldName)
}
if _, timestampFieldFound := TimestampFields[fieldName]; timestampFieldFound {
fieldName = fmt.Sprintf("to_char(%s, 'YYYY-MM-DD HH24:MI:SS')", fieldName)
Expand Down

0 comments on commit 13b4805

Please sign in to comment.