Skip to content
This repository has been archived by the owner on Oct 31, 2021. It is now read-only.

Commit

Permalink
Add vaccinations data to GetCovidData response
Browse files Browse the repository at this point in the history
  • Loading branch information
Matěj Jehlička authored and dizider committed Apr 6, 2021
1 parent de98c2a commit 05d7d71
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 42 deletions.
97 changes: 75 additions & 22 deletions internal/functions/coviddata/get-covid-data.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,30 @@ func fetchTotals(ctx context.Context, client store.Client, date string) (*Totals
return &totals, nil
}

func fetchVaccinations(ctx context.Context, client store.Client, date string) (*VaccinationsAggregatedData, error) {
logger := logging.FromContext(ctx).Named("fetchVaccinations")

snap, err := client.Doc(constants.CollectionVaccinations, date).Get(ctx)

if err != nil {
if status.Code(err) == codes.NotFound {
return nil, &errors.NotFoundError{Msg: fmt.Sprintf("Could not find vaccination data for %v", date)}
}

return nil, fmt.Errorf("Error while querying Firestore: %v", err)
}

var vaccinationData VaccinationsAggregatedData

if err := snap.DataTo(&vaccinationData); err != nil {
panic(fmt.Sprintf("could not parse input: %s", err))
}

logger.Infof("fetched vaccinations data: %+v", vaccinationData)

return &vaccinationData, nil
}

