Skip to content

Commit

Permalink
feat(template): Add graphics for each article in DB
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhbert committed Dec 16, 2024
1 parent 8e8d0f0 commit b994289
Show file tree
Hide file tree
Showing 4 changed files with 65 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 = 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
}
7 changes: 7 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,9 +71,15 @@ 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 = db_service.GetEachArticleStats(db)

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

for i := 0; i < len(data.EachArticleStats); i++ {
fileName := strconv.Itoa(data.EachArticleStats[i].HabrNumber) + ".csv"
csv_service.PrepareCSV("tmp", fileName, data.EachArticleStats[i].Stats)
}

return data
}

Expand Down
26 changes: 26 additions & 0 deletions templates/tex/stats.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,32 @@
\addplot table [col sep=comma,x=date,y=count] {articlesCount.csv};
\end{axis}
\end{tikzpicture}
\subsubsection{Графики просмотров для каждой статьи в блоге}
\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,
]
\legend{Просмоты за период (неделя)};
\addplot table [col sep=comma,x=date,y=count] {329830.csv};
\addplot table [col sep=comma,x=date,y=count] {330198.csv};
\end{axis}
\end{tikzpicture}
\section{Системная информация}
\subsection{Хабр}
Всего записей статистики в базе данных: {{ .StatsInBaseCount }}:
Expand Down

0 comments on commit b994289

Please sign in to comment.