var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var dashboard_exports = {};
__export(dashboard_exports, {
  anonymousGraphQLOperationSpans: () => anonymousGraphQLOperationSpans,
  apiPerformance: () => apiPerformance,
  databasePerformance: () => databasePerformance,
  graphQLOperationSpans: () => graphQLOperationSpans,
  graphqlPerformance: () => graphqlPerformance,
  miscPerformance: () => miscPerformance,
  networkPerformance: () => networkPerformance,
  prismaModelSpans: () => prismaModelSpans,
  sqlStatementSpans: () => sqlStatementSpans
});
module.exports = __toCommonJS(dashboard_exports);
var import_db = require("../../lib/db");
const spansByAttributeKeyAndType = async ({
  typeId,
  attributeKey,
  attributeValue,
  secondsAgo
}) => await import_db.db.$queryRaw`
SELECT
  -- id must be unique per row or Apollo client will reuse data in cache
  s.id || '-' || a.hash as id,
  s.spanId,
  s.createdAt,
  s.updatedAt,
  s.statusCode,
  strftime('%Y-%m-%dT%H:%M:%fZ',s.startTimeNano / 1000000000.000,'unixepoch') AS startedAt,
  strftime('%Y-%m-%dT%H:%M:%fZ',s.endTimeNano / 1000000000.000, 'unixepoch') AS endedAt,
  cast(((s.endTimeNano - s.startTimeNano) / 1000000.000) AS REAL) AS durationMs,
  cast(((s.endTimeNano - s.startTimeNano) / 1000000000.0000) AS REAL) AS durationSec,
  t.id AS spanType,
  a.key AS attributeKey,
	CASE WHEN a.type = 'string' THEN
    substr((substr(a.value, 1, instr(a.value, ' /* traceparent=') - 1) || substr(a.value, instr(a.value, '*/') + 1)), 2, length((substr(a.value, 1, instr(a.value, ' /* traceparent=') - 1) || substr(a.value, instr(a.value, '*/') + 1))) - 2)
	ELSE
		cast(a.value as text)
	END AS attributeValue
FROM
  OTelTraceSpan s
  JOIN OTelTraceSpanType t ON t. "id" = s.typeId
  -- here we use the Prisma implicit join table to join attributes A to spans B
  JOIN _OTelTraceAttributeToOTelTraceSpan atos on atos.A = a.id and atos.B = s.id
  JOIN OTelTraceAttribute a ON atos.A = a.id
WHERE
  t.id = ${typeId}
  AND (${attributeKey} IS NULL OR a.key = ${attributeKey})
  AND (${attributeValue} IS NULL OR a.value = ${attributeValue})
  AND (${secondsAgo} IS NULL OR s.startTimeNano / 1000000000.000 >= strftime('%s', 'now') - ${secondsAgo})

ORDER BY
  s.createdAt DESC
LIMIT 200`;
const recentSpans = async ({
  typeId,
  attributeKey,
  attributeValue
}) => {
  let results = [];
  results = await spansByAttributeKeyAndType({
    typeId,
    attributeKey,
    attributeValue,
    secondsAgo: 10 * 60
  });
  if (results.length === 0) {
    results = await spansByAttributeKeyAndType({
      typeId,
      attributeKey,
      attributeValue
    });
  }
  return results;
};
const sqlStatementSpans = async () => {
  const typeId = "SQL";
  const attributeKey = "db.statement";
  return await recentSpans({
    typeId,
    attributeKey
  });
};
const prismaModelSpans = async () => {
  const typeId = "PRISMA";
  const attributeKey = "model";
  return await recentSpans({
    typeId,
    attributeKey
  });
};
const graphQLOperationSpans = async () => {
  const typeId = "GRAPHQL";
  const attributeKey = "graphql.execute.operationName";
  return await recentSpans({
    typeId,
    attributeKey
  });
};
const anonymousGraphQLOperationSpans = async () => {
  const typeId = "GRAPHQL";
  const attributeKey = "graphql.execute.operationName";
  const attributeValue = '"Anonymous Operation"';
  return await recentSpans({
    typeId,
    attributeKey,
    attributeValue
  });
};
const databasePerformance = async ({
  filter
}) => {
  const secondsAgo = filter?.secondsAgo ?? null;
  return await import_db.db.$queryRaw`SELECT
    s.id,
    s.spanId,
    t.id AS spanType,
    t. "name" AS spanTypeName,
    strftime ('%Y-%m-%dT%H:%M:%fZ',
      s.startTimeNano / 1000000000.000,
      'unixepoch') AS startedAt,
    strftime ('%Y-%m-%dT%H:%M:%fZ',
      s.endTimeNano / 1000000000.000,
      'unixepoch') AS endedAt,
    cast(((s.endTimeNano - s.startTimeNano) / 1000000.000) AS REAL) AS durationMs,
    cast(((s.endTimeNano - s.startTimeNano) / 1000000000.0000) AS REAL) AS durationSec
  FROM
    OTelTraceSpan s
    JOIN OTelTraceSpanType t ON t.id = s.typeId
  WHERE
    t.id IN('SQL', 'PRISMA')
  AND
    (${secondsAgo} IS NULL OR startTimeNano / 1000000000.000 >= strftime('%s', 'now') - ${secondsAgo})
  ORDER BY
    s.startTimeNano ASC,
    t.id`;
};
const graphqlPerformance = async ({
  filter
}) => {
  const secondsAgo = filter?.secondsAgo ?? null;
  return await import_db.db.$queryRaw`SELECT
    s.id,
    t.id AS spanType,
    t. "name" AS spanTypeName,
    strftime ('%Y-%m-%dT%H:%M:%fZ',
      s.startTimeNano / 1000000000.000,
      'unixepoch') AS startedAt,
    strftime ('%Y-%m-%dT%H:%M:%fZ',
      s.endTimeNano / 1000000000.000,
      'unixepoch') AS endedAt,
    cast(((s.endTimeNano - s.startTimeNano) / 1000000.000) AS REAL) AS durationMs,
    cast(((s.endTimeNano - s.startTimeNano) / 1000000000.0000) AS REAL) AS durationSec
  FROM
    OTelTraceSpan s
    JOIN OTelTraceSpanType t ON t.id = s.typeId
  WHERE
    t.id IN('GRAPHQL')
  AND
    (${secondsAgo} IS NULL OR startTimeNano / 1000000000.000 >= strftime('%s', 'now') - ${secondsAgo})
  ORDER BY
    s.startTimeNano ASC,
    t.id`;
};
const apiPerformance = async ({
  filter
}) => {
  const secondsAgo = filter?.secondsAgo ?? null;
  return await import_db.db.$queryRaw`SELECT
    s.id,
    t.id AS spanType,
    t. "name" AS spanTypeName,
    strftime ('%Y-%m-%dT%H:%M:%fZ',
      s.startTimeNano / 1000000000.000,
      'unixepoch') AS startedAt,
    strftime ('%Y-%m-%dT%H:%M:%fZ',
      s.endTimeNano / 1000000000.000,
      'unixepoch') AS endedAt,
    cast(((s.endTimeNano - s.startTimeNano) / 1000000.000) AS REAL) AS durationMs,
    cast(((s.endTimeNano - s.startTimeNano) / 1000000000.0000) AS REAL) AS durationSec
  FROM
    OTelTraceSpan s
    JOIN OTelTraceSpanType t ON t.id = s.typeId
  WHERE
    t.id IN('REDWOOD_FUNCTION', 'REDWOOD_SERVICE')
  AND
    (${secondsAgo} IS NULL OR startTimeNano / 1000000000.000 >= strftime('%s', 'now') - ${secondsAgo})
  ORDER BY
    s.startTimeNano ASC,
    t.id`;
};
const miscPerformance = async ({
  filter
}) => {
  const secondsAgo = filter?.secondsAgo ?? null;
  return await import_db.db.$queryRaw`SELECT
    s.id,
    t.id AS spanType,
    t. "name" AS spanTypeName,
    strftime ('%Y-%m-%dT%H:%M:%fZ',
      s.startTimeNano / 1000000000.000,
      'unixepoch') AS startedAt,
    strftime ('%Y-%m-%dT%H:%M:%fZ',
      s.endTimeNano / 1000000000.000,
      'unixepoch') AS endedAt,
    cast(((s.endTimeNano - s.startTimeNano) / 1000000.000) AS REAL) AS durationMs,
    cast(((s.endTimeNano - s.startTimeNano) / 1000000000.0000) AS REAL) AS durationSec
  FROM
    OTelTraceSpan s
    JOIN OTelTraceSpanType t ON t.id = s.typeId
  WHERE
    t.id IN('GENERIC')
  AND
    (${secondsAgo} IS NULL OR startTimeNano / 1000000000.000 >= strftime('%s', 'now') - ${secondsAgo})
  ORDER BY
    s.startTimeNano ASC,
    t.id`;
};
const networkPerformance = async ({
  filter
}) => {
  const secondsAgo = filter?.secondsAgo ?? null;
  return await import_db.db.$queryRaw`SELECT
    s.id,
    t.id AS spanType,
    t. "name" AS spanTypeName,
    strftime ('%Y-%m-%dT%H:%M:%fZ',
      s.startTimeNano / 1000000000.000,
      'unixepoch') AS startedAt,
    strftime ('%Y-%m-%dT%H:%M:%fZ',
      s.endTimeNano / 1000000000.000,
      'unixepoch') AS endedAt,
    cast(((s.endTimeNano - s.startTimeNano) / 1000000.000) AS REAL) AS durationMs,
    cast(((s.endTimeNano - s.startTimeNano) / 1000000000.0000) AS REAL) AS durationSec
  FROM
    OTelTraceSpan s
    JOIN OTelTraceSpanType t ON t.id = s.typeId
  WHERE
    t.id IN('HTTP')
  AND
    (${secondsAgo} IS NULL OR startTimeNano / 1000000000.000 >= strftime('%s', 'now') - ${secondsAgo})
  ORDER BY
    s.startTimeNano ASC,
    t.id`;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  anonymousGraphQLOperationSpans,
  apiPerformance,
  databasePerformance,
  graphQLOperationSpans,
  graphqlPerformance,
  miscPerformance,
  networkPerformance,
  prismaModelSpans,
  sqlStatementSpans
});
//# sourceMappingURL=dashboard.js.map
