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

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

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

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

    display: flex;
    flex-direction: column;
    gap: ${t.spacing(2)};
    align-items: center;
    margin: auto auto;
  `,moreInstancesWarning:(0,g.css)`
    color: ${t.colors.warning.text};
    padding: ${t.spacing()};
  `,commonLabels:(0,g.css)`
    display: grid;
    grid-template-columns: max-content auto;
  `,highlightedLogRecord:(0,g.css)`
    background: ${t.colors.primary.transparent} !important;
    outline: 1px solid ${t.colors.primary.shade} !important;
  `}),Ce=Le},13405:(k,x,s)=>{s.d(x,{r:()=>e,z:()=>F});var g=s(56375),f=s.n(g);function e(h,y){return h.filter(T=>!y.find(p=>JSON.stringify(p)===JSON.stringify(T)))}function F(h){const y=h.flatMap(p=>p);return(0,g.uniqBy)(y.filter(p=>y.filter(S=>(0,g.isEqual)(p,S)).length===Object.keys(h).length),p=>JSON.stringify(p))}}}]);

//# sourceMappingURL=2113.8b68ce2058b927828cdd.js.map