<commit_before>package main

import (
	"compress/gzip"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"sort"
	"strconv"
	"strings"
	"time"

	"github.com/gorilla/mux"
)

// Generic structure which should fit any notification to be sent the user
type gnDiffDatum []*gnDiffData

type gnDiffData struct {
	Repo    link       `json:"repo"`
	Changed bool       `json:"changed"`
	Data    []diffData `json:"data"`
	MadeFor string     `json:"made_for"`
}

type diffData struct {
	Title      link   `json:"title"`
	Error      string `json:"error"`
	ChangeType string `json:"change_type"`
	Changed    bool   `json:"changed"`
	Changes    []link `json:"changes"`
}

type link struct {
	Text  string `json:"text"`
	Href  string `json:"href"`
	Title string `json:"title"`
}

func listAllDiffs(w http.ResponseWriter, r *http.Request) {

	hc := &httpContext{w, r}
	// Redirect user if not logged in
	redirected := hc.redirectUnlessLoggedIn()
	if redirected {
		return
	}

	userInfo := hc.userLoggedinInfo()
	configFile := userInfo.getConfigFile()

	conf := new(Setting)
	conf.load(configFile)
	files := (&gnDiffDatum{}).ListUserChanges(conf)

	page := newPage(hc, "Changes for "+conf.Auth.UserName, "Recent Changes", files, nil)
	displayPage(w, "changes_list", page)

	// TODO render in Page
}

func renderThisDiff(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	entry := vars["diffentry"]
	if entry == "" {
		listAllDiffs(w, r)
		return
	}

	hc := &httpContext{w, r}
	// Redirect user if not logged in
	redirected := hc.redirectUnlessLoggedIn()
	if redirected {
		return
	}

	userInfo := hc.userLoggedinInfo()
	configFile := userInfo.getConfigFile()

	conf := new(Setting)
	conf.load(configFile)

	diffs := &gnDiffDatum{}
	if err := diffs.load(entry, conf); err != nil {
		http.NotFound(w, r)
		return
	}

	intFilename, _ := strconv.ParseInt(entry, 10, 64)
	reference := parseUnixTimeToString(intFilename, "02 Jan 2006 | 15 Hrs", conf.User.TimeZoneName)

	title := "New Updates from " + reference
	page := newPage(hc, "Changes for "+entry, title, diffs, nil)
	displayPage(w, "changes", page)
}

// check if atleast one of the diffs has changed
func (r *gnDiffDatum) hasChanges() bool {
	// check if eligible to send email
	for _, a := range *r {
		if a.Changed {
			return true
		}
	}
	return false
}

type changeDetail struct {
	Display   string
	Reference int64
}

type ByInt []*changeDetail

func (a ByInt) Len() int           { return len(a) }
func (a ByInt) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a ByInt) Less(i, j int) bool { return a[i].Display < a[j].Display }

// at a user/repo level
func (r *gnDiffDatum) ListUserChanges(conf *Setting) []*changeDetail {
	dir := strings.Join([]string{conf.Auth.getConfigDir(), "diff"}, string(os.PathSeparator))

	fis, err := ioutil.ReadDir(dir)
	if err != nil {
		log.Print(err)<commit_msg>sort by time instead of filename<commit_after>package main

import (
	"compress/gzip"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"sort"
	"strconv"
	"strings"
	"time"

	"github.com/gorilla/mux"
)

// Generic structure which should fit any notification to be sent the user
type gnDiffDatum []*gnDiffData

type gnDiffData struct {
	Repo    link       `json:"repo"`
	Changed bool       `json:"changed"`
	Data    []diffData `json:"data"`
	MadeFor string     `json:"made_for"`
}

type diffData struct {
	Title      link   `json:"title"`
	Error      string `json:"error"`
	ChangeType string `json:"change_type"`
	Changed    bool   `json:"changed"`
	Changes    []link `json:"changes"`
}

type link struct {
	Text  string `json:"text"`
	Href  string `json:"href"`
	Title string `json:"title"`
}

func listAllDiffs(w http.ResponseWriter, r *http.Request) {

	hc := &httpContext{w, r}
	// Redirect user if not logged in
	redirected := hc.redirectUnlessLoggedIn()
	if redirected {
		return
	}

	userInfo := hc.userLoggedinInfo()
	configFile := userInfo.getConfigFile()

	conf := new(Setting)
	conf.load(configFile)
	files := (&gnDiffDatum{}).ListUserChanges(conf)

	page := newPage(hc, "Changes for "+conf.Auth.UserName, "Recent Changes", files, nil)
	displayPage(w, "changes_list", page)

	// TODO render in Page
}

func renderThisDiff(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	entry := vars["diffentry"]
	if entry == "" {
		listAllDiffs(w, r)
		return
	}

	hc := &httpContext{w, r}
	// Redirect user if not logged in
	redirected := hc.redirectUnlessLoggedIn()
	if redirected {
		return
	}

	userInfo := hc.userLoggedinInfo()
	configFile := userInfo.getConfigFile()

	conf := new(Setting)
	conf.load(configFile)

	diffs := &gnDiffDatum{}
	if err := diffs.load(entry, conf); err != nil {
		http.NotFound(w, r)
		return
	}

	intFilename, _ := strconv.ParseInt(entry, 10, 64)
	reference := parseUnixTimeToString(intFilename, "02 Jan 2006 | 15 Hrs", conf.User.TimeZoneName)

	title := "New Updates from " + reference
	page := newPage(hc, "Changes for "+entry, title, diffs, nil)
	displayPage(w, "changes", page)
}

// check if atleast one of the diffs has changed
func (r *gnDiffDatum) hasChanges() bool {
	// check if eligible to send email
	for _, a := range *r {
		if a.Changed {
			return true
		}
	}
	return false
}

type changeDetail struct {
	Display   string
	Reference int64
}

type ByInt []*changeDetail

func (a ByInt) Len() int           { return len(a) }
func (a ByInt) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a ByInt) Less(i, j int) bool { return a[i].Reference < a[j].Reference }

// at a user/repo level
func (r *gnDiffDatum) ListUserChanges(conf *Setting) []*changeDetail {
	dir := strings.Join([]string{conf.Auth.getConfigDir(), "diff"}, string(os.PathSeparator))

	fis, err := ioutil.ReadDir(dir)
	if err != nil {
		log.Print(err)
{'fraction_non_alphanumeric': 0.1441723119680389, 'fraction_numerical': 0.004863644259162758, 'mean_word_length': 3.9129692832764507, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4897099', 'n_tokens_mistral': 2150, 'n_tokens_neox': 2068, 'n_words': 566}