"use strict";(self.webpackChunkgrafana=self.webpackChunkgrafana||[]).push([[4679],{55476:(e,t,r)=>{r.r(t),r.d(t,{RuleViewer:()=>_,default:()=>W});var s=r(68490),n=r(76687),a=r(75652),i=r(19177),o=r(70534),l=r(11676),c=r(94528),u=r(14647),d=r(65677),h=r(10967),g=r(46852),f=r(99462),p=r(73285),m=r(58124),x=r(96680),j=r(52010);const v=["refId"];var b;function y(e){var t;const r=(0,o.useTheme2)(),s=(0,o.useStyles2)(w),{data:a,query:l,onChangeQuery:c}=e,u=(0,g.j)(l.model)?p.Fe:p.GC,[v,y]=(0,n.useState)(u),R=(0,h.getDataSourceSrv)().getInstanceSettings(l.datasourceUid),C=l.relativeTimeRange,[$,I]=(0,n.useState)({frameIndex:0,showHeader:!0}),k=(0,n.useCallback)((e=>{const t=(0,i.dateTime)().unix()-e.unix();if(C){const e=C.from-C.to;c(Object.assign({},l,{relativeTimeRange:{from:t+e,to:t}}))}}),[c,l,C]),q=(0,n.useCallback)((e=>0===e?(0,i.dateTime)():(0,i.dateTime)().subtract(e,"seconds")),[]);return a?R?(0,j.jsx)("div",{className:s.content,children:(0,j.jsx)(d.Z,{children:e=>{let{width:n,height:i}=e;return(0,j.jsxs)("div",{style:{width:n,height:i},children:[(0,j.jsxs)("div",{className:s.header,children:[(0,j.jsxs)("div",{children:[`Query ${l.refId}`,(0,j.jsxs)("span",{className:s.dataSource,children:["(",R.name,")"]})]}),(0,j.jsxs)("div",{className:s.actions,children:[!(0,g.j)(l.model)&&C?(0,j.jsx)(o.DateTimePicker,{date:q(C.to),onChange:k,maxDate:new Date}):null,t||(t=(0,j.jsx)(x.j,{onChange:y,value:v,size:"md"})),(0,j.jsx)(m.q,{actions:[f.bW.DataSourcesExplore],children:!(0,g.j)(l.model)&&(0,j.jsxs)(j.Fragment,{children:[(0,j.jsx)("div",{className:s.spacing}),(0,j.jsx)(o.LinkButton,{size:"md",variant:"secondary",icon:"compass",target:"_blank",href:S(R,l),children:"View in Explore"})]})})]})]}),(0,j.jsx)(h.PanelRenderer,{height:i-4*r.spacing.gridSize,width:n,data:a,pluginId:v,title:"",onOptionsChange:I,options:$})]})}})}):(0,j.jsxs)("div",{className:s.content,children:[b||(b=(0,j.jsx)(o.Alert,{title:"Could not find datasource for query"})),(0,j.jsx)(o.CodeEditor,{width:"100%",height:"250px",language:"json",showLineNumbers:!1,showMiniMap:!1,value:JSON.stringify(l,null,"\t"),readOnly:!0})]}):null}function S(e,t){const{name:r}=e,s=function(e,t){if(null==e)return{};var r,s,n={},a=Object.keys(e);for(s=0;s<a.length;s++)r=a[s],t.indexOf(r)>=0||(n[r]=e[r]);return n}(t.model,v),n=Object.assign({},s,{datasource:r});return i.urlUtil.renderUrl(`${h.config.appSubUrl}/explore`,{left:JSON.stringify(["now-1h","now",r,n])})}const w=e=>({content:s.css`
      width: 100%;
      height: 250px;
    `,header:s.css`
      height: ${e.spacing(4)};
      display: flex;
      align-items: center;
      justify-content: space-between;
      white-space: nowrap;
    `,refId:s.css`
      font-weight: ${e.typography.fontWeightMedium};
      color: ${e.colors.text.link};
      overflow: hidden;
    `,dataSource:s.css`
      margin-left: ${e.spacing(1)};
      font-style: italic;
      color: ${e.colors.text.secondary};
    `,actions:s.css`
      display: flex;
      align-items: center;
    `,spacing:s.css`
      padding: ${e.spacing(0,1,0,0)};
    `,errorMessage:s.css`
      white-space: pre-wrap;
    `});var R=r(75520),C=r(2291),$=r(26160),I=r(72578);const k=e=>{var t;let{group:r}=e;const s=null!==(t=r.source_tenants)&&void 0!==t?t:[];return(0,j.jsx)(c.C,{label:"Tenant sources",children:(0,j.jsx)(j.Fragment,{children:s.map((e=>(0,j.jsx)("div",{children:e},e)))})})};var q=r(15738),T=r(78623),N=r(65907),O=r(70434),L=r(62605),D=r(47536),M=r(11126),U=r(18077);function z(e){if(!e)return[];const{namespace:t,rulerRule:r}=e,{rulesSource:s}=t;if((0,M.HY)(s)&&(0,U.Pc)(r))return r.grafana_alert.data;if((0,M.jq)(s)){const t=function(e,t){const r="A";switch(e.type){case"prometheus":return{refId:r,expr:t.query};case"loki":return{refId:r,expr:t.query};default:throw new Error(`Query for datasource type ${e.type} is currently not supported by cloud alert rules.`)}}(s,e);return[{refId:t.refId,datasourceUid:s.uid,queryType:"",model:t,relativeTimeRange:{from:360,to:0}}]}return[]}var A,E,F,Q,P,B=r(42673);const Z="Could not find data source for rule",V="Could not view rule",G="Alerting / View rule";function _(e){let{match:t}=e;const r=(0,o.useStyles2)(J),{id:s}=t.params,i=B.OA(s,!0),{loading:d,error:h,result:g}=(0,L.H)(i,null==i?void 0:i.ruleSourceName),f=(0,n.useMemo)((()=>new D.v),[]),p=(0,a.Z)(f.get()),m=(0,n.useMemo)((()=>z(g)),[g]),[x,v]=(0,n.useState)([]),{allDataSourcesAvailable:b}=(0,O.S)(m),S=(0,n.useCallback)((()=>{x.length>0&&b&&f.run(x)}),[x,f,b]);(0,n.useEffect)((()=>{v(m)}),[m]),(0,n.useEffect)((()=>{b&&S()}),[S,b]),(0,n.useEffect)((()=>()=>f.destroy()),[f]);const w=(0,n.useCallback)((e=>{v((t=>t.map((t=>t.refId===e.refId?e:t))))}),[]);if(null==i||!i.ruleSourceName)return(0,j.jsx)(u.$,{title:G,children:(0,j.jsx)(o.Alert,{title:V,children:(0,j.jsx)("details",{className:r.errorMessage,children:Z})})});const _=(0,M.o_)(i.ruleSourceName);if(d)return A||(A=(0,j.jsx)(u.$,{title:G,children:(0,j.jsx)(o.LoadingPlaceholder,{text:"Loading rule..."})}));var W;if(h||!_)return(0,j.jsx)(u.$,{title:G,children:(0,j.jsx)(o.Alert,{title:V,children:(0,j.jsxs)("details",{className:r.errorMessage,children:[null!==(W=null==h?void 0:h.message)&&void 0!==W?W:Z,E||(E=(0,j.jsx)("br",{})),!(null==h||!h.stack)&&h.stack]})})});if(!g)return F||(F=(0,j.jsx)(u.$,{title:G,children:(0,j.jsx)("span",{children:"Rule could not be found."})}));const K=Object.entries(g.annotations).filter((e=>{let[t,r]=e;return!!r.trim()})),Y=(0,U.Jq)(g.group);return(0,j.jsxs)(u.$,{wrapInContent:!1,title:G,children:[Y&&(Q||(Q=(0,j.jsx)(o.Alert,{severity:"info",title:"This rule is part of a federated rule group.",children:(0,j.jsxs)(o.VerticalGroup,{children:["Federated rule groups are currently an experimental feature.",(0,j.jsx)(o.Button,{fill:"text",icon:"book",children:(0,j.jsx)("a",{href:"https://grafana.com/docs/metrics-enterprise/latest/tenant-management/tenant-federation/#cross-tenant-alerting-and-recording-rule-federation",children:"Read documentation"})})]})}))),(0,j.jsxs)(u.l,{children:[(0,j.jsxs)("div",{children:[(0,j.jsxs)("h4",{children:[P||(P=(0,j.jsx)(o.Icon,{name:"bell",size:"lg"}))," ",g.name]}),(0,j.jsx)(N.p,{rule:g,isCreating:!1,isDeleting:!1}),(0,j.jsx)(R.f,{rule:g,rulesSource:_})]}),(0,j.jsxs)("div",{className:r.details,children:[(0,j.jsxs)("div",{className:r.leftSide,children:[g.promRule&&(0,j.jsx)(c.C,{label:"Health",horizontal:!0,children:(0,j.jsx)(T.V,{rule:g.promRule})}),!!g.labels&&!!Object.keys(g.labels).length&&(0,j.jsx)(c.C,{label:"Labels",horizontal:!0,children:(0,j.jsx)(l.s,{labels:g.labels})}),(0,j.jsx)(I.C,{rulesSource:_,rule:g,annotations:K}),(0,j.jsx)(C.J,{annotations:K})]}),(0,j.jsxs)("div",{className:r.rightSide,children:[(0,j.jsx)($.C,{rule:g,rulesSource:_}),Y&&(0,j.jsx)(k,{group:g.group}),(0,j.jsx)(c.C,{label:"Namespace / Group",children:`${g.namespace.name} / ${g.group.name}`})]})]}),(0,j.jsx)("div",{children:(0,j.jsx)(q.M,{rule:g})})]}),!Y&&p&&Object.keys(p).length>0&&(0,j.jsxs)(j.Fragment,{children:[(0,j.jsxs)("div",{className:r.queriesTitle,children:["Query results ",(0,j.jsx)(o.PanelChromeLoadingIndicator,{loading:H(p),onCancel:()=>f.cancel()})]}),(0,j.jsx)(u.l,{padding:0,children:(0,j.jsx)("div",{className:r.queries,children:x.map((e=>(0,j.jsx)("div",{className:r.query,children:(0,j.jsx)(y,{query:e,data:p&&p[e.refId],onChangeQuery:w})},e.refId)))})})]}),!Y&&!b&&(0,j.jsx)(o.Alert,{title:"Query not available",severity:"warning",className:r.queryWarning,children:"Cannot display the query preview. Some of the data sources used in the queries are not available."})]})}function H(e){return!!Object.values(e).find((e=>e.state===i.LoadingState.Loading))}const J=e=>({errorMessage:s.css`
      white-space: pre-wrap;
    `,queries:s.css`
      height: 100%;
      width: 100%;
    `,queriesTitle:s.css`
      padding: ${e.spacing(2,.5)};
      font-size: ${e.typography.h5.fontSize};
      font-weight: ${e.typography.fontWeightBold};
      font-family: ${e.typography.h5.fontFamily};
    `,query:s.css`
      border-bottom: 1px solid ${e.colors.border.medium};
      padding: ${e.spacing(2)};
    `,queryWarning:s.css`
      margin: ${e.spacing(4,0)};
    `,details:s.css`
      display: flex;
      flex-direction: row;
    `,leftSide:s.css`
      flex: 1;
    `,rightSide:s.css`
      padding-left: 90px;
      width: 300px;
    `}),W=(0,o.withErrorBoundary)(_,{style:"page"})},58124:(e,t,r)=>{r.d(t,{q:()=>a});r(76687);var s=r(69369),n=r(52010);const a=e=>{let{actions:t,children:r,fallback:a=!0}=e;return t.some((e=>s.Vt.hasAccess(e,a)))?(0,n.jsx)(n.Fragment,{children:r}):null}},96680:(e,t,r)=>{r.d(t,{j:()=>l});var s=r(76687),n=r(10967),a=r(70534),i=r(73285),o=r(52010);function l(e){const{value:t,onChange:r,size:l="md"}=e,c=(0,s.useMemo)((()=>Object.values(n.config.panels).reduce(((e,t)=>(function(e){switch(e){case i.GC:case i.Fe:case i.Kd:return!0;default:return!1}}(t.id)&&e.push({value:t.id,label:t.name,imgUrl:t.info.logos.small}),e)),[])),[]);return(0,o.jsx)(a.RadioButtonGroup,{options:c,value:t,onChange:r,size:l})}},14647:(e,t,r)=>{r.d(t,{$:()=>l,l:()=>c});var s=r(68490),n=(r(76687),r(10967)),a=r(70534),i=r(36131),o=r(52010);function l(e){const{wrapInContent:t=!0,children:r,title:s}=e,l=(0,a.useStyles2)(u);return(0,o.jsxs)(i.T,{children:[(0,o.jsx)(a.PageToolbar,{title:s,pageIcon:"bell",onGoBack:()=>n.locationService.push("/alerting/list")}),(0,o.jsx)("div",{className:l.content,children:t?(0,o.jsx)(c,Object.assign({},e)):r})]})}function c(e){let{children:t,padding:r=2}=e;const s=(0,a.useStyles2)(d(r));return(0,o.jsx)("div",{className:s.wrapper,children:t})}const u=e=>({content:s.css`
      margin: ${e.spacing(0,2,2)};
      max-width: ${e.breakpoints.values.xxl}px;
    `}),d=e=>t=>({wrapper:s.css`
      background: ${t.colors.background.primary};
      border: 1px solid ${t.colors.border.weak};
      border-radius: ${t.shape.borderRadius()};
      padding: ${t.spacing(e)};
    `})},70434:(e,t,r)=>{r.d(t,{S:()=>a});var s=r(76687),n=r(10967);function a(e){return{allDataSourcesAvailable:(0,s.useMemo)((()=>e.every((e=>Boolean((0,n.getDataSourceSrv)().getInstanceSettings(e.datasourceUid))))),[e])}}},62605:(e,t,r)=>{r.d(t,{H:()=>h,X:()=>g});var s=r(76687),n=r(46958),a=r(8650),i=r(68175),o=r(27121),l=r(42673),c=r(18077),u=r(63373),d=r(7566);function h(e,t){const r=f(t),n=(0,u.Zo)(t),a=(0,s.useMemo)((()=>{if(e&&t&&0!==n.length)for(const r of n)for(const s of r.groups)for(const r of s.rules){const s=l.Yd(t,r);if(l.Dg(s,e))return r}}),[e,t,n]);return Object.assign({},r,{result:a})}function g(e,t){const r=f(t),n=(0,u.Zo)(t),a=(0,s.useMemo)((()=>{if(!e||!t||0===n.length)return[];const r=[];for(const t of n)for(const s of t.groups)for(const t of s.rules)t.name===e&&r.push(t);return r}),[e,t,n]);return Object.assign({},r,{result:a})}function f(e){var t;const r=(0,n.useDispatch)(),s=(0,d._)((e=>e.promRules)),o=p(e,s),l=(0,d._)((e=>e.rulerRules)),u=p(e,l),{loading:h}=(0,a.Z)((async()=>{e&&await r((0,i.dn)({rulesSourceName:e}))}),[r,e]);return{loading:h,error:(null!==(t=o.error)&&void 0!==t?t:(0,c.m$)(u))?void 0:u.error,dispatched:o.dispatched&&u.dispatched}}function p(e,t){if(!e)return o.oq;const r=t[e];return r||o.oq}},47536:(e,t,r)=>{r.d(t,{v:()=>R});var s=r(32461),n=r(41276),a=r(87569),i=r(31996),o=r(79978),l=r(816),c=r(19177),u=r(10967),d=r(22260),h=r(46852),g=r(97416),f=r(36724),p=r(9046),m=r(90411);const x={from:21600,to:0},j=(e,t)=>{switch(e.type){case m.Us.classic:return v(e);case m.Us.math:return y(e,t);case m.Us.resample:case m.Us.reduce:return S(e)}},v=e=>{var t;return null===(t=e.conditions)||void 0===t?void 0:t.map((e=>e.query.params[0]))},b=(e,t)=>{let r=[],s=[x.to];for(const n of e){const e=t.find((e=>e.refId===n));e&&e.relativeTimeRange&&(r.push(e.relativeTimeRange.from),s.push(e.relativeTimeRange.to))}return{from:r,to:s}},y=(e,t)=>t.filter((t=>{var r;return"query"===t.queryType&&(null===(r=e.expression)||void 0===r?void 0:r.includes(t.refId))})).map((e=>e.refId)),S=e=>e.expression?[e.expression]:void 0;function w(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}class R{constructor(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:(0,d.i)(),t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:(0,u.getDataSourceSrv)();w(this,"subject",void 0),w(this,"subscription",void 0),w(this,"lastResult",void 0),this.backendSrv=e,this.dataSourceSrv=t,this.subject=new s.t(1),this.lastResult={}}get(){return this.subject.asObservable()}async run(e){if(0===e.length){const t=$(e,c.LoadingState.Done);return this.subject.next(t)}for(const t of e)if(!(0,h.j)(t.model)){const r=await this.dataSourceSrv.get(t.datasourceUid);if(r.filterQuery&&!r.filterQuery(t.model)){const t=$(e,c.LoadingState.Done);return this.subject.next(t)}}this.subscription=C(this.backendSrv,e).subscribe({next:e=>{const t=T(e,((e,t)=>{const r=this.lastResult[e],s=(0,p.zR)(t,r);return(0,f.C)(s,r)}));this.lastResult=t,this.subject.next(this.lastResult)},error:e=>{this.lastResult=q(this.lastResult,e),this.subject.next(this.lastResult)}})}cancel(){if(!this.subscription)return;this.subscription.unsubscribe();let e=!1;const t=T(this.lastResult,((t,r)=>(r.state===c.LoadingState.Loading&&(e=!0),Object.assign({},r,{state:c.LoadingState.Done}))));e&&this.subject.next(t)}destroy(){this.subject&&this.subject.complete(),this.cancel()}}const C=(e,t)=>{const r=$(t,c.LoadingState.Loading),s={data:{data:t},url:"/api/v1/eval",method:"POST",requestId:(0,l.Z)()};return(0,c.withLoadingIndicator)({whileLoading:r,source:e.fetch(s).pipe(k(r),(0,a.K)((e=>(0,n.of)(q(r,e)))),(0,g.V)(e,s.requestId),(0,i.B)())})},$=(e,t)=>e.reduce(((r,s)=>(r[s.refId]={state:t,series:[],timeRange:I(s,e)},r)),{}),I=(e,t)=>{if((0,h.j)(e.model)){const r=((e,t)=>{const r=j(e,t);if(!r)return x;const{from:s,to:n}=b(r,t);return s.length||n.length?{from:Math.max(...s),to:Math.min(...n)}:x})(e.model,t);return c.rangeUtil.relativeToTimeRange(r)}return e.relativeTimeRange?c.rangeUtil.relativeToTimeRange(e.relativeTimeRange):(console.warn(`Query with refId: ${e.refId} did not have any relative time range, using default.`),(0,c.getDefaultTimeRange)())},k=e=>(0,o.U)((t=>{const{data:r}=t,s={};for(const[t,n]of Object.entries(r.results))s[t]={timeRange:e[t].timeRange,state:c.LoadingState.Done,series:n.frames.map(c.dataFrameFromJSON)};return s})),q=(e,t)=>{const r=(0,u.toDataQueryError)(t);return T(e,((e,t)=>Object.assign({},t,{state:c.LoadingState.Error,error:r})))},T=(e,t)=>{const r={};for(const[s,n]of Object.entries(e))r[s]=t(s,n);return r}},46852:(e,t,r)=>{r.d(t,{j:()=>a});var s=r(51182),n=r(90411);const a=e=>{if(!e)return!1;if((0,s.Pr)(e.datasource))return!0;const t=e;return"string"==typeof t.type&&Object.values(n.Us).includes(t.type)}},75652:(e,t,r)=>{r.d(t,{Z:()=>a});var s=r(76687),n=r(38198);const a=function(e,t){var r=(0,s.useState)(t),a=r[0],i=r[1];return(0,n.Z)((function(){var t=e.subscribe(i);return function(){return t.unsubscribe()}}),[e]),a}}}]);
//# sourceMappingURL=AlertingRule.2c11e849695cdc792ddb.js.map