diff --git a/internal/common/structs/structs.go b/internal/common/structs/structs.go index e59c4d9..3dad622 100644 --- a/internal/common/structs/structs.go +++ b/internal/common/structs/structs.go @@ -79,6 +79,7 @@ type TemplateData struct { AllDates []string StatsForDiagram []StatsForDiagram WeeksCount float64 + EachArticleStats [][]EachArticleStat } type AuthorsTop struct { @@ -91,3 +92,9 @@ type StatsForDiagram struct { Date string Count int } + +type EachArticleStat struct { + Name string + HabrNumber int + Stats []StatsForDiagram +} diff --git a/internal/db_service/habr_sqlite.go b/internal/db_service/habr_sqlite.go index 93ab38f..072896e 100644 --- a/internal/db_service/habr_sqlite.go +++ b/internal/db_service/habr_sqlite.go @@ -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 +} diff --git a/internal/latex_service/latex.go b/internal/latex_service/latex.go index 45dcb9e..bf8e8bf 100644 --- a/internal/latex_service/latex.go +++ b/internal/latex_service/latex.go @@ -14,6 +14,7 @@ import ( "os" "os/exec" "path/filepath" + "strconv" "time" ) @@ -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 { diff --git a/templates/tex/stats.tmpl b/templates/tex/stats.tmpl index f8a2430..2600359 100644 --- a/templates/tex/stats.tmpl +++ b/templates/tex/stats.tmpl @@ -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 }}: