"use strict";(self.webpackChunkgrafana=self.webpackChunkgrafana||[]).push([[4394],{46081:(F,I,a)=>{a.d(I,{_:()=>S});var g=a(9892),d=a(66111),e=a(26418),M=a(72648),T=a(39904);const S=({label:u,value:$,icon:R})=>{const O=(0,M.wW)(L);return d.createElement("div",{className:O.meta().wrapper},d.createElement(e.Stack,{direction:"row",gap:0,alignItems:"stretch"},d.createElement("div",{className:O.meta().label},d.createElement(e.Stack,{direction:"row",gap:.5,alignItems:"center"},R&&d.createElement(T.J,{name:R})," ",u??"")),d.createElement("div",{className:O.meta().value},$)))},L=u=>({meta:$=>({wrapper:g.css`
      font-size: ${u.typography.bodySmall.fontSize};
    `,label:g.css`
      display: flex;
      align-items: center;

      padding: ${u.spacing(.33)} ${u.spacing(1)};
      background: ${u.colors.secondary.transparent};

      border: solid 1px ${u.colors.border.medium};
      border-top-left-radius: ${u.shape.borderRadius(2)};
      border-bottom-left-radius: ${u.shape.borderRadius(2)};
    `,value:g.css`
      padding: ${u.spacing(.33)} ${u.spacing(1)};
      font-weight: ${u.typography.fontWeightBold};

      border: solid 1px ${u.colors.border.medium};
      border-left: none;
      border-top-right-radius: ${u.shape.borderRadius(2)};
      border-bottom-right-radius: ${u.shape.borderRadius(2)};
    `})})},4394:(F,I,a)=>{a.r(I),a.d(I,{default:()=>we,getStyles:()=>le});var g=a(9892),d=a(82897),e=a(66111),M=a(66698),T=a(67483),S=a(26418),L=a(72648),u=a(45253),$=a(61860),R=a(39904),O=a(34807),G=a(31403),ie=a(24799),ce=a(64353),me=a(46967),ue=a(29427);const de=ue.C.injectEndpoints({endpoints:t=>({getRuleHistory:t.query({query:({ruleUid:n,from:s,to:r,limit:i=100})=>({url:"/api/v1/rules/history",params:{ruleUID:n,from:s,to:r,limit:i}})})})});var P=a(20194),ge=a(42042),fe=a(10929),Y=a(8679),pe=a(68900),z=a(85719),ve=a(96529),Ee=a(81777),ye=a(93552),X=a(29122),k=1e3*60,B=60*24,q=B*30,_=B*365;function he(t,n,s){var r,i,m;(0,X.Z)(2,arguments);var o=(0,fe.j)(),c=(r=(i=s?.locale)!==null&&i!==void 0?i:o.locale)!==null&&r!==void 0?r:ye.Z;if(!c.formatDistance)throw new RangeError("locale must contain localize.formatDistance property");var y=(0,pe.Z)(t,n);if(isNaN(y))throw new RangeError("Invalid time value");var l=(0,Ee.Z)((0,ve.Z)(s),{addSuffix:Boolean(s?.addSuffix),comparison:y}),f,x;y>0?(f=(0,z.Z)(n),x=(0,z.Z)(t)):(f=(0,z.Z)(t),x=(0,z.Z)(n));var D=String((m=s?.roundingMethod)!==null&&m!==void 0?m:"round"),h;if(D==="floor")h=Math.floor;else if(D==="ceil")h=Math.ceil;else if(D==="round")h=Math.round;else throw new RangeError("roundingMethod must be 'floor', 'ceil' or 'round'");var p=x.getTime()-f.getTime(),E=p/k,A=(0,Y.Z)(x)-(0,Y.Z)(f),b=(p-A)/k,C=s?.unit,v;if(C?v=String(C):E<1?v="second":E<60?v="minute":E<B?v="hour":b<q?v="day":b<_?v="month":v="year",v==="second"){var W=h(p/1e3);return c.formatDistance("xSeconds",W,l)}else if(v==="minute"){var H=h(E);return c.formatDistance("xMinutes",H,l)}else if(v==="hour"){var K=h(E/60);return c.formatDistance("xHours",K,l)}else if(v==="day"){var j=h(b/B);return c.formatDistance("xDays",j,l)}else if(v==="month"){var w=h(b/q);return w===12&&C!=="month"?c.formatDistance("xYears",1,l):c.formatDistance("xMonths",w,l)}else if(v==="year"){var Q=h(b/_);return c.formatDistance("xYears",Q,l)}throw new RangeError("unit must be 'second', 'minute', 'hour', 'day', 'month' or 'year'")}function Se(t,n){return(0,X.Z)(1,arguments),he(t,Date.now(),n)}var be=a(77258),Te=a(46081),ee=a(45608),Z=a(38645);function Re(t){const n=t.reduce((s,r)=>{const i=s.get(r.timestamp);return i?i.push(r):s.set(r.timestamp,[r]),s},new Map);return new Map([...n].sort((s,r)=>r[0]-s[0]))}const te=e.memo(({records:t,commonLabels:n,onLabelClick:s,onRecordsRendered:r})=>{const i=(0,L.wW)(J),m=Re(t),o=new Map;return(0,e.useEffect)(()=>{r&&r(o)}),e.createElement("ul",{className:i.logsScrollable,"aria-label":"State history by timestamp"},Array.from(m.entries()).map(([c,y])=>e.createElement("li",{id:c.toString(10),key:c,"data-testid":c,ref:l=>l&&o.set(c,l)},e.createElement(xe,{time:c}),e.createElement("div",{className:i.logsContainer},y.map(({line:l})=>e.createElement(e.Fragment,{key:(0,d.uniqueId)()},e.createElement(ee.l,{state:l.previous,size:"sm",muted:!0}),e.createElement(R.J,{name:"arrow-right",size:"sm"}),e.createElement(ee.l,{state:l.current}),e.createElement(S.Stack,{direction:"row"},l.values&&e.createElement(V,{record:l.values})),e.createElement("div",null,l.labels&&e.createElement(O.P,{tags:(0,Z.r)(Object.entries(l.labels),n).map(([f,x])=>`${f}=${x}`),onClick:s}))))))))});te.displayName="LogRecordViewerByTimestamp";function Ve({records:t,commonLabels:n}){const s=useStyles2(J),r=groupBy(t,i=>JSON.stringify(i.line.labels));return React.createElement(React.Fragment,null,Object.entries(r).map(([i,m])=>React.createElement(Stack,{direction:"column",key:i},React.createElement("h4",null,React.createElement(TagList,{tags:omitLabels(Object.entries(m[0].line.labels??{}),n).map(([o,c])=>`${o}=${c}`)})),React.createElement("div",{className:s.logsContainer},m.map(({line:o,timestamp:c})=>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,{direction:"row"},o.values&&React.createElement(V,{record:o.values})),React.createElement("div",null,dateTimeFormat(c))))))))}const xe=({time:t})=>{const n=new Date(t),s=(0,L.wW)(J);return e.createElement("div",{className:s.timestampWrapper},e.createElement(S.Stack,{direction:"row",alignItems:"center",gap:1},e.createElement(R.J,{name:"clock-nine",size:"sm"}),e.createElement("span",{className:s.timestampText},(0,be.dq)(n)),e.createElement("small",null,"(",Se(n)," ago)")))},V=e.memo(({record:t})=>{const n=Object.entries(t);return e.createElement(e.Fragment,null,n.map(([s,r])=>e.createElement(Te._,{key:s,label:s,value:r})))});V.displayName="AlertInstanceValues";const J=t=>({logsContainer:g.css`
    display: grid;
    grid-template-columns: max-content max-content max-content auto max-content;
    gap: ${t.spacing(2,1)};
    align-items: center;
  `,logsScrollable:g.css`
    height: 500px;
    overflow: scroll;

    flex: 1;
  `,timestampWrapper:g.css`
    color: ${t.colors.text.secondary};
    padding: ${t.spacing(1)} 0;
  `,timestampText:g.css`
    color: ${t.colors.text.primary};
    font-size: ${t.typography.bodySmall.fontSize};
    font-weight: ${t.typography.fontWeightBold};
  `});var Ie=a(89050),Le=a(924),ae=a(40538),Me=a(59420),Ne=a(97551);const ne=e.memo(({frames:t,timeRange:n,onPointerMove:s=d.noop})=>{const r=(0,L.l4)(),{setupCursorTracking:i}=Ce(s);return e.createElement(Ie.Z,{disableHeight:!0},({width:m})=>e.createElement(Me.K,{frames:t,timeRange:n,timeZone:"browser",mode:Ne.KN.Changes,height:18*t.length+50,width:m,showValue:ae.Jp.Never,theme:r,rowHeight:.8,legend:{calcs:[],displayMode:ae.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}]},o=>(i(o),null)))});function Ce(t){const n=(0,e.useRef)(new Le.X({seriesIdx:0,pointIdx:0}));return(0,e.useEffect)(()=>{const r=n.current.subscribe(({seriesIdx:i,pointIdx:m})=>{t&&t(i,m)});return()=>{r.unsubscribe()}},[t]),{setupCursorTracking:r=>{r.setSync();const i=r.getTooltipInterpolator();i&&r.addHook("setCursor",m=>{i(o=>{if(o){const c=n.current.getValue();n.current.next({...c,seriesIdx:o})}},o=>{if(o){const c=n.current.getValue();n.current.next({...c,pointIdx:o})}},()=>{},m)})}}}ne.displayName="LogTimelineViewer";var re=a(99098),Oe=a(6194),De=a(76152),se=a(85596);function $e(t,n){const s=(0,L.l4)();return(0,e.useMemo)(()=>{const r=t?.data?.values[0]??[],i=Ae(r)?r:[],m=t?.data?.values[1]??[],o=i.reduce((p,E,A)=>{const b=m[A];return Fe(b)&&p.push({timestamp:E,line:b}),p},[]),c=(0,d.groupBy)(o,p=>JSON.stringify(p.line.labels)),l=Object.keys(c).map(p=>Object.entries(JSON.parse(p))),f=(0,Z.z)(l),x=n?(0,P.Zh)(n):[],h=Object.entries(c).filter(([p])=>{const E=JSON.parse(p);return(0,P.eD)(E,x)}).map(([p,E])=>Pe(p,E,f,s));return{historyRecords:o.filter(({line:p})=>p.labels&&(0,P.eD)(p.labels,x)),dataFrames:h,commonLabels:f,totalRecordsCount:o.length}},[t,n,s])}function Ae(t){return t.every(n=>typeof n=="number")}function Fe(t){return typeof t=="object"&&t!==null&&"current"in t&&"previous"in t}function Pe(t,n,s,r){const i=Object.entries(JSON.parse(t)),m={name:"time",type:re.fS.time,values:[...n.map(l=>l.timestamp),Date.now()],config:{displayName:"Time",custom:{fillOpacity:100}}},o=m.values.map((l,f)=>f);o.sort((0,De.Mo)(m));const c=[...n.map(l=>l.line.current),n.at(-1)?.line.current],y={fields:[{...m,values:m.values.map((l,f)=>m.values[o[f]])},{name:"state",type:re.fS.string,values:c.map((l,f)=>c[o[f]]),config:{displayName:(0,Z.r)(i,s).map(([l,f])=>`${l}=${f}`).join(", "),color:{mode:"thresholds"},custom:{fillOpacity:100},mappings:[{type:se.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:se.H3.Absolute,steps:[]}}}],length:m.values.length,name:t};return y.fields.forEach(l=>{l.display=(0,Oe.U_)({field:l,theme:r})}),y}const ze=12,Be=({ruleUID:t})=>{const n=(0,L.wW)(le),[s,r]=(0,e.useState)(""),i=(0,e.useRef)(new Map),{getValues:m,setValue:o,register:c,handleSubmit:y}=(0,M.cI)({defaultValues:{query:""}}),{useGetRuleHistoryQuery:l}=de,f=(0,e.useMemo)(()=>je(),[]),{currentData:x,isLoading:D,isError:h,error:p}=l({ruleUid:t,from:f.from.unix(),to:f.to.unix(),limit:250}),{dataFrames:E,historyRecords:A,commonLabels:b,totalRecordsCount:C}=$e(x,s),{frameSubset:v,frameSubsetTimestamps:W,frameTimeRange:H}=We(E),K=(0,e.useCallback)(N=>{const U=(0,P.vB)(m("query"),N);r(U),o("query",U)},[r,o,m]),j=(0,e.useCallback)(()=>{r(""),o("query","")},[r,o]),w=(0,e.useCallback)((N,U)=>{const Ze=W[U];i.current.get(Ze)?.scrollIntoView({behavior:"smooth",block:"start"})},[W]);if(D)return e.createElement("div",null,"Loading...");if(h)return e.createElement(u.b,{title:"Error fetching the state history",severity:"error"},p instanceof Error?p.message:"Unable to fetch alert state history");const Q=v.length<E.length,Ue=C>0?`No matches were found for the given filters among the ${C} instances`:"No state transitions have occurred in the last 30 days";return e.createElement("div",{className:n.fullSize},e.createElement("form",{onSubmit:y(N=>r(N.query))},e.createElement(oe,{...c("query"),showClearFilterSuffix:!!s,onClearFilterClick:j}),e.createElement("input",{type:"submit",hidden:!0})),!(0,d.isEmpty)(b)&&e.createElement("div",{className:n.commonLabels},e.createElement(S.Stack,{gap:1,alignItems:"center"},e.createElement("strong",null,"Common labels"),e.createElement($.u,{content:"Common labels are the ones attached to all of the alert instances"},e.createElement(R.J,{name:"info-circle"}))),e.createElement(O.P,{tags:b.map(N=>N.join("="))})),(0,d.isEmpty)(v)?e.createElement(e.Fragment,null,e.createElement("div",{className:n.emptyState},Ue,C>0&&e.createElement(G.zx,{variant:"secondary",type:"button",onClick:j},"Clear filters"))):e.createElement(e.Fragment,null,e.createElement("div",{className:n.graphWrapper},e.createElement(ne,{frames:v,timeRange:H,onPointerMove:w})),Q&&e.createElement("div",{className:n.moreInstancesWarning},e.createElement(S.Stack,{direction:"row",alignItems:"center",gap:1},e.createElement(R.J,{name:"exclamation-triangle",size:"sm"}),e.createElement("small",null,`Only showing ${v.length} out of ${E.length} instances. Click on the labels to narrow down the results`))),e.createElement(te,{records:A,commonLabels:b,onRecordsRendered:N=>i.current=N,onLabelClick:K})))};function We(t){return(0,e.useMemo)(()=>{const n=(0,d.take)(t,ze),s=(0,d.sortBy)((0,d.uniq)(n.flatMap(y=>y.fields[0].values))),r=Math.min(...s),i=Math.max(...s),m=(0,T.CQ)(r),o=(0,T.CQ)(i);return{frameSubset:n,frameSubsetTimestamps:s,frameTimeRange:{from:m,to:o,raw:{from:m,to:o}}}},[t])}const oe=e.forwardRef(({showClearFilterSuffix:t,onClearFilterClick:n,...s},r)=>e.createElement(ie.g,{label:e.createElement(ce._,{htmlFor:"instancesSearchInput"},e.createElement(S.Stack,{gap:.5},e.createElement("span",null,"Filter instances"),e.createElement(ge.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(R.J,{name:"info-circle",size:"sm"}))))},e.createElement(me.I,{id:"instancesSearchInput",prefix:e.createElement(R.J,{name:"search"}),suffix:t&&e.createElement(G.zx,{fill:"text",icon:"times",size:"sm",onClick:n},"Clear"),placeholder:"Filter instances",ref:r,...s})));oe.displayName="SearchFieldInput";function je(){const t=(0,T.CQ)().subtract(30,"days"),n=(0,T.CQ)();return{from:t,to:n,raw:{from:t,to:n}}}const le=t=>({fullSize:g.css`
    min-width: 100%;
    height: 100%;

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

    display: flex;
    flex-direction: column;
    gap: ${t.spacing(2)};
    align-items: center;
    margin: auto auto;
  `,moreInstancesWarning:g.css`
    color: ${t.colors.warning.text};
    padding: ${t.spacing()};
  `,commonLabels:g.css`
    display: grid;
    grid-template-columns: max-content auto;
  `}),we=Be},38645:(F,I,a)=>{a.d(I,{r:()=>e,z:()=>M});var g=a(82897),d=a.n(g);function e(T,S){return T.filter(L=>!S.find(u=>JSON.stringify(u)===JSON.stringify(L)))}function M(T){const S=T.flatMap(u=>u);return(0,g.uniqBy)(S.filter(u=>S.filter(R=>(0,g.isEqual)(u,R)).length===Object.keys(T).length),u=>JSON.stringify(u))}},81777:(F,I,a)=>{a.d(I,{Z:()=>g});function g(d,e){if(d==null)throw new TypeError("assign requires that input parameter not be null or undefined");for(var M in e)Object.prototype.hasOwnProperty.call(e,M)&&(d[M]=e[M]);return d}},96529:(F,I,a)=>{a.d(I,{Z:()=>d});var g=a(81777);function d(e){return(0,g.Z)({},e)}}}]);

//# sourceMappingURL=4394.ea47bf105730e1243d17.js.map