Skip to content

Commit

Permalink
feat(template): Add graphics with main info to the Habr section
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhbert committed Nov 14, 2024
1 parent d75e6cb commit 6af08b8
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 16 deletions.
6 changes: 6 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,9 @@ tasks:
cmds:
- |
rm -rf ./result
run-for-test-with-file:
desc: 'Run utility with generation of output file'
cmds:
- task: build
- |
./colligendis collect --habr --file -v
4 changes: 3 additions & 1 deletion cmd/collect/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/spf13/cobra"
"log"
"strconv"
"time"
)

const (
Expand All @@ -48,7 +49,8 @@ func GetCollectCommand(flags *common.ColligendisFlags, tmpls []structs.TemplateS
case true:
latex_service.GenerateLaTeXFiles(tmpls, flags)
case false:
log.Printf("Total habr views: %s", strconv.Itoa(db_service.GetHabrViewsCount()))
var zeroTime time.Time
log.Printf("Total habr views: %s", strconv.Itoa(db_service.GetHabrViewsCount(zeroTime)))
if flags.Full {
getFullHabrViewsCount(flags.Limit, flags.SortType)
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/collect/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"os"
"sort"
"strconv"
"time"
)

func getFullHabrViewsCount(limit int, sortType string) []structs.StatsArticle {
Expand All @@ -44,7 +45,8 @@ func getFullHabrViewsCount(limit int, sortType string) []structs.StatsArticle {
var rowStructs []structs.StatsArticle

for i, a := range articles {
stats, state := db_service.GetLatestStatsFromArticle(a.ID)
var zeroTime time.Time
stats, state := db_service.GetLatestStatsFromArticle(a.ID, zeroTime)
var stat structs.StatsArticle
if state {
stat.Id = i
Expand Down
6 changes: 6 additions & 0 deletions internal/common/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,16 @@ type TemplateData struct {
AuthorsTopGlobal []AuthorsTop
Authors []AuthorsTop
AllDates []string
StatsForDiagram []StatsForDiagram
}

type AuthorsTop struct {
Name string
ID int
ArticlesCount int64
}

type StatsForDiagram struct {
Date string
Count int
}
33 changes: 33 additions & 0 deletions internal/csv_service/csv_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package csv_service

import (
"colligendis/internal/common/structs"
"encoding/csv"
"log"
"os"
"path"
"strconv"
)

func PrepareCSV(pathToFile string, fileNAme string, data []structs.StatsForDiagram) {
file, err := os.Create(path.Join(pathToFile, fileNAme))
if err != nil {
log.Fatal(err)
}
defer file.Close()

writer := csv.NewWriter(file)
defer writer.Flush()
writer.Comma = ','

writer.Write([]string{"date", "count"})

count := 1
for _, d := range data {
if count != len(data) {
writer.Write([]string{d.Date, strconv.Itoa(d.Count)})
count++
}
}

}
50 changes: 38 additions & 12 deletions internal/db_service/habr_sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func GetLatestArticles() []domain.HabrStats {
return stats
}

func GetLatestStatsFromArticle(articleID uint) ([]domain.HabrStats, bool) {
func GetLatestStatsFromArticle(articleID uint, sinceDate time.Time) ([]domain.HabrStats, bool) {
var stats []domain.HabrStats
state := false

Expand All @@ -293,11 +293,20 @@ func GetLatestStatsFromArticle(articleID uint) ([]domain.HabrStats, bool) {
log.Fatal("Error opening db!")
} else {
state = true
db.
Where("habr_article_id = ?", articleID).
Order("date_of_stats DESC").
Find(&stats).
Limit(2)
if !sinceDate.IsZero() {
db.
Where("habr_article_id = ?", articleID).
Where("date_of_stats <= ?", sinceDate).
Order("date_of_stats DESC").
Find(&stats).
Limit(2)
} else {
db.
Where("habr_article_id = ?", articleID).
Order("date_of_stats DESC").
Find(&stats).
Limit(2)
}
}

if len(stats) > 1 {
Expand All @@ -310,12 +319,12 @@ func GetLatestStatsFromArticle(articleID uint) ([]domain.HabrStats, bool) {
return stats, state
}

func GetHabrViewsCount() int {
func GetHabrViewsCount(sinceDate time.Time) int {
articles := GetAllHabrArticles("")
count := 0

for _, a := range articles {
stats, state := GetLatestStatsFromArticle(a.ID)
stats, state := GetLatestStatsFromArticle(a.ID, sinceDate)
if state {
if len(stats) > 1 {
diff := stats[1].Views - stats[0].Views
Expand Down Expand Up @@ -357,7 +366,8 @@ func GetArticlesFormLastPeriod(dt time.Time, getAll bool, global bool) (int, []s
var latestArts []structs.StatsArticle
articles := GetAllHabrArticles("")
for i, a := range articles {
stats, state := GetLatestStatsFromArticle(a.ID)
var zeroTime time.Time
stats, state := GetLatestStatsFromArticle(a.ID, zeroTime)
var stat structs.StatsArticle
if state {
stat.Id = i
Expand Down Expand Up @@ -450,10 +460,11 @@ func GetCountOfStats() int64 {
return count
}

func GetAllDatesOfStats() []string {
func GetAllDatesOfStats() ([]string, []time.Time) {
var dates []string

var stats []domain.HabrStats
var timeDates []time.Time

db, err := gorm.Open(sqlite.Open("colligendis.db"),
&gorm.Config{Logger: logger.Default.LogMode(getLogger())})
Expand All @@ -464,11 +475,26 @@ func GetAllDatesOfStats() []string {
Group("date_of_stats").
Order("date_of_stats DESC").
Find(&stats)

for _, stat := range stats {
timeDates = append(timeDates, stat.DateOfStats)
dates = append(dates, stat.DateOfStats.Format("January 2, 2006"))
}
}

return dates
return dates, timeDates
}

func GetAllStatsAndDatesForDiagram() []structs.StatsForDiagram {
var statsForDiagram []structs.StatsForDiagram

_, dates := GetAllDatesOfStats()

for i := 0; i < len(dates); i++ {
var st structs.StatsForDiagram
st.Date = dates[i].Format("2006-01-02")
st.Count = GetHabrViewsCount(dates[i])
statsForDiagram = append(statsForDiagram, st)
}

return statsForDiagram
}
9 changes: 7 additions & 2 deletions internal/latex_service/latex.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"colligendis/cmd/common"
"colligendis/cmd/version"
"colligendis/internal/common/structs"
"colligendis/internal/csv_service"
"colligendis/internal/db_service"
"fmt"
"html/template"
Expand Down Expand Up @@ -50,7 +51,8 @@ func getHabrData() structs.TemplateData {
data.Version = version.GetVersion()

data.StatsInBaseCount = db_service.GetCountOfStats()
data.AllViewsCount = db_service.GetHabrViewsCount()
var zeroTime time.Time
data.AllViewsCount = db_service.GetHabrViewsCount(zeroTime)
data.PreviousDate, data.LatestDate = getDates()
data.ArticlesCount = db_service.GetHabrArticlesCount()
pd, _ := time.Parse("2006-01-02", data.PreviousDate)
Expand All @@ -64,7 +66,10 @@ func getHabrData() structs.TemplateData {
data.AuthorsTopGlobal = db_service.GetTopOfAuthors(false)
data.AuthorsTopGlobal = data.AuthorsTopGlobal[0:5]
data.Authors = db_service.GetTopOfAuthors(true)
data.AllDates = db_service.GetAllDatesOfStats()
data.AllDates, _ = db_service.GetAllDatesOfStats()
data.StatsForDiagram = db_service.GetAllStatsAndDatesForDiagram()

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

return data
}
Expand Down
3 changes: 3 additions & 0 deletions templates/tex/preamble
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
\usepackage{listings}
\usepackage{float}
\usepackage{longtable}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\usepgfplotslibrary{dateplot}
\usepackage{tocloft}
\usepackage{misccorr}
\usepackage{graphicx}
Expand Down
26 changes: 26 additions & 0 deletions templates/tex/stats.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,32 @@
{{ end }}
\caption{Все авторы блога}
\end{longtable}
\subsection{Графики изменений}
\subsubsection{График просмотров за последние N недель}
\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] {articlesCount.csv};
\end{axis}
\end{tikzpicture}
\section{Системная информация}
\subsection{Хабр}
Всего записей статистики в базе данных: {{ .StatsInBaseCount }}:
Expand Down

0 comments on commit 6af08b8

Please sign in to comment.