Skip to content

Commit

Permalink
Add SearchByInterval endpoint to query situation history
Browse files Browse the repository at this point in the history
  • Loading branch information
Ismail731404 committed Dec 31, 2024
1 parent a06e638 commit 2836334
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
51 changes: 51 additions & 0 deletions internals/handlers/search_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,54 @@ func SearchLastByCustomInterval(w http.ResponseWriter, r *http.Request) {

render.JSON(w, r, result)
}

// SearchByInterval godoc
// @Summary query situation history data
// @Description query situation history data
// @Tags Search
// @Accept json
// @Produce json
// @Param situationid query int false "situationid"
// @Param situationinstanceid query int false "situationinstanceid"
// @Param interval query string true "month | week | day | hour | minute"
// @Security Bearer
// @Success 200 {array} search.QueryResult "query result"
// @Failure 500 "internal server error"
// @Router /engine/search/byinterval [get]
func SearchByInterval(w http.ResponseWriter, r *http.Request) {

options, apiError, err := baseSearchOptions(w, r)
if err != nil {
render.Error(w, r, apiError, err)
return
}

userCtx, _ := GetUserFromContext(r)
if !userCtx.HasPermission(permissions.New(permissions.TypeSituation, strconv.FormatInt(options.SituationID, 10), permissions.ActionSearch)) {
render.Error(w, r, render.ErrAPISecurityNoPermissions, errors.New("missing permission"))
return
}

interval := r.URL.Query().Get("interval")
if interval != "month" && interval != "week" && interval != "day" && interval != "hour" && interval != "minute" {
zap.L().Warn("Error on parsing interval", zap.String("interval", interval), zap.Error(err))
render.Error(w, r, render.ErrAPIParsingDuration, fmt.Errorf("interval %s is not supported", interval))
return
}

historySituations, err := history.S().GetAllHistorySituationsIdsByStandardInterval(options, interval)
if err != nil {
render.Error(w, r, render.ErrAPIDBSelectFailed, err)
return
}

historyFacts, historySituationFacts, err := history.S().GetHistoryFactsFromSituation(historySituations)
if err != nil {
render.Error(w, r, render.ErrAPIDBSelectFailed, err)
return
}

result := history.ExtractHistoryDataSearch(historySituations, historySituationFacts, historyFacts)

render.JSON(w, r, result)
}
20 changes: 20 additions & 0 deletions internals/history/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,26 @@ func (service HistoryService) GetHistorySituationsIdsByStandardInterval(options
)
}

func (service HistoryService) GetAllHistorySituationsIdsByStandardInterval(options GetHistorySituationsOptions, interval string) ([]HistorySituationsV4, error) {
subQuery := service.HistorySituationsQuerier.Builder.
GetAllHistorySituationsIdsByStandardInterval(options, interval)

_, _, err := subQuery.ToSql()
if err != nil {
return nil, err
}

query := service.HistorySituationsQuerier.Builder.GetAllHistorySituationsDetails(subQuery)
// TODO remove
//queryString, interfacevalue, errr := query.ToSql()
////show values
//zap.L().Info("f", zap.String("queryString", queryString), zap.Any("interfacevalue", interfacevalue), zap.Error(errr))

return service.HistorySituationsQuerier.Query(
query,
)
}

func (service HistoryService) GetHistorySituationsIdsByCustomInterval(options GetHistorySituationsOptions, interval time.Duration, referenceDate time.Time) ([]HistorySituationsV4, error) {
subQuery, subQueryArgs, err := service.HistorySituationsQuerier.Builder.
GetHistorySituationsIdsByCustomInterval(options, interval, referenceDate).
Expand Down
35 changes: 35 additions & 0 deletions internals/history/situation_history_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ func (builder HistorySituationsBuilder) GetHistorySituationsIdsByStandardInterva
OrderBy("situation_id", "situation_instance_id", "date_trunc('"+interval+"', ts) desc, ts desc")
}

func (builder HistorySituationsBuilder) GetAllHistorySituationsIdsByStandardInterval(options GetHistorySituationsOptions, interval string) sq.SelectBuilder {
return builder.newStatement().
Select(fmt.Sprintf("situation_id, situation_instance_id, date_trunc('%s', ts) AS interval_ts, id", interval)).
From("situation_history_v5").
Where("situation_id = ?", options.SituationID).
Where("situation_instance_id = ?", options.SituationInstanceID).
//Where(fmt.Sprintf("date_trunc('%s', ts) = date_trunc('%s', CURRENT_DATE)", interval, interval)). //TODO remove
Where(fmt.Sprintf("ts >= date_trunc('%s', CURRENT_DATE)", interval)).
Where(fmt.Sprintf("ts < date_trunc('%s', CURRENT_DATE) + INTERVAL '1 %s'", interval, interval)).
OrderBy("situation_id, situation_instance_id, interval_ts DESC, ts DESC")
}
func (builder HistorySituationsBuilder) GetHistorySituationsIdsByCustomInterval(options GetHistorySituationsOptions, interval time.Duration, referenceDate time.Time) sq.SelectBuilder {
intervalSeconds := fmt.Sprintf("%d", int64(interval.Seconds()))
referenceDateStr := referenceDate.Format("2006-01-02T15:04:05Z07:00")
Expand All @@ -85,6 +96,30 @@ func (builder HistorySituationsBuilder) GetHistorySituationsDetails(subQueryIds
Where("sh.id = any ("+subQueryIds+")", subQueryIdsArgs...)
}

func (builder HistorySituationsBuilder) GetAllHistorySituationsDetails(subQuery sq.SelectBuilder) sq.SelectBuilder {
return builder.newStatement().
Select(`
sh.id,
sh.situation_id,
sh.situation_instance_id,
sh.ts,
sh.parameters,
sh.expression_facts,
sh.metadatas,
s.name AS situation_name,
coalesce(si.name, '') AS situation_instance_name,
c.id AS calendar_id,
c.name AS calendar_name,
c.description AS calendar_description,
c.timezone AS calendar_timezone
`).
FromSelect(subQuery, "sub").
LeftJoin("situation_definition_v1 s ON sub.situation_id = s.id").
LeftJoin("situation_template_instances_v1 si on s.id = si.situation_id").
LeftJoin("calendar_v1 c on c.id = COALESCE(si.calendar_id, s.calendar_id)").
InnerJoin("situation_history_v5 sh on (s.id = sh.situation_id and (sh.situation_instance_id = si.id OR sh.situation_instance_id = 0))")
}

func (builder HistorySituationsBuilder) Insert(history HistorySituationsV4, parametersJSON []byte, expressionFactsJSON []byte, metadatasJSON []byte) sq.InsertBuilder {
return builder.newStatement().Insert("situation_history_v5").
Columns("id", "situation_id", "situation_instance_id", "ts", "parameters", "expression_facts", "metadatas").
Expand Down
1 change: 1 addition & 0 deletions internals/router/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ func engineRouter(services Services) http.Handler {
r.Get("/search/last", handlers.SearchLast)
r.Get("/search/last/byinterval", handlers.SearchLastByInterval)
r.Get("/search/last/bycustominterval", handlers.SearchLastByCustomInterval)
r.Get("/search/byinterval", handlers.SearchByInterval)

r.Post("/history/facts/today/result", handlers.GetFactResultForTodayByCriteria)
r.Post("/history/facts/date/result", handlers.GetFactResultByDateCriteria)
Expand Down

0 comments on commit 2836334

Please sign in to comment.