snowchive-server/outlets.go
2026-11-19 04:40:59 +02:00

132 lines
3.1 KiB
Go

// outlets.go - /explore/outlets: publication activity per media outlet and
// year.
package main
import (
"net/http"
"sort"
)
type outletCell struct {
Docs int
Articles int
URL string
}
type outletRow struct {
Name string
URL string
Total int
Cells []outletCell
}
type outletTable struct {
Years []string
YearLinks []facetEntry
Rows []outletRow
Totals []int
}
func (co *Corpus) buildOutletTables() {
co.outletTables = map[string]outletTable{
"": co.buildOutletTable(""),
collectionNews: co.buildOutletTable(collectionNews),
}
}
func (co *Corpus) buildOutletTable(coll string) outletTable {
type key struct{ outlet, year string }
docs := map[key]int{}
stories := map[key]int{}
totals := map[string]int{}
yearSet := map[string]bool{}
display := map[string]string{}
canon := func(name string) string {
k := valueKey(name)
if d, ok := display[k]; ok {
return d
}
display[k] = name
return name
}
for i := range co.archive.Docs {
d := &co.archive.Docs[i]
if coll != "" && d.Collection != coll {
continue
}
for _, pub := range facetValues(d, "PU") {
totals[canon(pub)]++
}
// A document counts for a publisher in the years of THAT publisher's
// own entries: another outlet's earlier publication does not make it
// this outlet's document of that year (publishedBy, the cell's page).
counted := map[key]bool{}
for _, p := range d.Publication {
if p.ReportingOnly || p.Publisher == "" || len(p.DatePublished) < 4 {
continue
}
k := key{canon(p.Publisher), p.DatePublished[:4]}
if !counted[k] {
counted[k] = true
docs[k]++
yearSet[k.year] = true
}
}
}
for i := range co.articles {
a := &co.articles[i]
if a.Title == "" || a.Publisher == "" || len(a.Date) < 4 {
continue
}
// A story counts in a collection's table when one of its documents
// is in that collection.
if coll != "" && !a.inCollection(co, coll) {
continue
}
stories[key{canon(a.Publisher), a.Date[:4]}]++
}
years := make([]string, 0, len(yearSet))
for y := range yearSet {
years = append(years, y)
}
sort.Strings(years)
names := rankCounts(totals, -1)
scope := wholeArchive
if coll != "" {
scope = scopeOf(map[string]bool{coll: true})
}
t := outletTable{Years: years, Totals: make([]int, len(years))}
for _, y := range years {
t.YearLinks = append(t.YearLinks, facetEntry{Name: y, URL: scope.link(categoryValueURL("published", y))})
}
for _, name := range names {
row := outletRow{Name: name, URL: scope.link(categoryValueURL("publisher", name)), Total: totals[name]}
for yi, y := range years {
k := key{name, y}
cell := outletCell{Docs: docs[k], Articles: stories[k]}
if cell.Docs > 0 {
cell.URL = scope.link(publisherYearURL(name, y))
}
t.Totals[yi] += cell.Docs
row.Cells = append(row.Cells, cell)
}
t.Rows = append(t.Rows, row)
}
return t
}
func (co *Corpus) handleOutlets(w http.ResponseWriter, r *http.Request) {
coll := ""
if r.URL.Query().Get("coll") == collectionNews {
coll = collectionNews
}
render(w, r, &outletsPage{
Table: co.outletTables[coll],
NewsOnly: coll == collectionNews,
})
}