Skip to content

Commit

Permalink
zprovoznění všech statistik
Browse files Browse the repository at this point in the history
  • Loading branch information
Firu115 committed Nov 25, 2024
1 parent 1566a80 commit c06080a
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 73 deletions.
72 changes: 18 additions & 54 deletions backend/databaze/prikazy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package databaze

import (
"backend/utils"
"database/sql"
"encoding/json"
"errors"
Expand Down Expand Up @@ -410,7 +409,7 @@ func GetUzivByJmeno(jmeno string) (Uzivatel, error) {

func GetVsechnyJmenaUziv() ([]string, error) {
var uzivatele []string
rows, err := DB.Query(`SELECT jmeno FROM uzivatel WHERE NOT smazany;;`)
rows, err := DB.Query(`SELECT jmeno FROM uzivatel WHERE NOT smazany;`)
if err != nil {
return uzivatele, err
}
Expand Down Expand Up @@ -465,60 +464,26 @@ func GetDaystreak(uzivID uint) (int, error) {
return daystreak, nil
}

/* presnost, cpm, chybyPismenka, cas*/
func GetUdaje(uzivID uint) (float32, []float64, map[string]int, float64, error) {
/* presnost, cpm, chybyPismenka, cas, napsanychPismen */
func GetUdaje(uzivID uint) (float32, float32, map[string]int, [3]int, [3]int, error) {
var presnost float32 = -1
var delkaVsechTextu int = 0
var cpm []float64
var chybyPismenka map[string]int = make(map[string]int)
var casCelkem float64
var rychlost float32 = -1
var chybyPismenkaJsonb []byte
var chybyPismenka map[string]int

var rows *sql.Rows
var err error
rows, err = DB.Query(`SELECT neopravene, delka_textu, cas, chyby_pismenka, datum FROM dokoncene WHERE uziv_id = $1 AND datum::date > CURRENT_DATE - MAKE_INTERVAL(days => 13) UNION SELECT neopravene, delka_textu, cas, chyby_pismenka, datum FROM dokoncene_procvic WHERE uziv_id = $1 AND datum::date > CURRENT_DATE - MAKE_INTERVAL(days => 13);`, uzivID)
if err != nil {
return presnost, cpm, chybyPismenka, casCelkem, err
}
defer rows.Close()

var soucetChyb int = 0
for rows.Next() {
var neopravene, delka int
var cas float64
var chybyPismenkaRowByte []byte
var datumNezajima date.Date
err := rows.Scan(&neopravene, &delka, &cas, &chybyPismenkaRowByte, &datumNezajima)
if err != nil {
return presnost, cpm, chybyPismenka, casCelkem, err
}

var chybyPismenkaRow map[string]int
err = json.Unmarshal(chybyPismenkaRowByte, &chybyPismenkaRow)
if err == nil {
for key, value := range chybyPismenkaRow {
chybyPismenka[key] += value // když to ještě neexistuje, default value je 0
soucetChyb += value
}
}
soucetChyb += neopravene
cpm = append(cpm, utils.CPM(delka, cas, neopravene))
delkaVsechTextu += delka
}
var cas [3]int
var napsanychPismen [3]int

if delkaVsechTextu != 0 {
presnost = float32(delkaVsechTextu-soucetChyb) / float32(delkaVsechTextu) * 100
if presnost < 0 {
presnost = 0 // kvuli adamovi kterej big troulin a měl -10%
}
err := DB.QueryRow(`WITH vsechny_za_dva_tydny AS ( SELECT neopravene, delka_textu, cas, datum, ( SELECT SUM(value::NUMERIC) FROM jsonb_each_text(chyby_pismenka) ) AS opravene, chyby_pismenka FROM dokoncene WHERE uziv_id = $1 AND datum::date > CURRENT_DATE - MAKE_INTERVAL(days => 13) UNION SELECT neopravene, delka_textu, cas, datum, ( SELECT SUM(value::NUMERIC) FROM jsonb_each_text(chyby_pismenka) ) AS opravene, chyby_pismenka FROM dokoncene_procvic WHERE uziv_id = $1 AND datum::date > CURRENT_DATE - MAKE_INTERVAL(days => 13) ), soucty_pismenek AS ( SELECT key AS pismeno, SUM(value::NUMERIC) AS soucet FROM vsechny_za_dva_tydny, jsonb_each_text(chyby_pismenka) GROUP BY key ), vsechny AS ( SELECT delka_textu, cas, datum::date FROM dokoncene WHERE uziv_id = $1 UNION SELECT delka_textu, cas, datum::date FROM dokoncene_procvic WHERE uziv_id = $1 ), soucty_dnes AS ( SELECT SUM(cas) AS cas_dnes, SUM(delka_textu) AS napsanych_pismen_dnes FROM vsechny WHERE datum = CURRENT_DATE ), soucty_dva_tydny AS ( SELECT SUM(cas) AS cas_dva_tydny, SUM(delka_textu) AS napsanych_pismen_dva_tydny FROM vsechny WHERE datum > CURRENT_DATE - MAKE_INTERVAL(days => 14) ), soucty_celkem AS ( SELECT SUM(cas) AS cas_celkem, SUM(delka_textu) AS napsanych_pismen_celkem FROM vsechny ) SELECT GREATEST( ( ( SUM(delka_textu) - 10 * SUM(neopravene) ) / SUM(cas)::NUMERIC ) * 60, 0 ) AS rychlost, COALESCE( ( ( SUM(delka_textu) - SUM(neopravene) - COALESCE(SUM(opravene), 0) ) / SUM(delka_textu)::NUMERIC ) * 100, -1 ) AS presnost, jsonb_object_agg(pismeno, soucet), COALESCE(max(cas_dnes), 0) AS cas_dnes, COALESCE(max(cas_dva_tydny), 0) AS cas_dva_tydny, COALESCE(max(cas_celkem), 0) AS cas_celkem, COALESCE(max(napsanych_pismen_dnes), 0) AS napsanych_pismen_dnes, COALESCE( max(napsanych_pismen_dva_tydny), 0 ) AS napsanych_pismen_dva_tydny, COALESCE( max(napsanych_pismen_celkem), 0 ) AS napsanych_pismen_celkem FROM soucty_pismenek, vsechny_za_dva_tydny, soucty_dnes, soucty_dva_tydny, soucty_celkem;`, uzivID).Scan(&rychlost, &presnost, &chybyPismenkaJsonb, &cas[0], &cas[1], &cas[2], &napsanychPismen[0], &napsanychPismen[1], &napsanychPismen[2])
if err != nil {
return presnost, rychlost, chybyPismenka, cas, napsanychPismen, err
}

// čas
err = DB.QueryRow(`SELECT COALESCE(SUM(cas), 0) AS cas FROM ( SELECT cas FROM dokoncene WHERE uziv_id = $1 UNION ALL SELECT cas FROM dokoncene_procvic WHERE uziv_id = $1 );`, uzivID).Scan(&casCelkem)
err = json.Unmarshal(chybyPismenkaJsonb, &chybyPismenka)
if err != nil {
return presnost, cpm, chybyPismenka, casCelkem, err
return presnost, rychlost, chybyPismenka, cas, napsanychPismen, err
}

return presnost, cpm, chybyPismenka, casCelkem, nil
return presnost, rychlost, chybyPismenka, cas, napsanychPismen, nil
}

func GetUdajeProGraf(uzivID uint) ([13]float32, [13]float32, error) {
Expand All @@ -527,7 +492,7 @@ func GetUdajeProGraf(uzivID uint) ([13]float32, [13]float32, error) {

var rows *sql.Rows
var err error
rows, err = DB.Query(`WITH dny AS ( SELECT CURRENT_DATE - gs.n AS datum FROM generate_series(0, 12, 1) AS gs (n) ), vsechny_zaznamy AS ( SELECT neopravene, delka_textu, cas, datum, ( SELECT SUM(value::NUMERIC) FROM jsonb_each_text(chyby_pismenka) ) AS opravene FROM dokoncene WHERE uziv_id = $1 AND datum::date > CURRENT_DATE - MAKE_INTERVAL(days => 13) UNION SELECT neopravene, delka_textu, cas, datum, ( SELECT SUM(value::NUMERIC) FROM jsonb_each_text(chyby_pismenka) ) AS opravene FROM dokoncene_procvic WHERE uziv_id = $1 AND datum::date > CURRENT_DATE - MAKE_INTERVAL(days => 13) ), vypocteny_dny AS ( SELECT datum::date, GREATEST( ( ( SUM(delka_textu) - 10 * SUM(neopravene) ) / SUM(cas) ) * 60, 0 ) AS rychlost, COALESCE( ( ( SUM(delka_textu) - SUM(neopravene) - COALESCE(SUM(opravene), 0) ) / SUM(delka_textu)::FLOAT ) * 100, -1 ) AS presnost FROM vsechny_zaznamy GROUP BY datum::date ) SELECT dny.datum, COALESCE(vypocteny_dny.rychlost, -1) AS rychlost, COALESCE(vypocteny_dny.presnost, -1) AS presnost FROM dny LEFT JOIN vypocteny_dny ON dny.datum = vypocteny_dny.datum ORDER BY dny.datum;`, uzivID)
rows, err = DB.Query(`WITH dny AS ( SELECT CURRENT_DATE - gs.n AS datum FROM generate_series(0, 12, 1) AS gs (n) ), vsechny_zaznamy AS ( SELECT neopravene, delka_textu, cas, datum, ( SELECT SUM(value::NUMERIC) FROM jsonb_each_text(chyby_pismenka) ) AS opravene FROM dokoncene WHERE uziv_id = $1 AND datum::date > CURRENT_DATE - MAKE_INTERVAL(days => 13) UNION SELECT neopravene, delka_textu, cas, datum, ( SELECT SUM(value::NUMERIC) FROM jsonb_each_text(chyby_pismenka) ) AS opravene FROM dokoncene_procvic WHERE uziv_id = $1 AND datum::date > CURRENT_DATE - MAKE_INTERVAL(days => 13) ), vypocteny_dny AS ( SELECT datum::date, GREATEST( ( ( SUM(delka_textu) - 10 * SUM(neopravene) ) / SUM(cas)::NUMERIC ) * 60, 0 ) AS rychlost, COALESCE( ( ( SUM(delka_textu) - SUM(neopravene) - COALESCE(SUM(opravene), 0) ) / SUM(delka_textu)::NUMERIC ) * 100, -1 ) AS presnost FROM vsechny_zaznamy GROUP BY datum::date ) SELECT dny.datum, COALESCE(vypocteny_dny.rychlost, -1) AS rychlost, COALESCE(vypocteny_dny.presnost, -1) AS presnost FROM dny LEFT JOIN vypocteny_dny ON dny.datum = vypocteny_dny.datum ORDER BY dny.datum;`, uzivID)
if err != nil {
return rychlosti, presnosti, err
}
Expand All @@ -546,7 +511,6 @@ func GetUdajeProGraf(uzivID uint) ([13]float32, [13]float32, error) {
rychlosti[i] = rychlost
presnosti[i] = presnost
}
/* log.Println(rychlosti, presnosti) */
return rychlosti, presnosti, nil
}

Expand All @@ -572,7 +536,7 @@ func CreateUziv(email string, hesloHash string, jmeno string) (uint, error) {
return uzivID, nil
}

func PridatDokonceneCvic(cvicID, uzivID uint, neopravene int, cas float32, delkaTextu int, chybyPismenka map[string]int) error {
func PridatDokonceneCvic(cvicID, uzivID uint, neopravene int, cas int, delkaTextu int, chybyPismenka map[string]int) error {
chybyPismenkaJSON, err := json.Marshal(chybyPismenka)
if err != nil {
return errors.New("konverze mapy chyb na json se nepovedla")
Expand All @@ -581,7 +545,7 @@ func PridatDokonceneCvic(cvicID, uzivID uint, neopravene int, cas float32, delka
return err
}

func PridatDokonceneProcvic(procvicID, uzivID uint, neopravene int, cas float32, delkaTextu int, chybyPismenka map[string]int) error {
func PridatDokonceneProcvic(procvicID, uzivID uint, neopravene int, cas int, delkaTextu int, chybyPismenka map[string]int) error {
chybyPismenkaJSON, err := json.Marshal(chybyPismenka)

// pokud je procvic 0 neboli je to test psaní, vložim NULL
Expand Down Expand Up @@ -1022,7 +986,7 @@ func GetPrace(praceID, studentID uint) (string, int, error) {
return text, cas, err
}

func DokoncitPraci(praceID, studentID uint, neopravene int, cas float32, delkaTextu int, chybyPismenka map[string]int) (int, error) {
func DokoncitPraci(praceID, studentID uint, neopravene int, cas int, delkaTextu int, chybyPismenka map[string]int) (int, error) {
chybyPismenkaJSON, err := json.Marshal(chybyPismenka)
if err != nil {
return 0, err
Expand Down
7 changes: 4 additions & 3 deletions backend/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
type (
bodyDokoncit struct {
Preklepy int `json:"neopravenePreklepy" validate:"min=0"` //sus reqired nebere nulu takze min=0 asi ok
Cas float32 `json:"cas" validate:"required"`
Cas int `json:"cas" validate:"required"`
DelkaTextu int `json:"delkaTextu" validate:"required"`
NejcastejsiChyby map[string]int `json:"nejcastejsiChyby" validate:"required"`
}
Expand Down Expand Up @@ -641,7 +641,7 @@ func statistiky(c echo.Context) error {
log.Print(err)
return c.JSON(http.StatusInternalServerError, chyba(""))
}
presnost, cpm, chybyPismenka, cas, err := databaze.GetUdaje(id)
presnost, rychlost, chybyPismenka, cas, napsanychPismen, err := databaze.GetUdaje(id)
if err != nil {
log.Print(err)
return c.JSON(http.StatusInternalServerError, chyba(""))
Expand All @@ -660,9 +660,10 @@ func statistiky(c echo.Context) error {
"daystreak": daystreak,
"postupVKurzu": dokonceno,
"uspesnost": presnost,
"rychlost": utils.Prumer(cpm),
"rychlost": rychlost,
"cas": cas,
"nejcastejsiChyby": chybyPismenka,
"napsanychPismen": napsanychPismen,
"rychlosti": rychlosti,
"presnosti": presnosti,
})
Expand Down
4 changes: 2 additions & 2 deletions backend/skola.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ func student(c echo.Context) error {
log.Print(err)
return c.JSON(http.StatusInternalServerError, chyba(""))
}
presnost, cpm, chybyPismenka, _, err := databaze.GetUdaje(student.ID)
presnost, rychlost, chybyPismenka, _, _, err := databaze.GetUdaje(student.ID)
if err != nil {
log.Print(err)
return c.JSON(http.StatusInternalServerError, chyba(""))
Expand All @@ -416,7 +416,7 @@ func student(c echo.Context) error {
"jmeno": student.SkolniJmeno,
"daystreak": daystreak,
"uspesnost": presnost,
"rychlost": utils.Prumer(cpm),
"rychlost": rychlost,
"dokonceno": dokonceno,
"nejcastejsiChyby": chybyPismenka,
"klavesnice": student.Klavesnice,
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/AnimaceCisla.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const props = defineProps({
desetinaMista: {
type: Number,
default: 1
},
}
})
const zobrazeneCislo = ref("0")
Expand Down
10 changes: 8 additions & 2 deletions frontend/src/components/GrafStatistiky.vue
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,12 @@ onMounted(() => {
},
ticks: {
count: 4,
precision: -1,
color: color,
padding: 6,
callback: function (value) {
if (typeof value == 'string') return value
return value.toFixed(0)
}
},
border: {
display: false
Expand All @@ -144,9 +147,12 @@ onMounted(() => {
},
ticks: {
count: 4,
precision: 0,
color: color,
padding: 4,
callback: function (value) {
if (typeof value == 'string') return value
return value.toFixed(1)
}
},
border: {
display: false
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/Vysledek.vue
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ function e1(e: KeyboardEvent) {
<div id="bloky">
<div class="blok">
<Tooltip
zprava="Za neopravené chyby je adekvátní penalizace. Chybu opravíš pomocí klávesy <span class='klavesa-v-textu-mensi'>Backspace</span>"
zprava="Za <b>neopravené</b> chyby je adekvátní <b>penalizace</b>. Chybu opravíš pomocí klávesy <span class='klavesa-v-textu-mensi'>Backspace</span>"
:sirka="180" :vzdalenost="6">
<AnimaceCisla class="cislo" :cislo="rychlost > 0 ? Math.round(rychlost * 10) / 10 : 0" :desetina-mista="0" />
</Tooltip>
Expand Down
31 changes: 21 additions & 10 deletions frontend/src/views/Statistiky.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ useHead({
title: "Statistiky"
})
const info = ref({ rychlost: -1, cas: 0, cas1: 0, cas14: 0, napsanychPismen: 0, napsanychPismen1: 0, napsanychPismen14: 0, uspesnost: -1, postupVKurzu: 0, daystreak: 0, nejcastejsiChyby: new Map<string, number>(), rychlosti: [] as number[], presnosti: [] as number[] })
const info = ref({ rychlost: -1, cas: [] as number[], napsanychPismen: [] as number[], uspesnost: -1, postupVKurzu: 0, daystreak: 0, nejcastejsiChyby: new Map<string, number>(), rychlosti: [] as number[], presnosti: [] as number[] })
const nejcastejsiChyby = ref([] as { znak: string, pocet: number }[])
const cas = ref(0)
const napsanychPismen = ref(0)
Expand Down Expand Up @@ -58,14 +58,14 @@ function zaokrouhlit(cislo: number | null) {
function prepnoutStatistiky() {
if (prepinacTabu.value?.tab == "dnes") {
cas.value = info.value["cas1"]
napsanychPismen.value = info.value["napsanychPismen1"]
cas.value = info.value.cas[0]
napsanychPismen.value = info.value.napsanychPismen[0]
} else if (prepinacTabu.value?.tab == "dva tydny") {
cas.value = info.value["cas14"]
napsanychPismen.value = info.value["napsanychPismen14"]
cas.value = info.value.cas[1]
napsanychPismen.value = info.value.napsanychPismen[1]
} else if (prepinacTabu.value?.tab == "celkem") {
cas.value = info.value["cas"]
napsanychPismen.value = info.value["napsanychPismen"]
cas.value = info.value.cas[2]
napsanychPismen.value = info.value.napsanychPismen[2]
}
console.log("prepinam")
}
Expand Down Expand Up @@ -172,17 +172,27 @@ onMounted(() => {
</span>
</div>
<div class="blok">
<img src="../assets/icony/cas.svg" alt="Čas" width="68">
<div id="fake-obrazek">
aA
</div>
<span class="popis">
Napsaných písmen: <br>
<span>
<AnimaceCisla class="cislo" :cislo="napsanychPismen" :desetina-mista="0" /> h
<span v-if="napsanychPismen != 0">
<AnimaceCisla class="cislo" :cislo="napsanychPismen >= 10000 ? napsanychPismen / 1000 : napsanychPismen" :desetina-mista="0" />
<span v-if="napsanychPismen >= 10000"> tis.</span>
</span>
<span v-else class="nic">Zatím nic</span>
</span>
</div>
</div>
</template>
<style scoped>
.blok>#fake-obrazek {
font-size: 55px;
font-weight: 700;
user-select: none;
}
#graf {
grid-column-start: 1;
grid-column-end: 3;
Expand All @@ -209,6 +219,7 @@ onMounted(() => {
.nic {
font-weight: 600;
line-height: 49px;
}
#pismena {
Expand Down

0 comments on commit c06080a

Please sign in to comment.