def diffFromDb(request): if request.method == 'GET': lastday = getLast() day = lastday[0] form = DiffForm(initial={'day': lastday[1]}) else: form = DiffForm(request.POST) if form.is_valid(): day = dayCSVFormat(form.cleaned_data['day']) new = datetime.strptime(day, '%m-%d-%Y') old = new - timedelta(days=1) newdata = readDaily(day) olddata = readDaily(old.strftime('%m-%d-%Y')) results = [] if newdata and olddata: for cname, value in newdata.items(): if cname in olddata: results.append( ResultType(cname, str(olddata[cname]['Confirmed']), str(value['Confirmed']))) results.sort(key=lambda d: d.newcount, reverse=True) return render( request, 'cov19diff/diffdb.html', { 'results': results, 'oldtime': old.strftime('%m-%d-%Y'), 'newtime': new.strftime('%m-%d-%Y'), 'form': form })
def doTS(request, cname): today = date.today() targetday = today - timedelta(days=30) pv = None days = dict() while targetday < today: tday = targetday.strftime('%m-%d-%Y') datas = readDaily(tday) if len(datas) > 0 and cname in datas: if pv: nv = DaylyStatus(cname, datas[cname]['Confirmed'], datas[cname]['Deaths'], datas[cname]['Recovered']) days[tday] = nv.setdiff(pv) pv = nv else: pv = DaylyStatus(cname, datas[cname]['Confirmed'], datas[cname]['Deaths'], datas[cname]['Recovered']) targetday = targetday + timedelta(days=1) wdays = list(days.keys()) wdays.reverse() rdays = dict() for key in wdays: rdays[key] = days[key] return render(request, 'cov19diff/ts.html', { 'cname': cname, 'days': days, 'rdays': rdays })
def dailyJson(request, day, ord): datas = readDaily(day) daylys = [] for data in datas: daylys.append( DaylyStatus(data, datas[data]['Confirmed'], datas[data]['Deaths'], datas[data]['Recovered'])) if ord == 'C': daylys.sort(key=lambda d: d.confirmed, reverse=True) elif ord == 'A': daylys.sort(key=lambda d: d.active(), reverse=True) elif ord == 'D': daylys.sort(key=lambda d: d.deaths, reverse=True) elif ord == 'R': daylys.sort(key=lambda d: d.recover, reverse=True) elif ord == 'DR': daylys.sort(key=lambda d: float(d.deathRatio()), reverse=True) elif ord == 'RR': daylys.sort(key=lambda d: float(d.recoverRatio()), reverse=True) elif ord == 'AR': daylys.sort(key=lambda d: float(d.activeRatio()), reverse=True) elif ord == 'CN': daylys.sort(key=lambda d: d.cname) response = HttpResponse( json.dumps({ 'daylys': daylys, 'day': day, 'ord': ord }, cls=DaylyStatusEncoder)) response['Access-Control-Allow-Origin'] = '*' return response
def daylyStat(request, day, ord, form): datas = readDaily(day) daylys = [] for data in datas: daylys.append( DaylyStatus(data, datas[data]['Confirmed'], datas[data]['Deaths'], datas[data]['Recovered'])) if ord == 'C': daylys.sort(key=lambda d: d.confirmed, reverse=True) elif ord == 'A': daylys.sort(key=lambda d: d.active(), reverse=True) elif ord == 'D': daylys.sort(key=lambda d: d.deaths, reverse=True) elif ord == 'R': daylys.sort(key=lambda d: d.recover, reverse=True) elif ord == 'DR': daylys.sort(key=lambda d: float(d.deathRatio()), reverse=True) elif ord == 'RR': daylys.sort(key=lambda d: float(d.recoverRatio()), reverse=True) elif ord == 'AR': daylys.sort(key=lambda d: float(d.activeRatio()), reverse=True) elif ord == 'CN': daylys.sort(key=lambda d: d.cname) return render(request, 'cov19diff/dayly.html', { 'daylys': daylys, 'day': day, 'form': form })
def diffJson(request, day): new = datetime.strptime(day, '%m-%d-%Y') old = new - timedelta(days=1) newdata = readDaily(day) olddata = readDaily(old.strftime('%m-%d-%Y')) results = [] if newdata and olddata: for cname, value in newdata.items(): if cname in olddata: results.append( ResultType(cname, str(olddata[cname]['Confirmed']), str(value['Confirmed']))) results.sort(key=lambda d: d.newcount, reverse=True) response = HttpResponse( json.dumps( #results, { 'results': results, 'oldtime': old.strftime('%m-%d-%Y'), 'newtime': new.strftime('%m-%d-%Y') }, cls=ResultTypeEncoder)) response['Access-Control-Allow-Origin'] = '*' return response
def doDaylyChart(request): if request.method == 'GET': form = DailyStatusForm() daylys = [] day = None else: form = DailyStatusForm(request.POST) if form.is_valid(): day = dayCSVFormat(form.cleaned_data['day']) ord = form.cleaned_data['order'] datas = readDaily(day) daylys = [] for data in datas: daylys.append( DaylyStatus(data, datas[data]['Confirmed'], datas[data]['Deaths'], datas[data]['Recovered'])) return render(request, 'cov19diff/dschart.html', { 'daylys': daylys, 'day': day, 'form': form })
def getData(self): lines = [] while self.now <= self.start: datas = readDaily(self.now.strftime('%m-%d-%Y')) self.now = self.now + timedelta(days=1) if len(datas) > 0: lines.append( 'name\tconfirmed\tdeaths\tdeathRatio\trecover\trecoverRatio\tactive\tactiveRatio' ) dss = [] for name, data in datas.items(): ds = DaylyStatus(name, data['Confirmed'], data['Deaths'], data['Recovered']) dss.append(ds) dss = sorted(dss, key=lambda d: random.random()) for ds in dss: lines.append( '%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s' % (ds.cname, ds.confirmed, ds.deaths, ds.deathRatio(), ds.recover, ds.recoverRatio(), ds.active(), ds.activeRatio())) break return '\n'.join(lines)
def TSJson(request, cname): today = date.today() targetday = today - timedelta(days=30) pv = None days = dict() while targetday < today: tday = targetday.strftime('%m-%d-%Y') datas = readDaily(tday) if len(datas) > 0 and cname in datas: if pv: nv = DaylyStatus(cname, datas[cname]['Confirmed'], datas[cname]['Deaths'], datas[cname]['Recovered']) nv.day = tday days[tday] = nv.setdiff(pv) pv = nv else: pv = DaylyStatus(cname, datas[cname]['Confirmed'], datas[cname]['Deaths'], datas[cname]['Recovered']) targetday = targetday + timedelta(days=1) wdays = list(days.keys()) wdays.reverse() rdays = dict() for key in wdays: rdays[key] = days[key] response = HttpResponse( json.dumps( #results, { 'cname': cname, 'days': list(days.values()), 'rdays': list(rdays.values()) }, cls=DaylyStatusEncoder)) response['Access-Control-Allow-Origin'] = '*' return response