The Swarm

mail@pastecode.io avatar
unknown
golang
25 days ago
3.7 kB
2
Indexable
Never
package main

import (
	"encoding/json"
	"fmt"
	"os"
	"slices"
	"sort"
	"time"
)

// Główna ścieżka do folderu z danymi
const mainPath = "C:\\Users\\ojpkm\\documents\\InterviewProject\\"

func main() {

	var path = fmt.Sprint(mainPath + "data.json")
	records := readData(path)

	SortNames(records)
	SortCompanies(records)
	BiggestCompany(records)
	MeasureWorkingTime(records)

	SaveData(records)

}

// Customowy typ do parsowania daty na odpowiedni format
type CustomTime struct {
	*time.Time
}

// Metoda parsująca
func (t *CustomTime) UnmarshalJSON(b []byte) error {

	formattedTime, err := time.Parse(`"2006-01-02"`, string(b))
	t.Time = &formattedTime

	return err
}

// Struct trzymający dane z JSON'a
type Records struct {
	Name      string      `json:"name"`
	Company   string      `json:"company"`
	JobTitle  string      `json:"job_title"`
	StartedAt *CustomTime `json:"started_at"`
	EndedAt   *CustomTime `json:"ended_at,omitempty"`
}

// Odczyt danych z pliku JSON
func readData(path string) []Records {

	jsonFile, err := os.Open(path)
	if err != nil {
		fmt.Printf("err: %v\n", err)
	}
	defer jsonFile.Close()

	records := []Records{}

	jsonDec := json.NewDecoder(jsonFile)
	if err != nil {
		fmt.Printf("err: %v\n", err)
	}

	for jsonDec.More() {

		user := Records{}

		err = jsonDec.Decode(&user)
		if err != nil {
			fmt.Printf("err: %v\n", err)
		}

		records = append(records, user)
	}

	return records
}

// Alfabetyczne sortowanie nazwisk
func SortNames(records []Records) []string {
	names := []string{}
	for _, v := range records {

		if !slices.Contains(names, v.Name) {
			names = append(names, v.Name)

		}

	}
	sort.Strings(names)
	return names
}

// Alfabetyczne sortowanie firm
func SortCompanies(records []Records) []string {

	companies := []string{}
	for _, v := range records {

		if !slices.Contains(companies, v.Company) {
			companies = append(companies, v.Company)
		}

	}
	sort.Strings(companies)
	return companies
}

// Firma z największą ilością pracowników
func BiggestCompany(records []Records) string {

	someMap := map[string][]string{}

	for _, v := range records {
		if !slices.Contains(someMap[v.Company], v.Name) {
			someMap[v.Company] = append(someMap[v.Company], v.Name)
		}
	}

	var largestCompany string
	employeesAmount := 0

	for company, employees := range someMap {
		numEmployees := len(employees)
		if numEmployees > employeesAmount {
			employeesAmount = numEmployees
			largestCompany = company
		}
	}

	return largestCompany
}

// Funkcja zwracająca nazwisko pracownika z największą ilością przepracowanego czasu
func MeasureWorkingTime(records []Records) string {

	workingTime := make(map[string]time.Duration)

	for _, v := range records {

		if v.EndedAt == nil {
			now := time.Now()
			v.EndedAt = &CustomTime{Time: &now}
		}

		duration := v.EndedAt.Sub(*v.StartedAt.Time)

		if v.EndedAt.IsZero() {
			duration = time.Since(*v.StartedAt.Time)
		}

		workingTime[v.Name] += duration
	}

	var name string
	var longest time.Duration
	for i, v := range workingTime {
		if v > longest {
			name = i
			longest = v
		}
	}

	return name
}

// Zapis danych w pliku jsonl
func SaveData(records []Records) error {

	file, err := os.Create("output.jsonl")
	if err != nil {
		fmt.Printf("err: %v\n", err)
	}
	defer file.Close()

	for _, v := range records {

		json, err := json.Marshal(v)
		if err != nil {
			fmt.Printf("err: %v\n", err)
		}

		_, err = file.Write(append(json, '\n'))
		if err != nil {
			fmt.Printf("err: %v\n", err)
		}
	}

	return err
}
Leave a Comment