Skip to content

Commit

Permalink
Add UK data updates from gov source,add frequent updates from JHU/UK
Browse files Browse the repository at this point in the history
  • Loading branch information
kennygrant committed Apr 5, 2020
1 parent 4deb210 commit cac006f
Show file tree
Hide file tree
Showing 9 changed files with 685 additions and 44 deletions.
8 changes: 7 additions & 1 deletion index.html.got
Expand Up @@ -42,6 +42,9 @@
h3.confirmed {
color:rgba(163,32,32,0.7);
}
h3.tested {
color:rgba(32,163,32,0.7);
}
h4 {
margin:0;
font-weight:100;
Expand All @@ -55,6 +58,9 @@
h2 .confirmed {
color:rgba(163,32,32,0.7);
}
h2 .tested {
color:rgba(32,163,32,0.7);
}
.filters {
padding:0.5rem 1rem;
margin:0 auto;
Expand Down Expand Up @@ -167,7 +173,7 @@
<a name="top"></a>
<header>
<h1><span id="chart_title">{{.series.Title}}</span> Coronavirus</h1>
<h2><span class="deaths">{{.series.Format .allTimeDeaths}} Deaths</span> &nbsp; <span class="confirmed">{{.series.Format .allTimeConfirmed}} Confirmed</span></h2>
<h2><span class="deaths">{{.series.Format .allTimeDeaths}} Deaths</span> &nbsp; <span class="confirmed">{{.series.Format .allTimeConfirmed}} Confirmed</span> {{ if gt .allTimeTested 0 }}&nbsp; <span class="tested">{{.series.Format .allTimeTested}} Tested</span>{{end}}</h2>
</header>

<article>
Expand Down
6 changes: 6 additions & 0 deletions main.go
Expand Up @@ -103,6 +103,8 @@ func handleHome(w http.ResponseWriter, r *http.Request) {
// Get the total counts first for the page
allTimeDeaths := s.TotalDeaths()
allTimeConfirmed := s.TotalConfirmed()
allTimeRecovered := s.TotalRecovered() // unreliable as yet
allTimeTested := s.TotalTested()

mobile := strings.Contains(strings.ToLower(r.UserAgent()), "mobile")

Expand Down Expand Up @@ -155,6 +157,8 @@ func handleHome(w http.ResponseWriter, r *http.Request) {
"series": s,
"allTimeDeaths": allTimeDeaths,
"allTimeConfirmed": allTimeConfirmed,
"allTimeRecovered": allTimeRecovered,
"allTimeTested": allTimeTested,
"periodOptions": series.PeriodOptions(),
"countryOptions": series.CountryOptions(),
"provinceOptions": series.ProvinceOptions(s.Country),
Expand Down Expand Up @@ -205,6 +209,8 @@ func handleReload(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/", 302)
}

// Also reload today from online data
go updateFrequent()
}

// param returns one param string value
Expand Down
82 changes: 81 additions & 1 deletion series/series_test.go
@@ -1,7 +1,8 @@
package series

import (
//"os"
"encoding/json"
"os"
"path/filepath"
"testing"
"time"
Expand Down Expand Up @@ -32,6 +33,85 @@ func TestFormat(t *testing.T) {

}

// Test parse of UK json
func TestUKJSON(t *testing.T) {

p, _ := filepath.Abs("testdata/uk.json")
f, err := os.Open(p)
if err != nil {
t.Fatalf("json open err:%s", err)
}

jsonData := make(map[string]interface{})
err = json.NewDecoder(f).Decode(&jsonData)
if err != nil {
t.Fatalf("json parse err:%s", err)
}

stats, err := parseUKJSON(jsonData)
if err != nil {
t.Fatalf("failed to parse UK json:%s", err)
}

if stats.UKCases != 47806 {
t.Errorf("ukjson: incorrect UK Deaths want:%d got:%d", 47806, stats.UKCases)
}

if stats.WalesDeaths != 166 {
t.Errorf("ukjson: incorrect UK Deaths want:%d got:%d", 166, stats.UKCases)
}

if stats.NIDeaths != 56 {
t.Errorf("ukjson: incorrect UK Deaths want:%d got:%d", 56, stats.UKCases)
}

}

// TestUKJSONAssign tests parse and update from uk json
func TestUKJSONAssign(t *testing.T) {
// Load areas first
p, _ := filepath.Abs("testdata/areas.csv")
err := LoadAreas(p)
if err != nil {
t.Fatalf("areas: failed to load file:%s", err)
}

// Add days up to today so that we can update them
days := int(time.Now().UTC().Sub(seriesStartDate).Hours() / 24)
// For every series add the right number of days up to and including today
for _, series := range dataset {
series.AddDays(days)
}

// Now load json
p, _ = filepath.Abs("testdata/uk.json")
f, err := os.Open(p)
if err != nil {
t.Fatalf("json open err:%s", err)
}

jsonData := make(map[string]interface{})
err = json.NewDecoder(f).Decode(&jsonData)
if err != nil {
t.Fatalf("json parse err:%s", err)
}

err = UpdateFromUKStats(jsonData)
if err != nil {
t.Fatalf("failed to update from UK json:%s", err)
}

// Test fetch of wales and value
wales, err := dataset.FetchSeries("United Kingdom", "Wales")
if err != nil {
t.Fatalf("failed to fetch wales:%s", err)
}
if wales.TotalDeaths() != 166 {
t.Errorf("wales deaths wrong want:%d got:%d", 166, wales.TotalDeaths())
}

}

// TestLoadAreas tests loading our static test area file (with just a few areas in it)
func TestLoadAreas(t *testing.T) {
p, _ := filepath.Abs("testdata/areas.csv")
Expand Down
2 changes: 1 addition & 1 deletion series/slice.go
Expand Up @@ -32,7 +32,7 @@ func (slice Slice) AddToday() error {
// Work out whether we already have today in the first slice data
// NB we assume a certain start date for today
days := int(time.Now().UTC().Sub(seriesStartDate).Hours()/24) + 1
if days < len(slice[0].Days) {
if days <= len(slice[0].Days) {
return nil
}

Expand Down
22 changes: 22 additions & 0 deletions series/storage.go
Expand Up @@ -34,6 +34,28 @@ const (
DataTodayCountry = 21
)

// AddToday adds a day to our dataset
// usually called after zero hours
func AddToday() error {

// Lock during add operation
mutex.Lock()
defer mutex.Unlock()

// If we don't have it already, add a set of data for today
dataset.AddToday()

// We should also save out in case of restart?
/*
err := Save("data/series.csv")
if err != nil {
return fmt.Errorf("series: failed to save series data:%s", err)
}
*/

return nil
}

// LoadData reloads all data from our data files in dataPath
// Dataset is locked for writing inside functions below
func LoadData(dataPath string) error {
Expand Down

0 comments on commit cac006f

Please sign in to comment.