예제 #1
0
파일: views.py 프로젝트: StephenGrey/my_api
def index(request, place='Birmingham'):
    log.info('loading page')
    try:
        areacode = places[place]
        nation = ons_week.nation[areacode]
        nation_index = ['England', 'Wales', 'Scotland', 'Northern Ireland'
                        ].index(nation) + 1
        print(f'loading: {areacode} in {nation} ({nation_index})')
        PHEstored = configs.config.get('PHE')
        try:
            edition = PHEstored.get('latest_update')
            lastupdate = time_utils.parseISO(edition).date()
            lastupdate_str = f'{lastupdate: %a %d %b}'

        except Exception as e:
            print(e)
            lastupdate = None
    except:
        nation = None
        areacode = None
        nation_index = 1
    return render(
        request, 'graph/covid_chart_map.html', {
            'last_update': lastupdate_str,
            'place': place,
            'nation': nation,
            'nation_index': nation_index,
            'areacode': areacode,
            'api_status': 'true',
            'covid-rates': model_calcs.output_rates()
        })
예제 #2
0
	def ingest(self,check=True):
		"""ingest all the data"""
		data=self.data_all
		pubdate=time_utils.parseISO(self.api.last_update).date()
		
		counter=0
		for item in data:
			areacode=item['areaCode']
			datestring=item['specimenDate']
			_date=fetchdate(datestring)
			row,created=DailyCases.objects.get_or_create(specimenDate=_date,areacode=areacode)
			row.areaname=item['areaName']
			daily=item['newCasesBySpecimenDate']
			total=item['cumCasesBySpecimenDate']
			
			#log.debug(f'{row.areaname}: {datestring}')			
			if created:
				row.dailyLabConfirmedCases=daily
				row.totalLabConfirmedCases=total
				row.save()
				
				if daily:
					lag=(pubdate-_date.date()).days
					log.debug(f'date:{_date} lag: {lag} daily:{daily}')
					drow,dcreated=DailyReport.objects.get_or_create(specimenDate=_date,areacode=areacode,publag=lag)
					drow.dailycases=daily
					drow.add_cases=daily #if a new daily case, assume no prior report
					drow.save()
			
			if not created:
				existing_daily=row.dailyLabConfirmedCases
				existing_total=row.totalLabConfirmedCases
				if daily is not None:
					if existing_daily !=daily or existing_total!=total:
						row.dailyLabConfirmedCases=daily
						row.totalLabConfirmedCases=total
						row.save()
						if existing_daily !=daily:
							log.debug(f'Updating {row.areaname} on {datestring}: Daily: {existing_daily} to {daily}  Total: {existing_total} to {total}')
							if existing_daily:
								_increase=daily-existing_daily
							else:
								_increase=daily
							lag=(pubdate-_date.date()).days
							drow,dcreated=DailyReport.objects.get_or_create(specimenDate=_date,areacode=areacode,publag=lag)
							drow.dailycases=daily
							drow.add_cases=_increase
							drow.save()
					
			counter+=1
			if counter%1000==0:
				log.info(f'Processing row {counter}')
		log.info(f'Processed: {counter} rows')

		if self.edition:
			configs.userconfig.update('PHE','latest_update',self.edition)
		else:
			log.info('Latest update not updated')
예제 #3
0
def latest_update():
	PHEstored=configs.config.get('PHE')
	edition=PHEstored.get('latest_update')
	lastupdate=time_utils.parseISO(edition).date()
	return lastupdate
#	lastupdate_str=f'{lastupdate: %a %d %b}'



		
	
