"use strict";(self.webpackChunkgrafana=self.webpackChunkgrafana||[]).push([[7303,2113],{97303:(Y,x,n)=>{n.r(x),n.d(x,{default:()=>Ie,getStyles:()=>de});var u=n(66681),d=n(56375),e=n(85960),E=n(53285),T=n(85745),S=n(47257),F=n(49199),f=n(13086),B=n(14607),y=n(97666),H=n(29165),X=n(20519),ee=n(57445),te=n(24956),O=n(58712);const D=O.C.injectEndpoints({endpoints:t=>({getRuleHistory:t.query({query:({ruleUid:a,from:s,to:r,limit:l=100})=>({url:"/api/v1/rules/history",params:{ruleUID:a,from:s,to:r,limit:l}})})})});var g=n(98724),J=n(2698),P=n(23595),W=n(57582),ne=n(12537),R=n(7042),w=n(34340),L=n(14591),M=n(13405);function $(t){const a=t.reduce((s,r)=>{const l=s.get(r.timestamp);return l?l.push(r):s.set(r.timestamp,[r]),s},new Map);return new Map([...a].sort((s,r)=>r[0]-s[0]))}const z=e.memo(({records:t,commonLabels:a,onLabelClick:s,onRecordsRendered:r})=>{const l=(0,S.wW)(K),i=$(t),o=new Map;return(0,e.useEffect)(()=>{r&&r(o)}),e.createElement("ul",{className:l.logsScrollable,"aria-label":"State history by timestamp"},Array.from(i.entries()).map(([m,I])=>e.createElement("li",{id:m.toString(10),key:m,"data-testid":m,ref:c=>c&&o.set(m,c),className:l.listItemWrapper},e.createElement(U,{time:m}),e.createElement("div",{className:l.logsContainer},I.map(({line:c})=>e.createElement(e.Fragment,{key:(0,d.uniqueId)()},e.createElement(L.l,{state:c.previous,size:"sm",muted:!0}),e.createElement(y.J,{name:"arrow-right",size:"sm"}),e.createElement(L.l,{state:c.current}),e.createElement(f.K,null,c.values&&e.createElement(N,{record:c.values})),e.createElement("div",null,c.labels&&e.createElement(R.P,{tags:(0,M.r)(Object.entries(c.labels),a).map(([h,Z])=>`${h}=${Z}`),onClick:s}))))))))});z.displayName="LogRecordViewerByTimestamp";function b({records:t,commonLabels:a}){const s=useStyles2(K),r=groupBy(t,l=>JSON.stringify(l.line.labels));return React.createElement(React.Fragment,null,Object.entries(r).map(([l,i])=>React.createElement(Stack,{direction:"column",key:l},React.createElement("h4",null,React.createElement(TagList,{tags:omitLabels(Object.entries(i[0].line.labels??{}),a).map(([o,m])=>`${o}=${m}`)})),React.createElement("div",{className:s.logsContainer},i.map(({line:o,timestamp:m})=>React.createElement("div",{key:uniqueId()},React.createElement(AlertStateTag,{state:o.previous,size:"sm",muted:!0}),React.createElement(Icon,{name:"arrow-right",size:"sm"}),React.createElement(AlertStateTag,{state:o.current}),React.createElement(Stack,null,o.values&&React.createElement(N,{record:o.values})),React.createElement("div",null,dateTimeFormat(m))))))))}const U=({time:t})=>{const a=new Date(t),s=(0,S.wW)(K);return e.createElement("div",{className:s.timestampWrapper},e.createElement(f.K,{alignItems:"center",gap:1},e.createElement(y.J,{name:"clock-nine",size:"sm"}),e.createElement("span",{className:s.timestampText},(0,ne.dq)(a)),e.createElement("small",null,"(",(0,W.Z)(a)," ago)")))},N=e.memo(({record:t})=>{const a=Object.entries(t);return e.createElement(e.Fragment,null,a.map(([s,r])=>e.createElement(w._,{key:s,label:s,value:r})))});N.displayName="AlertInstanceValues";const K=t=>({logsContainer:(0,u.css)`
    display: grid;
    grid-template-columns: max-content max-content max-content auto max-content;
    gap: ${t.spacing(2,1)};
    align-items: center;
  `,logsScrollable:(0,u.css)`
    height: 500px;
    overflow: scroll;

    flex: 1;
  `,timestampWrapper:(0,u.css)`
    color: ${t.colors.text.secondary};
  `,timestampText:(0,u.css)`
    color: ${t.colors.text.primary};
    font-size: ${t.typography.bodySmall.fontSize};
    font-weight: ${t.typography.fontWeightBold};
  `,listItemWrapper:(0,u.css)`
    background: transparent;
    outline: 1px solid transparent;

    transition:
      background 150ms,
      outline 150ms;
    padding: ${t.spacing(1)} ${t.spacing(1.5)};
  `});var A=n(53565),Q=n(81517),p=n(1713),ae=n(99532),re=n(7012);const k=e.memo(({frames:t,timeRange:a,onPointerMove:s=d.noop})=>{const r=(0,S.l4)(),{setupCursorTracking:l}=se(s);return e.createElement(A.Z,{disableHeight:!0},({width:i})=>e.createElement(ae.K,{frames:t,timeRange:a,timeZone:"browser",mode:re.KN.Changes,height:18*t.length+50,width:i,showValue:p.Jp.Never,theme:r,rowHeight:.8,legend:{calcs:[],displayMode:p.jK.List,placement:"bottom",showLegend:!0},legendItems:[{label:"Normal",color:r.colors.success.main,yAxis:1},{label:"Pending",color:r.colors.warning.main,yAxis:1},{label:"Alerting",color:r.colors.error.main,yAxis:1},{label:"NoData",color:r.colors.info.main,yAxis:1},{label:"Mixed",color:r.colors.text.secondary,yAxis:1}]},o=>(l(o),null)))});function se(t){const a=(0,e.useRef)(new Q.X({seriesIdx:0,pointIdx:0}));return(0,e.useEffect)(()=>{const r=a.current.subscribe(({seriesIdx:l,pointIdx:i})=>{t&&t(l,i)});return()=>{r.unsubscribe()}},[t]),{setupCursorTracking:r=>{r.setSync();const l=r.getTooltipInterpolator();l&&r.addHook("setCursor",i=>{l(o=>{if(o){const m=a.current.getValue();a.current.next({...m,seriesIdx:o})}},o=>{if(o){const m=a.current.getValue();a.current.next({...m,pointIdx:o})}},()=>{},i)})}}}k.displayName="LogTimelineViewer";var G=n(58309),oe=n(64282),ye=n(70129),me=n(91470);function Ee(t,a){const s=(0,S.l4)();return(0,e.useMemo)(()=>{const r=t?.data?.values[0]??[],l=Se(r)?r:[],i=t?.data?.values[1]??[],o=l.reduce((v,C,ie)=>{const j=i[ie];return Te(j)&&v.push({timestamp:C,line:j}),v},[]),m=(0,d.groupBy)(o,v=>JSON.stringify(v.line.labels)),c=Object.keys(m).map(v=>Object.entries(JSON.parse(v))),h=(0,M.z)(c),Z=a?(0,g.Zh)(a):[],le=Object.entries(m).filter(([v])=>{const C=JSON.parse(v);return(0,g.eD)(C,Z)}).map(([v,C])=>xe(v,C,h,s));return{historyRecords:o.filter(({line:v})=>v.labels&&(0,g.eD)(v.labels,Z)),dataFrames:le,commonLabels:h,totalRecordsCount:o.length}},[t,a,s])}function Se(t){return t.every(a=>typeof a=="number")}function Te(t){return typeof t=="object"&&t!==null&&"current"in t&&"previous"in t}function xe(t,a,s,r){const l=Object.entries(JSON.parse(t)),i={name:"time",type:G.fS.time,values:[...a.map(c=>c.timestamp),Date.now()],config:{displayName:"Time",custom:{fillOpacity:100}}},o=i.values.map((c,h)=>h);o.sort((0,ye.Mo)(i));const m=[...a.map(c=>c.line.current),a.at(-1)?.line.current],I={fields:[{...i,values:i.values.map((c,h)=>i.values[o[h]])},{name:"state",type:G.fS.string,values:m.map((c,h)=>m[o[h]]),config:{displayName:(0,M.r)(l,s).map(([c,h])=>`${c}=${h}`).join(", "),color:{mode:"thresholds"},custom:{fillOpacity:100},mappings:[{type:me.Hi.ValueToText,options:{Alerting:{color:r.colors.error.main},Pending:{color:r.colors.warning.main},Normal:{color:r.colors.success.main},NoData:{color:r.colors.info.main}}}],thresholds:{mode:me.H3.Absolute,steps:[]}}}],length:i.values.length,name:t};return I.fields.forEach(c=>{c.display=(0,oe.U)({field:c,theme:r})}),I}const Re=12,Le=({ruleUID:t})=>{const a=(0,S.wW)(de),[s,r]=(0,e.useState)(""),l=(0,e.useRef)(new Map),{getValues:i,setValue:o,register:m,handleSubmit:I}=(0,E.cI)({defaultValues:{query:""}}),{useGetRuleHistoryQuery:c}=D,h=(0,e.useMemo)(()=>Ne(),[]),{currentData:Z,isLoading:ge,isError:le,error:v}=c({ruleUid:t,from:h.from.unix(),to:h.to.unix(),limit:250}),{dataFrames:C,historyRecords:ie,commonLabels:j,totalRecordsCount:ce}=Ee(Z,s),{frameSubset:q,frameSubsetTimestamps:fe,frameTimeRange:Ce}=be(C),Me=(0,e.useCallback)(V=>{const _=(0,g.vB)(i("query"),V);r(_),o("query",_)},[r,o,i]),pe=(0,e.useCallback)(()=>{r(""),o("query","")},[r,o]),ve=(0,e.useRef)(void 0),Oe=(0,e.useCallback)((V,_)=>{ve.current?.classList.remove(a.highlightedLogRecord);const Fe=fe[_],he=l.current.get(Fe);he?.classList.add(a.highlightedLogRecord),ve.current=he},[fe,a.highlightedLogRecord]);if(ge)return e.createElement("div",null,"Loading...");if(le)return e.createElement(F.b,{title:"Error fetching the state history",severity:"error"},v instanceof Error?v.message:"Unable to fetch alert state history");const De=q.length<C.length,Ae=ce>0?`No matches were found for the given filters among the ${ce} instances`:"No state transitions have occurred in the last 30 days";return e.createElement("div",{className:a.fullSize},e.createElement("form",{onSubmit:I(V=>r(V.query))},e.createElement(ue,{...m("query"),showClearFilterSuffix:!!s,onClearFilterClick:pe}),e.createElement("input",{type:"submit",hidden:!0})),!(0,d.isEmpty)(j)&&e.createElement("div",{className:a.commonLabels},e.createElement(f.K,{gap:1,alignItems:"center"},e.createElement("strong",null,"Common labels"),e.createElement(B.u,{content:"Common labels are the ones attached to all of the alert instances"},e.createElement(y.J,{name:"info-circle"})),e.createElement(J.s,{labels:(0,d.fromPairs)(j),size:"sm"}))),(0,d.isEmpty)(q)?e.createElement(e.Fragment,null,e.createElement("div",{className:a.emptyState},Ae,ce>0&&e.createElement(H.zx,{variant:"secondary",type:"button",onClick:pe},"Clear filters"))):e.createElement(e.Fragment,null,e.createElement("div",{className:a.graphWrapper},e.createElement(k,{frames:q,timeRange:Ce,onPointerMove:Oe})),De&&e.createElement("div",{className:a.moreInstancesWarning},e.createElement(f.K,{direction:"row",alignItems:"center",gap:1},e.createElement(y.J,{name:"exclamation-triangle",size:"sm"}),e.createElement("small",null,`Only showing ${q.length} out of ${C.length} instances. Click on the labels to narrow down the results`))),e.createElement(z,{records:ie,commonLabels:j,onRecordsRendered:V=>l.current=V,onLabelClick:Me})))};function be(t){return(0,e.useMemo)(()=>{const a=(0,d.take)(t,Re),s=(0,d.sortBy)((0,d.uniq)(a.flatMap(I=>I.fields[0].values))),r=Math.min(...s),l=Math.max(...s),i=(0,T.CQ)(r),o=(0,T.CQ)(l);return{frameSubset:a,frameSubsetTimestamps:s,frameTimeRange:{from:i,to:o,raw:{from:i,to:o}}}},[t])}const ue=e.forwardRef(({showClearFilterSuffix:t,onClearFilterClick:a,...s},r)=>e.createElement(X.g,{label:e.createElement(ee._,{htmlFor:"instancesSearchInput"},e.createElement(f.K,{gap:.5},e.createElement("span",null,"Filter instances"),e.createElement(P.z,{content:e.createElement(e.Fragment,null,"Use label matcher expression (like ",e.createElement("code",null,"{foo=bar}"),") or click on an instance label to filter instances")},e.createElement(y.J,{name:"info-circle",size:"sm"}))))},e.createElement(te.I,{id:"instancesSearchInput",prefix:e.createElement(y.J,{name:"search"}),suffix:t&&e.createElement(H.zx,{fill:"text",icon:"times",size:"sm",onClick:a},"Clear"),placeholder:"Filter instances",ref:r,...s})));ue.displayName="SearchFieldInput";function Ne(){const t=(0,T.CQ)().subtract(30,"days"),a=(0,T.CQ)();return{from:t,to:a,raw:{from:t,to:a}}}const de=t=>({fullSize:(0,u.css)`
    min-width: 100%;
    height: 100%;

    display: flex;
    flex-direction: column;
  `,graphWrapper:(0,u.css)`
    padding: ${t.spacing()} 0;
  `,emptyState:(0,u.css)`
    color: ${t.colors.text.secondary};

    display: flex;
    flex-direction: column;
    gap: ${t.spacing(2)};
    align-items: center;
    margin: auto auto;
  `,moreInstancesWarning:(0,u.css)`
    color: ${t.colors.warning.text};
    padding: ${t.spacing()};
  `,commonLabels:(0,u.css)`
    display: grid;
    grid-template-columns: max-content auto;
  `,highlightedLogRecord:(0,u.css)`
    background: ${t.colors.primary.transparent} !important;
    outline: 1px solid ${t.colors.primary.shade} !important;
  `}),Ie=Le},13405:(Y,x,n)=>{n.d(x,{r:()=>e,z:()=>E});var u=n(56375),d=n.n(u);function e(T,S){return T.filter(F=>!S.find(f=>JSON.stringify(f)===JSON.stringify(F)))}function E(T){const S=T.flatMap(f=>f);return(0,u.uniqBy)(S.filter(f=>S.filter(y=>(0,u.isEqual)(f,y)).length===Object.keys(T).length),f=>JSON.stringify(f))}},87474:(Y,x,n)=>{n.d(x,{Z:()=>u});function u(d,e){if(d==null)throw new TypeError("assign requires that input parameter not be null or undefined");for(var E in e)Object.prototype.hasOwnProperty.call(e,E)&&(d[E]=e[E]);return d}},37166:(Y,x,n)=>{n.d(x,{Z:()=>d});var u=n(87474);function d(e){return(0,u.Z)({},e)}},57582:(Y,x,n)=>{n.d(x,{Z:()=>te});var u=n(91671),d=n(89933),e=n(22799),E=n(78321),T=n(37166),S=n(87474),F=n(75847),f=n(12173),B=1e3*60,y=60*24,H=y*30,X=y*365;function ee(O,D,g){var J,P,W;(0,f.Z)(2,arguments);var ne=(0,u.j)(),R=(J=(P=g?.locale)!==null&&P!==void 0?P:ne.locale)!==null&&J!==void 0?J:F.Z;if(!R.formatDistance)throw new RangeError("locale must contain localize.formatDistance property");var w=(0,e.Z)(O,D);if(isNaN(w))throw new RangeError("Invalid time value");var L=(0,S.Z)((0,T.Z)(g),{addSuffix:!!g?.addSuffix,comparison:w}),M,$;w>0?(M=(0,E.Z)(D),$=(0,E.Z)(O)):(M=(0,E.Z)(O),$=(0,E.Z)(D));var z=String((W=g?.roundingMethod)!==null&&W!==void 0?W:"round"),b;if(z==="floor")b=Math.floor;else if(z==="ceil")b=Math.ceil;else if(z==="round")b=Math.round;else throw new RangeError("roundingMethod must be 'floor', 'ceil' or 'round'");var U=$.getTime()-M.getTime(),N=U/B,K=(0,d.Z)($)-(0,d.Z)(M),A=(U-K)/B,Q=g?.unit,p;if(Q?p=String(Q):N<1?p="second":N<60?p="minute":N<y?p="hour":A<H?p="day":A<X?p="month":p="year",p==="second"){var ae=b(U/1e3);return R.formatDistance("xSeconds",ae,L)}else if(p==="minute"){var re=b(N);return R.formatDistance("xMinutes",re,L)}else if(p==="hour"){var k=b(N/60);return R.formatDistance("xHours",k,L)}else if(p==="day"){var se=b(A/y);return R.formatDistance("xDays",se,L)}else if(p==="month"){var G=b(A/H);return G===12&&Q!=="month"?R.formatDistance("xYears",1,L):R.formatDistance("xMonths",G,L)}else if(p==="year"){var oe=b(A/X);return R.formatDistance("xYears",oe,L)}throw new RangeError("unit must be 'second', 'minute', 'hour', 'day', 'month' or 'year'")}function te(O,D){return(0,f.Z)(1,arguments),ee(O,Date.now(),D)}}}]);

//# sourceMappingURL=7303.ca275f241492226c0dbd.js.map