Skip to content

Commit

Permalink
feat(template): Add graphics for each article in DB (#37)
Browse files Browse the repository at this point in the history
* feat(template): Add graphics for each article in DB
  • Loading branch information
Zhbert authored Dec 17, 2024
1 parent 8e8d0f0 commit 29b4445
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
7 changes: 7 additions & 0 deletions internal/common/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ type TemplateData struct {
AllDates []string
StatsForDiagram []StatsForDiagram
WeeksCount float64
EachArticleStats [][]EachArticleStat
}

type AuthorsTop struct {
Expand All @@ -91,3 +92,9 @@ type StatsForDiagram struct {
Date string
Count int
}

type EachArticleStat struct {
Name string
HabrNumber int
Stats []StatsForDiagram
}
25 changes: 25 additions & 0 deletions internal/db_service/habr_sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,3 +434,28 @@ func GetAllStatsAndDatesForDiagram(db *gorm.DB) ([]structs.StatsForDiagram, floa

return statsForDiagram, weeks
}

func GetEachArticleStats(db *gorm.DB) []structs.EachArticleStat {
articles := GetAllHabrArticles("", db)
_, dates := GetAllDatesOfStats(db)
var eachArticleStats []structs.EachArticleStat

for i := 0; i < len(articles); i++ {
var eas structs.EachArticleStat
eas.Name = text.CleanText(articles[i].Name)
eas.HabrNumber = articles[i].HabrNumber
for y := 0; y < len(dates); y++ {
var st structs.StatsForDiagram
st.Date = dates[y].Format("2006-01-02")
stats := GetLatestStatsFromArticle(articles[i].ID, dates[y], db)
if len(stats) > 1 {
st.Count = stats[1].Views - stats[0].Views
} else if len(stats) == 1 {
st.Count = stats[0].Views
}
eas.Stats = append(eas.Stats, st)
}
eachArticleStats = append(eachArticleStats, eas)
}
return eachArticleStats
}
29 changes: 29 additions & 0 deletions internal/latex_service/latex.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"os"
"os/exec"
"path/filepath"
"strconv"
"time"
)

Expand Down Expand Up @@ -70,12 +71,40 @@ func getHabrData(db *gorm.DB) structs.TemplateData {
data.Authors = db_service.GetTopOfAuthors(true, db)
data.AllDates, _ = db_service.GetAllDatesOfStats(db)
data.StatsForDiagram, data.WeeksCount = db_service.GetAllStatsAndDatesForDiagram(db)
data.EachArticleStats = prepareDataForEachArticles(db)

csv_service.PrepareCSV("tmp", "articlesCount.csv", data.StatsForDiagram)

return data
}

func prepareDataForEachArticles(db *gorm.DB) [][]structs.EachArticleStat {
var data [][]structs.EachArticleStat
eas := db_service.GetEachArticleStats(db)

latest := 0
for i := 0; i < len(eas); i++ {
fileName := "habr-csv-" + strconv.Itoa(eas[i].HabrNumber) + ".csv"
csv_service.PrepareCSV("tmp", fileName, eas[i].Stats)

if i > latest || i == 0 {
var tmp []structs.EachArticleStat
tmp = append(tmp, eas[i])
if i+1 < len(eas) {
tmp = append(tmp, eas[i+1])
latest = i + 1
}
if i+2 < len(eas) {
tmp = append(tmp, eas[i+2])
latest = i + 2
}
data = append(data, tmp)
}
}

return data
}

func getDates(db *gorm.DB) (string, string) {
stats, state := db_service.GetTwoLatestStats(db)
if state {
Expand Down
37 changes: 37 additions & 0 deletions templates/tex/stats.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,43 @@
\addplot table [col sep=comma,x=date,y=count] {articlesCount.csv};
\end{axis}
\end{tikzpicture}
\subsubsection{Графики просмотров для каждой статьи в блоге}
{{ range .EachArticleStats }}
\begin{tikzpicture}
\begin{axis}[
xlabel={Дата статистики},
ylabel={Кол-во просмотров},
scaled ticks=false,
legend pos = north west,
table/col sep = semicolon,
width = 0.85\paperwidth,
grid = major,
date coordinates in=x,
date ZERO=2024-05-01, % <-- needs to be set for v1.12 and below
xtick=data,
xticklabel style={
rotate=90,
anchor=near xticklabel,
},
yticklabel style={/pgf/number format/fixed},
% set the label style of the `xtick's
xticklabel=\day.\month.\year,
]
{{ range . }}
\addplot table [col sep=comma,x=date,y=count] {habr-csv-{{ .HabrNumber }}.csv};
\addlegendentry{ {{ .HabrNumber }} }
{{ end }}
\end{axis}
\end{tikzpicture}

Статьи на графике:
\begin{itemize}
\setlength{\itemsep}{-2mm}
{{ range . }}
\item {{ .HabrNumber }} --- {{ .Name }}
{{ end }}
\end{itemize}
{{ end }}
\section{Системная информация}
\subsection{Хабр}
Всего записей статистики в базе данных: {{ .StatsInBaseCount }}:
Expand Down

0 comments on commit 29b4445

Please sign in to comment.