Created
January 31, 2024 03:43
-
-
Save jkuester/a542636ecf08db3f6865fcf428221083 to your computer and use it in GitHub Desktop.
Couch view comparison - Erlang vs JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% Map | |
fun({Doc}) -> | |
case proplists:get_value(<<"type">>, Doc, nil) of | |
<<"telemetry">> -> | |
{Metadata} = proplists:get_value(<<"metadata">>, Doc, {nil}), | |
User = proplists:get_value(<<"user">>, Metadata, nil), | |
Year = proplists:get_value(<<"year">>, Metadata, 0), | |
Month = proplists:get_value(<<"month">>, Metadata, 0), | |
Day = proplists:get_value(<<"day">>, Metadata, 0), | |
GregorianDays = calendar:date_to_gregorian_days(Year, Month + 1, Day + 1), | |
Date = list_to_binary(lists:concat([Year, "-", Month, "-", Day])), | |
{DocDevice} = proplists:get_value(<<"device">>, Doc, {nil}), | |
{DeviceInfo} = proplists:get_value(<<"deviceInfo">>, DocDevice, {nil}), | |
{App} = proplists:get_value(<<"app">>, DeviceInfo, {nil}), | |
{Software} = proplists:get_value(<<"software">>, DeviceInfo, {nil}), | |
{MetaVersions} = proplists:get_value(<<"versions">>, Metadata, {nil}), | |
Result = {[ | |
{<<"date">>, Date}, | |
{<<"id">>, proplists:get_value(<<"_id">>, Doc)}, | |
{<<"device">>, {[ | |
{<<"deviceId">>, proplists:get_value(<<"deviceId">>, Metadata, nil)}, | |
{<<"userAgent">>, proplists:get_value(<<"userAgent">>, DocDevice, nil)}, | |
{<<"versions">>, {[ | |
{<<"apk">>, proplists:get_value(<<"version">>, App, nil)}, | |
{<<"android">>, proplists:get_value(<<"androidVersion">>, Software, nil)}, | |
{<<"cht">>, proplists:get_value(<<"app">>, MetaVersions, nil)}, | |
{<<"settings">>, proplists:get_value(<<"settings">>, MetaVersions, nil)} | |
]}} | |
]}}, | |
{<<"gregorianDays">>, GregorianDays} | |
]}, | |
Emit(User, Result); | |
_ -> nil | |
end | |
end. | |
% Reduce | |
fun(Keys, Values, ReReduce) -> | |
Calc_max = fun(Value, Acc) -> | |
case Acc of | |
nil -> Value; | |
_ -> | |
AccTotalDays = proplists:get_value(<<"gregorianDays">>, element(1, Acc)), | |
EmTotalDays = proplists:get_value(<<"gregorianDays">>, element(1, Value)), | |
if | |
EmTotalDays > AccTotalDays -> Value; | |
true -> Acc | |
end | |
end | |
end, | |
lists:foldl(Calc_max, nil, Values) | |
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Map | |
function(doc) { | |
if (doc.type === 'telemetry') { | |
emit(doc.metadata.user, { | |
date: `${doc.metadata.year}-${doc.metadata.month}-${doc.metadata.day}`, | |
id: doc._id, | |
device: { | |
deviceId: doc.metadata.deviceId, | |
userAgent: doc.device.userAgent, | |
versions: { | |
apk: doc.device.deviceInfo.app ? doc.device.deviceInfo.app.version : undefined, | |
android: doc.device.deviceInfo.software ? doc.device.deviceInfo.software.androidVersion : undefined, | |
cht: doc.metadata.versions.app, | |
settings: doc.metadata.versions.settings, | |
}, | |
}, | |
}); | |
} | |
} | |
// Reduce | |
function (keys, values) { | |
let latest = { date: '0000-00-00' }; | |
values.forEach(function (value) { | |
if (value.date > latest.date) { | |
latest = value; | |
} | |
}); | |
return latest; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment