Mac Snippets

Permanently add SSH Key Password to the Keychain ssh-add --apple-use-keychain ~/.ssh/THEPRIVATEKEY Tool for Window Management Install rectangle Install with brew brew install rectangle Useful Shortcuts Move current app to previous or next display Ctrl+Opt+Cmd+LEFT or Ctrl+Opt+Cmd+RIGHT Maximize window Ctrl+Opt+ENTER Beginners List of Shortcuts coming from Linux …​ Finder Show hidden files in Finder: cmd + shift + . Open Finder: Option + Cmd + Space Open Location: Shift + Cmd + G Change to parent dir in Finder: super + UP ...

March 1, 2010 · 1 min · 166 words · Micha Kops

Maven Snippets

Extract Coordinates from the POM Helpful for build and integration environments, pipelines etc. Exctract the Project version from the pom.xml mvn org.apache.maven.plugins:maven-help-plugin:3.1.0:evaluate -Dexpression=project.version -q -DforceStdout Override a Property via local configuration e.g. to override the property xxx from your project’s pom.xml Create a directory .mvn in your project directory root Create a file named maven.config in this directory Insert -Dxxx=newvalue into the file to override this property Don’t forget to add .mvn to your .gitignore! ...

March 1, 2010 · 2 min · 401 words · Micha Kops

Misc Snippets

Data Normalizing Formula newVal = (oldVal-min) / (max-min) Ugly Scala Example package com.hascode import scala.collection.mutable.LinkedList object NormalizerExample extends App { val dataSet = LinkedList(1., 6.5, 3., 6.2, 20., 31.2, 50.2, 12., 0.24, 1.224, 2.2, 3.) for ((num, index) <- dataSet.zipWithIndex) { dataSet(index) = (num - dataSet.min) / (dataSet.max - dataSet.min) } println("Normalized: " + dataSet) } Normalized: LinkedList(0.01521216973578863, 0.12921819759798853, 0.05947594797769014, 0.12324029048767723, 0.3982240175619966, 0.6213992163469515, 1.0, 1.0, 0.07531115879828326, 0.40498283261802576, 0.7319742489270387, 1.0)

March 1, 2010 · 1 min · 69 words · Micha Kops

MyBatis Snippets

Use List of Parameters in Annotation-based Query Possible using MyBatis Dynamic SQL feature package com.hascode.example.mybatis; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import java.util.List; @Mapper public interface SampleMapper { @Select({"<script>", "SELECT sample.bar", "FROM sampletable sample", "WHERE sample.id IN", "<foreach item='item' index='index' collection='ids'", "open='(' separator=',' close=')'>", "#{item}", "</foreach>", "</script>"}) List<Foo> getSamplesMatchingIds(@Param("ids") List<String> ids); } The mapper may now be used with a list of parameter objects: var samples = sampleMapper.getSamplesMatchingIds(List.of("24059e5b-aa07-424d-855e-50f499b8f697", "65140fc0-fc9f-42d2-9531-5e5d6caeba30")); Call a Procedure package com.hascode.example.mybatis; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Options; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.mapping.StatementType; @Mapper public interface SampleMapper { @Select("CALL SCHEMA.CL.setScope(#{scope})") @Options(statementType = StatementType.CALLABLE) void setScope(int scope); } ...

March 1, 2010 · 1 min · 101 words · Micha Kops

Neo4j Snippets

Aggregate existing Labels MATCH (n) RETURN DISTINCT labels(n) Or unwinding label pairs MATCH (n) WITH DISTINCT labels(n) AS labels UNWIND labels AS label RETURN DISTINCT label ORDER BY label

March 1, 2010 · 1 min · 29 words · Micha Kops

Observability Snippets

Observability Strategies USE USE stands for: Utilization - Percent time the resource is busy, such as node CPU usage Saturation - Amount of work a resource has to do, often queue length or node load Errors - Count of error events This method is best for hardware resources in infrastructure, such as CPU, memory, and network devices. For more information, refer to The USE Method. Source: https://grafana.com/docs/grafana/latest/dashboards/build-dashboards/best-practices/ RED RED stands for: Rate - Requests per second Errors - Number of requests that are failing Duration - Amount of time these requests take, distribution of latency measurements This method is most applicable to services, especially a microservices environment. For each of your services, instrument the code to expose these metrics for each component. RED dashboards are good for alerting and SLAs. A well-designed RED dashboard is a proxy for user experience. ...

March 1, 2010 · 2 min · 231 words · Micha Kops

PHP Snippets

WordPress anonymize IP in comments Add as last line in theme’s functions.php: function wpb_remove_commentsip($comment_author_ip) { return '127.0.0.1'; } add_filter('pre_comment_user_ip','wpb_remove_commentsip'); WordPress remove version info Add as last line in theme’s functions.php: function remove_version_info() { return ''; } add_filter('the_generator', 'remove_version_info'); List of all WordPress Hooks

March 1, 2010 · 1 min · 43 words · Micha Kops

Postgres Snippets

Get size of a table SELECT pg_size_pretty(pg_total_relation_size('schemaname.tablename')); Select unique combination of fields / tuples SELECT DISTINCT ON(field1, field2) field1, field2 FROM thetable Select rows where a combination of fields is not unique SELECT columnA, columnB, count(*) AS count FROM thetable GROUP BY columnA, columnB HAVING count(*) > 1 Search for rows with array containing value Assuming, the field appointments has the type date[] SELECT * FROM mtable WHERE appointments @> ARRAY['2023-09-19'::date] ...

March 1, 2010 · 8 min · 1655 words · Micha Kops

Python Snippets

Virtual Environments Create and activate a venv python -m venv /path/to/new-env (1) source /path/to/new-env/bin/activate (2) 1 Creates a new virtual environment 2 Activate the new virtual environment Deactivate venv deactivate Save used dependencies to a file pip freeze > dependencies.txt Install dependencies from file pip install -r dependencies.txt Storing and fetching Credentials from the System Keyring I am using jaraco/keyring here: pip install keyring import keyring keyring.set_password("system", "db.sample.password", "xoxo") print(keyring.get_password("system", "db.sample.password")) ...

March 1, 2010 · 1 min · 100 words · Micha Kops

Rust Snippets

Installation Installing rustup first…​ install rustup via shell script curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh install rustup via brew brew install rustup-init (1) rustup-init (2) Welcome to Rust! [...] Current installation options: default host triple: aarch64-apple-darwin default toolchain: stable (default) profile: default modify PATH variable: yes 1) Proceed with standard installation (default - just press enter) 2) Customize installation 3) Cancel installation 1 install rustup 2 install rust binaries and add them to paths etc…​ ...

March 1, 2010 · 1 min · 111 words · Micha Kops