<commit_before>package api

import (
	"context"
	"encoding/json"
	"net/http"

	"github.com/grafana/grafana/pkg/api/dtos"
	"github.com/grafana/grafana/pkg/bus"
	"github.com/grafana/grafana/pkg/components/simplejson"
	"github.com/grafana/grafana/pkg/metrics"
	"github.com/grafana/grafana/pkg/middleware"
	"github.com/grafana/grafana/pkg/models"
	"github.com/grafana/grafana/pkg/tsdb"
	"github.com/grafana/grafana/pkg/tsdb/testdata"
	"github.com/grafana/grafana/pkg/util"
)

// POST /api/tsdb/query
func QueryMetrics(c *middleware.Context, reqDto dtos.MetricRequest) Response {
	timeRange := tsdb.NewTimeRange(reqDto.From, reqDto.To)

	if len(reqDto.Queries) == 0 {
		return ApiError(400, "No queries found in query", nil)
	}

	dsId, err := reqDto.Queries[0].Get("datasourceId").Int64()
	if err != nil {
		return ApiError(400, "Query missing datasourceId", nil)
	}

	dsQuery := models.GetDataSourceByIdQuery{Id: dsId}
	if err := bus.Dispatch(&dsQuery); err != nil {
		return ApiError(500, "failed to fetch data source", err)
	}

	request := &tsdb.Request{TimeRange: timeRange}

	for _, query := range reqDto.Queries {
		request.Queries = append(request.Queries, &tsdb.Query{<commit_msg>api: tsdb api orgId fix<commit_after>package api

import (
	"context"
	"encoding/json"
	"net/http"

	"github.com/grafana/grafana/pkg/api/dtos"
	"github.com/grafana/grafana/pkg/bus"
	"github.com/grafana/grafana/pkg/components/simplejson"
	"github.com/grafana/grafana/pkg/metrics"
	"github.com/grafana/grafana/pkg/middleware"
	"github.com/grafana/grafana/pkg/models"
	"github.com/grafana/grafana/pkg/tsdb"
	"github.com/grafana/grafana/pkg/tsdb/testdata"
	"github.com/grafana/grafana/pkg/util"
)

// POST /api/tsdb/query
func QueryMetrics(c *middleware.Context, reqDto dtos.MetricRequest) Response {
	timeRange := tsdb.NewTimeRange(reqDto.From, reqDto.To)

	if len(reqDto.Queries) == 0 {
		return ApiError(400, "No queries found in query", nil)
	}

	dsId, err := reqDto.Queries[0].Get("datasourceId").Int64()
	if err != nil {
		return ApiError(400, "Query missing datasourceId", nil)
	}

	dsQuery := models.GetDataSourceByIdQuery{Id: dsId, OrgId: c.OrgId}
	if err := bus.Dispatch(&dsQuery); err != nil {
		return ApiError(500, "failed to fetch data source", err)
	}

	request := &tsdb.Request{TimeRange: timeRange}

	for _, query := range reqDto.Queries {
		request.Queries = append(request.Queries, &tsdb.Query{
{'fraction_non_alphanumeric': 0.15133779264214048, 'fraction_numerical': 0.010869565217391304, 'mean_word_length': 4.697619047619048, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1412315', 'n_tokens_mistral': 980, 'n_tokens_neox': 954, 'n_words': 155}