<commit_before>// Libraries
import {cloneDeep} from 'lodash'
import {fromFlux} from '@influxdata/giraffe'

// Utils
import {runQuery, RunQueryResult} from 'src/shared/apis/query'
import {parseSearchInput, searchExprToFlux} from 'src/eventViewer/utils/search'
import {findNodes} from 'src/shared/utils/ast'
import {readQueryParams} from 'src/shared/utils/queryParams'

// Constants
import {
  MONITORING_BUCKET,
  HISTORY_TYPE_QUERY_PARAM,
  SEARCH_QUERY_PARAM,
} from 'src/alerting/constants/history'

// Types
import {State as EventViewerState} from 'src/eventViewer/components/EventViewer.reducer'

import {
  CancelBox,
  StatusRow,
  NotificationRow,
  AlertHistoryType,
} from 'src/types'

import {
  LoadRowsOptions,
  Row,
  SearchExpr,
  SearchTagExpr,
} from 'src/eventViewer/types'

export const loadStatuses = (
  orgID: string,
  {offset, limit, since, until, filter}: LoadRowsOptions
): CancelBox<StatusRow[]> => {
  const start = since ? Math.round(since / 1000) : '-60d'
  const fluxFilter = filter ? searchExprToFlux(renameTagKeys(filter)) : null

  const query = `
from(bucket: "${MONITORING_BUCKET}")
  |> range(start: ${start}, stop: ${Math.round(until / 1000)})
  |> filter(fn: (r) => r._measurement == "statuses" and r._field == "_message")
  |> keep(columns: ["_time", "_value", "_check_id", "_check_name", "_level"])
  |> rename(columns: {"_time": "time",
                      "_value": "message",
                      "_check_id": "checkID",
                      "_check_name": "checkName",
                      "_level": "level"})
  |> group()${fluxFilter ? `\n  |> filter(fn: (r) => ${fluxFilter})` : ''}
  |> limit(n: ${limit}, offset: ${offset})
`

  return processResponse(runQuery(orgID, query)) as CancelBox<StatusRow[]>
}

export const loadNotifications = (
  orgID: string,
  {offset, limit, since, until, filter}: LoadRowsOptions
): CancelBox<NotificationRow[]> => {
  const start = since ? Math.round(since / 1000) : '-60d'
  const fluxFilter = filter ? searchExprToFlux(renameTagKeys(filter)) : null

  const query = `
from(bucket: "${MONITORING_BUCKET}")
  |> range(start: ${start}, stop: ${Math.round(until / 1000)})
  |> filter(fn: (r) => r._measurement == "notifications")
  |> keep(columns: ["_time",
                    "_check_id",
                    "_check_name",
                    "_notification_rule_id",
                    "_notification_rule_name",
                    "_notification_endpoint_id",
                    "_notification_endpoint_name",
                    "_level",
                    "_sent"])
  |> rename(columns: {"_time": "time",
                      "_check_id": "checkID",
                      "_check_name": "checkName",
                      "_notification_rule_id": "notificationRuleID",
                      "_notification_rule_name": "notificationRule",
                      "_notification_endpoint_id": "notificationEndpointID",
                      "_notification_endpoint_name": "notificationEndpoint",
                      "_level": "level",
                      "_sent": "sent"})
  |> group()${fluxFilter ? `\n  |> filter(fn: (r) => ${fluxFilter})` : ''}<commit_msg>fix(ui): sort alerting history page from newest to oldest<commit_after>// Libraries
import {cloneDeep} from 'lodash'
import {fromFlux} from '@influxdata/giraffe'

// Utils
import {runQuery, RunQueryResult} from 'src/shared/apis/query'
import {parseSearchInput, searchExprToFlux} from 'src/eventViewer/utils/search'
import {findNodes} from 'src/shared/utils/ast'
import {readQueryParams} from 'src/shared/utils/queryParams'

// Constants
import {
  MONITORING_BUCKET,
  HISTORY_TYPE_QUERY_PARAM,
  SEARCH_QUERY_PARAM,
} from 'src/alerting/constants/history'

// Types
import {State as EventViewerState} from 'src/eventViewer/components/EventViewer.reducer'

import {
  CancelBox,
  StatusRow,
  NotificationRow,
  AlertHistoryType,
} from 'src/types'

import {
  LoadRowsOptions,
  Row,
  SearchExpr,
  SearchTagExpr,
} from 'src/eventViewer/types'

export const loadStatuses = (
  orgID: string,
  {offset, limit, since, until, filter}: LoadRowsOptions
): CancelBox<StatusRow[]> => {
  const start = since ? Math.round(since / 1000) : '-60d'
  const fluxFilter = filter ? searchExprToFlux(renameTagKeys(filter)) : null

  const query = `
from(bucket: "${MONITORING_BUCKET}")
  |> range(start: ${start}, stop: ${Math.round(until / 1000)})
  |> filter(fn: (r) => r._measurement == "statuses" and r._field == "_message")
  |> keep(columns: ["_time", "_value", "_check_id", "_check_name", "_level"])
  |> rename(columns: {"_time": "time",
                      "_value": "message",
                      "_check_id": "checkID",
                      "_check_name": "checkName",
                      "_level": "level"})
  |> group()${fluxFilter ? `\n  |> filter(fn: (r) => ${fluxFilter})` : ''}
  |> sort(columns: ["time"], desc: true)
  |> limit(n: ${limit}, offset: ${offset})
`

  return processResponse(runQuery(orgID, query)) as CancelBox<StatusRow[]>
}

export const loadNotifications = (
  orgID: string,
  {offset, limit, since, until, filter}: LoadRowsOptions
): CancelBox<NotificationRow[]> => {
  const start = since ? Math.round(since / 1000) : '-60d'
  const fluxFilter = filter ? searchExprToFlux(renameTagKeys(filter)) : null

  const query = `
from(bucket: "${MONITORING_BUCKET}")
  |> range(start: ${start}, stop: ${Math.round(until / 1000)})
  |> filter(fn: (r) => r._measurement == "notifications")
  |> keep(columns: ["_time",
                    "_check_id",
                    "_check_name",
                    "_notification_rule_id",
                    "_notification_rule_name",
                    "_notification_endpoint_id",
                    "_notification_endpoint_name",
                    "_level",
                    "_sent"])
  |> rename(columns: {"_time": "time",
                      "_check_id": "checkID",
                      "_check_name": "checkName",
                      "_notification_rule_id": "notificationRuleID",
                      "_notification_rule_name": "notificationRule",
                      "_notification_endpoint_id": "notificationEndpointID",
                      "_notification_endpoint_name": "notificationEndpoint",
                      "_level": "level",
                      "_sent": "sent"})
  |> group()${fluxFilter ? `\n  |> filter(fn: (r) => ${fluxFilter})` : ''}
  |> sort(columns: ["time"], desc: true)
{'fraction_non_alphanumeric': 0.15125659323611543, 'fraction_numerical': 0.006205398696866274, 'mean_word_length': 2.9191489361702128, 'pattern_counts': {'":': 28, '<': 9, '<?xml version=': 0, '>': 49, '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': '4311265', 'n_tokens_mistral': 2055, 'n_tokens_neox': 1936, 'n_words': 410}