Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync50a #407

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft

Sync50a #407

wants to merge 3 commits into from

Conversation

MysticJay
Copy link
Contributor

@MysticJay MysticJay commented Dec 7, 2020

SYNC collision handling

Summary: multiple active clients accessing the drive might cause IITC to "forget" recently done changes when polling the drive.

Added a collision detection that will ask the user with which data-set to continue.

Clean copy of PR #403
addressing Issue #394
Status: RC1

@MysticJay MysticJay linked an issue Dec 7, 2020 that may be closed by this pull request
@MysticJay MysticJay mentioned this pull request Dec 7, 2020
@MysticJay MysticJay added the enhancement New feature or request label Dec 7, 2020
@McBen
Copy link
Contributor

McBen commented Dec 8, 2020

sry to say but the "comments" stuff is annoying.
I don't say they are bad at all. But please reduce them to a minimum. Only write a comment that is worth to read or when the code need explanation. In most cases a varialbe rename or an extra variable or moving stuff to an extra function can do a far better job.

code is read by programmers

grafik
stuff like this doesn't help anybody. But makes the code messy and outdated comments can lead to wrong assumpations.

@MysticJay
Copy link
Contributor Author

@McBen It took (me) hours to understand SYNC as it was hardly commented at all. Yes code is read by programmers, but some are more skilled than others and some "see" the symantics from long constructs like x= a ? b : c; esp if a and b are implemented as functions.

when I read code I endorse evrey single bit of comment the programmer added as this will explain his intention and makes debugging way more easy (for me).

You may call me a bad programmer, but if we want new DEVs the current uncommented code is tough stuff.

Copy link
Contributor

@McBen McBen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just started to add some comments to the comments :)
Didn't had time for all..that was simply too much.

@@ -35,7 +35,7 @@ window.plugin.sync.authorizer = null;
window.plugin.sync.registeredPluginsFields = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not comment the real important stuff like this?
What is it for? What's the structure of that variable?

Comment on lines +69 to +70
registeredMap = new plugin.sync.RegisteredMap(options); // create a new datasructure
// for the calling plugin and
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

double line comments

some one might (accidently) add a code line between them and split the comment

plugin.sync.registeredPluginsFields.add(registeredMap);
registeredMap = new plugin.sync.RegisteredMap(options); // create a new datasructure
// for the calling plugin and
plugin.sync.registeredPluginsFields.add(registeredMap); // add it to the main structure
Copy link
Contributor

@McBen McBen Dec 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as said above. would a explanlation of "sync.registeredPluginFields" wouldn't tell much more ?

};



//// RegisteredMap
//// RegisteredMap *************************************************************************
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: comments like "*************" usually mean: split this here in 2 files

Comment on lines +101 to +106
this.doSync = 'remote'; // normal 'sync'
// 'no' sync needed
// force 'remote' (1st sync)
// force 'local'
// sync is on 'stop'

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the comment. I assume "sync","no","remote","local","stop" are valid states of doSync ?

maybe better something like - just my guess what they could mean:

Suggested change
this.doSync = 'remote'; // normal 'sync'
// 'no' sync needed
// force 'remote' (1st sync)
// force 'local'
// sync is on 'stop'
/**
* doSync : state of current sync
* = "sync" : normal sync (what ever this means)
* = "no" : sync needed (or "no sync running, start delay" ?)
* = "stop" : sync stopped by user
* = "remote" : get values from remote
* = "local : use local values instead of remote
*/
this.doSync = 'remote';


this.dataStorage.initialize( // initalize the dataStorage to gain
this.forceFileSearch, // find the related file on drive
assignIdCallback, // onSuccess:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is it named "assignIdCallback" ? it only triggers the callback? isn't it?

};

window.plugin.sync.RegisteredMap.prototype.initialize = function(callback) {
this.initFile(this.loadDocument);
};

// prepend UUID to data
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// prepend UUID to data
// prepend UUID to data

space

);

