feat(assist-api): adapted body request parser to current chalice worker schema
This commit is contained in:
parent
f25575a0a8
commit
bcb3b407e8
2 changed files with 27 additions and 24 deletions
|
|
@ -16,17 +16,24 @@ type Query struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Filter struct {
|
type Filter struct {
|
||||||
Type string `json:"type"`
|
Value []string `json:"values"`
|
||||||
Value []string `json:"value"`
|
|
||||||
Operator string `json:"operator"` // is|contains
|
Operator string `json:"operator"` // is|contains
|
||||||
Source string `json:"source"` // for metadata only
|
}
|
||||||
|
|
||||||
|
type Pagination struct {
|
||||||
|
Limit int `json:"limit"`
|
||||||
|
Page int `json:"page"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Sort struct {
|
||||||
|
Key string `json:"key"` // useless
|
||||||
|
Order string `json:"order"` // [ASC|DESC]
|
||||||
}
|
}
|
||||||
|
|
||||||
type Request struct {
|
type Request struct {
|
||||||
Filters []Filter `json:"filters"`
|
Filters map[string]Filter `json:"filter"`
|
||||||
Order string `json:"order"` // sort.order [asc|desc]
|
Pagination Pagination `json:"pagination"`
|
||||||
Limit int `json:"limit"` // pagination.limit
|
Sort Sort `json:"sort"`
|
||||||
Page int `json:"page"` // pagination.page
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type assistImpl struct {
|
type assistImpl struct {
|
||||||
|
|
@ -102,19 +109,18 @@ func (a *assistImpl) GetAll(projectKey string, request *Request) (interface{}, e
|
||||||
return nil, fmt.Errorf("failed to get project by key: %s", err)
|
return nil, fmt.Errorf("failed to get project by key: %s", err)
|
||||||
}
|
}
|
||||||
order := sessionmanager.Asc
|
order := sessionmanager.Asc
|
||||||
if request.Order == "desc" {
|
if request.Sort.Order == "DESC" {
|
||||||
order = sessionmanager.Desc
|
order = sessionmanager.Desc
|
||||||
}
|
}
|
||||||
filters := make([]*sessionmanager.Filter, 0, len(request.Filters))
|
filters := make([]*sessionmanager.Filter, 0, len(request.Filters))
|
||||||
for _, f := range request.Filters {
|
for name, f := range request.Filters {
|
||||||
filters = append(filters, &sessionmanager.Filter{
|
filters = append(filters, &sessionmanager.Filter{
|
||||||
Type: sessionmanager.FilterType(f.Type),
|
Type: sessionmanager.FilterType(name),
|
||||||
Value: f.Value,
|
Value: f.Value,
|
||||||
Operator: f.Operator == "is",
|
Operator: f.Operator == "is",
|
||||||
Source: f.Source,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return a.sessions.GetAll(strconv.Itoa(int(project.ProjectID)), filters, order, request.Page, request.Limit)
|
return a.sessions.GetAll(strconv.Itoa(int(project.ProjectID)), filters, order, request.Pagination.Page, request.Pagination.Limit)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *assistImpl) GetByID(projectKey, sessionID string, filters *Request) (interface{}, error) {
|
func (a *assistImpl) GetByID(projectKey, sessionID string, filters *Request) (interface{}, error) {
|
||||||
|
|
|
||||||
|
|
@ -329,6 +329,9 @@ func (sm *sessionManagerImpl) updateSessions() {
|
||||||
|
|
||||||
sm.mutex.RLock()
|
sm.mutex.RLock()
|
||||||
toAdd := make([]string, 0, len(updatedSessIDs))
|
toAdd := make([]string, 0, len(updatedSessIDs))
|
||||||
|
if updatedSessIDs == nil {
|
||||||
|
updatedSessIDs = make(map[string]struct{})
|
||||||
|
}
|
||||||
for sessID, _ := range sessIDs {
|
for sessID, _ := range sessIDs {
|
||||||
if _, exists := sm.cache[sessID]; !exists {
|
if _, exists := sm.cache[sessID]; !exists {
|
||||||
updatedSessIDs[sessID] = struct{}{} // Add to updated sessions if not in cache
|
updatedSessIDs[sessID] = struct{}{} // Add to updated sessions if not in cache
|
||||||
|
|
@ -442,12 +445,10 @@ func matchesFilters(session *SessionData, filters []*Filter) bool {
|
||||||
if session.UserCity != nil {
|
if session.UserCity != nil {
|
||||||
value = *session.UserCity
|
value = *session.UserCity
|
||||||
}
|
}
|
||||||
case Metadata:
|
|
||||||
if session.Metadata != nil {
|
|
||||||
value = (*session.Metadata)[filter.Source]
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
return false // Unknown filter type
|
if val, ok := (*session.Metadata)[string(filter.Type)]; ok {
|
||||||
|
value = val
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, filterValue := range filter.Value {
|
for _, filterValue := range filter.Value {
|
||||||
|
|
@ -503,14 +504,10 @@ func (sm *sessionManagerImpl) Autocomplete(projectID string, key FilterType, val
|
||||||
if session.UserCity != nil {
|
if session.UserCity != nil {
|
||||||
fieldValue = *session.UserCity
|
fieldValue = *session.UserCity
|
||||||
}
|
}
|
||||||
case Metadata:
|
|
||||||
if session.Metadata != nil {
|
|
||||||
if v, ok := (*session.Metadata)[string(key)]; ok {
|
|
||||||
fieldValue = v
|
|
||||||
}
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("unknown filter type: %s", key)
|
if v, ok := (*session.Metadata)[string(key)]; ok {
|
||||||
|
fieldValue = v
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if fieldValue != "" && strings.Contains(strings.ToLower(fieldValue), lowerValue) {
|
if fieldValue != "" && strings.Contains(strings.ToLower(fieldValue), lowerValue) {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue