Skip to content

Commit

Permalink
feat: add withAgent to context
Browse files Browse the repository at this point in the history
[skip ci]
  • Loading branch information
adityathebe committed Jan 17, 2024
1 parent 097e733 commit 668bbb4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
17 changes: 17 additions & 0 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/flanksource/duty/types"
"github.com/flanksource/kommons"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/samber/lo"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"gorm.io/gorm"
Expand Down Expand Up @@ -96,6 +97,22 @@ func (k Context) User() *models.Person {
return v.(*models.Person)
}

// WithAgent sets the current session's agent in the context
func (k Context) WithAgent(agent models.Agent) Context {
k.GetSpan().SetAttributes(attribute.String("agent-id", agent.ID.String()))
return Context{
Context: k.WithValue("agent", agent),
}
}

func (k Context) Agent() *models.Agent {
v := k.Value("agent")
if v == nil {
return nil
}
return lo.ToPtr(v.(models.Agent))
}

func (k Context) WithTrace() Context {
k.Context = k.Context.WithTrace()
return k
Expand Down
4 changes: 3 additions & 1 deletion views/018_playbooks.sql
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ FOR EACH ROW
EXECUTE PROCEDURE notify_playbook_update();

-- List of all the playbooks that can be run by an agent
DROP VIEW IF EXISTS playbooks_for_agent;

CREATE OR REPLACE VIEW
playbooks_for_agent AS
WITH interim AS (
Expand All @@ -50,7 +52,7 @@ SELECT
interim.agent_name,
agents.person_id,
agents.id as agent_id,
json_agg(interim.id) AS playbook_ids
jsonb_agg(interim.id) AS playbook_ids
FROM
interim
INNER JOIN agents ON interim.agent_name :: TEXT = agents.name
Expand Down

0 comments on commit 668bbb4

Please sign in to comment.