Skip to content

Commit

Permalink
revert: feat: Switching to new exas model
Browse files Browse the repository at this point in the history
This reverts commit 6f817e8.
  • Loading branch information
ViBiOh committed May 25, 2024
1 parent dc25fda commit 1ab285d
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 34 deletions.
4 changes: 0 additions & 4 deletions .prettierrc

This file was deleted.

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ In order to work, your user **must have** `admin` profile sets with the `-authPr

With help of different sidecars, Fibr can generate image, video and PDF thumbnails. These sidecars can be self hosted with ease. It can also extract and enrich content displayed by looking at [EXIF Data](https://en.wikipedia.org/wiki/Exif), also with the help of a little sidecar. These behaviours are opt-out (if you remove the `url` of the service, Fibr will do nothing).

For the last mile, Fibr can try to reverse geocoding the GPS data found in EXIF, using [Open Street Map](https://wiki.openstreetmap.org/wiki/Nominatim). Self-hosting this kind of service can be complicated and calling a third-party party with such sensible datas is an opt-in decision.

### Metrics

Fibr exposes a lot of metrics via OpenTelemetry gRPC mode. Common metrics are exposed: Golang statistics, HTTP statuses and response time, AMQP statuses and sidecars/metadatas actions.
Expand Down
11 changes: 6 additions & 5 deletions cmd/fibr/templates/exif.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ <h2 class="flex flex-center header no-margin">
</p>
{{ end }}

{{ if or .Exif.Coordinates .Exif.HasAddress }}
{{ if or .Exif.Geocode.HasAddress .Exif.Geocode.HasCoordinates }}
<p class="padding no-margin code">
{{ if .Exif.Coordinates }}
<strong>Coordinates:</strong> <a class="primary" href="http://www.openstreetmap.org/?mlat={{ index .Exif.Coordinates 0 }}&mlon={{ index .Exif.Coordinates 1 }}&zoom=16">{{ index .Exif.Coordinates 0 }},{{ index .Exif.Coordinates 1 }}</a><br>
{{ if .Exif.Geocode.HasCoordinates }}
<strong>Coordinates:</strong> <a class="primary" href="http://www.openstreetmap.org/?mlat={{ .Exif.Geocode.Latitude }}&mlon={{ .Exif.Geocode.Longitude }}&zoom=16">{{ .Exif.Geocode.Latitude }},{{ .Exif.Geocode.Longitude }}</a><br>
{{ end }}

<strong>Location</strong><br>
  <em>City:</em> {{ .Exif.GetCity }}<br>
  <em>Country:</em> {{ .Exif.GetCountry }}<br>
{{ range $key, $value := .Exif.Geocode.Address }}
  <em>{{ $key }}:</em> {{ $value }}<br>
{{ end }}
</p>
{{ end }}

Expand Down
20 changes: 14 additions & 6 deletions cmd/fibr/templates/story.html
Original file line number Diff line number Diff line change
Expand Up @@ -193,19 +193,27 @@
</a>

<div class="description relative">
{{ if or (.Exif.HasAddress) (not .Exif.Date.IsZero) }}
{{ if or (.Exif.Geocode.HasAddress) (not .Exif.Date.IsZero) }}
<p class="context">
{{ if .Exif.HasAddress }}
{{ if .Exif.Geocode.HasAddress }}
<img class="icon margin-right-half" src="{{ url "/svg/location?fill=%23000000" }}" alt="location">
<span>
{{ if .Exif.GetCity }}
{{ .Exif.GetCity }},
{{- end }} {{ .Exif.GetCountry }}
{{ if index .Exif.Geocode.Address "city_district" }}
{{ index .Exif.Geocode.Address "city_district" }},
{{ else }}
{{ if index .Exif.Geocode.Address "village" }}
{{ index .Exif.Geocode.Address "village" }},
{{ else }}
{{ if index .Exif.Geocode.Address "county" }}
{{ index .Exif.Geocode.Address "county" }},
{{- end }}
{{- end }}
{{- end }} {{ index .Exif.Geocode.Address "country" }}
{{ end }}
</span>

{{ if not .Exif.Date.IsZero }}
<img class="icon margin-right-half{{ if .Exif.HasAddress }} margin-left-half{{ end }}" src="{{ url "/svg/calendar?fill=%23000000" }}" alt="calendar">
<img class="icon margin-right-half{{ if .Exif.Geocode.HasAddress }} margin-left-half{{ end }}" src="{{ url "/svg/calendar?fill=%23000000" }}" alt="calendar">
<span class="date">{{ .Exif.Date.Format "2006-01-02T15:04:05Z07:00" }}</span>
{{ end }}
</p>
Expand Down
1 change: 1 addition & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ services:
environment:
EXAS_AMQP_URI: 'amqp://guest:guest@rabbit:5672/'
EXAS_AMQP_WAIT_TIMEOUT: '30s'
EXAS_GEOCODE_URL: 'https://nominatim.openstreetmap.org'
EXAS_STORAGE_FILE_SYSTEM_DIRECTORY: '/data'
volumes:
- '${DATA_DIR}:/data'
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/ViBiOh/ChatPotte v0.4.6
github.com/ViBiOh/absto v1.7.10
github.com/ViBiOh/auth/v2 v2.18.4
github.com/ViBiOh/exas v0.8.0
github.com/ViBiOh/exas v0.7.1
github.com/ViBiOh/flags v1.5.0
github.com/ViBiOh/httputils/v4 v4.76.0
github.com/ViBiOh/vith v0.6.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/ViBiOh/absto v1.7.10 h1:8z+F/46Y+uyddacBS9PQvHUJDev0K0LZLv4L4YgZXRQ=
github.com/ViBiOh/absto v1.7.10/go.mod h1:MWd8MRDAAUOl6wGqte7U4wugXfRGageltfchOK7K6AQ=
github.com/ViBiOh/auth/v2 v2.18.4 h1:XV825Y3oTJZrg9yBe74rLvqaCJ7bjUBoPdp4SN2CVXI=
github.com/ViBiOh/auth/v2 v2.18.4/go.mod h1:vZ00r7g+aQ8RF6fApTruQRZyKZ1n7ZSrIpo02uLrZk4=
github.com/ViBiOh/exas v0.8.0 h1:C+3zeKH+VAK2HQxR3wvLqLxOwUXM+R3iYFlR6aJta/k=
github.com/ViBiOh/exas v0.8.0/go.mod h1:aldybvPY4yJQlvUpq6GJluGxH+7prpaJjDQ03t3VvCw=
github.com/ViBiOh/exas v0.7.1 h1:nzh6E1668OFRgoTIJZJa1RybCW64Gd918qi0Z+0Q4WA=
github.com/ViBiOh/exas v0.7.1/go.mod h1:MvIWQZXpWgg45Vsde9bMV48Kb0J8frya8vimmrkQ04E=
github.com/ViBiOh/flags v1.5.0 h1:nwuFS8tAwtV6rTPpv2pCB+r12WjZYLjluW7yT+SeVpQ=
github.com/ViBiOh/flags v1.5.0/go.mod h1:39UMuTnKsIp6walgD8dK99KRCb4DJt9vPtbWehHh1T0=
github.com/ViBiOh/httputils/v4 v4.76.0 h1:fNMACQxmyPsSHFVeXitLw7BpO1Pm0yrWrTfKk2hSdvQ=
Expand Down
6 changes: 3 additions & 3 deletions pkg/crud/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func (s Service) generateGeoJSON(ctx context.Context, w io.Writer, request provi
return
}

if exif.Coordinates == nil {
if !exif.Geocode.HasCoordinates() {
continue
}

Expand All @@ -267,8 +267,8 @@ func (s Service) generateGeoJSON(ctx context.Context, w io.Writer, request provi
commaNeeded = true
}

point.Coordinates.Latitude = exif.Coordinates[0]
point.Coordinates.Longitude = exif.Coordinates[1]
point.Coordinates.Latitude = exif.Geocode.Latitude
point.Coordinates.Longitude = exif.Geocode.Longitude

feature.Properties["url"] = request.RelativeURL(item)
feature.Properties["date"] = exif.Date.Format(time.RFC850)
Expand Down
21 changes: 15 additions & 6 deletions pkg/crud/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,29 @@ func BenchmarkServeGeoJSON(b *testing.B) {
mockExif.EXPECT().GetAllMetadataFor(gomock.Any(), gomock.Any()).Return(map[string]provider.Metadata{
"9012": {
Exif: exas.Exif{
Coordinates: &exas.LatLng{1.0, 1.0},
Date: time.Date(2022, 0o2, 22, 22, 0o2, 22, 0, time.UTC),
Geocode: exas.Geocode{
Latitude: 1.0,
Longitude: 1.0,
},
Date: time.Date(2022, 0o2, 22, 22, 0o2, 22, 0, time.UTC),
},
},
"5678": {
Exif: exas.Exif{
Coordinates: &exas.LatLng{1.0, 1.0},
Date: time.Date(2022, 0o2, 22, 22, 0o2, 22, 0, time.UTC),
Geocode: exas.Geocode{
Latitude: 1.0,
Longitude: 1.0,
},
Date: time.Date(2022, 0o2, 22, 22, 0o2, 22, 0, time.UTC),
},
},
"1234": {
Exif: exas.Exif{
Coordinates: &exas.LatLng{1.0, 1.0},
Date: time.Date(2022, 0o2, 22, 22, 0o2, 22, 0, time.UTC),
Geocode: exas.Geocode{
Latitude: 1.0,
Longitude: 1.0,
},
Date: time.Date(2022, 0o2, 22, 22, 0o2, 22, 0, time.UTC),
},
},
}, nil).AnyTimes()
Expand Down
2 changes: 1 addition & 1 deletion pkg/crud/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (s Service) search(r *http.Request, request provider.Request, item absto.It

items[i] = renderItem

if !hasMap && metadata.Coordinates != nil {
if !hasMap && metadata.Geocode.Longitude != 0 && metadata.Geocode.Latitude != 0 {
hasMap = true
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/crud/story.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (s Service) story(r *http.Request, request provider.Request, item absto.Ite

exif := exifs[file.ID]

if !hasMap && exif.Coordinates != nil {
if !hasMap && exif.Geocode.HasCoordinates() {
hasMap = true
}

Expand Down
18 changes: 15 additions & 3 deletions pkg/metadata/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import (
"github.com/ViBiOh/httputils/v4/pkg/telemetry"
)

var (
aggregateRatio = 0.4

levels = []string{"city", "state", "country"}
)

func redisKey(item absto.Item) string {
return version.Redis("exif:" + item.ID)
}
Expand Down Expand Up @@ -105,6 +111,7 @@ func (s Service) aggregate(ctx context.Context, item absto.Item) error {
}

func (s Service) computeAndSaveAggregate(ctx context.Context, dir absto.Item) error {
directoryAggregate := newAggregate()
var minDate, maxDate time.Time

previousAggregate, _ := s.GetAggregateFor(ctx, dir)
Expand All @@ -127,16 +134,21 @@ func (s Service) computeAndSaveAggregate(ctx context.Context, dir absto.Item) er
minDate, maxDate = aggregateDate(minDate, maxDate, exifData.Date)
}

if exifData.Geocode.HasAddress() {
directoryAggregate.ingest(exifData.Geocode)
}

return nil
})
if err != nil {
return fmt.Errorf("aggregate: %w", err)
}

return s.SaveAggregateFor(ctx, dir, provider.Aggregate{
Cover: previousAggregate.Cover,
Start: minDate,
End: maxDate,
Cover: previousAggregate.Cover,
Location: directoryAggregate.value(),
Start: minDate,
End: maxDate,
})
}

Expand Down
70 changes: 70 additions & 0 deletions pkg/metadata/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package metadata

import (
"strings"

"github.com/ViBiOh/exas/pkg/model"
)

type locationAggregate map[string]map[string]int64

func newAggregate() locationAggregate {
return make(map[string]map[string]int64)
}

func (a locationAggregate) ingest(geocoding model.Geocode) {
for _, level := range levels {
a.inc(level, geocoding.Address[level])
}
}

func (a locationAggregate) inc(key, value string) {
if len(value) == 0 {
return
}

if level, ok := a[key]; ok {
level[value]++
} else {
a[key] = map[string]int64{
value: 1,
}
}
}

func (a locationAggregate) value() string {
if len(a) == 0 {
return ""
}

for _, level := range levels {
if val := a.valueOf(level); len(val) > 0 {
return val
}
}

return "Worldwide"
}

func (a locationAggregate) valueOf(key string) string {
values, ok := a[key]
if !ok {
return ""
}

var sum int64
for _, v := range values {
sum += v
}

var names []string
minSum := int64(float64(sum) * aggregateRatio)

for k, v := range values {
if v > minSum {
names = append(names, k)
}
}

return strings.Join(names, ", ")
}
2 changes: 1 addition & 1 deletion pkg/provider/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ type ExifResponse struct {
}

type Metadata struct {
exas.Exif
Description string `json:"description,omitempty"`
Tags []string `json:"tags,omitempty"`
exas.Exif
}

type Aggregate struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

var (
cacheVersion = provider.Hash("vibioh/fibr/4")[:8]
cacheVersion = provider.Hash("vibioh/fibr/3")[:8]
cachePrefix = "fibr:" + cacheVersion
)

Expand Down

0 comments on commit 1ab285d

Please sign in to comment.