this.dataStorage.initialize( // initalize the dataStorage to gain
this.forceFileSearch, // find the related file on drive
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this.forceFileSearch, // find the related file on drive
this.forceFileSearch, // if true: find the related file on drive

But the "initialze" function should explain what the parameters are for. Not the function call.
If using a javadoc style comments most IDE's will display this informations.

@@ -162,46 +183,109 @@ window.plugin.sync.RegisteredMap.prototype.loadDocument = function(callback) {
// this function called when the document is created first time
// and the JSON file is populated with data in plugin field
initializeFile = function() {
_this.map = {};
_this.map = {}; // create an empty map
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_this.map = {}; // create an empty map
_this.map = {};

$.each(window.plugin[_this.pluginName][_this.fieldName], function(key, val) {
_this.map[key] = val;
});

_this.dataStorage.saveFile(_this.prepareFileData());
_this.dataStorage.saveFile(_this.prepareFileData()); // save the map to drive
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_this.dataStorage.saveFile(_this.prepareFileData()); // save the map to drive
_this.dataStorage.saveFile(_this.prepareFileData());

@johnd0e
Copy link
Contributor

johnd0e commented Dec 8, 2020

In general agree with @McBen.
Also: initial message is not enough: "Clean PR fro SYNC50".
What is it all about? Please add some context.

See also https://github.com/IITC-CE/ingress-intel-total-conversion/wiki/Contributing-Code about commit messages style.

@modos189 modos189 force-pushed the master branch 3 times, most recently from a34b800 to 7b9edd5 Compare December 12, 2020 07:21
@MysticJay
Copy link
Contributor Author

@johnd0e @McBen

In general agree with @McBen.

Commenting SYNC is "work in progress" and some comments might even need to be revised.
I prefer comments that start at the same column (if possible) and do not exceed certain boundary.
IMHO this helps to "understand" the code without the need to constantly jump between code and comments.
The above remarks you made will be taken forward the one or the other way.

Also: initial message is not enough: "Clean PR fro SYNC50".
What is it all about? Please add some context.
See also https://github.com/IITC-CE/ingress-intel-total-conversion/wiki/Contributing-Code about commit messages style.

fixed

@McBen
Copy link
Contributor

McBen commented Dec 26, 2020

The proper use of comments is to compensate for our failure to express ourself in code. Note that I used the word failure. I meant it. Comments are always failures. We must have them because we cannot always figure out how to express ourselft without them, but their use is not a cause of celebration.
(Robert C. Martin / Clean Code)

Some comments are required. Some comments might by helpful. But they always cost time to write, read and maintain. They can be boring and in worst case be missleading or wrong.

Style

[..] without the need to constantly jump between code and comments.

right now you're jumping left & right.
Even worse: when reviewing code on a smaller screen (or as side-by-side) you've messed up code or have to scroll left & right

This style of comments are fine when teaching someone to code. You've the pure code on the left and the documention on the right. This way you can focus and explain the code without the distraction of comments.

While developing this is terrible wrong. While developing code & comments should be a unit. I expect a comment is important, has a valid reason to be there and I've to read it. (like function feedGremlin() { // never call it after midnight)

@MysticJay
Copy link
Contributor Author

This style of comments are fine when teaching someone to code. You've the pure code on the left and the documention on the right. This way you can focus and explain the code without the distraction of comments.

Still I prefer that way of explaining the code and if Xelio had done so in 2013 it wouldn't have cost us/me weeks to understand the code and someone else could have taken the job to refurbish it way earlier. Up to now all we received from other DEVs was "SYNC is a mess, I do not understand it, I will not touch it, I use my own code."

SYNC is very "difficult" code and digging out the ideas @Xelio had in 2013 is time consuming, but unavoidable if we want to keep SYNC alive and compatible.

For the time being the comment style will be "Code-Teaching". When the process is done, we can think about dropping, merging or rewriting comments.

@johnd0e
Copy link
Contributor

johnd0e commented Dec 26, 2020

SYNC is very "difficult" code

So may be it's worth some refactoring to make it easier?

Better spent time simplifying things than explaining wh things are so complicated.

@MysticJay
Copy link
Contributor Author

@johnd0e I explained my intention, the plan, the future development and that the remarks are valuable and will be taken forward into the next steps.

If you do not like my changed comments but still feel the issue is valid you are free to cherry-pick the dev-part only or ignore the PR.

// this function called when the document is loaded
// update local data if the document is updated by other (overwrite!)
// and adding a timer to further check for updates
// this function gets called when the document is loaded
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'document' usually reference to window.document . Here we talk about a g-drive file

Copy link
Contributor

@McBen McBen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some more comments

Comment on lines +259 to +265
// execute the callback functions
if(_this.callback) _this.callback( // execute the callback functions with
_this.pluginName, // pluginName
_this.fieldName, // fieldName
null, // null (for backwards compatibility)
true // true = full file load from Drive
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// execute the callback functions
if(_this.callback) _this.callback( // execute the callback functions with
_this.pluginName, // pluginName
_this.fieldName, // fieldName
null, // null (for backwards compatibility)
true // true = full file load from Drive
);
// execute the callback functions
if(_this.callback) _this.callback(
_this.pluginName,
_this.fieldName,
null, // obsolete, for backwards compatibility
true // load complete file
);

// Store RegisteredMap and handle initialization of RegisteredMap
window.plugin.sync.RegisteredPluginsFields = function(options) {
this.authorizer = options['authorizer'];
this.authorizer = options['authorizer']; //
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty comment

Suggested change
this.authorizer = options['authorizer']; //
this.authorizer = options['authorizer'];

clearTimeout(this.timer); // reset the timer
if(Object.keys(this.waitingInitialize).length > 0) { // if queue still not empty
this.timer = setTimeout(
function() {_this.initializeWorker()}, 10000 // retry in 10 sec
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move time constant to extra variable

Suggested change
function() {_this.initializeWorker()}, 10000 // retry in 10 sec
function() {_this.initializeWorker()}, RETRY_TIME

NB: also comment in 318 references this

}
};
//// end RegisteredPluginsFields




//// DataManager
//// DataManager ***************************************************************************
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


is a sign for "split this file here"

function () {
gapi.client.request({
path: '/upload/drive/v3/files/'+_this.fileId,
method: 'PATCH', // why patch?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't ask me

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try with PUT https://developers.google.com/drive/api/v2/reference/files/update
Google is likely to translate PATCH by PUT on the route https://www.googleapis.com/upload/drive/v2/files/{fileId}

window.plugin.sync.updateLog = function(messages) {
$('#sync-log').html(messages);
};

// AuthButon that shows IN the dialog
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// AuthButon that shows IN the dialog
// Toggle the state of the AuthButton in sync dialog

@@ -717,6 +790,7 @@ window.plugin.sync.toggleAuthButton = function() {
$('#sync-authButton').toggleClass('sync-authButton-dimmed', authed || authorizing);
};

// Sync menuitem in the toolbox
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"toggle" and "sync" sounds really differnt..one of it might be wrong

Comment on lines +857 to +966
window.plugin.sync.logger = new plugin.sync.Logger(
{'logLimit':10, 'logUpdateCallback': plugin.sync.updateLog}
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

go the full way

Suggested change
window.plugin.sync.logger = new plugin.sync.Logger(
{'logLimit':10, 'logUpdateCallback': plugin.sync.updateLog}
);
window.plugin.sync.logger = new plugin.sync.Logger({
logLimit: 10,
logUpdateCallback: plugin.sync.updateLog
});

window.plugin.sync.authorizer = new window.plugin.sync.Authorizer({
'authCallback': [plugin.sync.toggleAuthButton, plugin.sync.toggleDialogLink]
});
// create an authorizer structure
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep the empty line as separator

Comment on lines +866 to +974
'authCallback': [plugin.sync.toggleAuthButton, // functions to execute after
plugin.sync.toggleDialogLink // successfull authorization
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another good example why this comment format is bad,
if you add another callback this comment get's messed up ... and it's currently hard to read if you're just looking for "toggleDialogLink"

Comments & linebreaks
added to many lines to help understand the code
Commenting the code
Conflict detection implemented.
@Suburbanno
Copy link
Contributor

@MysticJay Do you intend to continue this contribution? If not, could you close it?

@MysticJay MysticJay marked this pull request as draft August 3, 2023 19:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request plugin
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SYNC - Collision handling
5 participants