|
| 1 | +import Controller from '@ember/controller'; |
| 2 | +import { action, get } from '@ember/object'; |
| 3 | +import { tracked } from '@glimmer/tracking'; |
| 4 | +import { MetricsReportAttrs } from './route'; |
| 5 | + |
| 6 | + |
| 7 | +type ReportFields = { |
| 8 | + keywordFields: string[], |
| 9 | + numericFields: string[], |
| 10 | +}; |
| 11 | + |
| 12 | + |
| 13 | +function gatherFields(obj: any): ReportFields { |
| 14 | + const keywordFields: string[] = [] |
| 15 | + const numericFields: string[] = [] |
| 16 | + for (const fieldName in obj) { |
| 17 | + if (fieldName === 'report_date' || fieldName === 'timestamp') { |
| 18 | + continue; |
| 19 | + } |
| 20 | + const fieldValue = obj[fieldName]; |
| 21 | + switch (typeof fieldValue) { |
| 22 | + case 'string': |
| 23 | + keywordFields.push(fieldName); |
| 24 | + break; |
| 25 | + case 'number': |
| 26 | + numericFields.push(fieldName); |
| 27 | + break; |
| 28 | + case 'object': |
| 29 | + const nestedFields = gatherFields(fieldValue); |
| 30 | + keywordFields.push(...nestedFields.keywordFields.map( |
| 31 | + nestedFieldName => `${fieldName}.${nestedFieldName}`, |
| 32 | + )); |
| 33 | + numericFields.push(...nestedFields.numericFields.map( |
| 34 | + nestedFieldName => `${fieldName}.${nestedFieldName}`, |
| 35 | + )); |
| 36 | + break; |
| 37 | + default: |
| 38 | + console.log(`ignoring unexpected ${fieldName}: ${fieldValue}`) |
| 39 | + } |
| 40 | + } |
| 41 | + return { |
| 42 | + keywordFields, |
| 43 | + numericFields, |
| 44 | + }; |
| 45 | +} |
| 46 | + |
| 47 | + |
| 48 | +export default class MetricsReportDetailController extends Controller { |
| 49 | + queryParams = [ |
| 50 | + { daysBack: { scope: 'controller' as const } }, |
| 51 | + 'yFields', |
| 52 | + 'xGroupField', |
| 53 | + 'xGroupFilter', |
| 54 | + ] |
| 55 | + |
| 56 | + @tracked daysBack: string = '13'; |
| 57 | + @tracked model: MetricsReportAttrs[] = []; |
| 58 | + @tracked yFields: string[] = []; |
| 59 | + @tracked xGroupField?: string; |
| 60 | + @tracked xField: string = 'report_date'; |
| 61 | + @tracked xGroupFilter: string = ''; |
| 62 | + |
| 63 | + get reportFields(): ReportFields { |
| 64 | + const aReport: MetricsReportAttrs = this.model![0]; |
| 65 | + return gatherFields(aReport); |
| 66 | + } |
| 67 | + |
| 68 | + get chartRows(): Array<Array<string|number|null>>{ |
| 69 | + if (!this.xGroupField) { |
| 70 | + const fieldNames = [this.xField, ...this.yFields]; |
| 71 | + const rows = this.model.map( |
| 72 | + datum => fieldNames.map( |
| 73 | + fieldName => (get(datum, fieldName) as string | number | undefined) ?? null, |
| 74 | + ), |
| 75 | + ); |
| 76 | + return [fieldNames, ...rows]; |
| 77 | + } |
| 78 | + const groupedFieldNames = new Set<string>(); |
| 79 | + const rowsByX: any = {}; |
| 80 | + for (const datum of this.model) { |
| 81 | + const xValue = get(datum, this.xField) as string; |
| 82 | + if (!rowsByX[xValue]) { |
| 83 | + rowsByX[xValue] = {}; |
| 84 | + } |
| 85 | + const groupName = get(datum, this.xGroupField) as string; |
| 86 | + if (!this.xGroupFilter || groupName.includes(this.xGroupFilter)) { |
| 87 | + this.yFields.forEach(fieldName => { |
| 88 | + const groupedField = `${groupName} ${fieldName}`; |
| 89 | + groupedFieldNames.add(groupedField); |
| 90 | + const fieldValue = get(datum, fieldName); |
| 91 | + rowsByX[xValue][groupedField] = fieldValue; |
| 92 | + }); |
| 93 | + } |
| 94 | + } |
| 95 | + const rows = Object.entries(rowsByX).map( |
| 96 | + ([xValue, rowData]: [string, any]) => { |
| 97 | + const yValues = [...groupedFieldNames].map( |
| 98 | + groupedFieldName => (rowData[groupedFieldName] as string | number | undefined) ?? null, |
| 99 | + ); |
| 100 | + return [xValue, ...yValues]; |
| 101 | + }, |
| 102 | + ); |
| 103 | + return [ |
| 104 | + [this.xField, ...groupedFieldNames], |
| 105 | + ...rows, |
| 106 | + ]; |
| 107 | + } |
| 108 | + |
| 109 | + @action |
| 110 | + yFieldToggle(fieldName: string) { |
| 111 | + if (this.yFields.includes(fieldName)) { |
| 112 | + this.yFields = this.yFields.filter(f => f !== fieldName); |
| 113 | + } else { |
| 114 | + this.yFields = [...this.yFields, fieldName]; |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +declare module '@ember/controller' { |
| 120 | + interface Registry { |
| 121 | + 'osf-metrics.report-detail': MetricsReportDetailController; |
| 122 | + } |
| 123 | +} |
| 124 | + |
0 commit comments