// GetCovidData handler.
func GetCovidData(w http.ResponseWriter, r *http.Request) {
var ctx = r.Context()
Expand Down Expand Up @@ -78,6 +102,7 @@ func GetCovidData(w http.ResponseWriter, r *http.Request) {
}

failed := false
vaccinationsFailed := false

totalsData, err := fetchTotals(ctx, storeClient, date)
if err != nil {
Expand All @@ -92,16 +117,39 @@ func GetCovidData(w http.ResponseWriter, r *http.Request) {
if failed && shouldFallback {
// we try to fetch data from yesterday
t, _ := time.Parse("20060102", date)
date = t.AddDate(0, 0, -1).Format("20060102")
totalsDataDate := t.AddDate(0, 0, -1).Format("20060102")

totalsData, err = fetchTotals(ctx, storeClient, date)
totalsData, err = fetchTotals(ctx, storeClient, totalsDataDate)
if err != nil {
logger.Errorf("Error refetching data from firestore: %v", err)
httputils.SendErrorResponse(w, r, err)
return
}
}

vaccinationData, err := fetchVaccinations(ctx, storeClient, date)
if err != nil {
logger.Errorf("Error fatching vaccination data from firestore: %v", err)
vaccinationsFailed = true
if !shouldFallback {
httputils.SendErrorResponse(w, r, err)
return
}
}

if vaccinationsFailed && shouldFallback {
// we try to fetch data from yesterday
t, _ := time.Parse("20060102", date)
vaccinationsDataDate := t.AddDate(0, 0, -1).Format("20060102")

vaccinationData, err = fetchVaccinations(ctx, storeClient, vaccinationsDataDate)
if err != nil {
logger.Errorf("Error refetching vaccinations firestore: %v", err)
httputils.SendErrorResponse(w, r, err)
return
}
}

// backward compatibility (when the date where PCR tests attributes are not defined is queried)
testsTotal := totalsData.PCRTestsTotal
testsIncrease := totalsData.PCRTestsIncrease
Expand All @@ -120,26 +168,31 @@ func GetCovidData(w http.ResponseWriter, r *http.Request) {
}

res := v1.GetCovidDataResponse{
Date: date,
ActiveCasesTotal: totalsData.ActiveCasesTotal,
CuredTotal: totalsData.CuredTotal,
DeceasedTotal: totalsData.DeceasedTotal,
CurrentlyHospitalizedTotal: totalsData.CurrentlyHospitalizedTotal,
TestsTotal: testsTotal, // this value is duplicated for backward compatibility
TestsIncrease: testsIncrease, // this value is duplicated for backward compatibility
TestsIncreaseDate: testsIncreaseDate, // this value is duplicated for backward compatibility
ConfirmedCasesTotal: totalsData.ConfirmedCasesTotal,
ConfirmedCasesIncrease: totalsData.ConfirmedCasesIncrease,
ConfirmedCasesIncreaseDate: totalsData.ConfirmedCasesIncreaseDate,
AntigenTestsTotal: totalsData.AntigenTestsTotal,
AntigenTestsIncrease: totalsData.AntigenTestsIncrease,
AntigenTestsIncreaseDate: totalsData.AntigenTestsIncreaseDate,
PCRTestsTotal: testsTotal,
PCRTestsIncrease: testsIncrease,
PCRTestsIncreaseDate: testsIncreaseDate,
VaccinationsTotal: totalsData.VaccinationsTotal,
VaccinationsIncrease: totalsData.VaccinationsIncrease,
VaccinationsIncreaseDate: totalsData.VaccinationsIncreaseDate,
Date: totalsData.Date,
ActiveCasesTotal: totalsData.ActiveCasesTotal,
CuredTotal: totalsData.CuredTotal,
DeceasedTotal: totalsData.DeceasedTotal,
CurrentlyHospitalizedTotal: totalsData.CurrentlyHospitalizedTotal,
TestsTotal: testsTotal, // this value is duplicated for backward compatibility
TestsIncrease: testsIncrease, // this value is duplicated for backward compatibility
TestsIncreaseDate: testsIncreaseDate, // this value is duplicated for backward compatibility
ConfirmedCasesTotal: totalsData.ConfirmedCasesTotal,
ConfirmedCasesIncrease: totalsData.ConfirmedCasesIncrease,
ConfirmedCasesIncreaseDate: totalsData.ConfirmedCasesIncreaseDate,
AntigenTestsTotal: totalsData.AntigenTestsTotal,
AntigenTestsIncrease: totalsData.AntigenTestsIncrease,
AntigenTestsIncreaseDate: totalsData.AntigenTestsIncreaseDate,
PCRTestsTotal: testsTotal,
PCRTestsIncrease: testsIncrease,
PCRTestsIncreaseDate: testsIncreaseDate,
VaccinationsTotal: totalsData.VaccinationsTotal,
VaccinationsIncrease: totalsData.VaccinationsIncrease,
VaccinationsIncreaseDate: totalsData.VaccinationsIncreaseDate,
VaccinationsDailyDosesDate: vaccinationData.Date,
VaccinationsDailyFirstDose: vaccinationData.DailyFirstDose,
VaccinationsDailySecondDose: vaccinationData.DailySecondDose,
VaccinationsTotalFirstDose: vaccinationData.TotalFirstDose,
VaccinationsTotalSecondDose: vaccinationData.TotalSecondDose,
}

httputils.SendResponse(w, r, res)
Expand Down
45 changes: 25 additions & 20 deletions pkg/api/v1/api-types.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,31 @@ type GetCovidDataRequest struct {

//GetCovidDataResponse Response for GetCovidData function
type GetCovidDataResponse struct {
Date string `json:"date"`
ActiveCasesTotal int `json:"activeCasesTotal" validate:"required"`
CuredTotal int `json:"curedTotal" validate:"required"`
DeceasedTotal int `json:"deceasedTotal" validate:"required"`
CurrentlyHospitalizedTotal int `json:"currentlyHospitalizedTotal" validate:"required"`
TestsTotal int `json:"testsTotal" validate:"required"`
TestsIncrease int `json:"testsIncrease" validate:"required"`
TestsIncreaseDate string `json:"testsIncreaseDate" validate:"required"`
ConfirmedCasesTotal int `json:"confirmedCasesTotal" validate:"required"`
ConfirmedCasesIncrease int `json:"confirmedCasesIncrease" validate:"required"`
ConfirmedCasesIncreaseDate string `json:"confirmedCasesIncreaseDate" validate:"required"`
AntigenTestsTotal int `json:"antigenTestsTotal" validate:"required"`
AntigenTestsIncrease int `json:"antigenTestsIncrease" validate:"required"`
AntigenTestsIncreaseDate string `json:"antigenTestsIncreaseDate" validate:"required"`
PCRTestsTotal int `json:"pcrTestsTotal" validate:"required"`
PCRTestsIncrease int `json:"pcrTestsIncrease" validate:"required"`
PCRTestsIncreaseDate string `json:"pcrTestsIncreaseDate" validate:"required"`
VaccinationsTotal int `json:"vaccinationsTotal" validate:"required"`
VaccinationsIncrease int `json:"vaccinationsIncrease" validate:"required"`
VaccinationsIncreaseDate string `json:"vaccinationsIncreaseDate" validate:"required"`
Date string `json:"date"`
ActiveCasesTotal int `json:"activeCasesTotal" validate:"required"`
CuredTotal int `json:"curedTotal" validate:"required"`
DeceasedTotal int `json:"deceasedTotal" validate:"required"`
CurrentlyHospitalizedTotal int `json:"currentlyHospitalizedTotal" validate:"required"`
TestsTotal int `json:"testsTotal" validate:"required"`
TestsIncrease int `json:"testsIncrease" validate:"required"`
TestsIncreaseDate string `json:"testsIncreaseDate" validate:"required"`
ConfirmedCasesTotal int `json:"confirmedCasesTotal" validate:"required"`
ConfirmedCasesIncrease int `json:"confirmedCasesIncrease" validate:"required"`
ConfirmedCasesIncreaseDate string `json:"confirmedCasesIncreaseDate" validate:"required"`
AntigenTestsTotal int `json:"antigenTestsTotal" validate:"required"`
AntigenTestsIncrease int `json:"antigenTestsIncrease" validate:"required"`
AntigenTestsIncreaseDate string `json:"antigenTestsIncreaseDate" validate:"required"`
PCRTestsTotal int `json:"pcrTestsTotal" validate:"required"`
PCRTestsIncrease int `json:"pcrTestsIncrease" validate:"required"`
PCRTestsIncreaseDate string `json:"pcrTestsIncreaseDate" validate:"required"`
VaccinationsTotal int `json:"vaccinationsTotal" validate:"required"`
VaccinationsIncrease int `json:"vaccinationsIncrease" validate:"required"`
VaccinationsIncreaseDate string `json:"vaccinationsIncreaseDate" validate:"required"`
VaccinationsDailyDosesDate string `json:"vaccinationsDailyDosesDate validate:"required"`
VaccinationsDailyFirstDose int `json:"vaccinationsDailyFirstDose" validate:"required"`
VaccinationsDailySecondDose int `json:"vaccinationsDailySecondDose" validate:"required"`
VaccinationsTotalFirstDose int `json:"vaccinationsTotalFirstDose" validate:"required"`
VaccinationsTotalSecondDose int `json:"vaccinationsTotalSecondDose" validate:"required"`
}

//DownloadMetricsRequest Request for DownloadMetrics function
Expand Down

0 comments on commit 05d7d71

Please sign in to comment.