예제 #4
0
    def ingest(self, check=True):
        """ingest all the data"""
        data = self.data_all
        pubdate = time_utils.parseISO(self.api.last_update).date()

        counter = 0
        for item in data:
            areacode = item['areaCode']
            datestring = item['specimenDate']
            _date = phe_fetch.fetchdate(datestring)
            row, created = DailyCases.objects.get_or_create(specimenDate=_date,
                                                            areacode=areacode)
            row.areaname = item['areaName']

            deaths = item['newDeaths28DaysByDeathDate']
            total_published_deaths = item['cumDeaths28DaysByPublishDate']
            published_deaths = item['newDeaths28DaysByPublishDate']
            #log.debug(f'{row.areaname}: {datestring}')

            if created:
                row.deaths = deaths
                row.total_published_deaths = total_published_deaths
                row.published_deaths = published_deaths
                row.save()

            if not created:
                existing_deaths = row.deaths
                existing_pubdeaths = row.published_deaths
                existing_pubtotal = row.total_published_deaths
                if deaths is not None or published_deaths is not None or total_published_deaths is not None:
                    """ update is there is some data and it has changed"""
                    if existing_deaths != deaths or existing_pubtotal != total_published_deaths or published_deaths != existing_pubdeaths:
                        row.deaths = deaths
                        row.total_published_deaths = total_published_deaths
                        row.published_deaths = published_deaths
                        row.save()
            counter += 1
            if counter % 1000 == 0:
                log.debug(f'Processing row {counter}')
        log.info(f'Processed: {counter} rows')

        if self.latest_deaths:
            configs.userconfig.update('PHE', 'england_total_deaths',
                                      str(self.latest_deaths))
예제 #5
0
	def __init__(self,pubdate,data,start_date=date(2020,8,20)):
		self.pubdate=time_utils.parseISO(pubdate).date() #date when data published
		self.data=data #list of entries
		self.start_date=start_date
예제 #6
0
 def update_edition(self):
     self.edition = self.api.last_update
     self.edition_date = time_utils.parseISO(self.edition).date()
예제 #7
0
def index(request, place='index'):
    log.info('loading page')

    rates = model_calcs.output_rates()
    try:
        PHEstored = configs.config.get('PHE')
        edition = PHEstored.get('latest_update')
        lastupdate = time_utils.parseISO(edition).date()
        lastupdate_str = f'{lastupdate: %a %d %b}'
        last_cases = f'{lastupdate-model_calcs.DELAY:%d %b}'

    except Exception as e:
        log.error(e)
        lastupdate = None

    if place == 'index':
        regions = model_calcs.sort_rate(
            model_calcs.output_rates(subset=ons_week.REGIONS))
        nations = model_calcs.output_rates(subset=ons_week.NATIONS)
        districts = model_calcs.sort_rate(
            model_calcs.output_rates(exclude=ons_week.NATIONS +
                                     ons_week.REGIONS,
                                     subset=ons_week.stored_names.values()))
        top_districts = model_calcs.top_rate(districts)

        return render(
            request, 'graph/covid_chart_index.html', {
                'api_status': 'true',
                'last_update': lastupdate_str,
                'last_cases': last_cases,
                'covid_rates': rates,
                'regions': regions,
                'nations': nations,
                'districts': districts,
                'top_districts': top_districts,
                "england_select": ons_week.england_select,
                "wales_select": ons_week.wales_select,
                "scotland_select": ons_week.scotland_select,
                "ni_select": ons_week.ni_select
            })

    try:
        areacode = places[place]
        nation = ons_week.nation[areacode]
        nation_index = [
            'England', 'Wales', 'Scotland', 'Northern Ireland',
            'England and Wales'
        ].index(nation) + 1
        log.info(f'loading: {areacode} in {nation} ({nation_index})')
    except Exception as e:
        log.error(e)
        nation = None
        areacode = None
        nation_index = 1
    return render(
        request, 'graph/covid_chart_map.html', {
            'last_update': lastupdate_str,
            'last_cases': last_cases,
            'place': place,
            'nation': nation,
            'nation_index': nation_index,
            'areacode': areacode,
            'api_status': 'true',
            'covid-rates': rates,
            "england_select": ons_week.england_select,
            "wales_select": ons_week.wales_select,
            "scotland_select": ons_week.scotland_select,
            "ni_select": ons_week.ni_select
        })