query.ts 759 B

12345678910111213141516171819202122232425262728293031
  1. export function buildQueryOptions({ format, interval, instant, now, queries }) {
  2. const to = now;
  3. const from = to - 1000 * 60 * 60 * 3;
  4. return {
  5. interval,
  6. range: {
  7. from,
  8. to,
  9. },
  10. targets: queries.map(expr => ({
  11. expr,
  12. format,
  13. instant,
  14. })),
  15. };
  16. }
  17. export function generateQueryKey(index = 0) {
  18. return `Q-${Date.now()}-${Math.random()}-${index}`;
  19. }
  20. export function ensureQueries(queries?) {
  21. if (queries && typeof queries === 'object' && queries.length > 0 && typeof queries[0] === 'string') {
  22. return queries.map((query, i) => ({ key: generateQueryKey(i), query }));
  23. }
  24. return [{ key: generateQueryKey(), query: '' }];
  25. }
  26. export function hasQuery(queries) {
  27. return queries.some(q => q.query);
  28. }