From 23134fdf3dc1cd8962d4336789610631a2cee842 Mon Sep 17 00:00:00 2001 From: yashsinghcodes Date: Sat, 30 Nov 2024 02:46:29 +0530 Subject: [PATCH] auto scale time window func --- shared.go | 47 +++++++++++++++++++++++++++++++++++++---------- structs.go | 17 ++++++++++++----- 2 files changed, 49 insertions(+), 15 deletions(-) diff --git a/shared.go b/shared.go index 0ed5f18..60ad6c5 100755 --- a/shared.go +++ b/shared.go @@ -5674,20 +5674,20 @@ func SetNewWorkflow(resp http.ResponseWriter, request *http.Request) { AppVersion: app.AppVersion, AppID: app.ID, LargeImage: app.LargeImage, - } + } - newAction.Position = Position{ - X: 449.5, - Y: 446, + newAction.Position = Position{ + X: 449.5, + Y: 446, + } + + newActions = append(newActions, newAction) } - newActions = append(newActions, newAction) + } else { + // figure out a way to activate Shuffle-Tools-Fork for everyone onprem } - } else { - // figure out a way to activate Shuffle-Tools-Fork for everyone onprem - } - } else { for _, item := range workflowapps { //log.Printf("NAME: %s", item.Name) @@ -17886,7 +17886,6 @@ func PrepareSingleAction(ctx context.Context, user User, fileId string, body []b } } - if runValidationAction { log.Printf("[INFO] Running validation action for %s for org %s (%s)", app.Name, user.ActiveOrg.Name, user.ActiveOrg.Id) @@ -29881,3 +29880,31 @@ func SendDeleteWorkflowRequest(childWorkflow Workflow, request *http.Request) er return nil } + +func NewTimeWindow(duration time.Duration) *TimeWindow { + return &TimeWindow{ + Duration: duration, + Events: []time.Time{}, + } +} + +func (tw *TimeWindow) AddEvent(event time.Time) { + tw.mu.Lock() + defer tw.mu.Unlock() + tw.Events = append(tw.Events, event) + tw.cleanOldEvents(event) +} + +func (tw *TimeWindow) CountEvents(now time.Time) int { + tw.mu.Lock() + defer tw.mu.Unlock() + tw.cleanOldEvents(now) + return len(tw.Events) +} + +func (tw *TimeWindow) cleanOldEvents(now time.Time) { + cutoff := now.Add(-tw.Duration) + for len(tw.Events) > 0 && tw.Events[0].Before(cutoff) { + tw.Events = tw.Events[1:] + } +} diff --git a/structs.go b/structs.go index 3481a5f..3609391 100755 --- a/structs.go +++ b/structs.go @@ -2,6 +2,7 @@ package shuffle import ( "encoding/xml" + "sync" "time" ) @@ -24,7 +25,7 @@ type LogRequest struct { } type PipelineRequest struct { - ID string `json:"id"` + ID string `json:"id"` Name string `json:"name"` Type string `json:"type"` Command string `json:"command"` @@ -39,7 +40,7 @@ type PipelineRequest struct { type Pipeline struct { Name string `json:"name" datastore:"name"` - ID string `json:"id" datastore:"id"` + ID string `json:"id" datastore:"id"` Type string `json:"type" datastore:"type"` Command string `json:"command" datastore:"command"` Environment string `json:"environment" datastore:"environment"` @@ -1263,14 +1264,14 @@ type InputQuestion struct { } type FormControl struct { - InputMarkdown string `json:"input_markdown" datastore:"input_markdown,noindex"` - OutputYields []string `json:"output_yields" datastore:"output_yields"` // Defines the nodes that will YIELD their output to the frontend during execution + InputMarkdown string `json:"input_markdown" datastore:"input_markdown,noindex"` + OutputYields []string `json:"output_yields" datastore:"output_yields"` // Defines the nodes that will YIELD their output to the frontend during execution FormWidth int64 `json:"form_width" datastore:"form_width"` } type Workflow struct { - WorkflowAsCode bool `json:"workflow_as_code" datastore:"workflow_as_code"` + WorkflowAsCode bool `json:"workflow_as_code" datastore:"workflow_as_code"` Actions []Action `json:"actions" datastore:"actions,noindex"` Branches []Branch `json:"branches" datastore:"branches,noindex"` VisualBranches []Branch `json:"visual_branches" datastore:"visual_branches,noindex"` @@ -4169,3 +4170,9 @@ type RequestResponse struct { Reason string `json:"reason"` Details string `json:"details"` } + +type TimeWindow struct { + Duration time.Duration + Events []time.Time + mu sync.Mutex +}