|
| 1 | +import React, { useEffect, useMemo, useState } from "react"; |
| 2 | +// import { Field } from '@grafana/data'; |
| 3 | +import { useTheme2, CollapsableSection, Icon } from '@grafana/ui'; |
| 4 | +import { LogContextProps } from "./LogContextUI"; |
| 5 | +import { css, cx } from "@emotion/css"; |
| 6 | +import { LuceneQuery } from "utils/lucene"; |
| 7 | +import { LuceneQueryBuilder } from '@/QueryBuilder/lucene'; |
| 8 | + |
| 9 | + |
| 10 | +// TODO : define sensible defaults here |
| 11 | +const excludedFields = [ |
| 12 | + '_source', |
| 13 | + 'sort', |
| 14 | + 'attributes', |
| 15 | + 'attributes.message', |
| 16 | + 'body', |
| 17 | + 'body.message', |
| 18 | + 'resource_attributes', |
| 19 | + 'observed_timestamp_nanos', |
| 20 | + 'timestamp_nanos', |
| 21 | +]; |
| 22 | + |
| 23 | +function isPrimitive(valT: any) { |
| 24 | + return ['string', 'number', "boolean", "undefined"].includes(valT) |
| 25 | +} |
| 26 | + |
| 27 | +type FieldContingency = { [value: string]: { |
| 28 | + count: number, pinned: boolean, active?: boolean |
| 29 | +}}; |
| 30 | +type Field = { |
| 31 | + name: string, |
| 32 | + contingency: FieldContingency |
| 33 | +} |
| 34 | + |
| 35 | +function LogContextFieldSection(field: Field) { |
| 36 | + const theme = useTheme2() |
| 37 | + const hasActiveFilters = Object.entries(field.contingency).map(([_,entry])=>!!entry.active).reduce((a,b)=>a || b, false); |
| 38 | + return( |
| 39 | + <span className={css({fontSize:theme.typography.body.fontSize, display:"flex", alignItems: "baseline", gap:"0.5rem", width:"100%"})}> |
| 40 | + {hasActiveFilters && <Icon name={"filter"} className={css({ color:theme.colors.primary.text })}/>} |
| 41 | + <span>{field.name}</span> |
| 42 | + </span> |
| 43 | + ) |
| 44 | +} |
| 45 | + |
| 46 | +type FieldItemProps = { |
| 47 | + label: any, |
| 48 | + contingency: { |
| 49 | + count: number, |
| 50 | + pinned: boolean |
| 51 | + }, |
| 52 | + active?: boolean, |
| 53 | + onClick: () => void |
| 54 | +} |
| 55 | + |
| 56 | +function LogContextFieldItem(props: FieldItemProps){ |
| 57 | + const theme = useTheme2() |
| 58 | + const lcAttributeItemStyle = css({ |
| 59 | + display: "flex", |
| 60 | + justifyContent: "space-between", |
| 61 | + paddingLeft: "10px", |
| 62 | + fontSize: theme.typography.bodySmall.fontSize, |
| 63 | + "&[data-active=true]": { |
| 64 | + backgroundColor: theme.colors.primary.transparent, |
| 65 | + }, |
| 66 | + "&:hover": { |
| 67 | + backgroundColor: theme.colors.secondary.shade, |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + const formatLabel = (value: any)=> { |
| 72 | + let shouldEmphasize = false; |
| 73 | + let label = `${value}`; |
| 74 | + |
| 75 | + if (value === null || value === '' || value === undefined){ |
| 76 | + shouldEmphasize = true; |
| 77 | + } |
| 78 | + if (value === '') { |
| 79 | + label = '<empty string>' |
| 80 | + } |
| 81 | + return (shouldEmphasize ? <em>{label}</em> : label); |
| 82 | + } |
| 83 | + |
| 84 | + return ( |
| 85 | + <a className={lcAttributeItemStyle} onClick={props.onClick} data-active={props.active}> |
| 86 | + <span className={css`text-overflow:ellipsis; min-width:0; flex:1 1`}>{ formatLabel(props.label) }</span> |
| 87 | + <span className={css`flex-grow:0`}>{props.contingency.pinned && <Icon name={"crosshair"}/>}{props.contingency.count}</span> |
| 88 | + </a> |
| 89 | + ) |
| 90 | +} |
| 91 | + |
| 92 | +const lcSidebarStyle = css` |
| 93 | + width: 300px; |
| 94 | + min-width: 300px; |
| 95 | + flex-shrink: 0; |
| 96 | + overflow-y: scroll; |
| 97 | + padding-right: 1rem; |
| 98 | +` |
| 99 | + |
| 100 | +type QueryBuilderProps = { |
| 101 | + builder: LuceneQueryBuilder, |
| 102 | + searchableFields: any[], |
| 103 | + updateQuery: (query: LuceneQuery) => void |
| 104 | +} |
| 105 | + |
| 106 | +export function LogContextQueryBuilderSidebar(props: LogContextProps & QueryBuilderProps) { |
| 107 | + |
| 108 | + const {row, builder, updateQuery, searchableFields} = props; |
| 109 | + const [fields, setFields] = useState<Field[]>([]); |
| 110 | + |
| 111 | + const filteredFields = useMemo(() => { |
| 112 | + const searchableFieldsNames = searchableFields.map(f=>f.text); |
| 113 | + return row.dataFrame.fields |
| 114 | + .filter(f=>searchableFieldsNames.includes(f.name)) |
| 115 | + // exclude some low-filterability fields |
| 116 | + .filter((f)=> !excludedFields.includes(f.name) && isPrimitive(f.type)) |
| 117 | + // sort fields by name |
| 118 | + .sort((f1, f2)=> (f1.name>f2.name ? 1 : -1)) |
| 119 | + }, [row, searchableFields]); |
| 120 | + |
| 121 | + useEffect(() => { |
| 122 | + const fields = filteredFields |
| 123 | + .map((f) => { |
| 124 | + const contingency: FieldContingency = {}; |
| 125 | + f.values.forEach((value, i) => { |
| 126 | + if (!contingency[value]) { |
| 127 | + contingency[value] = { |
| 128 | + count: 0, |
| 129 | + pinned: false, |
| 130 | + active: builder.parsedQuery ? !!builder.parsedQuery.findFilter(f.name, `${value}`) : false |
| 131 | + } |
| 132 | + } |
| 133 | + contingency[value].count += 1; |
| 134 | + if (i === row.rowIndex) { |
| 135 | + contingency[value].pinned = true; |
| 136 | + } |
| 137 | + }); |
| 138 | + return { name: f.name, contingency }; |
| 139 | + }) |
| 140 | + |
| 141 | + setFields(fields); |
| 142 | + }, [filteredFields, row.rowIndex, builder.parsedQuery]); |
| 143 | + |
| 144 | + |
| 145 | + const selectQueryFilter = (key: string, value: string): void => { |
| 146 | + // Compute mutation to apply to the query and send to parent |
| 147 | + // check if that filter is in the query |
| 148 | + if (!builder.parsedQuery) { return; } |
| 149 | + |
| 150 | + const newParsedQuery = ( |
| 151 | + builder.parsedQuery.hasFilter(key, value) |
| 152 | + ? builder.parsedQuery.removeFilter(key, value) |
| 153 | + : builder.parsedQuery.addFilter(key, value) |
| 154 | + ) |
| 155 | + |
| 156 | + if (newParsedQuery) { |
| 157 | + updateQuery(newParsedQuery); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + const renderFieldSection = (field: Field)=>{ |
| 162 | + return ( |
| 163 | + <CollapsableSection |
| 164 | + label={LogContextFieldSection(field)} |
| 165 | + className={css`& > div { flex-grow:1; }` } |
| 166 | + isOpen={false} key="log-attribute-field-{field.name}" |
| 167 | + contentClassName={cx(css`margin:0; padding:0`)}> |
| 168 | + <div className={css`display:flex; flex-direction:column; gap:5px`}> |
| 169 | + |
| 170 | + {field.contingency && Object.entries(field.contingency) |
| 171 | + .sort(([na, ca], [nb, cb])=>(cb.count - ca.count)) |
| 172 | + .map(([fieldValue, contingency], i) => ( |
| 173 | + <LogContextFieldItem |
| 174 | + label={fieldValue} contingency={contingency} key={`field-opt${i}`} |
| 175 | + onClick={() => {selectQueryFilter(field.name, fieldValue)}} |
| 176 | + active={contingency.active} |
| 177 | + /> |
| 178 | + )) |
| 179 | + } |
| 180 | + </div> |
| 181 | + </CollapsableSection> |
| 182 | + ) |
| 183 | + } |
| 184 | + |
| 185 | + return ( |
| 186 | + <div className={lcSidebarStyle}> |
| 187 | + {fields && fields.map((field) => { |
| 188 | + return( renderFieldSection(field) ); |
| 189 | + }) } </div> |
| 190 | + ); |
| 191 | +} |
0 commit comments