|
| 1 | +// Copyright © 2021 Kaleido, Inc. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | + |
| 17 | +package sqlcommon |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "database/sql" |
| 22 | + "fmt" |
| 23 | + |
| 24 | + sq "github.com/Masterminds/squirrel" |
| 25 | + "github.com/hyperledger/firefly/internal/i18n" |
| 26 | + "github.com/hyperledger/firefly/pkg/database" |
| 27 | + "github.com/hyperledger/firefly/pkg/fftypes" |
| 28 | +) |
| 29 | + |
| 30 | +func (s *SQLCommon) getCaseQueries(ns string, intervals []fftypes.ChartHistogramInterval) (caseQueries []sq.CaseBuilder) { |
| 31 | + for _, interval := range intervals { |
| 32 | + caseQueries = append(caseQueries, sq.Case(). |
| 33 | + When( |
| 34 | + sq.And{ |
| 35 | + sq.GtOrEq{"created": interval.StartTime}, |
| 36 | + sq.Lt{"created": interval.EndTime}, |
| 37 | + sq.Eq{"namespace": ns}, |
| 38 | + }, |
| 39 | + "1", |
| 40 | + ). |
| 41 | + Else("0")) |
| 42 | + } |
| 43 | + |
| 44 | + return caseQueries |
| 45 | +} |
| 46 | + |
| 47 | +func (s *SQLCommon) getTableNameFromCollection(ctx context.Context, collection database.CollectionName) (tableName string, err error) { |
| 48 | + switch collection { |
| 49 | + case database.CollectionName(database.CollectionMessages): |
| 50 | + return "messages", nil |
| 51 | + case database.CollectionName(database.CollectionTransactions): |
| 52 | + return "transactions", nil |
| 53 | + case database.CollectionName(database.CollectionOperations): |
| 54 | + return "operations", nil |
| 55 | + case database.CollectionName(database.CollectionEvents): |
| 56 | + return "events", nil |
| 57 | + default: |
| 58 | + return "", i18n.NewError(ctx, i18n.MsgUnsupportedCollection, collection) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func (s *SQLCommon) histogramResult(ctx context.Context, rows *sql.Rows, cols []*fftypes.ChartHistogram) ([]*fftypes.ChartHistogram, error) { |
| 63 | + results := []interface{}{} |
| 64 | + |
| 65 | + for i := range cols { |
| 66 | + results = append(results, &cols[i].Count) |
| 67 | + } |
| 68 | + err := rows.Scan(results...) |
| 69 | + if err != nil { |
| 70 | + return nil, i18n.NewError(ctx, i18n.MsgDBReadErr, "histogram") |
| 71 | + } |
| 72 | + |
| 73 | + return cols, nil |
| 74 | +} |
| 75 | + |
| 76 | +func (s *SQLCommon) GetChartHistogram(ctx context.Context, ns string, intervals []fftypes.ChartHistogramInterval, collection database.CollectionName) (histogram []*fftypes.ChartHistogram, err error) { |
| 77 | + tableName, err := s.getTableNameFromCollection(ctx, collection) |
| 78 | + if err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + |
| 82 | + qb := sq.Select() |
| 83 | + |
| 84 | + for i, caseQuery := range s.getCaseQueries(ns, intervals) { |
| 85 | + query, args, _ := caseQuery.ToSql() |
| 86 | + |
| 87 | + histogram = append(histogram, &fftypes.ChartHistogram{ |
| 88 | + Count: "", |
| 89 | + Timestamp: intervals[i].StartTime, |
| 90 | + }) |
| 91 | + |
| 92 | + qb = qb.Column(sq.Alias(sq.Expr("SUM("+query+")", args...), fmt.Sprintf("case_%d", i))) |
| 93 | + } |
| 94 | + |
| 95 | + rows, _, err := s.query(ctx, qb.From(tableName)) |
| 96 | + if err != nil { |
| 97 | + return nil, err |
| 98 | + } |
| 99 | + defer rows.Close() |
| 100 | + |
| 101 | + if !rows.Next() { |
| 102 | + return []*fftypes.ChartHistogram{}, nil |
| 103 | + } |
| 104 | + |
| 105 | + return s.histogramResult(ctx, rows, histogram) |
| 106 | +} |
0 commit comments