From b4c93ff80d29f85aa60434b5af218035c2c4aef5 Mon Sep 17 00:00:00 2001 From: Felix Date: Mon, 19 Aug 2019 15:23:58 +0100 Subject: [PATCH] writing functions to get org graph data --- ios/Podfile.lock | 4 +- ios/Runner.xcodeproj/project.pbxproj | 2 +- lib/common/apifunctions/get_graph_data.dart | 154 ++++++++++++++++++++ lib/pages/stats_page.dart | 2 +- 4 files changed, 158 insertions(+), 4 deletions(-) diff --git a/ios/Podfile.lock b/ios/Podfile.lock index a4e1203..8132310 100644 --- a/ios/Podfile.lock +++ b/ios/Podfile.lock @@ -6,13 +6,13 @@ PODS: - Flutter DEPENDENCIES: - - Flutter (from `.symlinks/flutter/ios`) + - Flutter (from `.symlinks/flutter/ios-profile`) - shared_preferences (from `.symlinks/plugins/shared_preferences/ios`) - url_launcher (from `.symlinks/plugins/url_launcher/ios`) EXTERNAL SOURCES: Flutter: - :path: ".symlinks/flutter/ios" + :path: ".symlinks/flutter/ios-profile" shared_preferences: :path: ".symlinks/plugins/shared_preferences/ios" url_launcher: diff --git a/ios/Runner.xcodeproj/project.pbxproj b/ios/Runner.xcodeproj/project.pbxproj index 5b4844c..a0f8e33 100644 --- a/ios/Runner.xcodeproj/project.pbxproj +++ b/ios/Runner.xcodeproj/project.pbxproj @@ -281,7 +281,7 @@ ); inputPaths = ( "${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh", - "${PODS_ROOT}/../.symlinks/flutter/ios/Flutter.framework", + "${PODS_ROOT}/../.symlinks/flutter/ios-profile/Flutter.framework", ); name = "[CP] Embed Pods Frameworks"; outputPaths = ( diff --git a/lib/common/apifunctions/get_graph_data.dart b/lib/common/apifunctions/get_graph_data.dart index e2d1bb1..8846917 100644 --- a/lib/common/apifunctions/get_graph_data.dart +++ b/lib/common/apifunctions/get_graph_data.dart @@ -156,6 +156,160 @@ class GraphData { } +} + +class OrgGraphData { + var chartType; + List> graph; + + OrgGraphData( + this.chartType, + ); + + List cachedData; + bool loaded = false; + + Future setGraphData() async { + if (loaded == true) { + this.graph = [ + new charts.Series( + id: 'Spend', + colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault, + domainFn: (TimeSeriesSpend spend, _) => spend.time, + measureFn: (TimeSeriesSpend spend, _) => spend.spend, + data: cachedData, + ) + ]; + } + + final url = "https://dev.peartrade.org/api/v1/customer/graphs"; + SharedPreferences preferences = await SharedPreferences.getInstance(); + + Map body = { + 'graph': this.chartType, + 'session_key': preferences.get('LastToken'), + }; + + final response = await http.post( + url, + body: json.encode(body), + ); + + if (response.statusCode == 200) { + final responseJson = jsonDecode(response.body); + final List labels = responseJson['graph']['labels']; + final List data = responseJson['graph']['data']; + + List timeSeriesSpendList = new List(); + + for (int i = 0; i < labels.length; i++) { + timeSeriesSpendList.add(new TimeSeriesSpend(data[i]*1.00, DateTime.parse(labels[i]))); +// print(timeSeriesSpendList[i].time.toString() + " : " + timeSeriesSpendList[i].spend.toString()); + } + + cachedData = timeSeriesSpendList; + loaded = true; + + this.graph = [ + new charts.Series( + id: 'Spend', + colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault, + domainFn: (TimeSeriesSpend spend, _) => spend.time, + measureFn: (TimeSeriesSpend spend, _) => spend.spend, + data: timeSeriesSpendList, + ) + ]; + } else { + final errorMessage = json.decode(response.body)['message']; + print("Error: " + errorMessage); + + this.graph = null; + } + } + + Future>> getGraphData() async { + if (loaded == true) { + return [ + new charts.Series( + id: 'Spend', + colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault, + domainFn: (TimeSeriesSpend spend, _) => spend.time, + measureFn: (TimeSeriesSpend spend, _) => spend.spend, + data: cachedData, + ) + ]; + } + + // TODO: Show available graph types based on whether or not the user is an Organisation or User + + /// Customer graph types: https://dev.peartrade.org/api/v1/customer/graphs + /// - total_last_week + /// - avg_spend_last_week + /// - total_last_month + /// - avg_spend_last_month + /// + /// Organisations' graphs types: to fetch, POST to https://dev.peartrade.org/api/stats/[name] as {"session_key":"[boop beep]"} + /// - organisations_all : organisation + /// - pies : organisation/pies + /// - snippets : organisation/snippets + /// - graphs : organisation/graphs + /// - {"graph":"customers_last_7_days","session_key":"[bleep]"} + /// - {"graph":"customers_last_30_days","session_key":"[blah]"} + /// - {"graph":"sales_last_7_days","session_key":"[bloop]"} + /// - {"graph":"sales_last_7_days","session_key":"[reee]"} + /// - {"graph":"purchases_last_7_days","session_key":"[yee]"} + /// - {"graph":"purchases_last_30_days","session_key":"[yah]"} + /// - {"graph":"purchases_all;","session_key":"[kappa]"} // I don't think this one works + /// + /// HTTP POST request sample: + /// {"graph":"total_last_week","session_key":"blahblahblah"} + + final url = "https://dev.peartrade.org/api/v1/customer/graphs"; + SharedPreferences preferences = await SharedPreferences.getInstance(); + + Map body = { + 'graph': this.chartType, + 'session_key': preferences.get('LastToken'), + }; + + final response = await http.post( + url, + body: json.encode(body), + ); + + if (response.statusCode == 200) { + final responseJson = jsonDecode(response.body); + final List labels = responseJson['graph']['labels']; + final List data = responseJson['graph']['data']; + + List timeSeriesSpendList = new List(); + + for (int i = 0; i < labels.length; i++) { + timeSeriesSpendList.add(new TimeSeriesSpend(data[i]*1.00, DateTime.parse(labels[i]))); +// print(timeSeriesSpendList[i].time.toString() + " : " + timeSeriesSpendList[i].spend.toString()); + } + + cachedData = timeSeriesSpendList; + loaded = true; + + return [ + new charts.Series( + id: 'Spend', + colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault, + domainFn: (TimeSeriesSpend spend, _) => spend.time, + measureFn: (TimeSeriesSpend spend, _) => spend.spend, + data: timeSeriesSpendList, + ) + ]; + } else { + final errorMessage = json.decode(response.body)['message']; + print("Error: " + errorMessage); + + return null; + } + } + + } class TimeSeriesSpend { diff --git a/lib/pages/stats_page.dart b/lib/pages/stats_page.dart index a98f471..f9f9431 100644 --- a/lib/pages/stats_page.dart +++ b/lib/pages/stats_page.dart @@ -63,7 +63,7 @@ class StatsPageState extends State { ), ), Padding( - padding: EdgeInsets.symmetric(horizontal: 10) + padding: EdgeInsets.symmetric(horizontal: 7) ), Text(