|
| 1 | +package models |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "sync" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/nyaruka/goflow/legacy" |
| 10 | + "github.com/nyaruka/goflow/utils" |
| 11 | + "github.com/sirupsen/logrus" |
| 12 | + |
| 13 | + "github.com/jmoiron/sqlx" |
| 14 | + "github.com/nyaruka/goflow/flows" |
| 15 | +) |
| 16 | + |
| 17 | +type OrgID int |
| 18 | + |
| 19 | +// OrgAssets is the set of assets for an organization. These are loaded lazily from the database as asked for. |
| 20 | +// OrgAssets are thread safe and can be shared across goroutines. |
| 21 | +// OrgAssets implement the flows.SessionAssets interface. |
| 22 | +type OrgAssets struct { |
| 23 | + ctx context.Context |
| 24 | + db *sqlx.DB |
| 25 | + orgID OrgID |
| 26 | + env utils.Environment |
| 27 | + |
| 28 | + channels *flows.ChannelSet |
| 29 | + channelsByID map[flows.ChannelID]flows.Channel |
| 30 | + channelOnce sync.Once |
| 31 | + |
| 32 | + fields *flows.FieldSet |
| 33 | + fieldsByUUID map[FieldUUID]*flows.Field |
| 34 | + fieldsLock sync.RWMutex |
| 35 | + |
| 36 | + groups *flows.GroupSet |
| 37 | + groupsByID map[flows.GroupID]*flows.Group |
| 38 | + groupOnce sync.Once |
| 39 | + |
| 40 | + labels *flows.LabelSet |
| 41 | + labelsLock sync.RWMutex |
| 42 | + |
| 43 | + resthooks *flows.ResthookSet |
| 44 | + resthooksLock sync.RWMutex |
| 45 | + |
| 46 | + flows map[flows.FlowUUID]flows.Flow |
| 47 | + flowIDs map[flows.FlowUUID]FlowID |
| 48 | + flowsLock sync.RWMutex |
| 49 | + |
| 50 | + // TODO: implement locations |
| 51 | + locations *flows.LocationHierarchySet |
| 52 | + locationsLock sync.RWMutex |
| 53 | +} |
| 54 | + |
| 55 | +func NewOrgAssets(ctx context.Context, db *sqlx.DB, orgID OrgID) *OrgAssets { |
| 56 | + return &OrgAssets{ |
| 57 | + ctx: ctx, |
| 58 | + db: db, |
| 59 | + orgID: orgID, |
| 60 | + env: utils.NewDefaultEnvironment(), |
| 61 | + |
| 62 | + flows: make(map[flows.FlowUUID]flows.Flow), |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func (o *OrgAssets) GetOrgID() OrgID { |
| 67 | + return o.orgID |
| 68 | +} |
| 69 | + |
| 70 | +func (o *OrgAssets) GetChannelByID(id flows.ChannelID) (flows.Channel, error) { |
| 71 | + _, err := o.GetChannelSet() |
| 72 | + if err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + channel, found := o.channelsByID[id] |
| 76 | + if !found { |
| 77 | + return nil, fmt.Errorf("no channel found with ID: %d", id) |
| 78 | + } |
| 79 | + return channel, nil |
| 80 | +} |
| 81 | + |
| 82 | +func (o *OrgAssets) GetChannel(uuid flows.ChannelUUID) (flows.Channel, error) { |
| 83 | + cs, err := o.GetChannelSet() |
| 84 | + if err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + |
| 88 | + return cs.FindByUUID(uuid), nil |
| 89 | +} |
| 90 | + |
| 91 | +func (o *OrgAssets) GetChannelSet() (*flows.ChannelSet, error) { |
| 92 | + return o.channels, nil |
| 93 | +} |
| 94 | + |
| 95 | +func (o *OrgAssets) GetFieldByUUID(uuid FieldUUID) (*flows.Field, error) { |
| 96 | + _, err := o.GetFieldSet() |
| 97 | + if err != nil { |
| 98 | + return nil, err |
| 99 | + } |
| 100 | + field, found := o.fieldsByUUID[uuid] |
| 101 | + if !found { |
| 102 | + return nil, nil |
| 103 | + } |
| 104 | + |
| 105 | + return field, nil |
| 106 | +} |
| 107 | + |
| 108 | +func (o *OrgAssets) GetField(key string) (*flows.Field, error) { |
| 109 | + fs, err := o.GetFieldSet() |
| 110 | + if err != nil { |
| 111 | + return nil, err |
| 112 | + } |
| 113 | + return fs.FindByKey(key), nil |
| 114 | +} |
| 115 | + |
| 116 | +func (o *OrgAssets) GetFieldSet() (*flows.FieldSet, error) { |
| 117 | + return o.fields, nil |
| 118 | +} |
| 119 | + |
| 120 | +func (o *OrgAssets) GetFlow(uuid flows.FlowUUID) (flows.Flow, error) { |
| 121 | + o.flowsLock.RLock() |
| 122 | + flow, found := o.flows[uuid] |
| 123 | + o.flowsLock.RUnlock() |
| 124 | + if found { |
| 125 | + return flow, nil |
| 126 | + } |
| 127 | + |
| 128 | + flow, err := o.loadFlow(uuid) |
| 129 | + if err != nil { |
| 130 | + return nil, err |
| 131 | + } |
| 132 | + o.flowsLock.Lock() |
| 133 | + o.flows[uuid] = flow |
| 134 | + o.flowsLock.Unlock() |
| 135 | + |
| 136 | + return flow, nil |
| 137 | +} |
| 138 | + |
| 139 | +func (o *OrgAssets) GetFlowID(uuid flows.FlowUUID) (FlowID, error) { |
| 140 | + o.flowsLock.RLock() |
| 141 | + flowID, found := o.flowIDs[uuid] |
| 142 | + o.flowsLock.RUnlock() |
| 143 | + if !found { |
| 144 | + return -1, fmt.Errorf("no flow known with uuid: %s", uuid) |
| 145 | + } |
| 146 | + return flowID, nil |
| 147 | +} |
| 148 | + |
| 149 | +func (o *OrgAssets) GetGroupByID(id flows.GroupID) (*flows.Group, error) { |
| 150 | + _, err := o.GetGroupSet() |
| 151 | + if err != nil { |
| 152 | + return nil, err |
| 153 | + } |
| 154 | + group, found := o.groupsByID[id] |
| 155 | + if !found { |
| 156 | + return nil, fmt.Errorf("no group found with id: %d", id) |
| 157 | + } |
| 158 | + return group, nil |
| 159 | +} |
| 160 | + |
| 161 | +func (o *OrgAssets) GetGroup(uuid flows.GroupUUID) (*flows.Group, error) { |
| 162 | + gs, err := o.GetGroupSet() |
| 163 | + if err != nil { |
| 164 | + return nil, err |
| 165 | + } |
| 166 | + return gs.FindByUUID(uuid), nil |
| 167 | +} |
| 168 | + |
| 169 | +func (o *OrgAssets) GetGroupSet() (*flows.GroupSet, error) { |
| 170 | + return o.groups, nil |
| 171 | +} |
| 172 | + |
| 173 | +func (o *OrgAssets) GetLabel(uuid flows.LabelUUID) (*flows.Label, error) { |
| 174 | + ls, err := o.GetLabelSet() |
| 175 | + if err != nil { |
| 176 | + return nil, err |
| 177 | + } |
| 178 | + return ls.FindByUUID(uuid), nil |
| 179 | +} |
| 180 | + |
| 181 | +func (o *OrgAssets) GetLabelSet() (*flows.LabelSet, error) { |
| 182 | + return o.labels, nil |
| 183 | +} |
| 184 | + |
| 185 | +func (o *OrgAssets) HasLocations() bool { |
| 186 | + return false |
| 187 | +} |
| 188 | + |
| 189 | +func (o *OrgAssets) GetLocationHierarchySet() (*flows.LocationHierarchySet, error) { |
| 190 | + return nil, nil |
| 191 | +} |
| 192 | + |
| 193 | +func (o *OrgAssets) GetResthookSet() (*flows.ResthookSet, error) { |
| 194 | + return o.resthooks, nil |
| 195 | +} |
| 196 | + |
| 197 | +const selectFlowSQL = ` |
| 198 | +SELECT |
| 199 | + fr.definition::jsonb || |
| 200 | + jsonb_build_object( |
| 201 | + 'flow_type', f.flow_type, |
| 202 | + 'metadata', jsonb_build_object( |
| 203 | + 'uuid', f.uuid, |
| 204 | + 'id', f.id, |
| 205 | + 'name', f.name, |
| 206 | + 'revision', fr.revision, |
| 207 | + 'expires', f.expires_after_minutes |
| 208 | + ) |
| 209 | + ) as definition |
| 210 | +FROM |
| 211 | + flows_flowrevision fr, |
| 212 | + flows_flow f |
| 213 | +WHERE |
| 214 | + f.uuid = $1 AND |
| 215 | + fr.flow_id = f.id AND |
| 216 | + fr.is_active = TRUE AND |
| 217 | + f.is_active = TRUE |
| 218 | +ORDER BY |
| 219 | + revision DESC LIMIT 1;` |
| 220 | + |
| 221 | +// loads the flow with the passed in UUID |
| 222 | +func (o *OrgAssets) loadFlow(uuid flows.FlowUUID) (flows.Flow, error) { |
| 223 | + ctx, cancel := context.WithTimeout(o.ctx, time.Second*15) |
| 224 | + defer cancel() |
| 225 | + |
| 226 | + var definition string |
| 227 | + err := o.db.GetContext(ctx, &definition, selectFlowSQL, uuid) |
| 228 | + if err != nil { |
| 229 | + return nil, err |
| 230 | + } |
| 231 | + |
| 232 | + // load it in from our json |
| 233 | + legacyFlow, err := legacy.ReadLegacyFlow([]byte(definition)) |
| 234 | + if err != nil { |
| 235 | + logrus.WithField("definition", definition).WithError(err).Error("error loading flow") |
| 236 | + return nil, err |
| 237 | + } |
| 238 | + |
| 239 | + // migrate forwards returning our final flow definition |
| 240 | + flow, err := legacyFlow.Migrate(false, false) |
| 241 | + return flow, err |
| 242 | +} |
0 commit comments