consistant query formatting

This commit is contained in:
nityanandagohain 2022-07-27 10:46:33 +05:30
parent baf72610d6
commit 7b6a086b37

View File

@ -2865,7 +2865,7 @@ func (r *ClickHouseReader) GetLogs(ctx context.Context, params *model.LogsFilter
query := fmt.Sprintf("%s from %s.%s", constants.LogsSQLSelect, r.logsDB, r.logsTable) query := fmt.Sprintf("%s from %s.%s", constants.LogsSQLSelect, r.logsDB, r.logsTable)
if filterSql != "" { if filterSql != "" {
query += fmt.Sprintf(" where %s", filterSql) query = fmt.Sprintf("%s where %s", query, filterSql)
} }
query = fmt.Sprintf("%s order by %s %s limit %d", query, params.OrderBy, params.Order, params.Limit) query = fmt.Sprintf("%s order by %s %s limit %d", query, params.OrderBy, params.Order, params.Limit)
@ -2918,10 +2918,10 @@ func (r *ClickHouseReader) TailLogs(ctx context.Context, client *model.LogsTailC
// get the new 100 logs as anything more older won't make sense // get the new 100 logs as anything more older won't make sense
tmpQuery := fmt.Sprintf("%s where timestamp >='%d'", query, tsStart) tmpQuery := fmt.Sprintf("%s where timestamp >='%d'", query, tsStart)
if filterSql != "" { if filterSql != "" {
tmpQuery += fmt.Sprintf(" and %s", filterSql) tmpQuery = fmt.Sprintf("%s and %s", tmpQuery, filterSql)
} }
if idStart != "" { if idStart != "" {
tmpQuery += fmt.Sprintf(" and id > '%s'", idStart) tmpQuery = fmt.Sprintf("%s and id > '%s'", tmpQuery, idStart)
} }
tmpQuery = fmt.Sprintf("%s order by timestamp desc, id desc limit 100", tmpQuery) tmpQuery = fmt.Sprintf("%s order by timestamp desc, id desc limit 100", tmpQuery)
zap.S().Debug(tmpQuery) zap.S().Debug(tmpQuery)
@ -2932,8 +2932,7 @@ func (r *ClickHouseReader) TailLogs(ctx context.Context, client *model.LogsTailC
client.Error <- err client.Error <- err
return return
} }
len := len(response) for i := len(response) - 1; i >= 0; i-- {
for i := len - 1; i >= 0; i-- {
select { select {
case <-ctx.Done(): case <-ctx.Done():
done := true done := true
@ -2986,12 +2985,12 @@ func (r *ClickHouseReader) AggregateLogs(ctx context.Context, params *model.Logs
params.StepSeconds/60, function, r.logsDB, r.logsTable, params.TimestampStart, params.TimestampEnd) params.StepSeconds/60, function, r.logsDB, r.logsTable, params.TimestampStart, params.TimestampEnd)
} }
if filterSql != "" { if filterSql != "" {
query += fmt.Sprintf(" AND %s ", filterSql) query = fmt.Sprintf("%s AND %s ", query, filterSql)
} }
if params.GroupBy != "" { if params.GroupBy != "" {
query += fmt.Sprintf("GROUP BY time, toString(%s) as groupBy ORDER BY time", params.GroupBy) query = fmt.Sprintf("%s GROUP BY time, toString(%s) as groupBy ORDER BY time", query, params.GroupBy)
} else { } else {
query += "GROUP BY time ORDER BY time" query = fmt.Sprintf("%s GROUP BY time ORDER BY time", query)
} }
zap.S().Debug(query) zap.S().Debug(query)