Skip to content

Instantly share code, notes, and snippets.

@JoeCodeswell
Last active October 7, 2020 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoeCodeswell/cdff0e68ae266fa624eaac5c286f20b2 to your computer and use it in GitHub Desktop.
Save JoeCodeswell/cdff0e68ae266fa624eaac5c286f20b2 to your computer and use it in GitHub Desktop.
TimeSeriesC19
/// TimeSeriesC19.dart
/// C19app1Pj\jchart_c19\lib\c19_data.dart
/// [gist](https://gist.github.com/JoeCodeswell/cdff0e68ae266fa624eaac5c286f20b2)
/// https://dartpad.dev/cdff0e68ae266fa624eaac5c286f20b2
/// See VERY HELPFUL ANSWERS IN [Mapping JSON into Class Objects](https://stackoverflow.com/a/45189401/601770)
/// [HTML - Using getString() to load a file](https://dart.dev/tutorials/web/fetch-data#using-getString-function)
/// google: dart await result
/// [Asynchronous programming: futures, async, await](https://dart.dev/codelabs/async-await)
///
///
import 'dart:convert';
import 'dart:async';
import 'dart:html';
void main() async {
// https://joecodeswell.com/cov19cty/default/selj/from-to-countyFIPS/2020-08-01/2020-08-08/6067/cases
String w2p_cases_json_str =
'''{"countyFIPS": 6067, "valList": [{"ConfirmedCases": 10016, "DateStr": "2020-08-01"}, {"ConfirmedCases": 10067, "DateStr": "2020-08-02"}, {"ConfirmedCases": 10122, "DateStr": "2020-08-03"}, {"ConfirmedCases": 10174, "DateStr": "2020-08-04"}, {"ConfirmedCases": 10244, "DateStr": "2020-08-05"}, {"ConfirmedCases": 10544, "DateStr": "2020-08-06"}, {"ConfirmedCases": 10795, "DateStr": "2020-08-07"}, {"ConfirmedCases": 10795, "DateStr": "2020-08-08"}], "state": "CA", "county": "Sacramento County", "countyPop": "1552058", "thruDate": "2020-08-08", "fromDate": "2020-08-01", "fieldName": "cases"}''';
String w2p_json_str = '''{
"fieldName":"deaths",
"countyFIPS": 6067,
"county": "Sacramento County",
"countyPop": "2470546",
"state": "CA",
"fromDate": "2020-08-01",
"thruDate": "2020-08-08",
"valList": [
{"DateStr":"2020-08-01", "Deaths": 15},
{"DateStr":"2020-08-02", "Deaths": 16},
{"DateStr":"2020-08-03", "Deaths": 17},
{"DateStr":"2020-08-04", "Deaths": 18},
{"DateStr":"2020-08-05", "Deaths": 19},
{"DateStr":"2020-08-06", "Deaths": 20},
{"DateStr":"2020-08-07", "Deaths": 20},
{"DateStr":"2020-08-08", "Deaths": 20}
]
}''';
String json_str = '''{
"type":"deaths",
"countyFIPS": 6067,
"county": "Sacramento County",
"countyPop": "2470546",
"state": "CA",
"fromDate": "2020-08-01",
"thruDate": "2020-08-08",
"valList": [
{"time":"2020-08-01", "value": 15},
{"time":"2020-08-02", "value": 16},
{"time":"2020-08-03", "value": 17},
{"time":"2020-08-04", "value": 18},
{"time":"2020-08-05", "value": 19},
{"time":"2020-08-06", "value": 20},
{"time":"2020-08-07", "value": 20},
{"time":"2020-08-08", "value": 20}
]
}''';
var jsonObj = jsonDecode(json_str);
print('jsonObj.runtimeType(): $jsonObj.runtimeType()');
print('\n${jsonObj["type"]}');
print('\n${jsonObj["county"]}');
// for (var ix in [0,1,2,3,4,5,6,7]){
// print('${jsonObj["valList"][ix]}');
// }
List<TimeSeriesC19> result = [];
for (var itm in jsonObj["valList"]) {
DateTime dt = DateTime.parse(itm["time"]);
print('$dt');
// int theVal = int.parse(itm["value"]);
int theVal = itm["value"];
print('$dt $theVal');
result.add(new TimeSeriesC19(dt, theVal));
}
print('\nresult[0].time: ${result[0].time}');
print('\nresult[0]: ${result[0]}');
print('\nresult[0].time: ${result[0].time}');
print('\nresult[0].value: ${result[0].value}');
// https://stackoverflow.com/a/45189401/601770
var sacDeaths = new C19dataSet.fromJson(jsonDecode(w2p_json_str));
print('\n\nsacDeaths.county: ${sacDeaths.county}');
print('sacDeaths.valList[2].time: ${sacDeaths.valList[2].time}');
print('sacDeaths.valList[2].value: ${sacDeaths.valList[2].value}');
var sacCases = new C19dataSet.fromJson(jsonDecode(w2p_cases_json_str));
print('\n\nsacCases.county: ${sacCases.county}');
print('sacCases.valList[2].time: ${sacCases.valList[2].time}');
print('sacCases.valList[2].value: ${sacCases.valList[2].value}');
// // works OUT OF sequence when main is NOT delcared `async`
// Future<String> reqJsonStr = makeRequest();
// print('reqJsonStr: $reqJsonStr');
// works IN sequence when main IS delcared `async`
String reqJsonStr = await makeRequest();
print('reqJsonStr: $reqJsonStr');
print('\nSUCCESS Thanks, Holy Trinity.\nThis ran to the end as of: ${DateTime.now()}! \n Yahoo! \n Yahoo!\n I rest in Your Peace that passes understanding.');
} // end main
Future<String> makeRequest() async {
const path = 'https://dart.dev/f/portmanteaux.json';
String retString = '';
try {
// Make the GET request
final jsonString = await HttpRequest.getString(path);
// The request succeeded. Process the JSON.
retString = processResponse(jsonString);
} catch (e) {
// The GET request failed. Handle the error.
print('Couldn\'t open $path');
//wordList.children.add(LIElement()..text = 'Request failed.');
retString = 'Request failed.';
}
return retString;
}
String processResponse(String jsonString) {
print('jsonString: $jsonString');
// for (final portmanteau in json.decode(jsonString)) {
// wordList.children.add(LIElement()..text = portmanteau);
//}
return jsonString;
}
// Future<void> makeRequestOrig(Event _) async {
// const path = 'https://dart.dev/f/portmanteaux.json';
// try {
// // Make the GET request
// final jsonString = await HttpRequest.getString(path);
// // The request succeeded. Process the JSON.
// processResponse(jsonString);
// } catch (e) {
// // The GET request failed. Handle the error.
// print('Couldn\'t open $path');
// wordList.children.add(LIElement()..text = 'Request failed.');
// }
// }
// void processResponseOrig(String jsonString) {
// for (final portmanteau in json.decode(jsonString)) {
// wordList.children.add(LIElement()..text = portmanteau);
// }
// }
class C19dataSet {
/// see https://stackoverflow.com/a/45189401/601770
/// - properties can't be final
int countyFIPS;
String state;
String county;
int countyPop;
DateTime fromDate;
DateTime thruDate;
String fieldName;
List<TimeSeriesC19> valList;
C19dataSet.fromJson(Map json) {
this.countyFIPS = json['countyFIPS'];
this.state = json['state'];
this.county = json['county'];
this.countyPop = int.parse(json['countyPop']);
this.fromDate = DateTime.parse(json['fromDate']);
this.thruDate = DateTime.parse(json['thruDate']);
this.fieldName = json['fieldName'];
valList = [];
for (var itm in json['valList']) {
// print('DateStr: ${itm['DateStr']}');
DateTime dt = DateTime.parse(itm['DateStr']);
// print('dt: $dt');
int theVal;
// UNFORTUNATELY theVal fieldName VARIES in web2py can be 'ConfirmedCases' also 'Deaths'
if (json['fieldName'] == 'deaths') {
theVal = itm['Deaths'];
} else if (json['fieldName'] == 'cases') {
theVal = itm['ConfirmedCases'];
} else {
print("Houston...Problem >> json['fieldName']");
}
valList.add(new TimeSeriesC19(dt, theVal));
}
}
}
class TimeSeriesC19 {
final DateTime time;
final int value;
TimeSeriesC19(
this.time,
this.value,
);
}
class C19dataSetOrig {
final String type;
final int countyFIPS;
final String county;
final String state;
final String fromDate;
final String thruDate;
final List<TimeSeriesC19> valList;
C19dataSetOrig(
this.type,
this.countyFIPS,
this.county,
this.state,
this.fromDate,
this.thruDate,
this.valList,
);
}
/* console out
jsonObj.runtimeType(): {type: deaths, countyFIPS: 6067, county: Sacramento County, countyPop: 2470546, state: CA, fromDate: 2020-08-01, thruDate: 2020-08-08, valList: [{time: 2020-08-01, value: 15}, {time: 2020-08-02, value: 16}, {time: 2020-08-03, value: 17}, {time: 2020-08-04, value: 18}, {time: 2020-08-05, value: 19}, {time: 2020-08-06, value: 20}, {time: 2020-08-07, value: 20}, {time: 2020-08-08, value: 20}]}.runtimeType()
deaths
Sacramento County
2020-08-01 00:00:00.000
2020-08-01 00:00:00.000 15
2020-08-02 00:00:00.000
2020-08-02 00:00:00.000 16
2020-08-03 00:00:00.000
2020-08-03 00:00:00.000 17
2020-08-04 00:00:00.000
2020-08-04 00:00:00.000 18
2020-08-05 00:00:00.000
2020-08-05 00:00:00.000 19
2020-08-06 00:00:00.000
2020-08-06 00:00:00.000 20
2020-08-07 00:00:00.000
2020-08-07 00:00:00.000 20
2020-08-08 00:00:00.000
2020-08-08 00:00:00.000 20
result[0].time: 2020-08-01 00:00:00.000
result[0]: Instance of 'TimeSeriesC19'
result[0].time: 2020-08-01 00:00:00.000
result[0].value: 15
sacDeaths.county: Sacramento County
sacDeaths.valList[2].time: 2020-08-03 00:00:00.000
sacDeaths.valList[2].value: 17
sacCases.county: Sacramento County
sacCases.valList[2].time: 2020-08-03 00:00:00.000
sacCases.valList[2].value: 10122
jsonString: [
"portmanteau", "fantabulous", "spork", "smog",
"spanglish", "gerrymander", "turducken", "stagflation",
"bromance", "freeware", "oxbridge", "palimony", "netiquette",
"brunch", "blog", "chortle", "Hassenpfeffer", "Schnitzelbank"
]
reqJsonStr: [
"portmanteau", "fantabulous", "spork", "smog",
"spanglish", "gerrymander", "turducken", "stagflation",
"bromance", "freeware", "oxbridge", "palimony", "netiquette",
"brunch", "blog", "chortle", "Hassenpfeffer", "Schnitzelbank"
]
SUCCESS Thanks, Holy Trinity.
This ran to the end as of: 2020-10-07 07:30:49.544!
Yahoo!
Yahoo!
I rest in Your Peace that passes understanding.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment