func createCumulativeMetric()

in internal/oraclemetrics/oraclemetrics.go [528:571]


func createCumulativeMetric(ctx context.Context, c *configpb.Column, val any, labels map[string]string, opts queryOptions, endTime *tspb.Timestamp) (*mrpb.TimeSeries, bool) {
	metricPath := metricURL + "/" + opts.query.GetName() + "/" + c.GetName()
	if c.GetNameOverride() != "" {
		metricPath = metricURL + "/" + c.GetNameOverride()
	}

	ts := timeseries.Params{
		CloudProp:    convertCloudProperties(opts.collector.Config.GetCloudProperties()),
		MetricType:   metricPath,
		MetricLabels: labels,
		Timestamp:    endTime,
		StartTime:    opts.collector.startTime,
		MetricKind:   mpb.MetricDescriptor_CUMULATIVE,
	}
	tsKey := prepareKey(metricPath, ts.MetricKind.String(), labels)

	// Type asserting to pointers due to the coupling with sql.Rows.Scan() populating the columns as such.
	switch c.GetValueType() {
	case configpb.ValueType_VALUE_INT64:
		if result, ok := val.(*int64); ok {
			ts.Int64Value = *result
		}
		if lastVal, ok := opts.runningSum[tsKey]; ok {
			log.CtxLogger(ctx).Debugw("Found already existing key.", "Key", tsKey, "prevVal", lastVal)
			ts.Int64Value = ts.Int64Value + lastVal.val.(int64)
			ts.StartTime = lastVal.startTime
		}
		opts.runningSum[tsKey] = prevVal{val: ts.Int64Value, startTime: ts.StartTime}
		return timeseries.BuildInt(ts), true
	case configpb.ValueType_VALUE_DOUBLE:
		if result, ok := val.(*float64); ok {
			ts.Float64Value = *result
		}
		if lastVal, ok := opts.runningSum[tsKey]; ok {
			log.CtxLogger(ctx).Debugw("Found already existing key.", "Key", tsKey, "prevVal", lastVal)
			ts.Float64Value = ts.Float64Value + lastVal.val.(float64)
			ts.StartTime = lastVal.startTime
		}
		opts.runningSum[tsKey] = prevVal{val: ts.Float64Value, startTime: ts.StartTime}
		return timeseries.BuildFloat64(ts), true
	default:
		return nil, false
	}
}