Skip to content

Commit

Permalink
multi-value filters and dashboard update (#162)
Browse files Browse the repository at this point in the history
* multi-value filters and dashboard update

* cleaned up code
  • Loading branch information
nicolesylvester authored Feb 28, 2025
1 parent 8060b3d commit a1fa3b0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
13 changes: 8 additions & 5 deletions lib/Analytics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,15 @@ class HomeAnalytics {

// Submit the user filtered search
Future<void> submitFilterSearch(Set<FilterItem> filter) {
Map<String, dynamic> submissionFilter = {};
for (var filterItem in filter) {
submissionFilter[filterItem.category] = filterItem.value;
}
return eventLog.uploadRecord("filter", submissionFilter);
// gather all values in a list keyed by the category
Map<String, List<String>> submissionFilter = {};

for (var filterItem in filter) {
// if the map already has a key for this category, append to the list
submissionFilter.putIfAbsent(filterItem.category, () => []).add(filterItem.value);
}
return eventLog.uploadRecord("filter", submissionFilter);
}

// Submit the resource clicked event
Future<void> submitClickedResource(String resource) {
Expand Down
47 changes: 25 additions & 22 deletions lib/dashboard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,28 @@ class _DashboardState extends State<Dashboard>
return csv.ListToCsvConverter().convert(rows);
}

// normalize the payload value for a given key
List<Map<String, dynamic>> normalizeAndAdd(
Map<String, dynamic> payload, String key, DateTime timestamp) {
final value = payload[key];
// if it's not a list, wrap it in one
final normalizedValues = value is List ? value : [value];
// create a map for each value
return normalizedValues.map((item) {
return {
'timestamp': timestamp,
key: item,
};
}).toList();
}

// function to fetch data from RRDBFilters
Future<List<Map<String, dynamic>>> fetchData() async {
try {
// check if data source is type or age
if (selectedData == 'Resource Type Searches' ||
selectedData == 'Age Range Searches' || selectedData == 'Health Focus Searches') {
selectedData == 'Age Range Searches' ||
selectedData == 'Health Focus Searches') {
// get documents in event log collection where event is filter
QuerySnapshot querySnapshot = await FirebaseFirestore.instance
.collection('RRDBEventLog')
Expand All @@ -250,34 +266,23 @@ class _DashboardState extends State<Dashboard>
// check if the document's payload contains the 'Type' key
if (docData['payload'] != null &&
docData['payload'].containsKey('Type')) {
Map<String, dynamic> validDocData = {
'timestamp': doc['timestamp'].toDate(),
'Type': docData['payload']['Type']
};
data.add(validDocData);
data.addAll(normalizeAndAdd( docData['payload'], 'Type', doc['timestamp'].toDate()));
}
}
// if the selected data source is 'Age Range Searches'
if (selectedData == 'Age Range Searches') {
// check if the document's payload contains the 'Age Range' key
if (docData['payload'] != null &&
docData['payload'].containsKey('Age Range')) {
Map<String, dynamic> validDocData = {
'timestamp': doc['timestamp'].toDate(),
'Age Range': docData['payload']['Age Range']
};
data.add(validDocData);
data.addAll(normalizeAndAdd( docData['payload'], 'Age Range', doc['timestamp'].toDate()));
}
}
if (selectedData == 'Health Focus Searches' &&
docData['payload'] != null &&
docData['payload'].containsKey("Health Focus")) {
Map<String, dynamic> validDocData = {
'timestamp': doc['timestamp'].toDate(),
'Health Focus': docData['payload']['Health Focus']
};
data.add(validDocData);
if (selectedData == 'Health Focus Searches'){
if(docData['payload'] != null &&
docData['payload'].containsKey("Health Focus")) {
data.addAll(normalizeAndAdd( docData['payload'], 'Health Focus', doc['timestamp'].toDate()));
}
}
});
return data;
}
Expand Down Expand Up @@ -374,8 +379,6 @@ class _DashboardState extends State<Dashboard>
final isSmallScreen = screenWidth < 600;
// variables to manage selection and export type
String? selectedOption = 'Graph';
String? selectedExportType = exportTypes.isNotEmpty ? exportTypes
.first : null;

return StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
Expand Down Expand Up @@ -434,7 +437,7 @@ class _DashboardState extends State<Dashboard>
value: selectedExportType,
onChanged: (String? newValue) {
setState(() {
selectedExportType = newValue;
selectedExportType = newValue!;
});
},
items: exportTypes
Expand Down

0 comments on commit a1fa3b0

Please sign in to comment.