This commit is contained in:
a-ill 2023-07-22 13:22:35 +03:00
parent 117e924f66
commit 2c80026aba
11 changed files with 197 additions and 85 deletions

View File

@ -213,33 +213,7 @@ end
function groups_add_post()
data = jsonpayload()
insert_into_table("groups",data)
insert_into_table("groups_requests",data)
end
#=
function compile_groups()
function table_to_json(name,t)
ar = []
for df_row in eachrow(t)
df_row = first(eachrow(df))
id = :town
location = String[]
for id in [:country,:state,:town]
if !isempty(df_row[id])
push!(location,df_row[id])
end
end
df = select_from_table(["groups" => ["*"]])
dict = Dict(
"location" => [location,[df_row[:latitude],df_row[:longitude]]],
"members" => df_row[:members],
"contact" => df_row[:contact]
)
end
return ar
end
df = select_from_table(["groups" => ["*"]])
end
=#
end

View File

@ -1,3 +1,4 @@
/*
export let groups = [
{
location: [["Bulgaria","Varna"],[43.21582161671174, 27.89896092161012]],
@ -85,17 +86,7 @@ export let groups = [
contact: ["https://discord.gg/Qk8KUk787z","DiscordInviteLink"]
}
]
export let groupsByCountry = {}
for (let g of groups) {
let country = g.location[0][0]
if (country in groupsByCountry) {
groupsByCountry[country].push(g)
}
else {
groupsByCountry[country] = [g]
}
}
*/
export let groupsMarkersLayer = L.layerGroup()
let groupsMarkersLayerOut = L.layerGroup()
@ -116,13 +107,14 @@ export function translate(content, x) {
function addMarkersToLayer(g,layer,content,locale) {
let coordinates
let text = "<b>"+content["Group"]+"</b><br>"
for (let field in g) {
for (let field of ["location","members","contact"]) {
let fieldText = content[field] + ": "
if (field=="contact") {
text += fieldText + "<a href='" + g.contact[0] + "' target='_blank' rel=noreferrer>" + content[g.contact[1]] + "</a>"
text += fieldText + "<a href='" + g.contact + "' target='_blank' rel=noreferrer>" + g.contact + "</a>"
}
else if (field=="location") {
let location = g[field][0]
let location = [g.country,g.state,g.town].filter(x => x!=null && x!=undefined)
let locationString
if (locale=="en") {
locationString = location.map(x => x).join(", ")
@ -131,7 +123,7 @@ function addMarkersToLayer(g,layer,content,locale) {
locationString = location.map(x => translate(content, x)).join(", ")
}
text += fieldText + locationString + "<br>"
coordinates = g[field][1]
coordinates = [g.latitude,g.longitude]
}
else {
text += fieldText + g[field] + "<br>"
@ -145,28 +137,29 @@ function addMarkersToLayer(g,layer,content,locale) {
popupAnchor: [1, -34],
shadowSize: [41, 41]
})
//console.log(text)
let marker = L.marker(coordinates, {icon: markerIcon})
marker.addTo(layer).bindPopup(text)
}
export function addMarkersGroups(map,content,locale) {
export function addMarkersGroups(groups,groupsByCountry,map,content,locale) {
for (let g of groups) {
addMarkersToLayer(g,groupsMarkersLayerIn,content,locale)
}
for (let gs of Object.values(groupsByCountry)) {
if (gs.length==1) {
let g = {...gs[0]}
g.location[0] = [g.location[0][0]]
g.country = [g.country]
addMarkersToLayer(g,groupsMarkersLayerOut,content,locale)
}
else {
let locationName = [gs[0].location[0][0]]
let locationName = gs[0].country
let locationCoordinates = [0,0]
let members = 0
let contact = gs[0].contact
for (let g of gs) {
locationCoordinates[0] += g.location[1][0]
locationCoordinates[1] += g.location[1][1]
locationCoordinates[0] += g.latitude
locationCoordinates[1] += g.longitude
members += g.members
if (g.contact[0]!=gs[0].contact[0]) {
contact = contactGeneral
@ -175,13 +168,16 @@ export function addMarkersGroups(map,content,locale) {
locationCoordinates[0] = locationCoordinates[0]/gs.length
locationCoordinates[1] = locationCoordinates[1]/gs.length
let gNew = {
location: [locationName,locationCoordinates],
country: locationName,
latitude: locationCoordinates[0],
longitude: locationCoordinates[1],
members: members,
contact: contact
}
addMarkersToLayer(gNew,groupsMarkersLayerOut,content,locale)
}
}
groupsMarkersLayerOut.addTo(groupsMarkersLayer)
groupsMarkersLayer.addTo(map)
map.on("zoomend", () => onZoomEnd(map))

View File

@ -4,7 +4,7 @@
// Import statements
import { onMount } from 'svelte'
import { writable } from 'svelte/store';
import { groupsByCountry, addMarkersGroups } from '/js/groups.js'
import { addMarkersGroups, translate } from '/js/groups.js'
import { loadLocaleContent, getData, sendData } from "/js/libraries/serverTools.js"
// Import components
@ -13,6 +13,30 @@
// Main code
let loaded = writable(0)
let content = writable({})
let groups
let groupsByCountry
let callback = (response) => {
groups = JSON.parse(response)
groupsByCountry = {}
for (let g of groups) {
let country = g.country
if (g.contact==null) {
g.contact = "https://discord.gg/Qk8KUk787z"
}
if (country in groupsByCountry) {
groupsByCountry[country].push(g)
}
else {
groupsByCountry[country] = [g]
}
}
loaded.update((val) => {
return val + 1
})
}
getData("/assets/groups.json",callback)
let confirmationMsg
let addressInput
@ -76,7 +100,7 @@
function mapCallbackGroups(createMap,content,locale) {
let map = createMap([22, 0],2)
addMarkersGroups(map,content,locale)
addMarkersGroups(groups,groupsByCountry,map,content,locale)
userPin.addTo(map)
map.on('click', function(event) {
@ -111,8 +135,7 @@
town: addressVec[2],
latitude: userPinLat,
longitude: userPinLng,
contact: contactInput.value,
members: 1
contact: contactInput.value
}
if (data.state=="") {
@ -139,7 +162,7 @@
</script>
{#key $loaded}
{#if $loaded==2}
{#if $loaded==3}
<div id="container">
<!--<img src="img/crowd.png" id="crowd" alt="crowd">-->
<div id="text-container">

View File

@ -4,7 +4,7 @@
// Import statements
import { onMount } from 'svelte'
import { writable } from 'svelte/store';
import { groupsByCountry, addMarkersGroups, translate } from '/js/groups.js'
import { addMarkersGroups, translate } from '/js/groups.js'
import { loadLocaleContent, getData, sendData } from "/js/libraries/serverTools.js"
// Import components
@ -13,21 +13,47 @@
// Main code
let loaded = writable(0)
let content = writable({})
let groups
let groupsByCountry
let locale = loadLocaleContent(content,"groups-component",loaded)
loadLocaleContent(content,"countries",loaded)
let callback = (response) => {
groups = JSON.parse(response)
groupsByCountry = {}
for (let g of groups) {
let country = g.country
if (g.contact==null) {
g.contact = "https://discord.gg/Qk8KUk787z"
}
if (country in groupsByCountry) {
groupsByCountry[country].push(g)
}
else {
groupsByCountry[country] = [g]
}
}
loaded.update((val) => {
return val + 1
})
}
getData("/assets/groups.json",callback)
function mapCallbackGroups(createMap,content,locale) {
let map = createMap([22, 0],2)
addMarkersGroups(map,content,locale)
addMarkersGroups(groups,groupsByCountry,map,content,locale)
}
function getCountry(x) {
return locale=="en" ? x : translate($content,x)
}
function getAddress(group) {
return group.location[0].map(x => locale=="en" ? x : translate($content,x)).join(", ")
function getAddress(g) {
let location = [g.country,g.state,g.town].filter(x => x!=null)
return location.map(x => locale=="en" ? x : translate($content,x)).join(", ")
}
onMount(() => {
@ -36,7 +62,7 @@
</script>
{#key $loaded}
{#if $loaded==2}
{#if $loaded==3}
<div id="container">
<!--<img src="img/crowd.png" id="crowd" alt="crowd">-->
<div id="text-container">
@ -53,7 +79,7 @@
<div class="location-info">
<p><b>{$content.location}: </b>{getAddress(group)}</p>
<p><b>{$content.members}: </b>{group.members}</p>
<p><b>{$content.contact}: </b><a href={group.contact[0]} target=;_blank; rel=noreferrer>{$content[group.contact[1]]}</a></p>
<p><b>{$content.contact}: </b><a href={group.contact} target=;_blank; rel=noreferrer>{group.contact}</a></p>
</div>
{/each}
</div>

View File

@ -0,0 +1,32 @@
module CreateTableGroupsRequests
import SearchLight.Migrations: create_table, column, primary_key, add_index, drop_table
include("../../lib/DatabaseSupport.jl")
using .DatabaseSupport
function up()
create_table(:groups_requests) do
[
primary_key()
column(:id_given, :integer)
column(:country, :string)
column(:state, :string)
column(:town, :string)
column(:contact, :string)
column(:latitude, :float)
column(:longitude, :float)
column(:verified, :bool)
column(:added, :bool)
]
end
set_default("groups_requests","verified",false)
set_default("groups_requests","added",false)
end
function down()
drop_table(:groups)
end
end

View File

@ -4,7 +4,7 @@ module DatabaseSupport
using SearchLight, SearchLightPostgreSQL, LibPQ
using DataFrames
export exist_in_table, insert_into_table, update_table, select_from_table, add_foreign_key
export exist_in_table, insert_into_table, update_table, select_from_table, add_foreign_key, set_default
options = SearchLight.Configuration.read_db_connection_data("db/connection.yml")
conn = SearchLight.connect(options)
@ -12,7 +12,7 @@ conn = SearchLight.connect(options)
function format(x)
if (x isa String) || (x isa Symbol)
return string("'",x,"'")
elseif (isnothing(x))
elseif (isnothing(x) || ismissing(x))
return "NULL"
else
return x
@ -33,7 +33,7 @@ end
function update_table(table_name,dict_values; where_data=nothing)
ns = collect(keys(dict_values))
vals_raw = values(dict_values)
vals = map(x -> x isa String ? string("'",x,"'") : x,vals_raw)
vals = map(x -> format(x),vals_raw)
ns_vals = join(map((x,y) -> string(x, " = ",y),ns,vals),", ")
query = "UPDATE $table_name SET $ns_vals"
@ -144,4 +144,11 @@ function add_foreign_key(table,name,table2,name2)
SearchLight.query(query)
end
function set_default(table,column,value)
query = """
ALTER TABLE $table
ALTER COLUMN $column SET DEFAULT $value ;"""
SearchLight.query(query)
end
end

View File

@ -0,0 +1,57 @@
using SearchLight, SearchLightPostgreSQL, LibPQ, JSON3
using DataFrames
include("../lib/DatabaseSupport.jl")
using .DatabaseSupport
function table_to_json(name,df)
ar = []
for df_row in eachrow(df)
dict = Dict()
for id in names(df_row)
dict[id] = df_row[id]
end
push!(ar,dict)
end
open("public/assets/"*name*".json", "w") do io
JSON3.write(io, ar)
end
end
function compile_groups()
df = select_from_table(["groups" => ["*"]])
table_to_json("groups",df)
end
function move_requests()
df_requests = select_from_table(["groups_requests" => ["*"]], where_data=["verified" => true, "added" => false])
df = select_from_table(["groups" => ["*"]])
latitudes = df.latitude
longitudes = df.longitude
for df_row in eachrow(df_requests)
ind_id_given = ismissing(df_row.id_given) ? nothing : findfirst(df_row.id_given.==df.id)
if (!isnothing(ind_id_given))
id = df[ind_id_given,:id]
row_found = df[ind_id_given,Not(:id)]
dict = Dict(zip(names(row_found),values(row_found)))
dict["members"] += 1
update_table("groups",dict, where_data=["id" => id])
else
id = df_row.id
dict_update = Dict("added" => true)
update_table("groups_requests",dict_update, where_data=["id" => id])
df_row_to_add = df_row[Not(:id_given)]
df_row_to_add = df_row_to_add[Not(:verified)]
df_row_to_add = df_row_to_add[Not(:added)]
df_row_to_add = df_row_to_add[Not(:id)]
dict = Dict(zip(names(df_row_to_add),values(df_row_to_add)))
dict["members"] = 1
insert_into_table("groups",dict)
end
end
end
move_requests()
compile_groups()

View File

@ -0,0 +1 @@
[{"town":"Atlanta","contact":null,"latitude":33.7243396617476,"longitude":-84.39697265625,"id":9,"members":1,"country":"United States","state":"Georgia"},{"town":null,"contact":null,"latitude":39.98855476000615,"longitude":-105.2105712890625,"id":10,"members":1,"country":"United States","state":"Colorado"},{"town":null,"contact":null,"latitude":28.27955105276024,"longitude":-81.47460937500001,"id":11,"members":1,"country":"United States","state":"Florida"},{"town":"Dublin","contact":null,"latitude":40.13360099478965,"longitude":-83.10607910156251,"id":12,"members":1,"country":"United States","state":"Ohio"},{"town":"Toronto","contact":null,"latitude":43.68959002213805,"longitude":-79.36523437500001,"id":13,"members":1,"country":"Canada","state":"Ontario"},{"town":"Halifax","contact":null,"latitude":44.64996307546047,"longitude":-63.60809326171876,"id":14,"members":1,"country":"Canada","state":"Nova Scotia"},{"town":null,"contact":null,"latitude":53.353612430518126,"longitude":-8.085937500000002,"id":15,"members":1,"country":"Ireland","state":null},{"town":"Cham","contact":null,"latitude":47.18444711300418,"longitude":8.461189270019533,"id":16,"members":1,"country":"Switzerland","state":"Zug"},{"town":"Wiesbaden","contact":null,"latitude":50.085975903187155,"longitude":8.240432739257814,"id":17,"members":1,"country":"Germany","state":"Hesse"},{"town":"Copenhagen","contact":null,"latitude":55.68832070332783,"longitude":12.568359375000002,"id":18,"members":1,"country":"Denmark","state":"Capital Region of Denmark"},{"town":"Kolding","contact":null,"latitude":55.5095568556412,"longitude":9.486694335937502,"id":19,"members":1,"country":"Denmark","state":null},{"town":"Municipal Unit of Moschato","contact":null,"latitude":37.950275539773436,"longitude":23.673992156982425,"id":20,"members":1,"country":"Greece","state":"Attica"},{"town":"Varna","contact":null,"latitude":43.18381722560103,"longitude":27.905273437500004,"id":21,"members":1,"country":"Bulgaria","state":null},{"town":"Riga","contact":null,"latitude":56.966939887376796,"longitude":24.142456054687504,"id":22,"members":1,"country":"Latvia","state":"Vidzeme"},{"town":"Kohtla-Järve linn","contact":null,"latitude":59.40196127188141,"longitude":27.28042602539063,"id":23,"members":1,"country":"Estonia","state":null},{"town":"Tallinn","contact":null,"latitude":59.39656672058008,"longitude":24.72610473655427,"id":24,"members":1,"country":"Estonia","state":null},{"town":"Chiang Mai","contact":null,"latitude":18.796128352413316,"longitude":98.98753015423392,"id":25,"members":1,"country":"Thailand","state":null}]

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,3 +1,4 @@
/*
export let groups = [
{
location: [["Bulgaria","Varna"],[43.21582161671174, 27.89896092161012]],
@ -85,17 +86,7 @@ export let groups = [
contact: ["https://discord.gg/Qk8KUk787z","DiscordInviteLink"]
}
]
export let groupsByCountry = {}
for (let g of groups) {
let country = g.location[0][0]
if (country in groupsByCountry) {
groupsByCountry[country].push(g)
}
else {
groupsByCountry[country] = [g]
}
}
*/
export let groupsMarkersLayer = L.layerGroup()
let groupsMarkersLayerOut = L.layerGroup()
@ -116,13 +107,14 @@ export function translate(content, x) {
function addMarkersToLayer(g,layer,content,locale) {
let coordinates
let text = "<b>"+content["Group"]+"</b><br>"
for (let field in g) {
for (let field of ["location","members","contact"]) {
let fieldText = content[field] + ": "
if (field=="contact") {
text += fieldText + "<a href='" + g.contact[0] + "' target='_blank' rel=noreferrer>" + content[g.contact[1]] + "</a>"
text += fieldText + "<a href='" + g.contact + "' target='_blank' rel=noreferrer>" + g.contact + "</a>"
}
else if (field=="location") {
let location = g[field][0]
let location = [g.country,g.state,g.town].filter(x => x!=null && x!=undefined)
let locationString
if (locale=="en") {
locationString = location.map(x => x).join(", ")
@ -131,7 +123,7 @@ function addMarkersToLayer(g,layer,content,locale) {
locationString = location.map(x => translate(content, x)).join(", ")
}
text += fieldText + locationString + "<br>"
coordinates = g[field][1]
coordinates = [g.latitude,g.longitude]
}
else {
text += fieldText + g[field] + "<br>"
@ -145,28 +137,29 @@ function addMarkersToLayer(g,layer,content,locale) {
popupAnchor: [1, -34],
shadowSize: [41, 41]
})
//console.log(text)
let marker = L.marker(coordinates, {icon: markerIcon})
marker.addTo(layer).bindPopup(text)
}
export function addMarkersGroups(map,content,locale) {
export function addMarkersGroups(groups,groupsByCountry,map,content,locale) {
for (let g of groups) {
addMarkersToLayer(g,groupsMarkersLayerIn,content,locale)
}
for (let gs of Object.values(groupsByCountry)) {
if (gs.length==1) {
let g = {...gs[0]}
g.location[0] = [g.location[0][0]]
g.country = [g.country]
addMarkersToLayer(g,groupsMarkersLayerOut,content,locale)
}
else {
let locationName = [gs[0].location[0][0]]
let locationName = gs[0].country
let locationCoordinates = [0,0]
let members = 0
let contact = gs[0].contact
for (let g of gs) {
locationCoordinates[0] += g.location[1][0]
locationCoordinates[1] += g.location[1][1]
locationCoordinates[0] += g.latitude
locationCoordinates[1] += g.longitude
members += g.members
if (g.contact[0]!=gs[0].contact[0]) {
contact = contactGeneral
@ -175,13 +168,16 @@ export function addMarkersGroups(map,content,locale) {
locationCoordinates[0] = locationCoordinates[0]/gs.length
locationCoordinates[1] = locationCoordinates[1]/gs.length
let gNew = {
location: [locationName,locationCoordinates],
country: locationName,
latitude: locationCoordinates[0],
longitude: locationCoordinates[1],
members: members,
contact: contact
}
addMarkersToLayer(gNew,groupsMarkersLayerOut,content,locale)
}
}
groupsMarkersLayerOut.addTo(groupsMarkersLayer)
groupsMarkersLayer.addTo(map)
map.on("zoomend", () => onZoomEnd(map))